Skip to content

Commit b31ec3e

Browse files
committed
test: Assert that state stores are present in provider schemas returned from providers schema.
1 parent d19eb83 commit b31ec3e

File tree

6 files changed

+177
-4
lines changed

6 files changed

+177
-4
lines changed

internal/command/providers_schema_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestProvidersSchema_output_withStateStore(t *testing.T) {
131131
// Create a temporary working directory that includes config using
132132
// a state store in the `test` provider
133133
td := t.TempDir()
134-
testCopyDir(t, testFixturePath("state-store-unchanged"), td)
134+
testCopyDir(t, testFixturePath("provider-schemas-state-store"), td)
135135
t.Chdir(td)
136136

137137
// Get bytes describing the state
@@ -169,10 +169,11 @@ func TestProvidersSchema_output_withStateStore(t *testing.T) {
169169
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
170170
}
171171

172+
// Does the output mention the 2 providers, and the name of the state store?
172173
wantOutput := []string{
173-
`{"format_version":"1.0","provider_schemas":{`, // Opening of JSON
174-
`"registry.terraform.io/hashicorp/baz":{`, // provider from state
175-
`"registry.terraform.io/hashicorp/test":{`, // provider from config
174+
mockProviderAddressBaz.String(), // provider from state
175+
mockProviderAddressTest.String(), // provider from config
176+
"test_store", // the name of the state store implemented in the provider
176177
}
177178

178179
output := ui.OutputWriter.String()
@@ -182,6 +183,32 @@ func TestProvidersSchema_output_withStateStore(t *testing.T) {
182183
}
183184
}
184185

186+
// Does the output match the full expected schema?
187+
var got, want providerSchemas
188+
189+
gotString := ui.OutputWriter.String()
190+
err := json.Unmarshal([]byte(gotString), &got)
191+
if err != nil {
192+
t.Fatal(err)
193+
}
194+
195+
wantFile, err := os.Open("output.json")
196+
if err != nil {
197+
t.Fatalf("err: %s", err)
198+
}
199+
defer wantFile.Close()
200+
byteValue, err := ioutil.ReadAll(wantFile)
201+
if err != nil {
202+
t.Fatalf("err: %s", err)
203+
}
204+
err = json.Unmarshal([]byte(byteValue), &want)
205+
if err != nil {
206+
t.Fatal(err)
207+
}
208+
209+
if !cmp.Equal(got, want) {
210+
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
211+
}
185212
}
186213

187214
type providerSchemas struct {
@@ -193,6 +220,7 @@ type providerSchema struct {
193220
Provider interface{} `json:"provider,omitempty"`
194221
ResourceSchemas map[string]interface{} `json:"resource_schemas,omitempty"`
195222
DataSourceSchemas map[string]interface{} `json:"data_source_schemas,omitempty"`
223+
StateStoreSchemas map[string]interface{} `json:"state_store_schemas,omitempty"`
196224
}
197225

198226
// testProvider returns a mock provider that is configured for basic

internal/command/testdata/provider-schemas-state-store/.terraform.lock.hcl

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": 3,
3+
"serial": 0,
4+
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
5+
"state_store": {
6+
"type": "test_store",
7+
"config": {
8+
"value": "foobar"
9+
},
10+
"provider": {
11+
"version": "1.2.3",
12+
"source": "registry.terraform.io/hashicorp/test",
13+
"config": {
14+
"region": null
15+
}
16+
},
17+
"hash": 4158988729
18+
}
19+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This test fixture is a working directory that contains a state_store block that matches the backend state file and dependency lock file.
2+
Any test using this fixture will need to set up a mock provider that describes a state store in its schema that matches this config.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
version = "1.2.3"
6+
}
7+
}
8+
state_store "test_store" {
9+
provider "test" {}
10+
11+
value = "foobar" # matches backend state file
12+
}
13+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"format_version": "1.0",
3+
"provider_schemas": {
4+
"registry.terraform.io/hashicorp/baz": {
5+
"provider": {
6+
"version": 0,
7+
"block": {
8+
"attributes": {
9+
"region": {
10+
"description_kind": "plain",
11+
"optional": true,
12+
"type": "string"
13+
}
14+
},
15+
"description_kind": "plain"
16+
}
17+
},
18+
"resource_schemas": {
19+
"test_instance": {
20+
"version": 0,
21+
"block": {
22+
"attributes": {
23+
"id": {
24+
"type": "string",
25+
"computed": true,
26+
"description_kind": "plain"
27+
},
28+
"input": {
29+
"description_kind": "plain",
30+
"optional": true,
31+
"type": "string"
32+
}
33+
},
34+
"description_kind": "plain"
35+
}
36+
}
37+
},
38+
"state_store_schemas": {
39+
"test_store": {
40+
"version": 0,
41+
"block": {
42+
"attributes": {
43+
"value": {
44+
"description_kind": "plain",
45+
"required": true,
46+
"type": "string"
47+
}
48+
},
49+
"description_kind": "plain"
50+
}
51+
}
52+
}
53+
},
54+
"registry.terraform.io/hashicorp/test": {
55+
"provider": {
56+
"version": 0,
57+
"block": {
58+
"attributes": {
59+
"region": {
60+
"description_kind": "plain",
61+
"optional": true,
62+
"type": "string"
63+
}
64+
},
65+
"description_kind": "plain"
66+
}
67+
},
68+
"resource_schemas": {
69+
"test_instance": {
70+
"version": 0,
71+
"block": {
72+
"attributes": {
73+
"id": {
74+
"type": "string",
75+
"computed": true,
76+
"description_kind": "plain"
77+
},
78+
"input": {
79+
"description_kind": "plain",
80+
"optional": true,
81+
"type": "string"
82+
}
83+
},
84+
"description_kind": "plain"
85+
}
86+
}
87+
},
88+
"state_store_schemas": {
89+
"test_store": {
90+
"version": 0,
91+
"block": {
92+
"attributes": {
93+
"value": {
94+
"description_kind": "plain",
95+
"required": true,
96+
"type": "string"
97+
}
98+
},
99+
"description_kind": "plain"
100+
}
101+
}
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)