2019-02-25 15:32:47 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-03-14 16:52:07 -05:00
|
|
|
"path/filepath"
|
2019-02-25 15:32:47 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-05-17 14:17:09 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 12:40:40 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-05-17 14:46:19 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2019-02-25 15:32:47 -06:00
|
|
|
"github.com/mitchellh/cli"
|
2021-03-12 07:28:22 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2019-02-25 15:32:47 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProvidersSchema_error(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ProvidersSchemaCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(nil); code != 1 {
|
|
|
|
fmt.Println(ui.OutputWriter.String())
|
|
|
|
t.Fatalf("expected error: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvidersSchema_output(t *testing.T) {
|
2019-06-30 02:38:36 -05:00
|
|
|
fixtureDir := "testdata/providers-schema"
|
2019-03-14 16:52:07 -05:00
|
|
|
testDirs, err := ioutil.ReadDir(fixtureDir)
|
2019-02-25 15:32:47 -06:00
|
|
|
if err != nil {
|
2019-03-14 16:52:07 -05:00
|
|
|
t.Fatal(err)
|
2019-02-25 15:32:47 -06:00
|
|
|
}
|
|
|
|
|
2019-03-14 16:52:07 -05:00
|
|
|
for _, entry := range testDirs {
|
|
|
|
if !entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Run(entry.Name(), func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
inputDir := filepath.Join(fixtureDir, entry.Name())
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, inputDir, td)
|
2019-03-14 16:52:07 -05:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-03-31 16:02:40 -05:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
2021-03-12 07:28:22 -06:00
|
|
|
"test": {"1.2.3"},
|
2020-03-31 16:02:40 -05:00
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2021-03-12 07:28:22 -06:00
|
|
|
p := providersSchemaFixtureProvider()
|
2019-03-14 16:52:07 -05:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2020-03-31 16:02:40 -05:00
|
|
|
ProviderSource: providerSource,
|
2019-03-14 16:52:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// `terrafrom init`
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the init output from the mock ui
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
// `terraform provider schemas` command
|
|
|
|
pc := &ProvidersSchemaCommand{Meta: m}
|
|
|
|
if code := pc.Run([]string{"-json"}); code != 0 {
|
|
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
var got, want providerSchemas
|
|
|
|
|
|
|
|
gotString := ui.OutputWriter.String()
|
|
|
|
json.Unmarshal([]byte(gotString), &got)
|
|
|
|
|
|
|
|
wantFile, err := os.Open("output.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer wantFile.Close()
|
|
|
|
byteValue, err := ioutil.ReadAll(wantFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(byteValue), &want)
|
|
|
|
|
|
|
|
if !cmp.Equal(got, want) {
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
|
|
|
|
}
|
|
|
|
})
|
2019-02-25 15:32:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type providerSchemas struct {
|
|
|
|
FormatVersion string `json:"format_version"`
|
|
|
|
Schemas map[string]providerSchema `json:"provider_schemas"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type providerSchema struct {
|
|
|
|
Provider interface{} `json:"provider,omitempty"`
|
|
|
|
ResourceSchemas map[string]interface{} `json:"resource_schemas,omitempty"`
|
|
|
|
DataSourceSchemas map[string]interface{} `json:"data_source_schemas,omitempty"`
|
|
|
|
}
|
2021-03-12 07:28:22 -06:00
|
|
|
|
|
|
|
// testProvider returns a mock provider that is configured for basic
|
|
|
|
// operation with the configuration in testdata/providers-schema.
|
|
|
|
func providersSchemaFixtureProvider() *terraform.MockProvider {
|
|
|
|
p := testProvider()
|
|
|
|
p.GetProviderSchemaResponse = providersSchemaFixtureSchema()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// providersSchemaFixtureSchema returns a schema suitable for processing the
|
|
|
|
// configuration in testdata/providers-schema.ß
|
|
|
|
func providersSchemaFixtureSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"region": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"test_instance": {
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
"volumes": {
|
|
|
|
NestedType: &configschema.Object{
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"size": {Type: cty.String, Required: true},
|
|
|
|
"mount_point": {Type: cty.String, Required: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|