github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/command/providers_schema_test.go (about) 1 package command 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/google/go-cmp/cmp" 12 "github.com/muratcelep/terraform/not-internal/configs/configschema" 13 "github.com/muratcelep/terraform/not-internal/providers" 14 "github.com/muratcelep/terraform/not-internal/terraform" 15 "github.com/mitchellh/cli" 16 "github.com/zclconf/go-cty/cty" 17 ) 18 19 func TestProvidersSchema_error(t *testing.T) { 20 ui := new(cli.MockUi) 21 c := &ProvidersSchemaCommand{ 22 Meta: Meta{ 23 testingOverrides: metaOverridesForProvider(testProvider()), 24 Ui: ui, 25 }, 26 } 27 28 if code := c.Run(nil); code != 1 { 29 fmt.Println(ui.OutputWriter.String()) 30 t.Fatalf("expected error: \n%s", ui.OutputWriter.String()) 31 } 32 } 33 34 func TestProvidersSchema_output(t *testing.T) { 35 fixtureDir := "testdata/providers-schema" 36 testDirs, err := ioutil.ReadDir(fixtureDir) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 for _, entry := range testDirs { 42 if !entry.IsDir() { 43 continue 44 } 45 t.Run(entry.Name(), func(t *testing.T) { 46 td := tempDir(t) 47 inputDir := filepath.Join(fixtureDir, entry.Name()) 48 testCopyDir(t, inputDir, td) 49 defer os.RemoveAll(td) 50 defer testChdir(t, td)() 51 52 providerSource, close := newMockProviderSource(t, map[string][]string{ 53 "test": {"1.2.3"}, 54 }) 55 defer close() 56 57 p := providersSchemaFixtureProvider() 58 ui := new(cli.MockUi) 59 m := Meta{ 60 testingOverrides: metaOverridesForProvider(p), 61 Ui: ui, 62 ProviderSource: providerSource, 63 } 64 65 // `terrafrom init` 66 ic := &InitCommand{ 67 Meta: m, 68 } 69 if code := ic.Run([]string{}); code != 0 { 70 t.Fatalf("init failed\n%s", ui.ErrorWriter) 71 } 72 73 // flush the init output from the mock ui 74 ui.OutputWriter.Reset() 75 76 // `terraform provider schemas` command 77 pc := &ProvidersSchemaCommand{Meta: m} 78 if code := pc.Run([]string{"-json"}); code != 0 { 79 t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String()) 80 } 81 var got, want providerSchemas 82 83 gotString := ui.OutputWriter.String() 84 json.Unmarshal([]byte(gotString), &got) 85 86 wantFile, err := os.Open("output.json") 87 if err != nil { 88 t.Fatalf("err: %s", err) 89 } 90 defer wantFile.Close() 91 byteValue, err := ioutil.ReadAll(wantFile) 92 if err != nil { 93 t.Fatalf("err: %s", err) 94 } 95 json.Unmarshal([]byte(byteValue), &want) 96 97 if !cmp.Equal(got, want) { 98 t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want)) 99 } 100 }) 101 } 102 } 103 104 type providerSchemas struct { 105 FormatVersion string `json:"format_version"` 106 Schemas map[string]providerSchema `json:"provider_schemas"` 107 } 108 109 type providerSchema struct { 110 Provider interface{} `json:"provider,omitempty"` 111 ResourceSchemas map[string]interface{} `json:"resource_schemas,omitempty"` 112 DataSourceSchemas map[string]interface{} `json:"data_source_schemas,omitempty"` 113 } 114 115 // testProvider returns a mock provider that is configured for basic 116 // operation with the configuration in testdata/providers-schema. 117 func providersSchemaFixtureProvider() *terraform.MockProvider { 118 p := testProvider() 119 p.GetProviderSchemaResponse = providersSchemaFixtureSchema() 120 return p 121 } 122 123 // providersSchemaFixtureSchema returns a schema suitable for processing the 124 // configuration in testdata/providers-schema.ß 125 func providersSchemaFixtureSchema() *providers.GetProviderSchemaResponse { 126 return &providers.GetProviderSchemaResponse{ 127 Provider: providers.Schema{ 128 Block: &configschema.Block{ 129 Attributes: map[string]*configschema.Attribute{ 130 "region": {Type: cty.String, Optional: true}, 131 }, 132 }, 133 }, 134 ResourceTypes: map[string]providers.Schema{ 135 "test_instance": { 136 Block: &configschema.Block{ 137 Attributes: map[string]*configschema.Attribute{ 138 "id": {Type: cty.String, Optional: true, Computed: true}, 139 "ami": {Type: cty.String, Optional: true}, 140 "volumes": { 141 NestedType: &configschema.Object{ 142 Nesting: configschema.NestingList, 143 Attributes: map[string]*configschema.Attribute{ 144 "size": {Type: cty.String, Required: true}, 145 "mount_point": {Type: cty.String, Required: true}, 146 }, 147 }, 148 Optional: true, 149 }, 150 }, 151 }, 152 }, 153 }, 154 } 155 }