github.com/hugorut/terraform@v1.1.3/src/command/cliconfig/cliconfig_test.go (about) 1 package cliconfig 2 3 import ( 4 "os" 5 "path/filepath" 6 "reflect" 7 "testing" 8 9 "github.com/davecgh/go-spew/spew" 10 "github.com/google/go-cmp/cmp" 11 ) 12 13 // This is the directory where our test fixtures are. 14 const fixtureDir = "./testdata" 15 16 func TestLoadConfig(t *testing.T) { 17 c, err := loadConfigFile(filepath.Join(fixtureDir, "config")) 18 if err != nil { 19 t.Fatalf("err: %s", err) 20 } 21 22 expected := &Config{ 23 Providers: map[string]string{ 24 "aws": "foo", 25 "do": "bar", 26 }, 27 } 28 29 if !reflect.DeepEqual(c, expected) { 30 t.Fatalf("bad: %#v", c) 31 } 32 } 33 34 func TestLoadConfig_env(t *testing.T) { 35 defer os.Unsetenv("TFTEST") 36 os.Setenv("TFTEST", "hello") 37 38 c, err := loadConfigFile(filepath.Join(fixtureDir, "config-env")) 39 if err != nil { 40 t.Fatalf("err: %s", err) 41 } 42 43 expected := &Config{ 44 Providers: map[string]string{ 45 "aws": "hello", 46 "google": "bar", 47 }, 48 Provisioners: map[string]string{ 49 "local": "hello", 50 }, 51 } 52 53 if !reflect.DeepEqual(c, expected) { 54 t.Fatalf("bad: %#v", c) 55 } 56 } 57 58 func TestLoadConfig_hosts(t *testing.T) { 59 got, diags := loadConfigFile(filepath.Join(fixtureDir, "hosts")) 60 if len(diags) != 0 { 61 t.Fatalf("%s", diags.Err()) 62 } 63 64 want := &Config{ 65 Hosts: map[string]*ConfigHost{ 66 "example.com": { 67 Services: map[string]interface{}{ 68 "modules.v1": "https://example.com/", 69 }, 70 }, 71 }, 72 } 73 74 if !reflect.DeepEqual(got, want) { 75 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) 76 } 77 } 78 79 func TestLoadConfig_credentials(t *testing.T) { 80 got, err := loadConfigFile(filepath.Join(fixtureDir, "credentials")) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 want := &Config{ 86 Credentials: map[string]map[string]interface{}{ 87 "example.com": map[string]interface{}{ 88 "token": "foo the bar baz", 89 }, 90 "example.net": map[string]interface{}{ 91 "username": "foo", 92 "password": "baz", 93 }, 94 }, 95 CredentialsHelpers: map[string]*ConfigCredentialsHelper{ 96 "foo": &ConfigCredentialsHelper{ 97 Args: []string{"bar", "baz"}, 98 }, 99 }, 100 } 101 102 if !reflect.DeepEqual(got, want) { 103 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) 104 } 105 } 106 107 func TestConfigValidate(t *testing.T) { 108 tests := map[string]struct { 109 Config *Config 110 DiagCount int 111 }{ 112 "nil": { 113 nil, 114 0, 115 }, 116 "empty": { 117 &Config{}, 118 0, 119 }, 120 "host good": { 121 &Config{ 122 Hosts: map[string]*ConfigHost{ 123 "example.com": {}, 124 }, 125 }, 126 0, 127 }, 128 "host with bad hostname": { 129 &Config{ 130 Hosts: map[string]*ConfigHost{ 131 "example..com": {}, 132 }, 133 }, 134 1, // host block has invalid hostname 135 }, 136 "credentials good": { 137 &Config{ 138 Credentials: map[string]map[string]interface{}{ 139 "example.com": map[string]interface{}{ 140 "token": "foo", 141 }, 142 }, 143 }, 144 0, 145 }, 146 "credentials with bad hostname": { 147 &Config{ 148 Credentials: map[string]map[string]interface{}{ 149 "example..com": map[string]interface{}{ 150 "token": "foo", 151 }, 152 }, 153 }, 154 1, // credentials block has invalid hostname 155 }, 156 "credentials helper good": { 157 &Config{ 158 CredentialsHelpers: map[string]*ConfigCredentialsHelper{ 159 "foo": {}, 160 }, 161 }, 162 0, 163 }, 164 "credentials helper too many": { 165 &Config{ 166 CredentialsHelpers: map[string]*ConfigCredentialsHelper{ 167 "foo": {}, 168 "bar": {}, 169 }, 170 }, 171 1, // no more than one credentials_helper block allowed 172 }, 173 "provider_installation good none": { 174 &Config{ 175 ProviderInstallation: nil, 176 }, 177 0, 178 }, 179 "provider_installation good one": { 180 &Config{ 181 ProviderInstallation: []*ProviderInstallation{ 182 {}, 183 }, 184 }, 185 0, 186 }, 187 "provider_installation too many": { 188 &Config{ 189 ProviderInstallation: []*ProviderInstallation{ 190 {}, 191 {}, 192 }, 193 }, 194 1, // no more than one provider_installation block allowed 195 }, 196 "plugin_cache_dir does not exist": { 197 &Config{ 198 PluginCacheDir: "fake", 199 }, 200 1, // The specified plugin cache dir %s cannot be opened 201 }, 202 } 203 204 for name, test := range tests { 205 t.Run(name, func(t *testing.T) { 206 diags := test.Config.Validate() 207 if len(diags) != test.DiagCount { 208 t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.DiagCount) 209 for _, diag := range diags { 210 t.Logf("- %#v", diag.Description()) 211 } 212 } 213 }) 214 } 215 } 216 217 func TestConfig_Merge(t *testing.T) { 218 c1 := &Config{ 219 Providers: map[string]string{ 220 "foo": "bar", 221 "bar": "blah", 222 }, 223 Provisioners: map[string]string{ 224 "local": "local", 225 "remote": "bad", 226 }, 227 Hosts: map[string]*ConfigHost{ 228 "example.com": { 229 Services: map[string]interface{}{ 230 "modules.v1": "http://example.com/", 231 }, 232 }, 233 }, 234 Credentials: map[string]map[string]interface{}{ 235 "foo": { 236 "bar": "baz", 237 }, 238 }, 239 CredentialsHelpers: map[string]*ConfigCredentialsHelper{ 240 "buz": {}, 241 }, 242 ProviderInstallation: []*ProviderInstallation{ 243 { 244 Methods: []*ProviderInstallationMethod{ 245 {Location: ProviderInstallationFilesystemMirror("a")}, 246 {Location: ProviderInstallationFilesystemMirror("b")}, 247 }, 248 }, 249 { 250 Methods: []*ProviderInstallationMethod{ 251 {Location: ProviderInstallationFilesystemMirror("c")}, 252 }, 253 }, 254 }, 255 } 256 257 c2 := &Config{ 258 Providers: map[string]string{ 259 "bar": "baz", 260 "baz": "what", 261 }, 262 Provisioners: map[string]string{ 263 "remote": "remote", 264 }, 265 Hosts: map[string]*ConfigHost{ 266 "example.net": { 267 Services: map[string]interface{}{ 268 "modules.v1": "https://example.net/", 269 }, 270 }, 271 }, 272 Credentials: map[string]map[string]interface{}{ 273 "fee": { 274 "bur": "bez", 275 }, 276 }, 277 CredentialsHelpers: map[string]*ConfigCredentialsHelper{ 278 "biz": {}, 279 }, 280 ProviderInstallation: []*ProviderInstallation{ 281 { 282 Methods: []*ProviderInstallationMethod{ 283 {Location: ProviderInstallationFilesystemMirror("d")}, 284 }, 285 }, 286 }, 287 } 288 289 expected := &Config{ 290 Providers: map[string]string{ 291 "foo": "bar", 292 "bar": "baz", 293 "baz": "what", 294 }, 295 Provisioners: map[string]string{ 296 "local": "local", 297 "remote": "remote", 298 }, 299 Hosts: map[string]*ConfigHost{ 300 "example.com": { 301 Services: map[string]interface{}{ 302 "modules.v1": "http://example.com/", 303 }, 304 }, 305 "example.net": { 306 Services: map[string]interface{}{ 307 "modules.v1": "https://example.net/", 308 }, 309 }, 310 }, 311 Credentials: map[string]map[string]interface{}{ 312 "foo": { 313 "bar": "baz", 314 }, 315 "fee": { 316 "bur": "bez", 317 }, 318 }, 319 CredentialsHelpers: map[string]*ConfigCredentialsHelper{ 320 "buz": {}, 321 "biz": {}, 322 }, 323 ProviderInstallation: []*ProviderInstallation{ 324 { 325 Methods: []*ProviderInstallationMethod{ 326 {Location: ProviderInstallationFilesystemMirror("a")}, 327 {Location: ProviderInstallationFilesystemMirror("b")}, 328 }, 329 }, 330 { 331 Methods: []*ProviderInstallationMethod{ 332 {Location: ProviderInstallationFilesystemMirror("c")}, 333 }, 334 }, 335 { 336 Methods: []*ProviderInstallationMethod{ 337 {Location: ProviderInstallationFilesystemMirror("d")}, 338 }, 339 }, 340 }, 341 } 342 343 actual := c1.Merge(c2) 344 if diff := cmp.Diff(expected, actual); diff != "" { 345 t.Fatalf("wrong result\n%s", diff) 346 } 347 } 348 349 func TestConfig_Merge_disableCheckpoint(t *testing.T) { 350 c1 := &Config{ 351 DisableCheckpoint: true, 352 } 353 354 c2 := &Config{} 355 356 expected := &Config{ 357 Providers: map[string]string{}, 358 Provisioners: map[string]string{}, 359 DisableCheckpoint: true, 360 } 361 362 actual := c1.Merge(c2) 363 if !reflect.DeepEqual(actual, expected) { 364 t.Fatalf("bad: %#v", actual) 365 } 366 } 367 368 func TestConfig_Merge_disableCheckpointSignature(t *testing.T) { 369 c1 := &Config{ 370 DisableCheckpointSignature: true, 371 } 372 373 c2 := &Config{} 374 375 expected := &Config{ 376 Providers: map[string]string{}, 377 Provisioners: map[string]string{}, 378 DisableCheckpointSignature: true, 379 } 380 381 actual := c1.Merge(c2) 382 if !reflect.DeepEqual(actual, expected) { 383 t.Fatalf("bad: %#v", actual) 384 } 385 }