github.com/lamielle/terraform@v0.3.2-0.20141121070651-81f008ba53d5/config/merge_test.go (about) 1 package config 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestMerge(t *testing.T) { 9 cases := []struct { 10 c1, c2, result *Config 11 err bool 12 }{ 13 // Normal good case. 14 { 15 &Config{ 16 Modules: []*Module{ 17 &Module{Name: "foo"}, 18 }, 19 Outputs: []*Output{ 20 &Output{Name: "foo"}, 21 }, 22 ProviderConfigs: []*ProviderConfig{ 23 &ProviderConfig{Name: "foo"}, 24 }, 25 Resources: []*Resource{ 26 &Resource{Name: "foo"}, 27 }, 28 Variables: []*Variable{ 29 &Variable{Name: "foo"}, 30 }, 31 32 unknownKeys: []string{"foo"}, 33 }, 34 35 &Config{ 36 Modules: []*Module{ 37 &Module{Name: "bar"}, 38 }, 39 Outputs: []*Output{ 40 &Output{Name: "bar"}, 41 }, 42 ProviderConfigs: []*ProviderConfig{ 43 &ProviderConfig{Name: "bar"}, 44 }, 45 Resources: []*Resource{ 46 &Resource{Name: "bar"}, 47 }, 48 Variables: []*Variable{ 49 &Variable{Name: "bar"}, 50 }, 51 52 unknownKeys: []string{"bar"}, 53 }, 54 55 &Config{ 56 Modules: []*Module{ 57 &Module{Name: "foo"}, 58 &Module{Name: "bar"}, 59 }, 60 Outputs: []*Output{ 61 &Output{Name: "foo"}, 62 &Output{Name: "bar"}, 63 }, 64 ProviderConfigs: []*ProviderConfig{ 65 &ProviderConfig{Name: "foo"}, 66 &ProviderConfig{Name: "bar"}, 67 }, 68 Resources: []*Resource{ 69 &Resource{Name: "foo"}, 70 &Resource{Name: "bar"}, 71 }, 72 Variables: []*Variable{ 73 &Variable{Name: "foo"}, 74 &Variable{Name: "bar"}, 75 }, 76 77 unknownKeys: []string{"foo", "bar"}, 78 }, 79 80 false, 81 }, 82 83 // Test that when merging duplicates, it merges into the 84 // first, but keeps the duplicates so that errors still 85 // happen. 86 { 87 &Config{ 88 Outputs: []*Output{ 89 &Output{Name: "foo"}, 90 }, 91 ProviderConfigs: []*ProviderConfig{ 92 &ProviderConfig{Name: "foo"}, 93 }, 94 Resources: []*Resource{ 95 &Resource{Name: "foo"}, 96 }, 97 Variables: []*Variable{ 98 &Variable{Name: "foo", Default: "foo"}, 99 &Variable{Name: "foo"}, 100 }, 101 102 unknownKeys: []string{"foo"}, 103 }, 104 105 &Config{ 106 Outputs: []*Output{ 107 &Output{Name: "bar"}, 108 }, 109 ProviderConfigs: []*ProviderConfig{ 110 &ProviderConfig{Name: "bar"}, 111 }, 112 Resources: []*Resource{ 113 &Resource{Name: "bar"}, 114 }, 115 Variables: []*Variable{ 116 &Variable{Name: "foo", Default: "bar"}, 117 &Variable{Name: "bar"}, 118 }, 119 120 unknownKeys: []string{"bar"}, 121 }, 122 123 &Config{ 124 Outputs: []*Output{ 125 &Output{Name: "foo"}, 126 &Output{Name: "bar"}, 127 }, 128 ProviderConfigs: []*ProviderConfig{ 129 &ProviderConfig{Name: "foo"}, 130 &ProviderConfig{Name: "bar"}, 131 }, 132 Resources: []*Resource{ 133 &Resource{Name: "foo"}, 134 &Resource{Name: "bar"}, 135 }, 136 Variables: []*Variable{ 137 &Variable{Name: "foo", Default: "bar"}, 138 &Variable{Name: "foo"}, 139 &Variable{Name: "bar"}, 140 }, 141 142 unknownKeys: []string{"foo", "bar"}, 143 }, 144 145 false, 146 }, 147 } 148 149 for i, tc := range cases { 150 actual, err := Merge(tc.c1, tc.c2) 151 if (err != nil) != tc.err { 152 t.Fatalf("%d: error fail", i) 153 } 154 155 if !reflect.DeepEqual(actual, tc.result) { 156 t.Fatalf("%d: bad:\n\n%#v", i, actual) 157 } 158 } 159 }