github.com/emc-cmd/terraform@v0.7.8-0.20161101145618-f16309630e7c/config/module/tree_test.go (about) 1 package module 2 3 import ( 4 "os" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/go-getter" 10 "github.com/hashicorp/terraform/config" 11 "github.com/hashicorp/terraform/helper/copy" 12 ) 13 14 func TestTreeChild(t *testing.T) { 15 var nilTree *Tree 16 if nilTree.Child(nil) != nil { 17 t.Fatal("child should be nil") 18 } 19 20 storage := testStorage(t) 21 tree := NewTree("", testConfig(t, "child")) 22 if err := tree.Load(storage, GetModeGet); err != nil { 23 t.Fatalf("err: %s", err) 24 } 25 26 // Should be able to get the root child 27 if c := tree.Child([]string{}); c == nil { 28 t.Fatal("should not be nil") 29 } else if c.Name() != "root" { 30 t.Fatalf("bad: %#v", c.Name()) 31 } else if !reflect.DeepEqual(c.Path(), []string(nil)) { 32 t.Fatalf("bad: %#v", c.Path()) 33 } 34 35 // Should be able to get the root child 36 if c := tree.Child(nil); c == nil { 37 t.Fatal("should not be nil") 38 } else if c.Name() != "root" { 39 t.Fatalf("bad: %#v", c.Name()) 40 } else if !reflect.DeepEqual(c.Path(), []string(nil)) { 41 t.Fatalf("bad: %#v", c.Path()) 42 } 43 44 // Should be able to get the foo child 45 if c := tree.Child([]string{"foo"}); c == nil { 46 t.Fatal("should not be nil") 47 } else if c.Name() != "foo" { 48 t.Fatalf("bad: %#v", c.Name()) 49 } else if !reflect.DeepEqual(c.Path(), []string{"foo"}) { 50 t.Fatalf("bad: %#v", c.Path()) 51 } 52 53 // Should be able to get the nested child 54 if c := tree.Child([]string{"foo", "bar"}); c == nil { 55 t.Fatal("should not be nil") 56 } else if c.Name() != "bar" { 57 t.Fatalf("bad: %#v", c.Name()) 58 } else if !reflect.DeepEqual(c.Path(), []string{"foo", "bar"}) { 59 t.Fatalf("bad: %#v", c.Path()) 60 } 61 } 62 63 func TestTreeLoad(t *testing.T) { 64 storage := testStorage(t) 65 tree := NewTree("", testConfig(t, "basic")) 66 67 if tree.Loaded() { 68 t.Fatal("should not be loaded") 69 } 70 71 // This should error because we haven't gotten things yet 72 if err := tree.Load(storage, GetModeNone); err == nil { 73 t.Fatal("should error") 74 } 75 76 if tree.Loaded() { 77 t.Fatal("should not be loaded") 78 } 79 80 // This should get things 81 if err := tree.Load(storage, GetModeGet); err != nil { 82 t.Fatalf("err: %s", err) 83 } 84 85 if !tree.Loaded() { 86 t.Fatal("should be loaded") 87 } 88 89 // This should no longer error 90 if err := tree.Load(storage, GetModeNone); err != nil { 91 t.Fatalf("err: %s", err) 92 } 93 94 actual := strings.TrimSpace(tree.String()) 95 expected := strings.TrimSpace(treeLoadStr) 96 if actual != expected { 97 t.Fatalf("bad: \n\n%s", actual) 98 } 99 } 100 101 func TestTreeLoad_duplicate(t *testing.T) { 102 storage := testStorage(t) 103 tree := NewTree("", testConfig(t, "dup")) 104 105 if tree.Loaded() { 106 t.Fatal("should not be loaded") 107 } 108 109 // This should get things 110 if err := tree.Load(storage, GetModeGet); err == nil { 111 t.Fatalf("should error") 112 } 113 } 114 115 func TestTreeLoad_copyable(t *testing.T) { 116 dir := tempDir(t) 117 storage := &getter.FolderStorage{StorageDir: dir} 118 cfg := testConfig(t, "basic") 119 tree := NewTree("", cfg) 120 121 // This should get things 122 if err := tree.Load(storage, GetModeGet); err != nil { 123 t.Fatalf("err: %s", err) 124 } 125 126 if !tree.Loaded() { 127 t.Fatal("should be loaded") 128 } 129 130 // This should no longer error 131 if err := tree.Load(storage, GetModeNone); err != nil { 132 t.Fatalf("err: %s", err) 133 } 134 135 // Now we copy the directory, this COPIES symlink values, and 136 // doesn't create symlinks themselves. That is important. 137 dir2 := tempDir(t) 138 os.RemoveAll(dir2) 139 defer os.RemoveAll(dir2) 140 if err := copy.CopyDir(dir, dir2); err != nil { 141 t.Fatalf("err: %s", err) 142 } 143 144 // Now copy the configuration 145 cfgDir := tempDir(t) 146 os.RemoveAll(cfgDir) 147 defer os.RemoveAll(cfgDir) 148 if err := copy.CopyDir(cfg.Dir, cfgDir); err != nil { 149 t.Fatalf("err: %s", err) 150 } 151 152 { 153 cfg, err := config.LoadDir(cfgDir) 154 if err != nil { 155 t.Fatalf("err: %s", err) 156 } 157 158 tree := NewTree("", cfg) 159 storage := &getter.FolderStorage{StorageDir: dir2} 160 161 // This should not error since we already got it! 162 if err := tree.Load(storage, GetModeNone); err != nil { 163 t.Fatalf("err: %s", err) 164 } 165 166 if !tree.Loaded() { 167 t.Fatal("should be loaded") 168 } 169 } 170 } 171 172 func TestTreeLoad_parentRef(t *testing.T) { 173 storage := testStorage(t) 174 tree := NewTree("", testConfig(t, "basic-parent")) 175 176 if tree.Loaded() { 177 t.Fatal("should not be loaded") 178 } 179 180 // This should error because we haven't gotten things yet 181 if err := tree.Load(storage, GetModeNone); err == nil { 182 t.Fatal("should error") 183 } 184 185 if tree.Loaded() { 186 t.Fatal("should not be loaded") 187 } 188 189 // This should get things 190 if err := tree.Load(storage, GetModeGet); err != nil { 191 t.Fatalf("err: %s", err) 192 } 193 194 if !tree.Loaded() { 195 t.Fatal("should be loaded") 196 } 197 198 // This should no longer error 199 if err := tree.Load(storage, GetModeNone); err != nil { 200 t.Fatalf("err: %s", err) 201 } 202 203 actual := strings.TrimSpace(tree.String()) 204 expected := strings.TrimSpace(treeLoadParentStr) 205 if actual != expected { 206 t.Fatalf("bad: \n\n%s", actual) 207 } 208 } 209 210 func TestTreeLoad_subdir(t *testing.T) { 211 storage := testStorage(t) 212 tree := NewTree("", testConfig(t, "basic-subdir")) 213 214 if tree.Loaded() { 215 t.Fatal("should not be loaded") 216 } 217 218 // This should error because we haven't gotten things yet 219 if err := tree.Load(storage, GetModeNone); err == nil { 220 t.Fatal("should error") 221 } 222 223 if tree.Loaded() { 224 t.Fatal("should not be loaded") 225 } 226 227 // This should get things 228 if err := tree.Load(storage, GetModeGet); err != nil { 229 t.Fatalf("err: %s", err) 230 } 231 232 if !tree.Loaded() { 233 t.Fatal("should be loaded") 234 } 235 236 // This should no longer error 237 if err := tree.Load(storage, GetModeNone); err != nil { 238 t.Fatalf("err: %s", err) 239 } 240 241 actual := strings.TrimSpace(tree.String()) 242 expected := strings.TrimSpace(treeLoadSubdirStr) 243 if actual != expected { 244 t.Fatalf("bad: \n\n%s", actual) 245 } 246 } 247 248 func TestTreeModules(t *testing.T) { 249 tree := NewTree("", testConfig(t, "basic")) 250 actual := tree.Modules() 251 252 expected := []*Module{ 253 &Module{Name: "foo", Source: "./foo"}, 254 } 255 256 if !reflect.DeepEqual(actual, expected) { 257 t.Fatalf("bad: %#v", actual) 258 } 259 } 260 261 func TestTreeName(t *testing.T) { 262 tree := NewTree("", testConfig(t, "basic")) 263 actual := tree.Name() 264 265 if actual != RootName { 266 t.Fatalf("bad: %#v", actual) 267 } 268 } 269 270 func TestTreeValidate_badChild(t *testing.T) { 271 tree := NewTree("", testConfig(t, "validate-child-bad")) 272 273 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 274 t.Fatalf("err: %s", err) 275 } 276 277 if err := tree.Validate(); err == nil { 278 t.Fatal("should error") 279 } 280 } 281 282 func TestTreeValidate_badChildOutput(t *testing.T) { 283 tree := NewTree("", testConfig(t, "validate-bad-output")) 284 285 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 286 t.Fatalf("err: %s", err) 287 } 288 289 if err := tree.Validate(); err == nil { 290 t.Fatal("should error") 291 } 292 } 293 294 func TestTreeValidate_badChildOutputToModule(t *testing.T) { 295 tree := NewTree("", testConfig(t, "validate-bad-output-to-module")) 296 297 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 298 t.Fatalf("err: %s", err) 299 } 300 301 if err := tree.Validate(); err == nil { 302 t.Fatal("should error") 303 } 304 } 305 306 func TestTreeValidate_badChildVar(t *testing.T) { 307 tree := NewTree("", testConfig(t, "validate-bad-var")) 308 309 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 310 t.Fatalf("err: %s", err) 311 } 312 313 if err := tree.Validate(); err == nil { 314 t.Fatal("should error") 315 } 316 } 317 318 func TestTreeValidate_badRoot(t *testing.T) { 319 tree := NewTree("", testConfig(t, "validate-root-bad")) 320 321 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 322 t.Fatalf("err: %s", err) 323 } 324 325 if err := tree.Validate(); err == nil { 326 t.Fatal("should error") 327 } 328 } 329 330 func TestTreeValidate_good(t *testing.T) { 331 tree := NewTree("", testConfig(t, "validate-child-good")) 332 333 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 334 t.Fatalf("err: %s", err) 335 } 336 337 if err := tree.Validate(); err != nil { 338 t.Fatalf("err: %s", err) 339 } 340 } 341 342 func TestTreeValidate_notLoaded(t *testing.T) { 343 tree := NewTree("", testConfig(t, "basic")) 344 345 if err := tree.Validate(); err == nil { 346 t.Fatal("should error") 347 } 348 } 349 350 func TestTreeValidate_requiredChildVar(t *testing.T) { 351 tree := NewTree("", testConfig(t, "validate-required-var")) 352 353 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 354 t.Fatalf("err: %s", err) 355 } 356 357 if err := tree.Validate(); err == nil { 358 t.Fatal("should error") 359 } 360 } 361 362 const treeLoadStr = ` 363 root 364 foo (path: foo) 365 ` 366 367 const treeLoadParentStr = ` 368 root 369 a (path: a) 370 b (path: a, b) 371 ` 372 const treeLoadSubdirStr = ` 373 root 374 foo (path: foo) 375 bar (path: foo, bar) 376 `