github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/tflint/provider_test.go (about) 1 package tflint 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/hashicorp/terraform/configs/configschema" 8 "github.com/wata727/tflint/client" 9 "github.com/zclconf/go-cty/cty" 10 ) 11 12 func Test_Get(t *testing.T) { 13 withinFixtureDir(t, "provider_config", func() { 14 runner := testRunnerWithOsFs(t, EmptyConfig()) 15 providerConfig, err := NewProviderConfig( 16 runner.TFConfig.Module.ProviderConfigs["aws"], 17 runner, 18 client.AwsProviderBlockSchema, 19 ) 20 if err != nil { 21 t.Fatalf("Unexpected error occrred: %s", err) 22 } 23 24 cases := []struct { 25 Key string 26 Value string 27 Exists bool 28 Err error 29 }{ 30 { 31 Key: "access_key", 32 Value: "AWS_ACCESS_KEY", 33 Exists: true, 34 Err: nil, 35 }, 36 { 37 Key: "secret_key", 38 Value: "", 39 Exists: true, 40 Err: nil, 41 }, 42 { 43 Key: "region", 44 Value: "us-east-1", 45 Exists: true, 46 Err: nil, 47 }, 48 { 49 Key: "profile", 50 Value: "", 51 Exists: true, 52 Err: nil, 53 }, 54 { 55 Key: "shared_credentials_file", 56 Value: "", 57 Exists: true, 58 Err: nil, 59 }, 60 { 61 Key: "undefined", 62 Value: "", 63 Exists: false, 64 Err: nil, 65 }, 66 } 67 68 for _, tc := range cases { 69 val, exists, err := providerConfig.Get(tc.Key) 70 if val != tc.Value { 71 t.Fatalf("Expected `%s` as the key value of `%s`, but got `%s`", tc.Value, tc.Key, val) 72 } 73 if exists != tc.Exists { 74 t.Fatalf("Expected `%t` as the exists, but got `%t`", tc.Exists, exists) 75 } 76 if err != tc.Err { 77 t.Fatalf("Expected `%s` as the error, but got `%s`", tc.Err, err) 78 } 79 } 80 }) 81 } 82 83 func Test_Get_withEmptyProvider(t *testing.T) { 84 withinFixtureDir(t, "provider_config", func() { 85 runner := testRunnerWithOsFs(t, EmptyConfig()) 86 providerConfig, err := NewProviderConfig( 87 nil, 88 runner, 89 client.AwsProviderBlockSchema, 90 ) 91 if err != nil { 92 t.Fatalf("Unexpected error occrred: %s", err) 93 } 94 95 val, exists, err := providerConfig.Get("key") 96 if val != "" { 97 t.Fatalf("Expected empty string, but got `%s`", val) 98 } 99 if exists { 100 t.Fatal("Expected not exists, but exists") 101 } 102 if err != nil { 103 t.Fatalf("Expected to return nil, but got `%s`", err) 104 } 105 }) 106 } 107 108 func Test_GetBlock(t *testing.T) { 109 withinFixtureDir(t, "provider_config", func() { 110 runner := testRunnerWithOsFs(t, EmptyConfig()) 111 providerConfig, err := NewProviderConfig( 112 runner.TFConfig.Module.ProviderConfigs["aws"], 113 runner, 114 client.AwsProviderBlockSchema, 115 ) 116 if err != nil { 117 t.Fatalf("Unexpected error occrred: %s", err) 118 } 119 120 cases := []struct { 121 Key string 122 Schema *configschema.Block 123 Value map[string]string 124 Exists bool 125 Err error 126 }{ 127 { 128 Key: "assume_role", 129 Schema: &configschema.Block{ 130 Attributes: map[string]*configschema.Attribute{ 131 "role_arn": {Type: cty.String}, 132 "session_name": {Type: cty.String}, 133 "external_id": {Type: cty.String}, 134 "policy": {Type: cty.String}, 135 }, 136 }, 137 Value: map[string]string{ 138 "role_arn": "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME", 139 "session_name": "SESSION_NAME", 140 "external_id": "EXTERNAL_ID", 141 "policy": "POLICY_NAME", 142 }, 143 Exists: true, 144 Err: nil, 145 }, 146 { 147 Key: "undefined", 148 Value: map[string]string{}, 149 Exists: false, 150 Err: nil, 151 }, 152 } 153 154 for _, tc := range cases { 155 val, exists, err := providerConfig.GetBlock(tc.Key, tc.Schema) 156 if err != tc.Err { 157 t.Fatalf("Expected `%s` as the error, but got `%s`", tc.Err, err) 158 } 159 if exists != tc.Exists { 160 t.Fatalf("Expected `%t` as the exists, but got `%t`", tc.Exists, exists) 161 } 162 if !cmp.Equal(tc.Value, val) { 163 t.Fatalf("Expected value is not matched:\n %s\n", cmp.Diff(tc.Value, val)) 164 } 165 } 166 }) 167 }