github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/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/hashicorp/terraform/internal/configs/configschema"
    13  	"github.com/hashicorp/terraform/internal/providers"
    14  	"github.com/hashicorp/terraform/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 := t.TempDir()
    47  			inputDir := filepath.Join(fixtureDir, entry.Name())
    48  			testCopyDir(t, inputDir, td)
    49  			defer testChdir(t, td)()
    50  
    51  			providerSource, close := newMockProviderSource(t, map[string][]string{
    52  				"test": {"1.2.3"},
    53  			})
    54  			defer close()
    55  
    56  			p := providersSchemaFixtureProvider()
    57  			ui := new(cli.MockUi)
    58  			m := Meta{
    59  				testingOverrides: metaOverridesForProvider(p),
    60  				Ui:               ui,
    61  				ProviderSource:   providerSource,
    62  			}
    63  
    64  			// `terrafrom init`
    65  			ic := &InitCommand{
    66  				Meta: m,
    67  			}
    68  			if code := ic.Run([]string{}); code != 0 {
    69  				t.Fatalf("init failed\n%s", ui.ErrorWriter)
    70  			}
    71  
    72  			// flush the init output from the mock ui
    73  			ui.OutputWriter.Reset()
    74  
    75  			// `terraform provider schemas` command
    76  			pc := &ProvidersSchemaCommand{Meta: m}
    77  			if code := pc.Run([]string{"-json"}); code != 0 {
    78  				t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
    79  			}
    80  			var got, want providerSchemas
    81  
    82  			gotString := ui.OutputWriter.String()
    83  			json.Unmarshal([]byte(gotString), &got)
    84  
    85  			wantFile, err := os.Open("output.json")
    86  			if err != nil {
    87  				t.Fatalf("err: %s", err)
    88  			}
    89  			defer wantFile.Close()
    90  			byteValue, err := ioutil.ReadAll(wantFile)
    91  			if err != nil {
    92  				t.Fatalf("err: %s", err)
    93  			}
    94  			json.Unmarshal([]byte(byteValue), &want)
    95  
    96  			if !cmp.Equal(got, want) {
    97  				t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  type providerSchemas struct {
   104  	FormatVersion string                    `json:"format_version"`
   105  	Schemas       map[string]providerSchema `json:"provider_schemas"`
   106  }
   107  
   108  type providerSchema struct {
   109  	Provider          interface{}            `json:"provider,omitempty"`
   110  	ResourceSchemas   map[string]interface{} `json:"resource_schemas,omitempty"`
   111  	DataSourceSchemas map[string]interface{} `json:"data_source_schemas,omitempty"`
   112  }
   113  
   114  // testProvider returns a mock provider that is configured for basic
   115  // operation with the configuration in testdata/providers-schema.
   116  func providersSchemaFixtureProvider() *terraform.MockProvider {
   117  	p := testProvider()
   118  	p.GetProviderSchemaResponse = providersSchemaFixtureSchema()
   119  	return p
   120  }
   121  
   122  // providersSchemaFixtureSchema returns a schema suitable for processing the
   123  // configuration in testdata/providers-schema.ß
   124  func providersSchemaFixtureSchema() *providers.GetProviderSchemaResponse {
   125  	return &providers.GetProviderSchemaResponse{
   126  		Provider: providers.Schema{
   127  			Block: &configschema.Block{
   128  				Attributes: map[string]*configschema.Attribute{
   129  					"region": {Type: cty.String, Optional: true},
   130  				},
   131  			},
   132  		},
   133  		ResourceTypes: map[string]providers.Schema{
   134  			"test_instance": {
   135  				Block: &configschema.Block{
   136  					Attributes: map[string]*configschema.Attribute{
   137  						"id":  {Type: cty.String, Optional: true, Computed: true},
   138  						"ami": {Type: cty.String, Optional: true},
   139  						"volumes": {
   140  							NestedType: &configschema.Object{
   141  								Nesting: configschema.NestingList,
   142  								Attributes: map[string]*configschema.Attribute{
   143  									"size":        {Type: cty.String, Required: true},
   144  									"mount_point": {Type: cty.String, Required: true},
   145  								},
   146  							},
   147  							Optional: true,
   148  						},
   149  					},
   150  				},
   151  			},
   152  		},
   153  	}
   154  }