github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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  	} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
    22  		t.Fatalf("bad: %#v", c.Path())
    23  	}
    24  
    25  	// Should be able to get the root child
    26  	if c := tree.Child(nil); c == nil {
    27  		t.Fatal("should not be nil")
    28  	} else if c.Name() != "root" {
    29  		t.Fatalf("bad: %#v", c.Name())
    30  	} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
    31  		t.Fatalf("bad: %#v", c.Path())
    32  	}
    33  
    34  	// Should be able to get the foo child
    35  	if c := tree.Child([]string{"foo"}); c == nil {
    36  		t.Fatal("should not be nil")
    37  	} else if c.Name() != "foo" {
    38  		t.Fatalf("bad: %#v", c.Name())
    39  	} else if !reflect.DeepEqual(c.Path(), []string{"foo"}) {
    40  		t.Fatalf("bad: %#v", c.Path())
    41  	}
    42  
    43  	// Should be able to get the nested child
    44  	if c := tree.Child([]string{"foo", "bar"}); c == nil {
    45  		t.Fatal("should not be nil")
    46  	} else if c.Name() != "bar" {
    47  		t.Fatalf("bad: %#v", c.Name())
    48  	} else if !reflect.DeepEqual(c.Path(), []string{"foo", "bar"}) {
    49  		t.Fatalf("bad: %#v", c.Path())
    50  	}
    51  }
    52  
    53  func TestTreeLoad(t *testing.T) {
    54  	storage := testStorage(t)
    55  	tree := NewTree("", testConfig(t, "basic"))
    56  
    57  	if tree.Loaded() {
    58  		t.Fatal("should not be loaded")
    59  	}
    60  
    61  	// This should error because we haven't gotten things yet
    62  	if err := tree.Load(storage, GetModeNone); err == nil {
    63  		t.Fatal("should error")
    64  	}
    65  
    66  	if tree.Loaded() {
    67  		t.Fatal("should not be loaded")
    68  	}
    69  
    70  	// This should get things
    71  	if err := tree.Load(storage, GetModeGet); err != nil {
    72  		t.Fatalf("err: %s", err)
    73  	}
    74  
    75  	if !tree.Loaded() {
    76  		t.Fatal("should be loaded")
    77  	}
    78  
    79  	// This should no longer error
    80  	if err := tree.Load(storage, GetModeNone); err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  
    84  	actual := strings.TrimSpace(tree.String())
    85  	expected := strings.TrimSpace(treeLoadStr)
    86  	if actual != expected {
    87  		t.Fatalf("bad: \n\n%s", actual)
    88  	}
    89  }
    90  
    91  func TestTreeLoad_duplicate(t *testing.T) {
    92  	storage := testStorage(t)
    93  	tree := NewTree("", testConfig(t, "dup"))
    94  
    95  	if tree.Loaded() {
    96  		t.Fatal("should not be loaded")
    97  	}
    98  
    99  	// This should get things
   100  	if err := tree.Load(storage, GetModeGet); err == nil {
   101  		t.Fatalf("should error")
   102  	}
   103  }
   104  
   105  func TestTreeLoad_parentRef(t *testing.T) {
   106  	storage := testStorage(t)
   107  	tree := NewTree("", testConfig(t, "basic-parent"))
   108  
   109  	if tree.Loaded() {
   110  		t.Fatal("should not be loaded")
   111  	}
   112  
   113  	// This should error because we haven't gotten things yet
   114  	if err := tree.Load(storage, GetModeNone); err == nil {
   115  		t.Fatal("should error")
   116  	}
   117  
   118  	if tree.Loaded() {
   119  		t.Fatal("should not be loaded")
   120  	}
   121  
   122  	// This should get things
   123  	if err := tree.Load(storage, GetModeGet); err != nil {
   124  		t.Fatalf("err: %s", err)
   125  	}
   126  
   127  	if !tree.Loaded() {
   128  		t.Fatal("should be loaded")
   129  	}
   130  
   131  	// This should no longer error
   132  	if err := tree.Load(storage, GetModeNone); err != nil {
   133  		t.Fatalf("err: %s", err)
   134  	}
   135  
   136  	actual := strings.TrimSpace(tree.String())
   137  	expected := strings.TrimSpace(treeLoadParentStr)
   138  	if actual != expected {
   139  		t.Fatalf("bad: \n\n%s", actual)
   140  	}
   141  }
   142  
   143  func TestTreeLoad_subdir(t *testing.T) {
   144  	storage := testStorage(t)
   145  	tree := NewTree("", testConfig(t, "basic-subdir"))
   146  
   147  	if tree.Loaded() {
   148  		t.Fatal("should not be loaded")
   149  	}
   150  
   151  	// This should error because we haven't gotten things yet
   152  	if err := tree.Load(storage, GetModeNone); err == nil {
   153  		t.Fatal("should error")
   154  	}
   155  
   156  	if tree.Loaded() {
   157  		t.Fatal("should not be loaded")
   158  	}
   159  
   160  	// This should get things
   161  	if err := tree.Load(storage, GetModeGet); err != nil {
   162  		t.Fatalf("err: %s", err)
   163  	}
   164  
   165  	if !tree.Loaded() {
   166  		t.Fatal("should be loaded")
   167  	}
   168  
   169  	// This should no longer error
   170  	if err := tree.Load(storage, GetModeNone); err != nil {
   171  		t.Fatalf("err: %s", err)
   172  	}
   173  
   174  	actual := strings.TrimSpace(tree.String())
   175  	expected := strings.TrimSpace(treeLoadSubdirStr)
   176  	if actual != expected {
   177  		t.Fatalf("bad: \n\n%s", actual)
   178  	}
   179  }
   180  
   181  func TestTreeModules(t *testing.T) {
   182  	tree := NewTree("", testConfig(t, "basic"))
   183  	actual := tree.Modules()
   184  
   185  	expected := []*Module{
   186  		&Module{Name: "foo", Source: "./foo"},
   187  	}
   188  
   189  	if !reflect.DeepEqual(actual, expected) {
   190  		t.Fatalf("bad: %#v", actual)
   191  	}
   192  }
   193  
   194  func TestTreeName(t *testing.T) {
   195  	tree := NewTree("", testConfig(t, "basic"))
   196  	actual := tree.Name()
   197  
   198  	if actual != RootName {
   199  		t.Fatalf("bad: %#v", actual)
   200  	}
   201  }
   202  
   203  func TestTreeValidate_badChild(t *testing.T) {
   204  	tree := NewTree("", testConfig(t, "validate-child-bad"))
   205  
   206  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   207  		t.Fatalf("err: %s", err)
   208  	}
   209  
   210  	if err := tree.Validate(); err == nil {
   211  		t.Fatal("should error")
   212  	}
   213  }
   214  
   215  func TestTreeValidate_badChildOutput(t *testing.T) {
   216  	tree := NewTree("", testConfig(t, "validate-bad-output"))
   217  
   218  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   219  		t.Fatalf("err: %s", err)
   220  	}
   221  
   222  	if err := tree.Validate(); err == nil {
   223  		t.Fatal("should error")
   224  	}
   225  }
   226  
   227  func TestTreeValidate_badChildOutputToModule(t *testing.T) {
   228  	tree := NewTree("", testConfig(t, "validate-bad-output-to-module"))
   229  
   230  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   231  		t.Fatalf("err: %s", err)
   232  	}
   233  
   234  	if err := tree.Validate(); err == nil {
   235  		t.Fatal("should error")
   236  	}
   237  }
   238  
   239  func TestTreeValidate_badChildVar(t *testing.T) {
   240  	tree := NewTree("", testConfig(t, "validate-bad-var"))
   241  
   242  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   243  		t.Fatalf("err: %s", err)
   244  	}
   245  
   246  	if err := tree.Validate(); err == nil {
   247  		t.Fatal("should error")
   248  	}
   249  }
   250  
   251  func TestTreeValidate_badRoot(t *testing.T) {
   252  	tree := NewTree("", testConfig(t, "validate-root-bad"))
   253  
   254  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   255  		t.Fatalf("err: %s", err)
   256  	}
   257  
   258  	if err := tree.Validate(); err == nil {
   259  		t.Fatal("should error")
   260  	}
   261  }
   262  
   263  func TestTreeValidate_good(t *testing.T) {
   264  	tree := NewTree("", testConfig(t, "validate-child-good"))
   265  
   266  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   267  		t.Fatalf("err: %s", err)
   268  	}
   269  
   270  	if err := tree.Validate(); err != nil {
   271  		t.Fatalf("err: %s", err)
   272  	}
   273  }
   274  
   275  func TestTreeValidate_notLoaded(t *testing.T) {
   276  	tree := NewTree("", testConfig(t, "basic"))
   277  
   278  	if err := tree.Validate(); err == nil {
   279  		t.Fatal("should error")
   280  	}
   281  }
   282  
   283  func TestTreeValidate_requiredChildVar(t *testing.T) {
   284  	tree := NewTree("", testConfig(t, "validate-required-var"))
   285  
   286  	if err := tree.Load(testStorage(t), GetModeGet); err != nil {
   287  		t.Fatalf("err: %s", err)
   288  	}
   289  
   290  	if err := tree.Validate(); err == nil {
   291  		t.Fatal("should error")
   292  	}
   293  }
   294  
   295  const treeLoadStr = `
   296  root
   297    foo (path: foo)
   298  `
   299  
   300  const treeLoadParentStr = `
   301  root
   302    a (path: a)
   303      b (path: a, b)
   304  `
   305  const treeLoadSubdirStr = `
   306  root
   307    foo (path: foo)
   308      bar (path: foo, bar)
   309  `