github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/configs/module_call_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  	"github.com/hashicorp/hcl/v2"
     9  )
    10  
    11  func TestLoadModuleCall(t *testing.T) {
    12  	src, err := ioutil.ReadFile("testdata/invalid-files/module-calls.tf")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	parser := testParser(map[string]string{
    18  		"module-calls.tf": string(src),
    19  	})
    20  
    21  	file, diags := parser.LoadConfigFile("module-calls.tf")
    22  	assertExactDiagnostics(t, diags, []string{
    23  		`module-calls.tf:20,3-11: Invalid combination of "count" and "for_each"; The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created.`,
    24  	})
    25  
    26  	gotModules := file.ModuleCalls
    27  	wantModules := []*ModuleCall{
    28  		{
    29  			Name:       "foo",
    30  			SourceAddr: "./foo",
    31  			SourceSet:  true,
    32  			SourceAddrRange: hcl.Range{
    33  				Filename: "module-calls.tf",
    34  				Start:    hcl.Pos{Line: 3, Column: 12, Byte: 27},
    35  				End:      hcl.Pos{Line: 3, Column: 19, Byte: 34},
    36  			},
    37  			DeclRange: hcl.Range{
    38  				Filename: "module-calls.tf",
    39  				Start:    hcl.Pos{Line: 2, Column: 1, Byte: 1},
    40  				End:      hcl.Pos{Line: 2, Column: 13, Byte: 13},
    41  			},
    42  		},
    43  		{
    44  			Name:       "bar",
    45  			SourceAddr: "hashicorp/bar/aws",
    46  			SourceSet:  true,
    47  			SourceAddrRange: hcl.Range{
    48  				Filename: "module-calls.tf",
    49  				Start:    hcl.Pos{Line: 8, Column: 12, Byte: 113},
    50  				End:      hcl.Pos{Line: 8, Column: 31, Byte: 132},
    51  			},
    52  			DeclRange: hcl.Range{
    53  				Filename: "module-calls.tf",
    54  				Start:    hcl.Pos{Line: 7, Column: 1, Byte: 87},
    55  				End:      hcl.Pos{Line: 7, Column: 13, Byte: 99},
    56  			},
    57  		},
    58  		{
    59  			Name:       "baz",
    60  			SourceAddr: "git::https://example.com/",
    61  			SourceSet:  true,
    62  			SourceAddrRange: hcl.Range{
    63  				Filename: "module-calls.tf",
    64  				Start:    hcl.Pos{Line: 15, Column: 12, Byte: 193},
    65  				End:      hcl.Pos{Line: 15, Column: 39, Byte: 220},
    66  			},
    67  			DependsOn: []hcl.Traversal{
    68  				{
    69  					hcl.TraverseRoot{
    70  						Name: "module",
    71  						SrcRange: hcl.Range{
    72  							Filename: "module-calls.tf",
    73  							Start:    hcl.Pos{Line: 23, Column: 5, Byte: 295},
    74  							End:      hcl.Pos{Line: 23, Column: 11, Byte: 301},
    75  						},
    76  					},
    77  					hcl.TraverseAttr{
    78  						Name: "bar",
    79  						SrcRange: hcl.Range{
    80  							Filename: "module-calls.tf",
    81  							Start:    hcl.Pos{Line: 23, Column: 11, Byte: 301},
    82  							End:      hcl.Pos{Line: 23, Column: 15, Byte: 305},
    83  						},
    84  					},
    85  				},
    86  			},
    87  			Providers: []PassedProviderConfig{
    88  				{
    89  					InChild: &ProviderConfigRef{
    90  						Name: "aws",
    91  						NameRange: hcl.Range{
    92  							Filename: "module-calls.tf",
    93  							Start:    hcl.Pos{Line: 27, Column: 5, Byte: 332},
    94  							End:      hcl.Pos{Line: 27, Column: 8, Byte: 335},
    95  						},
    96  					},
    97  					InParent: &ProviderConfigRef{
    98  						Name: "aws",
    99  						NameRange: hcl.Range{
   100  							Filename: "module-calls.tf",
   101  							Start:    hcl.Pos{Line: 27, Column: 11, Byte: 338},
   102  							End:      hcl.Pos{Line: 27, Column: 14, Byte: 341},
   103  						},
   104  						Alias: "foo",
   105  						AliasRange: &hcl.Range{
   106  							Filename: "module-calls.tf",
   107  							Start:    hcl.Pos{Line: 27, Column: 14, Byte: 341},
   108  							End:      hcl.Pos{Line: 27, Column: 18, Byte: 345},
   109  						},
   110  					},
   111  				},
   112  			},
   113  			DeclRange: hcl.Range{
   114  				Filename: "module-calls.tf",
   115  				Start:    hcl.Pos{Line: 14, Column: 1, Byte: 167},
   116  				End:      hcl.Pos{Line: 14, Column: 13, Byte: 179},
   117  			},
   118  		},
   119  	}
   120  
   121  	// We'll hide all of the bodies/exprs since we're treating them as opaque
   122  	// here anyway... the point of this test is to ensure we handle everything
   123  	// else properly.
   124  	for _, m := range gotModules {
   125  		m.Config = nil
   126  		m.Count = nil
   127  		m.ForEach = nil
   128  	}
   129  
   130  	for _, problem := range deep.Equal(gotModules, wantModules) {
   131  		t.Error(problem)
   132  	}
   133  }