github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/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_LoadAnnotations(t *testing.T) {
   140  	withinFixtureDir(t, "annotation_files", 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  		ret, err := loader.LoadAnnotations(".")
   146  		if err != nil {
   147  			t.Fatalf("Unexpected error occurred: %s", err)
   148  		}
   149  
   150  		expected := map[string]Annotations{
   151  			"file1.tf": {
   152  				{
   153  					Content: "aws_instance_invalid_type",
   154  					Token: hclsyntax.Token{
   155  						Type:  hclsyntax.TokenComment,
   156  						Bytes: []byte(fmt.Sprintf("// tflint-ignore: aws_instance_invalid_type%s", newLine())),
   157  						Range: hcl.Range{
   158  							Filename: "file1.tf",
   159  							Start:    hcl.Pos{Line: 2, Column: 5},
   160  							End:      hcl.Pos{Line: 3, Column: 1},
   161  						},
   162  					},
   163  				},
   164  			},
   165  			"file2.tf": {
   166  				{
   167  					Content: "aws_instance_invalid_type",
   168  					Token: hclsyntax.Token{
   169  						Type:  hclsyntax.TokenComment,
   170  						Bytes: []byte(fmt.Sprintf("// tflint-ignore: aws_instance_invalid_type%s", newLine())),
   171  						Range: hcl.Range{
   172  							Filename: "file2.tf",
   173  							Start:    hcl.Pos{Line: 2, Column: 32},
   174  							End:      hcl.Pos{Line: 3, Column: 1},
   175  						},
   176  					},
   177  				},
   178  			},
   179  			"file3.tf": {},
   180  		}
   181  
   182  		opts := cmpopts.IgnoreFields(hcl.Pos{}, "Byte")
   183  		if !cmp.Equal(expected, ret, opts) {
   184  			t.Fatalf("Test failed. Diff: %s", cmp.Diff(expected, ret, opts))
   185  		}
   186  	})
   187  }
   188  
   189  func Test_LoadValuesFiles(t *testing.T) {
   190  	withinFixtureDir(t, "values_files", func() {
   191  		loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig())
   192  		if err != nil {
   193  			t.Fatalf("Unexpected error occurred: %s", err)
   194  		}
   195  		ret, err := loader.LoadValuesFiles("cli1.tfvars", "cli2.tfvars")
   196  		if err != nil {
   197  			t.Fatalf("Unexpected error occurred: %s", err)
   198  		}
   199  
   200  		expected := []terraform.InputValues{
   201  			{
   202  				"default": {
   203  					Value:      cty.StringVal("terraform.tfvars"),
   204  					SourceType: terraform.ValueFromAutoFile,
   205  				},
   206  			},
   207  			{
   208  				"auto1": {
   209  					Value:      cty.StringVal("auto1.auto.tfvars"),
   210  					SourceType: terraform.ValueFromAutoFile,
   211  				},
   212  			},
   213  			{
   214  				"auto2": {
   215  					Value:      cty.StringVal("auto2.auto.tfvars"),
   216  					SourceType: terraform.ValueFromAutoFile,
   217  				},
   218  			},
   219  			{
   220  				"cli1": {
   221  					Value:      cty.StringVal("cli1.tfvars"),
   222  					SourceType: terraform.ValueFromNamedFile,
   223  				},
   224  			},
   225  			{
   226  				"cli2": {
   227  					Value:      cty.StringVal("cli2.tfvars"),
   228  					SourceType: terraform.ValueFromNamedFile,
   229  				},
   230  			},
   231  		}
   232  
   233  		if !reflect.DeepEqual(expected, ret) {
   234  			t.Fatalf("Unexpected input values are received: expected=%#v actual=%#v", expected, ret)
   235  		}
   236  	})
   237  }
   238  
   239  func Test_LoadValuesFiles_invalidValuesFile(t *testing.T) {
   240  	withinFixtureDir(t, "invalid_values_files", func() {
   241  		loader, err := NewLoader(afero.Afero{Fs: afero.NewOsFs()}, EmptyConfig())
   242  		if err != nil {
   243  			t.Fatalf("Unexpected error occurred: %s", err)
   244  		}
   245  		_, err = loader.LoadValuesFiles()
   246  		if err == nil {
   247  			t.Fatal("Expected error is not occurred")
   248  		}
   249  
   250  		expected := "terraform.tfvars:3,1-9: Unexpected \"resource\" block; Blocks are not allowed here."
   251  		if err.Error() != expected {
   252  			t.Fatalf("Expected error is `%s`, but get `%s`", expected, err.Error())
   253  		}
   254  	})
   255  }