github.com/partitio/terraform@v0.11.12-beta1/config/append_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/davecgh/go-spew/spew" 9 ) 10 11 func TestAppend(t *testing.T) { 12 cases := []struct { 13 c1, c2, result *Config 14 err bool 15 }{ 16 { 17 &Config{ 18 Atlas: &AtlasConfig{ 19 Name: "foo", 20 }, 21 Modules: []*Module{ 22 &Module{Name: "foo"}, 23 }, 24 Outputs: []*Output{ 25 &Output{Name: "foo"}, 26 }, 27 ProviderConfigs: []*ProviderConfig{ 28 &ProviderConfig{Name: "foo"}, 29 }, 30 Resources: []*Resource{ 31 &Resource{Name: "foo"}, 32 }, 33 Variables: []*Variable{ 34 &Variable{Name: "foo"}, 35 }, 36 Locals: []*Local{ 37 &Local{Name: "foo"}, 38 }, 39 40 unknownKeys: []string{"foo"}, 41 }, 42 43 &Config{ 44 Atlas: &AtlasConfig{ 45 Name: "bar", 46 }, 47 Modules: []*Module{ 48 &Module{Name: "bar"}, 49 }, 50 Outputs: []*Output{ 51 &Output{Name: "bar"}, 52 }, 53 ProviderConfigs: []*ProviderConfig{ 54 &ProviderConfig{Name: "bar"}, 55 }, 56 Resources: []*Resource{ 57 &Resource{Name: "bar"}, 58 }, 59 Variables: []*Variable{ 60 &Variable{Name: "bar"}, 61 }, 62 Locals: []*Local{ 63 &Local{Name: "bar"}, 64 }, 65 66 unknownKeys: []string{"bar"}, 67 }, 68 69 &Config{ 70 Atlas: &AtlasConfig{ 71 Name: "bar", 72 }, 73 Modules: []*Module{ 74 &Module{Name: "foo"}, 75 &Module{Name: "bar"}, 76 }, 77 Outputs: []*Output{ 78 &Output{Name: "foo"}, 79 &Output{Name: "bar"}, 80 }, 81 ProviderConfigs: []*ProviderConfig{ 82 &ProviderConfig{Name: "foo"}, 83 &ProviderConfig{Name: "bar"}, 84 }, 85 Resources: []*Resource{ 86 &Resource{Name: "foo"}, 87 &Resource{Name: "bar"}, 88 }, 89 Variables: []*Variable{ 90 &Variable{Name: "foo"}, 91 &Variable{Name: "bar"}, 92 }, 93 Locals: []*Local{ 94 &Local{Name: "foo"}, 95 &Local{Name: "bar"}, 96 }, 97 98 unknownKeys: []string{"foo", "bar"}, 99 }, 100 101 false, 102 }, 103 104 // Terraform block 105 { 106 &Config{ 107 Terraform: &Terraform{ 108 RequiredVersion: "A", 109 }, 110 }, 111 &Config{}, 112 &Config{ 113 Terraform: &Terraform{ 114 RequiredVersion: "A", 115 }, 116 }, 117 false, 118 }, 119 120 { 121 &Config{}, 122 &Config{ 123 Terraform: &Terraform{ 124 RequiredVersion: "A", 125 }, 126 }, 127 &Config{ 128 Terraform: &Terraform{ 129 RequiredVersion: "A", 130 }, 131 }, 132 false, 133 }, 134 135 // appending configs merges terraform blocks 136 { 137 &Config{ 138 Terraform: &Terraform{ 139 RequiredVersion: "A", 140 }, 141 }, 142 &Config{ 143 Terraform: &Terraform{ 144 Backend: &Backend{ 145 Type: "test", 146 }, 147 }, 148 }, 149 &Config{ 150 Terraform: &Terraform{ 151 RequiredVersion: "A", 152 Backend: &Backend{ 153 Type: "test", 154 }, 155 }, 156 }, 157 false, 158 }, 159 } 160 161 for i, tc := range cases { 162 t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) { 163 actual, err := Append(tc.c1, tc.c2) 164 if err != nil != tc.err { 165 t.Errorf("unexpected error: %s", err) 166 } 167 168 if !reflect.DeepEqual(actual, tc.result) { 169 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(tc.result)) 170 } 171 }) 172 } 173 }