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