kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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 "kubeform.dev/terraform-backend-sdk/addrs" 10 ) 11 12 func TestLoadModuleCall(t *testing.T) { 13 src, err := ioutil.ReadFile("testdata/invalid-files/module-calls.tf") 14 if err != nil { 15 t.Fatal(err) 16 } 17 18 parser := testParser(map[string]string{ 19 "module-calls.tf": string(src), 20 }) 21 22 file, diags := parser.LoadConfigFile("module-calls.tf") 23 assertExactDiagnostics(t, diags, []string{ 24 `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.`, 25 }) 26 27 gotModules := file.ModuleCalls 28 wantModules := []*ModuleCall{ 29 { 30 Name: "foo", 31 SourceAddr: addrs.ModuleSourceLocal("./foo"), 32 SourceAddrRaw: "./foo", 33 SourceSet: true, 34 SourceAddrRange: hcl.Range{ 35 Filename: "module-calls.tf", 36 Start: hcl.Pos{Line: 3, Column: 12, Byte: 27}, 37 End: hcl.Pos{Line: 3, Column: 19, Byte: 34}, 38 }, 39 DeclRange: hcl.Range{ 40 Filename: "module-calls.tf", 41 Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, 42 End: hcl.Pos{Line: 2, Column: 13, Byte: 13}, 43 }, 44 }, 45 { 46 Name: "bar", 47 SourceAddr: addrs.ModuleSourceRegistry{ 48 PackageAddr: addrs.ModuleRegistryPackage{ 49 Host: addrs.DefaultModuleRegistryHost, 50 Namespace: "hashicorp", 51 Name: "bar", 52 TargetSystem: "aws", 53 }, 54 }, 55 SourceAddrRaw: "hashicorp/bar/aws", 56 SourceSet: true, 57 SourceAddrRange: hcl.Range{ 58 Filename: "module-calls.tf", 59 Start: hcl.Pos{Line: 8, Column: 12, Byte: 113}, 60 End: hcl.Pos{Line: 8, Column: 31, Byte: 132}, 61 }, 62 DeclRange: hcl.Range{ 63 Filename: "module-calls.tf", 64 Start: hcl.Pos{Line: 7, Column: 1, Byte: 87}, 65 End: hcl.Pos{Line: 7, Column: 13, Byte: 99}, 66 }, 67 }, 68 { 69 Name: "baz", 70 SourceAddr: addrs.ModuleSourceRemote{ 71 PackageAddr: addrs.ModulePackage("git::https://example.com/"), 72 }, 73 SourceAddrRaw: "git::https://example.com/", 74 SourceSet: true, 75 SourceAddrRange: hcl.Range{ 76 Filename: "module-calls.tf", 77 Start: hcl.Pos{Line: 15, Column: 12, Byte: 193}, 78 End: hcl.Pos{Line: 15, Column: 39, Byte: 220}, 79 }, 80 DependsOn: []hcl.Traversal{ 81 { 82 hcl.TraverseRoot{ 83 Name: "module", 84 SrcRange: hcl.Range{ 85 Filename: "module-calls.tf", 86 Start: hcl.Pos{Line: 23, Column: 5, Byte: 295}, 87 End: hcl.Pos{Line: 23, Column: 11, Byte: 301}, 88 }, 89 }, 90 hcl.TraverseAttr{ 91 Name: "bar", 92 SrcRange: hcl.Range{ 93 Filename: "module-calls.tf", 94 Start: hcl.Pos{Line: 23, Column: 11, Byte: 301}, 95 End: hcl.Pos{Line: 23, Column: 15, Byte: 305}, 96 }, 97 }, 98 }, 99 }, 100 Providers: []PassedProviderConfig{ 101 { 102 InChild: &ProviderConfigRef{ 103 Name: "aws", 104 NameRange: hcl.Range{ 105 Filename: "module-calls.tf", 106 Start: hcl.Pos{Line: 27, Column: 5, Byte: 332}, 107 End: hcl.Pos{Line: 27, Column: 8, Byte: 335}, 108 }, 109 }, 110 InParent: &ProviderConfigRef{ 111 Name: "aws", 112 NameRange: hcl.Range{ 113 Filename: "module-calls.tf", 114 Start: hcl.Pos{Line: 27, Column: 11, Byte: 338}, 115 End: hcl.Pos{Line: 27, Column: 14, Byte: 341}, 116 }, 117 Alias: "foo", 118 AliasRange: &hcl.Range{ 119 Filename: "module-calls.tf", 120 Start: hcl.Pos{Line: 27, Column: 14, Byte: 341}, 121 End: hcl.Pos{Line: 27, Column: 18, Byte: 345}, 122 }, 123 }, 124 }, 125 }, 126 DeclRange: hcl.Range{ 127 Filename: "module-calls.tf", 128 Start: hcl.Pos{Line: 14, Column: 1, Byte: 167}, 129 End: hcl.Pos{Line: 14, Column: 13, Byte: 179}, 130 }, 131 }, 132 } 133 134 // We'll hide all of the bodies/exprs since we're treating them as opaque 135 // here anyway... the point of this test is to ensure we handle everything 136 // else properly. 137 for _, m := range gotModules { 138 m.Config = nil 139 m.Count = nil 140 m.ForEach = nil 141 } 142 143 for _, problem := range deep.Equal(gotModules, wantModules) { 144 t.Error(problem) 145 } 146 } 147 148 func TestModuleSourceAddrEntersNewPackage(t *testing.T) { 149 tests := []struct { 150 Addr string 151 Want bool 152 }{ 153 { 154 "./", 155 false, 156 }, 157 { 158 "../bork", 159 false, 160 }, 161 { 162 "/absolute/path", 163 true, 164 }, 165 { 166 "github.com/example/foo", 167 true, 168 }, 169 { 170 "hashicorp/subnets/cidr", // registry module 171 true, 172 }, 173 { 174 "registry.terraform.io/hashicorp/subnets/cidr", // registry module 175 true, 176 }, 177 } 178 179 for _, test := range tests { 180 t.Run(test.Addr, func(t *testing.T) { 181 addr, err := addrs.ParseModuleSource(test.Addr) 182 if err != nil { 183 t.Fatalf("parsing failed for %q: %s", test.Addr, err) 184 } 185 186 got := moduleSourceAddrEntersNewPackage(addr) 187 if got != test.Want { 188 t.Errorf("wrong result for %q\ngot: %#v\nwant: %#v", addr, got, test.Want) 189 } 190 }) 191 } 192 }