Skip to content

Commit 61a7914

Browse files
authored
Add test helper to create mock provider that contains a state store (#37556)
* Add a helper for mocking a provider with a state store * Update tests to use the new helper * Update other test fixtures for consistency The related tests pass before and after this change * Update testing helper to return the underlying type, not the interface
1 parent 54af16e commit 61a7914

File tree

19 files changed

+131
-194
lines changed

19 files changed

+131
-194
lines changed

internal/command/meta_backend_test.go

Lines changed: 37 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,32 +2099,7 @@ func TestMetaBackend_configureNewStateStore(t *testing.T) {
20992099
//
21002100
// This imagines a provider called foo that contains
21012101
// a pluggable state store implementation called bar.
2102-
mock := &testing_provider.MockProvider{
2103-
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
2104-
Provider: providers.Schema{
2105-
Body: &configschema.Block{
2106-
Attributes: map[string]*configschema.Attribute{
2107-
"region": {Type: cty.String, Optional: true},
2108-
},
2109-
},
2110-
},
2111-
DataSources: map[string]providers.Schema{},
2112-
ResourceTypes: map[string]providers.Schema{},
2113-
ListResourceTypes: map[string]providers.Schema{},
2114-
StateStores: map[string]providers.Schema{
2115-
"foo_bar": {
2116-
Body: &configschema.Block{
2117-
Attributes: map[string]*configschema.Attribute{
2118-
"bar": {
2119-
Type: cty.String,
2120-
Required: true,
2121-
},
2122-
},
2123-
},
2124-
},
2125-
},
2126-
},
2127-
}
2102+
mock := testStateStoreMock(t)
21282103
factory := func() (providers.Interface, error) {
21292104
return mock, nil
21302105
}
@@ -2214,32 +2189,7 @@ func TestMetaBackend_reconfigureStateStoreChange(t *testing.T) {
22142189
//
22152190
// This imagines a provider called foo that contains
22162191
// a pluggable state store implementation called bar.
2217-
mock := &testing_provider.MockProvider{
2218-
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
2219-
Provider: providers.Schema{
2220-
Body: &configschema.Block{
2221-
Attributes: map[string]*configschema.Attribute{
2222-
"region": {Type: cty.String, Optional: true},
2223-
},
2224-
},
2225-
},
2226-
DataSources: map[string]providers.Schema{},
2227-
ResourceTypes: map[string]providers.Schema{},
2228-
ListResourceTypes: map[string]providers.Schema{},
2229-
StateStores: map[string]providers.Schema{
2230-
"foo_bar": {
2231-
Body: &configschema.Block{
2232-
Attributes: map[string]*configschema.Attribute{
2233-
"bar": {
2234-
Type: cty.String,
2235-
Required: true,
2236-
},
2237-
},
2238-
},
2239-
},
2240-
},
2241-
},
2242-
}
2192+
mock := testStateStoreMock(t)
22432193
factory := func() (providers.Interface, error) {
22442194
return mock, nil
22452195
}
@@ -2285,32 +2235,7 @@ func TestMetaBackend_changeConfiguredStateStore(t *testing.T) {
22852235
//
22862236
// This imagines a provider called foo that contains
22872237
// a pluggable state store implementation called bar.
2288-
mock := &testing_provider.MockProvider{
2289-
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
2290-
Provider: providers.Schema{
2291-
Body: &configschema.Block{
2292-
Attributes: map[string]*configschema.Attribute{
2293-
"region": {Type: cty.String, Optional: true},
2294-
},
2295-
},
2296-
},
2297-
DataSources: map[string]providers.Schema{},
2298-
ResourceTypes: map[string]providers.Schema{},
2299-
ListResourceTypes: map[string]providers.Schema{},
2300-
StateStores: map[string]providers.Schema{
2301-
"foo_bar": {
2302-
Body: &configschema.Block{
2303-
Attributes: map[string]*configschema.Attribute{
2304-
"bar": {
2305-
Type: cty.String,
2306-
Required: true,
2307-
},
2308-
},
2309-
},
2310-
},
2311-
},
2312-
},
2313-
}
2238+
mock := testStateStoreMock(t)
23142239
factory := func() (providers.Interface, error) {
23152240
return mock, nil
23162241
}
@@ -2353,32 +2278,7 @@ func TestMetaBackend_configuredBackendToStateStore(t *testing.T) {
23532278
//
23542279
// This imagines a provider called foo that contains
23552280
// a pluggable state store implementation called bar.
2356-
mock := &testing_provider.MockProvider{
2357-
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
2358-
Provider: providers.Schema{
2359-
Body: &configschema.Block{
2360-
Attributes: map[string]*configschema.Attribute{
2361-
"region": {Type: cty.String, Optional: true},
2362-
},
2363-
},
2364-
},
2365-
DataSources: map[string]providers.Schema{},
2366-
ResourceTypes: map[string]providers.Schema{},
2367-
ListResourceTypes: map[string]providers.Schema{},
2368-
StateStores: map[string]providers.Schema{
2369-
"foo_bar": {
2370-
Body: &configschema.Block{
2371-
Attributes: map[string]*configschema.Attribute{
2372-
"bar": {
2373-
Type: cty.String,
2374-
Required: true,
2375-
},
2376-
},
2377-
},
2378-
},
2379-
},
2380-
},
2381-
}
2281+
mock := testStateStoreMock(t)
23822282
factory := func() (providers.Interface, error) {
23832283
return mock, nil
23842284
}
@@ -2475,32 +2375,7 @@ func TestMetaBackend_configureStateStoreVariableUse(t *testing.T) {
24752375
//
24762376
// This imagines a provider called foo that contains
24772377
// a pluggable state store implementation called bar.
2478-
mock := &testing_provider.MockProvider{
2479-
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
2480-
Provider: providers.Schema{
2481-
Body: &configschema.Block{
2482-
Attributes: map[string]*configschema.Attribute{
2483-
"region": {Type: cty.String, Optional: true},
2484-
},
2485-
},
2486-
},
2487-
DataSources: map[string]providers.Schema{},
2488-
ResourceTypes: map[string]providers.Schema{},
2489-
ListResourceTypes: map[string]providers.Schema{},
2490-
StateStores: map[string]providers.Schema{
2491-
"foo_bar": {
2492-
Body: &configschema.Block{
2493-
Attributes: map[string]*configschema.Attribute{
2494-
"bar": {
2495-
Type: cty.String,
2496-
Required: true,
2497-
},
2498-
},
2499-
},
2500-
},
2501-
},
2502-
},
2503-
}
2378+
mock := testStateStoreMock(t)
25042379
factory := func() (providers.Interface, error) {
25052380
return mock, nil
25062381
}
@@ -2538,3 +2413,35 @@ func testMetaBackend(t *testing.T, args []string) *Meta {
25382413

25392414
return &m
25402415
}
2416+
2417+
// testStateStoreMock returns a mock provider that has a state store implementation
2418+
// The provider uses the name "test" and the store inside is "test_store".
2419+
func testStateStoreMock(t *testing.T) *testing_provider.MockProvider {
2420+
t.Helper()
2421+
return &testing_provider.MockProvider{
2422+
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
2423+
Provider: providers.Schema{
2424+
Body: &configschema.Block{
2425+
Attributes: map[string]*configschema.Attribute{
2426+
"region": {Type: cty.String, Optional: true},
2427+
},
2428+
},
2429+
},
2430+
DataSources: map[string]providers.Schema{},
2431+
ResourceTypes: map[string]providers.Schema{},
2432+
ListResourceTypes: map[string]providers.Schema{},
2433+
StateStores: map[string]providers.Schema{
2434+
"test_store": {
2435+
Body: &configschema.Block{
2436+
Attributes: map[string]*configschema.Attribute{
2437+
"value": {
2438+
Type: cty.String,
2439+
Required: true,
2440+
},
2441+
},
2442+
},
2443+
},
2444+
},
2445+
},
2446+
}
2447+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
terraform {
22
required_providers {
3-
foo = {
4-
source = "my-org/foo"
3+
test = {
4+
source = "hashicorp/test"
55
}
66
}
7-
state_store "foo_bar" {
8-
provider "foo" {}
7+
state_store "test_store" {
8+
provider "test" {}
99

10-
bar = "foobar"
10+
value = "foobar"
1111
}
1212
}

internal/command/testdata/state-store-changed/.terraform/terraform.tfstate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"serial": 0,
44
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
55
"state_store": {
6-
"type": "foo_bar",
6+
"type": "test_store",
77
"config": {
8-
"bar": "old-value"
8+
"value": "old-value"
99
},
1010
"provider": {
1111
"version": "1.2.3",
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
terraform {
22
required_providers {
3-
foo = {
4-
source = "my-org/foo"
3+
test = {
4+
source = "hashicorp/test"
55
}
66
}
7-
state_store "foo_bar" {
8-
provider "foo" {}
7+
state_store "test_store" {
8+
provider "test" {}
99

10-
bar = "changed-value" # changed versus backend state file
10+
value = "changed-value" # changed versus backend state file
1111
}
1212
}

internal/command/testdata/state-store-new-vars-in-provider/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ variable "foo" { default = "bar" }
22

33
terraform {
44
required_providers {
5-
foo = {
6-
source = "my-org/foo"
5+
test = {
6+
source = "hashicorp/test"
77
}
88
}
9-
state_store "foo_bar" {
10-
provider "foo" {
9+
state_store "test_store" {
10+
provider "test" {
1111
region = var.foo
1212
}
1313

14-
bar = "hardcoded"
14+
value = "hardcoded"
1515
}
1616
}

internal/command/testdata/state-store-new-vars-in-store/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ variable "foo" { default = "bar" }
22

33
terraform {
44
required_providers {
5-
foo = {
6-
source = "my-org/foo"
5+
test = {
6+
source = "hashicorp/test"
77
}
88
}
9-
state_store "foo_bar" {
10-
provider "foo" {
9+
state_store "test_store" {
10+
provider "test" {
1111
region = "hardcoded"
1212
}
1313

14-
bar = var.foo
14+
value = var.foo
1515
}
1616
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
terraform {
22
required_providers {
3-
foo = {
4-
source = "my-org/foo"
3+
test = {
4+
source = "hashicorp/test"
55
}
66
}
7-
state_store "foo_bar" {
8-
provider "foo" {}
7+
state_store "test_store" {
8+
provider "test" {}
99

10-
bar = "foobar"
10+
value = "foobar"
1111
}
1212
}

internal/command/testdata/state-store-reconfigure/.terraform/terraform.tfstate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"serial": 0,
44
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
55
"state_store": {
6-
"type": "foo_bar",
6+
"type": "test_store",
77
"config": {
8-
"bar": "old-value"
8+
"value": "old-value"
99
},
1010
"provider": {
1111
"version": "1.2.3",
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
terraform {
22
required_providers {
3-
foo = {
4-
source = "my-org/foo"
3+
test = {
4+
source = "hashicorp/test"
55
}
66
}
7-
state_store "foo_bar" {
8-
provider "foo" {}
7+
state_store "test_store" {
8+
provider "test" {}
99

10-
bar = "changed-value" # changed versus backend state file
10+
value = "changed-value" # changed versus backend state file
1111
}
1212
}

internal/command/testdata/state-store-to-backend/.terraform/terraform.tfstate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"serial": 0,
44
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
55
"state_store": {
6-
"type": "foo_bar",
6+
"type": "test_store",
77
"config": {
8-
"bar": "foobar"
8+
"value": "foobar"
99
},
1010
"provider": {
1111
"version": "1.2.3",

0 commit comments

Comments
 (0)