github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/internal/command/jsonprovider/provider_test.go (about)

     1  package jsonprovider
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/zclconf/go-cty/cty"
     9  
    10  	"github.com/eliastor/durgaform/internal/configs/configschema"
    11  	"github.com/eliastor/durgaform/internal/durgaform"
    12  )
    13  
    14  func TestMarshalProvider(t *testing.T) {
    15  	tests := []struct {
    16  		Input *durgaform.ProviderSchema
    17  		Want  *Provider
    18  	}{
    19  		{
    20  			nil,
    21  			&Provider{},
    22  		},
    23  		{
    24  			testProvider(),
    25  			&Provider{
    26  				Provider: &schema{
    27  					Block: &block{
    28  						Attributes: map[string]*attribute{
    29  							"region": {
    30  								AttributeType:   json.RawMessage(`"string"`),
    31  								Required:        true,
    32  								DescriptionKind: "plain",
    33  							},
    34  						},
    35  						DescriptionKind: "plain",
    36  					},
    37  				},
    38  				ResourceSchemas: map[string]*schema{
    39  					"test_instance": {
    40  						Version: 42,
    41  						Block: &block{
    42  							Attributes: map[string]*attribute{
    43  								"id": {
    44  									AttributeType:   json.RawMessage(`"string"`),
    45  									Optional:        true,
    46  									Computed:        true,
    47  									DescriptionKind: "plain",
    48  								},
    49  								"ami": {
    50  									AttributeType:   json.RawMessage(`"string"`),
    51  									Optional:        true,
    52  									DescriptionKind: "plain",
    53  								},
    54  								"volumes": {
    55  									AttributeNestedType: &nestedType{
    56  										NestingMode: "list",
    57  										Attributes: map[string]*attribute{
    58  											"size": {
    59  												AttributeType:   json.RawMessage(`"string"`),
    60  												Required:        true,
    61  												DescriptionKind: "plain",
    62  											},
    63  											"mount_point": {
    64  												AttributeType:   json.RawMessage(`"string"`),
    65  												Required:        true,
    66  												DescriptionKind: "plain",
    67  											},
    68  										},
    69  									},
    70  									Optional:        true,
    71  									DescriptionKind: "plain",
    72  								},
    73  							},
    74  							BlockTypes: map[string]*blockType{
    75  								"network_interface": {
    76  									Block: &block{
    77  										Attributes: map[string]*attribute{
    78  											"device_index": {
    79  												AttributeType:   json.RawMessage(`"string"`),
    80  												Optional:        true,
    81  												DescriptionKind: "plain",
    82  											},
    83  											"description": {
    84  												AttributeType:   json.RawMessage(`"string"`),
    85  												Optional:        true,
    86  												DescriptionKind: "plain",
    87  											},
    88  										},
    89  										DescriptionKind: "plain",
    90  									},
    91  									NestingMode: "list",
    92  								},
    93  							},
    94  							DescriptionKind: "plain",
    95  						},
    96  					},
    97  				},
    98  				DataSourceSchemas: map[string]*schema{
    99  					"test_data_source": {
   100  						Version: 3,
   101  						Block: &block{
   102  							Attributes: map[string]*attribute{
   103  								"id": {
   104  									AttributeType:   json.RawMessage(`"string"`),
   105  									Optional:        true,
   106  									Computed:        true,
   107  									DescriptionKind: "plain",
   108  								},
   109  								"ami": {
   110  									AttributeType:   json.RawMessage(`"string"`),
   111  									Optional:        true,
   112  									DescriptionKind: "plain",
   113  								},
   114  							},
   115  							BlockTypes: map[string]*blockType{
   116  								"network_interface": {
   117  									Block: &block{
   118  										Attributes: map[string]*attribute{
   119  											"device_index": {
   120  												AttributeType:   json.RawMessage(`"string"`),
   121  												Optional:        true,
   122  												DescriptionKind: "plain",
   123  											},
   124  											"description": {
   125  												AttributeType:   json.RawMessage(`"string"`),
   126  												Optional:        true,
   127  												DescriptionKind: "plain",
   128  											},
   129  										},
   130  										DescriptionKind: "plain",
   131  									},
   132  									NestingMode: "list",
   133  								},
   134  							},
   135  							DescriptionKind: "plain",
   136  						},
   137  					},
   138  				},
   139  			},
   140  		},
   141  	}
   142  
   143  	for _, test := range tests {
   144  		got := marshalProvider(test.Input)
   145  		if !cmp.Equal(got, test.Want) {
   146  			t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
   147  		}
   148  	}
   149  }
   150  
   151  func testProvider() *durgaform.ProviderSchema {
   152  	return &durgaform.ProviderSchema{
   153  		Provider: &configschema.Block{
   154  			Attributes: map[string]*configschema.Attribute{
   155  				"region": {Type: cty.String, Required: true},
   156  			},
   157  		},
   158  		ResourceTypes: map[string]*configschema.Block{
   159  			"test_instance": {
   160  				Attributes: map[string]*configschema.Attribute{
   161  					"id":  {Type: cty.String, Optional: true, Computed: true},
   162  					"ami": {Type: cty.String, Optional: true},
   163  					"volumes": {
   164  						Optional: true,
   165  						NestedType: &configschema.Object{
   166  							Nesting: configschema.NestingList,
   167  							Attributes: map[string]*configschema.Attribute{
   168  								"size":        {Type: cty.String, Required: true},
   169  								"mount_point": {Type: cty.String, Required: true},
   170  							},
   171  						},
   172  					},
   173  				},
   174  				BlockTypes: map[string]*configschema.NestedBlock{
   175  					"network_interface": {
   176  						Nesting: configschema.NestingList,
   177  						Block: configschema.Block{
   178  							Attributes: map[string]*configschema.Attribute{
   179  								"device_index": {Type: cty.String, Optional: true},
   180  								"description":  {Type: cty.String, Optional: true},
   181  							},
   182  						},
   183  					},
   184  				},
   185  			},
   186  		},
   187  		DataSources: map[string]*configschema.Block{
   188  			"test_data_source": {
   189  				Attributes: map[string]*configschema.Attribute{
   190  					"id":  {Type: cty.String, Optional: true, Computed: true},
   191  					"ami": {Type: cty.String, Optional: true},
   192  				},
   193  				BlockTypes: map[string]*configschema.NestedBlock{
   194  					"network_interface": {
   195  						Nesting: configschema.NestingList,
   196  						Block: configschema.Block{
   197  							Attributes: map[string]*configschema.Attribute{
   198  								"device_index": {Type: cty.String, Optional: true},
   199  								"description":  {Type: cty.String, Optional: true},
   200  							},
   201  						},
   202  					},
   203  				},
   204  			},
   205  		},
   206  
   207  		ResourceTypeSchemaVersions: map[string]uint64{
   208  			"test_instance":    42,
   209  			"test_data_source": 3,
   210  		},
   211  	}
   212  }