github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/config/module/tree_test.go (about) 1 package module 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 ) 8 9 func TestTreeChild(t *testing.T) { 10 storage := testStorage(t) 11 tree := NewTree("", testConfig(t, "child")) 12 if err := tree.Load(storage, GetModeGet); err != nil { 13 t.Fatalf("err: %s", err) 14 } 15 16 // Should be able to get the root child 17 if c := tree.Child([]string{}); c == nil { 18 t.Fatal("should not be nil") 19 } else if c.Name() != "root" { 20 t.Fatalf("bad: %#v", c.Name()) 21 } 22 23 // Should be able to get the root child 24 if c := tree.Child(nil); c == nil { 25 t.Fatal("should not be nil") 26 } else if c.Name() != "root" { 27 t.Fatalf("bad: %#v", c.Name()) 28 } 29 30 // Should be able to get the foo child 31 if c := tree.Child([]string{"foo"}); c == nil { 32 t.Fatal("should not be nil") 33 } else if c.Name() != "foo" { 34 t.Fatalf("bad: %#v", c.Name()) 35 } 36 37 // Should be able to get the nested child 38 if c := tree.Child([]string{"foo", "bar"}); c == nil { 39 t.Fatal("should not be nil") 40 } else if c.Name() != "bar" { 41 t.Fatalf("bad: %#v", c.Name()) 42 } 43 } 44 45 func TestTreeLoad(t *testing.T) { 46 storage := testStorage(t) 47 tree := NewTree("", testConfig(t, "basic")) 48 49 if tree.Loaded() { 50 t.Fatal("should not be loaded") 51 } 52 53 // This should error because we haven't gotten things yet 54 if err := tree.Load(storage, GetModeNone); err == nil { 55 t.Fatal("should error") 56 } 57 58 if tree.Loaded() { 59 t.Fatal("should not be loaded") 60 } 61 62 // This should get things 63 if err := tree.Load(storage, GetModeGet); err != nil { 64 t.Fatalf("err: %s", err) 65 } 66 67 if !tree.Loaded() { 68 t.Fatal("should be loaded") 69 } 70 71 // This should no longer error 72 if err := tree.Load(storage, GetModeNone); err != nil { 73 t.Fatalf("err: %s", err) 74 } 75 76 actual := strings.TrimSpace(tree.String()) 77 expected := strings.TrimSpace(treeLoadStr) 78 if actual != expected { 79 t.Fatalf("bad: \n\n%s", actual) 80 } 81 } 82 83 func TestTreeLoad_duplicate(t *testing.T) { 84 storage := testStorage(t) 85 tree := NewTree("", testConfig(t, "dup")) 86 87 if tree.Loaded() { 88 t.Fatal("should not be loaded") 89 } 90 91 // This should get things 92 if err := tree.Load(storage, GetModeGet); err == nil { 93 t.Fatalf("should error") 94 } 95 } 96 97 func TestTreeLoad_subdir(t *testing.T) { 98 storage := testStorage(t) 99 tree := NewTree("", testConfig(t, "basic-subdir")) 100 101 if tree.Loaded() { 102 t.Fatal("should not be loaded") 103 } 104 105 // This should error because we haven't gotten things yet 106 if err := tree.Load(storage, GetModeNone); err == nil { 107 t.Fatal("should error") 108 } 109 110 if tree.Loaded() { 111 t.Fatal("should not be loaded") 112 } 113 114 // This should get things 115 if err := tree.Load(storage, GetModeGet); err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 if !tree.Loaded() { 120 t.Fatal("should be loaded") 121 } 122 123 // This should no longer error 124 if err := tree.Load(storage, GetModeNone); err != nil { 125 t.Fatalf("err: %s", err) 126 } 127 128 actual := strings.TrimSpace(tree.String()) 129 expected := strings.TrimSpace(treeLoadSubdirStr) 130 if actual != expected { 131 t.Fatalf("bad: \n\n%s", actual) 132 } 133 } 134 135 func TestTreeModules(t *testing.T) { 136 tree := NewTree("", testConfig(t, "basic")) 137 actual := tree.Modules() 138 139 expected := []*Module{ 140 &Module{Name: "foo", Source: "./foo"}, 141 } 142 143 if !reflect.DeepEqual(actual, expected) { 144 t.Fatalf("bad: %#v", actual) 145 } 146 } 147 148 func TestTreeName(t *testing.T) { 149 tree := NewTree("", testConfig(t, "basic")) 150 actual := tree.Name() 151 152 if actual != RootName { 153 t.Fatalf("bad: %#v", actual) 154 } 155 } 156 157 func TestTreeValidate_badChild(t *testing.T) { 158 tree := NewTree("", testConfig(t, "validate-child-bad")) 159 160 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 161 t.Fatalf("err: %s", err) 162 } 163 164 if err := tree.Validate(); err == nil { 165 t.Fatal("should error") 166 } 167 } 168 169 func TestTreeValidate_badChildOutput(t *testing.T) { 170 tree := NewTree("", testConfig(t, "validate-bad-output")) 171 172 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 173 t.Fatalf("err: %s", err) 174 } 175 176 if err := tree.Validate(); err == nil { 177 t.Fatal("should error") 178 } 179 } 180 181 func TestTreeValidate_badChildVar(t *testing.T) { 182 tree := NewTree("", testConfig(t, "validate-bad-var")) 183 184 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 185 t.Fatalf("err: %s", err) 186 } 187 188 if err := tree.Validate(); err == nil { 189 t.Fatal("should error") 190 } 191 } 192 193 func TestTreeValidate_badRoot(t *testing.T) { 194 tree := NewTree("", testConfig(t, "validate-root-bad")) 195 196 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 197 t.Fatalf("err: %s", err) 198 } 199 200 if err := tree.Validate(); err == nil { 201 t.Fatal("should error") 202 } 203 } 204 205 func TestTreeValidate_good(t *testing.T) { 206 tree := NewTree("", testConfig(t, "validate-child-good")) 207 208 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 209 t.Fatalf("err: %s", err) 210 } 211 212 if err := tree.Validate(); err != nil { 213 t.Fatalf("err: %s", err) 214 } 215 } 216 217 func TestTreeValidate_notLoaded(t *testing.T) { 218 tree := NewTree("", testConfig(t, "basic")) 219 220 if err := tree.Validate(); err == nil { 221 t.Fatal("should error") 222 } 223 } 224 225 func TestTreeValidate_requiredChildVar(t *testing.T) { 226 tree := NewTree("", testConfig(t, "validate-required-var")) 227 228 if err := tree.Load(testStorage(t), GetModeGet); err != nil { 229 t.Fatalf("err: %s", err) 230 } 231 232 if err := tree.Validate(); err == nil { 233 t.Fatal("should error") 234 } 235 } 236 237 const treeLoadStr = ` 238 root 239 foo 240 ` 241 242 const treeLoadSubdirStr = ` 243 root 244 foo 245 bar 246 `