Skip to content

Commit 1ab5e53

Browse files
committed
test: Add integration test for using pluggable state storage with the providers schema command
1 parent 331950d commit 1ab5e53

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

internal/command/providers_schema_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
package command
55

66
import (
7+
"bytes"
78
"encoding/json"
89
"fmt"
910
"io/ioutil"
1011
"os"
1112
"path/filepath"
13+
"strings"
1214
"testing"
1315

1416
"github.com/google/go-cmp/cmp"
1517
"github.com/hashicorp/cli"
1618
"github.com/zclconf/go-cty/cty"
1719

20+
"github.com/hashicorp/terraform/internal/addrs"
1821
"github.com/hashicorp/terraform/internal/configs/configschema"
1922
"github.com/hashicorp/terraform/internal/providers"
2023
testing_provider "github.com/hashicorp/terraform/internal/providers/testing"
24+
"github.com/hashicorp/terraform/internal/states"
25+
"github.com/hashicorp/terraform/internal/states/statefile"
2126
)
2227

2328
func TestProvidersSchema_error(t *testing.T) {
@@ -103,6 +108,81 @@ func TestProvidersSchema_output(t *testing.T) {
103108
}
104109
}
105110

111+
func TestProvidersSchema_output_withStateStore(t *testing.T) {
112+
// State with a 'baz' provider not in the config
113+
originalState := states.BuildState(func(s *states.SyncState) {
114+
s.SetResourceInstanceCurrent(
115+
addrs.Resource{
116+
Mode: addrs.ManagedResourceMode,
117+
Type: "baz_instance",
118+
Name: "foo",
119+
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
120+
&states.ResourceInstanceObjectSrc{
121+
AttrsJSON: []byte(`{"id":"bar"}`),
122+
Status: states.ObjectReady,
123+
},
124+
addrs.AbsProviderConfig{
125+
Provider: addrs.NewDefaultProvider("baz"),
126+
Module: addrs.RootModule,
127+
},
128+
)
129+
})
130+
131+
// Create a temporary working directory that is empty
132+
td := t.TempDir()
133+
testCopyDir(t, testFixturePath("state-store-unchanged"), td)
134+
t.Chdir(td)
135+
136+
// Get bytes describing the state
137+
var stateBuf bytes.Buffer
138+
if err := statefile.Write(statefile.New(originalState, "", 1), &stateBuf); err != nil {
139+
t.Fatalf("error during test setup: %s", err)
140+
}
141+
142+
// Create a mock that contains a persisted "default" state that uses the bytes from above.
143+
mockProvider := mockPluggableStateStorageProvider(t)
144+
mockProvider.MockStates = map[string]interface{}{
145+
"default": stateBuf.Bytes(),
146+
}
147+
mockProviderAddressTest := addrs.NewDefaultProvider("test")
148+
149+
// Mock for the provider in the state
150+
mockProviderAddressBaz := addrs.NewDefaultProvider("baz")
151+
152+
ui := new(cli.MockUi)
153+
c := &ProvidersSchemaCommand{
154+
Meta: Meta{
155+
Ui: ui,
156+
AllowExperimentalFeatures: true,
157+
testingOverrides: &testingOverrides{
158+
Providers: map[addrs.Provider]providers.Factory{
159+
mockProviderAddressTest: providers.FactoryFixed(mockProvider),
160+
mockProviderAddressBaz: providers.FactoryFixed(mockProvider),
161+
},
162+
},
163+
},
164+
}
165+
166+
args := []string{"-json"}
167+
if code := c.Run(args); code != 0 {
168+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
169+
}
170+
171+
wantOutput := []string{
172+
`{"format_version":"1.0","provider_schemas":{`, // Opening of JSON
173+
`"registry.terraform.io/hashicorp/baz":{`, // provider from state
174+
`"registry.terraform.io/hashicorp/test":{`, // provider from config
175+
}
176+
177+
output := ui.OutputWriter.String()
178+
for _, want := range wantOutput {
179+
if !strings.Contains(output, want) {
180+
t.Errorf("output missing %s:\n%s", want, output)
181+
}
182+
}
183+
184+
}
185+
106186
type providerSchemas struct {
107187
FormatVersion string `json:"format_version"`
108188
Schemas map[string]providerSchema `json:"provider_schemas"`

0 commit comments

Comments
 (0)