github.com/lamielle/terraform@v0.3.2-0.20141121070651-81f008ba53d5/config/config_test.go (about) 1 package config 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "testing" 7 ) 8 9 // This is the directory where our test fixtures are. 10 const fixtureDir = "./test-fixtures" 11 12 func TestConfigCount(t *testing.T) { 13 c := testConfig(t, "count-int") 14 actual, err := c.Resources[0].Count() 15 if err != nil { 16 t.Fatalf("err: %s", err) 17 } 18 if actual != 5 { 19 t.Fatalf("bad: %#v", actual) 20 } 21 } 22 23 func TestConfigCount_string(t *testing.T) { 24 c := testConfig(t, "count-string") 25 actual, err := c.Resources[0].Count() 26 if err != nil { 27 t.Fatalf("err: %s", err) 28 } 29 if actual != 5 { 30 t.Fatalf("bad: %#v", actual) 31 } 32 } 33 34 func TestConfigCount_var(t *testing.T) { 35 c := testConfig(t, "count-var") 36 _, err := c.Resources[0].Count() 37 if err == nil { 38 t.Fatalf("should error") 39 } 40 } 41 42 func TestConfigValidate(t *testing.T) { 43 c := testConfig(t, "validate-good") 44 if err := c.Validate(); err != nil { 45 t.Fatalf("err: %s", err) 46 } 47 } 48 49 func TestConfigValidate_badDependsOn(t *testing.T) { 50 c := testConfig(t, "validate-bad-depends-on") 51 if err := c.Validate(); err == nil { 52 t.Fatal("should not be valid") 53 } 54 } 55 56 func TestConfigValidate_countInt(t *testing.T) { 57 c := testConfig(t, "validate-count-int") 58 if err := c.Validate(); err != nil { 59 t.Fatalf("err: %s", err) 60 } 61 } 62 63 func TestConfigValidate_countCountVar(t *testing.T) { 64 c := testConfig(t, "validate-count-count-var") 65 if err := c.Validate(); err == nil { 66 t.Fatal("should not be valid") 67 } 68 } 69 70 func TestConfigValidate_countModuleVar(t *testing.T) { 71 c := testConfig(t, "validate-count-module-var") 72 if err := c.Validate(); err == nil { 73 t.Fatal("should not be valid") 74 } 75 } 76 77 func TestConfigValidate_countNotInt(t *testing.T) { 78 c := testConfig(t, "validate-count-not-int") 79 if err := c.Validate(); err == nil { 80 t.Fatal("should not be valid") 81 } 82 } 83 84 func TestConfigValidate_countResourceVar(t *testing.T) { 85 c := testConfig(t, "validate-count-resource-var") 86 if err := c.Validate(); err == nil { 87 t.Fatal("should not be valid") 88 } 89 } 90 91 func TestConfigValidate_countUserVar(t *testing.T) { 92 c := testConfig(t, "validate-count-user-var") 93 if err := c.Validate(); err != nil { 94 t.Fatalf("err: %s", err) 95 } 96 } 97 98 func TestConfigValidate_countVar(t *testing.T) { 99 c := testConfig(t, "validate-count-var") 100 if err := c.Validate(); err != nil { 101 t.Fatalf("err: %s", err) 102 } 103 } 104 105 func TestConfigValidate_countVarInvalid(t *testing.T) { 106 c := testConfig(t, "validate-count-var-invalid") 107 if err := c.Validate(); err == nil { 108 t.Fatal("should not be valid") 109 } 110 } 111 112 func TestConfigValidate_dupModule(t *testing.T) { 113 c := testConfig(t, "validate-dup-module") 114 if err := c.Validate(); err == nil { 115 t.Fatal("should not be valid") 116 } 117 } 118 119 func TestConfigValidate_dupResource(t *testing.T) { 120 c := testConfig(t, "validate-dup-resource") 121 if err := c.Validate(); err == nil { 122 t.Fatal("should not be valid") 123 } 124 } 125 126 func TestConfigValidate_moduleNameBad(t *testing.T) { 127 c := testConfig(t, "validate-module-name-bad") 128 if err := c.Validate(); err == nil { 129 t.Fatal("should not be valid") 130 } 131 } 132 133 func TestConfigValidate_moduleSourceVar(t *testing.T) { 134 c := testConfig(t, "validate-module-source-var") 135 if err := c.Validate(); err == nil { 136 t.Fatal("should not be valid") 137 } 138 } 139 140 func TestConfigValidate_nil(t *testing.T) { 141 var c Config 142 if err := c.Validate(); err != nil { 143 t.Fatalf("err: %s", err) 144 } 145 } 146 147 func TestConfigValidate_outputBadField(t *testing.T) { 148 c := testConfig(t, "validate-output-bad-field") 149 if err := c.Validate(); err == nil { 150 t.Fatal("should not be valid") 151 } 152 } 153 154 func TestConfigValidate_pathVar(t *testing.T) { 155 c := testConfig(t, "validate-path-var") 156 if err := c.Validate(); err != nil { 157 t.Fatalf("err: %s", err) 158 } 159 } 160 161 func TestConfigValidate_pathVarInvalid(t *testing.T) { 162 c := testConfig(t, "validate-path-var-invalid") 163 if err := c.Validate(); err == nil { 164 t.Fatal("should not be valid") 165 } 166 } 167 168 func TestConfigValidate_unknownThing(t *testing.T) { 169 c := testConfig(t, "validate-unknownthing") 170 if err := c.Validate(); err == nil { 171 t.Fatal("should not be valid") 172 } 173 } 174 175 func TestConfigValidate_unknownResourceVar(t *testing.T) { 176 c := testConfig(t, "validate-unknown-resource-var") 177 if err := c.Validate(); err == nil { 178 t.Fatal("should not be valid") 179 } 180 } 181 182 func TestConfigValidate_unknownResourceVar_output(t *testing.T) { 183 c := testConfig(t, "validate-unknown-resource-var-output") 184 if err := c.Validate(); err == nil { 185 t.Fatal("should not be valid") 186 } 187 } 188 189 func TestConfigValidate_unknownVar(t *testing.T) { 190 c := testConfig(t, "validate-unknownvar") 191 if err := c.Validate(); err == nil { 192 t.Fatal("should not be valid") 193 } 194 } 195 196 func TestConfigValidate_unknownVarCount(t *testing.T) { 197 c := testConfig(t, "validate-unknownvar-count") 198 if err := c.Validate(); err == nil { 199 t.Fatal("should not be valid") 200 } 201 } 202 203 func TestConfigValidate_varDefault(t *testing.T) { 204 c := testConfig(t, "validate-var-default") 205 if err := c.Validate(); err != nil { 206 t.Fatalf("should be valid: %s", err) 207 } 208 } 209 210 func TestConfigValidate_varDefaultBadType(t *testing.T) { 211 c := testConfig(t, "validate-var-default-bad-type") 212 if err := c.Validate(); err == nil { 213 t.Fatal("should not be valid") 214 } 215 } 216 217 func TestConfigValidate_varDefaultInterpolate(t *testing.T) { 218 c := testConfig(t, "validate-var-default-interpolate") 219 if err := c.Validate(); err == nil { 220 t.Fatal("should not be valid") 221 } 222 } 223 224 func TestConfigValidate_varMultiExactNonSlice(t *testing.T) { 225 c := testConfig(t, "validate-var-multi-exact-non-slice") 226 if err := c.Validate(); err != nil { 227 t.Fatalf("should be valid: %s", err) 228 } 229 } 230 231 func TestConfigValidate_varMultiNonSlice(t *testing.T) { 232 c := testConfig(t, "validate-var-multi-non-slice") 233 if err := c.Validate(); err == nil { 234 t.Fatal("should not be valid") 235 } 236 } 237 238 func TestConfigValidate_varModule(t *testing.T) { 239 c := testConfig(t, "validate-var-module") 240 if err := c.Validate(); err != nil { 241 t.Fatalf("err: %s", err) 242 } 243 } 244 245 func TestConfigValidate_varModuleInvalid(t *testing.T) { 246 c := testConfig(t, "validate-var-module-invalid") 247 if err := c.Validate(); err == nil { 248 t.Fatal("should not be valid") 249 } 250 } 251 252 func TestNameRegexp(t *testing.T) { 253 cases := []struct { 254 Input string 255 Match bool 256 }{ 257 {"hello", true}, 258 {"foo-bar", true}, 259 {"foo_bar", true}, 260 {"_hello", true}, 261 {"foo bar", false}, 262 {"foo.bar", false}, 263 } 264 265 for _, tc := range cases { 266 if NameRegexp.Match([]byte(tc.Input)) != tc.Match { 267 t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match) 268 } 269 } 270 } 271 272 func TestProviderConfigName(t *testing.T) { 273 pcs := []*ProviderConfig{ 274 &ProviderConfig{Name: "aw"}, 275 &ProviderConfig{Name: "aws"}, 276 &ProviderConfig{Name: "a"}, 277 &ProviderConfig{Name: "gce_"}, 278 } 279 280 n := ProviderConfigName("aws_instance", pcs) 281 if n != "aws" { 282 t.Fatalf("bad: %s", n) 283 } 284 } 285 286 func TestVariableDefaultsMap(t *testing.T) { 287 cases := []struct { 288 Default interface{} 289 Output map[string]string 290 }{ 291 { 292 nil, 293 nil, 294 }, 295 296 { 297 "foo", 298 map[string]string{"var.foo": "foo"}, 299 }, 300 301 { 302 map[interface{}]interface{}{ 303 "foo": "bar", 304 "bar": "baz", 305 }, 306 map[string]string{ 307 "var.foo": "foo", 308 "var.foo.foo": "bar", 309 "var.foo.bar": "baz", 310 }, 311 }, 312 } 313 314 for i, tc := range cases { 315 v := &Variable{Name: "foo", Default: tc.Default} 316 actual := v.DefaultsMap() 317 if !reflect.DeepEqual(actual, tc.Output) { 318 t.Fatalf("%d: bad: %#v", i, actual) 319 } 320 } 321 } 322 323 func testConfig(t *testing.T, name string) *Config { 324 c, err := Load(filepath.Join(fixtureDir, name, "main.tf")) 325 if err != nil { 326 t.Fatalf("file: %s\n\nerr: %s", name, err) 327 } 328 329 return c 330 }