github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/tflint/loader_test.go (about) 1 package tflint 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/google/go-cmp/cmp/cmpopts" 10 hcl "github.com/hashicorp/hcl/v2" 11 "github.com/hashicorp/hcl/v2/hclsyntax" 12 "github.com/hashicorp/terraform/terraform" 13 "github.com/spf13/afero" 14 "github.com/zclconf/go-cty/cty" 15 ) 16 17 func Test_LoadConfig_v0_12_0(t *testing.T) { 18 withinFixtureDir(t, "v0.12.0_module", func() { 19 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, moduleConfig()) 20 if err != nil { 21 t.Fatalf("Unexpected error occurred: %s", err) 22 } 23 config, err := loader.LoadConfig(".") 24 if err != nil { 25 t.Fatalf("Unexpected error occurred: %s", err) 26 } 27 28 if _, exists := config.Children["ecs_on_spotfleet"]; !exists { 29 t.Fatalf("`ecs_on_spotfleet` module is not loaded: %#v", config.Children) 30 } 31 32 if _, exists := config.Children["ecs_on_spotfleet"].Module.ManagedResources["aws_ecs_cluster.main"]; !exists { 33 t.Fatalf("`ecs_on_spotfleet` module resource `aws_ecs_cluster.main` is not loaded: %#v", config.Children["ecs_on_spotfleet"].Module.ManagedResources) 34 } 35 36 if _, exists := config.Children["instance"]; !exists { 37 t.Fatalf("`instance` module is not loaded: %#v", config.Children) 38 } 39 40 if _, exists := config.Children["consul"]; !exists { 41 t.Fatalf("`consul` module is not loaded: %#v", config.Children) 42 } 43 44 if _, exists := config.Children["consul"].Children["consul_clients"]; !exists { 45 t.Fatalf("`consul.consul_clients` module is not loaded: %#v", config.Children["consul"].Children) 46 } 47 48 if _, exists := config.Children["consul"].Children["consul_clients"].Children["iam_policies"]; !exists { 49 t.Fatalf("`consule.consul_clients.iam_policies` module is not loaded: %#v", config.Children["consul"].Children["consul_clients"].Children) 50 } 51 52 if _, exists := config.Children["consul"].Children["consul_clients"].Children["iam_policies"].Module.ManagedResources["aws_iam_role_policy.auto_discover_cluster"]; !exists { 53 t.Fatalf("`consule.consul_clients.iam_policies` module resource `aws_iam_role_policy.auto_discover_cluster` is not loaded: %#v", config.Children["consul"].Children["consul_clients"].Children["iam_policies"].Module.ManagedResources) 54 } 55 56 if _, exists := config.Children["consul"].Children["consul_clients"].Children["security_group_rules"]; !exists { 57 t.Fatalf("`consule.consul_clients.security_group_rules` module is not loaded: %#v", config.Children["consul"].Children["consul_clients"].Children) 58 } 59 60 if _, exists := config.Children["consul"].Children["consul_clients"].Children["security_group_rules"].Module.ManagedResources["aws_security_group_rule.allow_server_rpc_inbound"]; !exists { 61 t.Fatalf("`consule.consul_clients.security_group_rules` module resource `aws_security_group_rule.allow_server_rpc_inbound` is not loaded: %#v", config.Children["consul"].Children["consul_clients"].Children["security_group_rules"].Module.ManagedResources) 62 } 63 64 if _, exists := config.Children["consul"].Children["consul_servers"]; !exists { 65 t.Fatalf("`consul.consul_servers` module is not loaded: %#v", config.Children["consul"].Children) 66 } 67 68 if _, exists := config.Children["consul"].Children["consul_servers"].Children["iam_policies"]; !exists { 69 t.Fatalf("`consule.consul_servers.iam_policies` module is not loaded: %#v", config.Children["consul"].Children["consul_servers"].Children) 70 } 71 72 if _, exists := config.Children["consul"].Children["consul_servers"].Children["iam_policies"].Module.ManagedResources["aws_iam_role_policy.auto_discover_cluster"]; !exists { 73 t.Fatalf("`consule.consul_servers.iam_policies` module resource `aws_iam_role_policy.auto_discover_cluster` is not loaded: %#v", config.Children["consul"].Children["consul_servers"].Children["iam_policies"].Module.ManagedResources) 74 } 75 76 if _, exists := config.Children["consul"].Children["consul_servers"].Children["security_group_rules"]; !exists { 77 t.Fatalf("`consule.consul_servers.security_group_rules` module is not loaded: %#v", config.Children["consul"].Children["consul_servers"].Children) 78 } 79 80 if _, exists := config.Children["consul"].Children["consul_servers"].Children["security_group_rules"].Module.ManagedResources["aws_security_group_rule.allow_server_rpc_inbound"]; !exists { 81 t.Fatalf("`consule.consul_servers.security_group_rules` module resource `aws_security_group_rule.allow_server_rpc_inbound` is not loaded: %#v", config.Children["consul"].Children["consul_servers"].Children["security_group_rules"].Module.ManagedResources) 82 } 83 }) 84 } 85 86 func Test_LoadConfig_moduleNotFound(t *testing.T) { 87 withinFixtureDir(t, "before_terraform_init", func() { 88 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, moduleConfig()) 89 if err != nil { 90 t.Fatalf("Unexpected error occurred: %s", err) 91 } 92 _, err = loader.LoadConfig(".") 93 if err == nil { 94 t.Fatal("Expected error is not occurred") 95 } 96 97 expected := "module.tf:1,1-22: `ec2_instance` module is not found. Did you run `terraform init`?; " 98 if err.Error() != expected { 99 t.Fatalf("Expected error is `%s`, but get `%s`", expected, err.Error()) 100 } 101 }) 102 } 103 104 func Test_LoadConfig_disableModules(t *testing.T) { 105 withinFixtureDir(t, "before_terraform_init", func() { 106 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig()) 107 if err != nil { 108 t.Fatalf("Unexpected error occurred: %s", err) 109 } 110 config, err := loader.LoadConfig(".") 111 if err != nil { 112 t.Fatalf("Unexpected error occurred: %s", err) 113 } 114 115 if len(config.Children) != 0 { 116 t.Fatalf("Root module has children unexpectedly: %#v", config.Children) 117 } 118 }) 119 } 120 121 func Test_LoadConfig_invalidConfiguration(t *testing.T) { 122 withinFixtureDir(t, "invalid_configuration", func() { 123 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig()) 124 if err != nil { 125 t.Fatalf("Unexpected error occurred: %s", err) 126 } 127 _, err = loader.LoadConfig(".") 128 if err == nil { 129 t.Fatal("Expected error is not occurred") 130 } 131 132 expected := "resource.tf:1,1-10: Unsupported block type; Blocks of type \"resources\" are not expected here. Did you mean \"resource\"?" 133 if err.Error() != expected { 134 t.Fatalf("Expected error is `%s`, but get `%s`", expected, err.Error()) 135 } 136 }) 137 } 138 139 func Test_Files(t *testing.T) { 140 withinFixtureDir(t, "v0.12.0_module", func() { 141 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig()) 142 if err != nil { 143 t.Fatalf("Unexpected error occurred: %s", err) 144 } 145 _, err = loader.LoadConfig(".") 146 if err != nil { 147 t.Fatalf("Unexpected error occurred: %s", err) 148 } 149 150 files, err := loader.Files() 151 if err != nil { 152 t.Fatalf("Unexpected error occurred: %s", err) 153 } 154 155 filename := "module.tf" 156 b, err := afero.ReadFile(loader.fs, filename) 157 158 expected := map[string]*hcl.File{ 159 "module.tf": {Bytes: b}, 160 } 161 162 opts := cmpopts.IgnoreFields(hcl.File{}, "Body", "Nav") 163 if !cmp.Equal(expected, files, opts) { 164 t.Fatalf("Test failed. Diff: %s", cmp.Diff(expected, files, opts)) 165 } 166 }) 167 } 168 169 func Test_LoadAnnotations(t *testing.T) { 170 withinFixtureDir(t, "annotation_files", func() { 171 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig()) 172 if err != nil { 173 t.Fatalf("Unexpected error occurred: %s", err) 174 } 175 ret, err := loader.LoadAnnotations(".") 176 if err != nil { 177 t.Fatalf("Unexpected error occurred: %s", err) 178 } 179 180 expected := map[string]Annotations{ 181 "file1.tf": { 182 { 183 Content: "aws_instance_invalid_type", 184 Token: hclsyntax.Token{ 185 Type: hclsyntax.TokenComment, 186 Bytes: []byte(fmt.Sprintf("// tflint-ignore: aws_instance_invalid_type%s", newLine())), 187 Range: hcl.Range{ 188 Filename: "file1.tf", 189 Start: hcl.Pos{Line: 2, Column: 5}, 190 End: hcl.Pos{Line: 3, Column: 1}, 191 }, 192 }, 193 }, 194 }, 195 "file2.tf": { 196 { 197 Content: "aws_instance_invalid_type", 198 Token: hclsyntax.Token{ 199 Type: hclsyntax.TokenComment, 200 Bytes: []byte(fmt.Sprintf("// tflint-ignore: aws_instance_invalid_type%s", newLine())), 201 Range: hcl.Range{ 202 Filename: "file2.tf", 203 Start: hcl.Pos{Line: 2, Column: 32}, 204 End: hcl.Pos{Line: 3, Column: 1}, 205 }, 206 }, 207 }, 208 }, 209 "file3.tf": {}, 210 } 211 212 opts := cmpopts.IgnoreFields(hcl.Pos{}, "Byte") 213 if !cmp.Equal(expected, ret, opts) { 214 t.Fatalf("Test failed. Diff: %s", cmp.Diff(expected, ret, opts)) 215 } 216 }) 217 } 218 219 func Test_LoadValuesFiles(t *testing.T) { 220 withinFixtureDir(t, "values_files", func() { 221 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig()) 222 if err != nil { 223 t.Fatalf("Unexpected error occurred: %s", err) 224 } 225 ret, err := loader.LoadValuesFiles("cli1.tfvars", "cli2.tfvars") 226 if err != nil { 227 t.Fatalf("Unexpected error occurred: %s", err) 228 } 229 230 expected := []terraform.InputValues{ 231 { 232 "default": { 233 Value: cty.StringVal("terraform.tfvars"), 234 SourceType: terraform.ValueFromAutoFile, 235 }, 236 }, 237 { 238 "auto1": { 239 Value: cty.StringVal("auto1.auto.tfvars"), 240 SourceType: terraform.ValueFromAutoFile, 241 }, 242 }, 243 { 244 "auto2": { 245 Value: cty.StringVal("auto2.auto.tfvars"), 246 SourceType: terraform.ValueFromAutoFile, 247 }, 248 }, 249 { 250 "cli1": { 251 Value: cty.StringVal("cli1.tfvars"), 252 SourceType: terraform.ValueFromNamedFile, 253 }, 254 }, 255 { 256 "cli2": { 257 Value: cty.StringVal("cli2.tfvars"), 258 SourceType: terraform.ValueFromNamedFile, 259 }, 260 }, 261 } 262 263 if !reflect.DeepEqual(expected, ret) { 264 t.Fatalf("Unexpected input values are received: expected=%#v actual=%#v", expected, ret) 265 } 266 }) 267 } 268 269 func Test_LoadValuesFiles_invalidValuesFile(t *testing.T) { 270 withinFixtureDir(t, "invalid_values_files", func() { 271 loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig()) 272 if err != nil { 273 t.Fatalf("Unexpected error occurred: %s", err) 274 } 275 _, err = loader.LoadValuesFiles() 276 if err == nil { 277 t.Fatal("Expected error is not occurred") 278 } 279 280 expected := "terraform.tfvars:3,1-9: Unexpected \"resource\" block; Blocks are not allowed here." 281 if err.Error() != expected { 282 t.Fatalf("Expected error is `%s`, but get `%s`", expected, err.Error()) 283 } 284 }) 285 }