2014-07-20 19:17:03 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2017-08-31 13:47:10 -05:00
|
|
|
"fmt"
|
2014-07-20 19:17:03 -05:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-08-31 13:47:10 -05:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2014-07-20 19:17:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerge(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
c1, c2, result *Config
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
// Normal good case.
|
|
|
|
{
|
|
|
|
&Config{
|
2015-03-05 14:56:31 -06:00
|
|
|
Atlas: &AtlasConfig{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
2014-09-11 21:54:02 -05:00
|
|
|
Modules: []*Module{
|
|
|
|
&Module{Name: "foo"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Name: "foo"},
|
|
|
|
},
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "foo"},
|
|
|
|
},
|
|
|
|
Resources: []*Resource{
|
|
|
|
&Resource{Name: "foo"},
|
|
|
|
},
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{Name: "foo"},
|
|
|
|
},
|
2017-08-31 13:47:10 -05:00
|
|
|
Locals: []*Local{
|
|
|
|
&Local{Name: "foo"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
|
|
|
|
unknownKeys: []string{"foo"},
|
|
|
|
},
|
|
|
|
|
|
|
|
&Config{
|
2015-03-05 14:56:31 -06:00
|
|
|
Atlas: &AtlasConfig{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2014-09-11 21:54:02 -05:00
|
|
|
Modules: []*Module{
|
|
|
|
&Module{Name: "bar"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Name: "bar"},
|
|
|
|
},
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "bar"},
|
|
|
|
},
|
|
|
|
Resources: []*Resource{
|
|
|
|
&Resource{Name: "bar"},
|
|
|
|
},
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{Name: "bar"},
|
|
|
|
},
|
2017-08-31 13:47:10 -05:00
|
|
|
Locals: []*Local{
|
|
|
|
&Local{Name: "bar"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
|
|
|
|
unknownKeys: []string{"bar"},
|
|
|
|
},
|
|
|
|
|
|
|
|
&Config{
|
2015-03-05 14:56:31 -06:00
|
|
|
Atlas: &AtlasConfig{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2014-09-11 21:54:02 -05:00
|
|
|
Modules: []*Module{
|
|
|
|
&Module{Name: "foo"},
|
|
|
|
&Module{Name: "bar"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Name: "foo"},
|
|
|
|
&Output{Name: "bar"},
|
|
|
|
},
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "foo"},
|
|
|
|
&ProviderConfig{Name: "bar"},
|
|
|
|
},
|
|
|
|
Resources: []*Resource{
|
|
|
|
&Resource{Name: "foo"},
|
|
|
|
&Resource{Name: "bar"},
|
|
|
|
},
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{Name: "foo"},
|
|
|
|
&Variable{Name: "bar"},
|
|
|
|
},
|
2017-08-31 13:47:10 -05:00
|
|
|
Locals: []*Local{
|
|
|
|
&Local{Name: "foo"},
|
|
|
|
&Local{Name: "bar"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
|
|
|
|
unknownKeys: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Test that when merging duplicates, it merges into the
|
|
|
|
// first, but keeps the duplicates so that errors still
|
|
|
|
// happen.
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Name: "foo"},
|
|
|
|
},
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "foo"},
|
|
|
|
},
|
|
|
|
Resources: []*Resource{
|
|
|
|
&Resource{Name: "foo"},
|
|
|
|
},
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{Name: "foo", Default: "foo"},
|
|
|
|
&Variable{Name: "foo"},
|
|
|
|
},
|
2017-08-31 13:47:10 -05:00
|
|
|
Locals: []*Local{
|
|
|
|
&Local{Name: "foo"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
|
|
|
|
unknownKeys: []string{"foo"},
|
|
|
|
},
|
|
|
|
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Name: "bar"},
|
|
|
|
},
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "bar"},
|
|
|
|
},
|
|
|
|
Resources: []*Resource{
|
|
|
|
&Resource{Name: "bar"},
|
|
|
|
},
|
|
|
|
Variables: []*Variable{
|
2014-07-21 09:32:36 -05:00
|
|
|
&Variable{Name: "foo", Default: "bar"},
|
2014-07-20 19:17:03 -05:00
|
|
|
&Variable{Name: "bar"},
|
|
|
|
},
|
2017-08-31 13:47:10 -05:00
|
|
|
Locals: []*Local{
|
|
|
|
&Local{Name: "foo"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
|
|
|
|
unknownKeys: []string{"bar"},
|
|
|
|
},
|
|
|
|
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Name: "foo"},
|
|
|
|
&Output{Name: "bar"},
|
|
|
|
},
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Name: "foo"},
|
|
|
|
&ProviderConfig{Name: "bar"},
|
|
|
|
},
|
|
|
|
Resources: []*Resource{
|
|
|
|
&Resource{Name: "foo"},
|
|
|
|
&Resource{Name: "bar"},
|
|
|
|
},
|
|
|
|
Variables: []*Variable{
|
2014-07-21 09:32:36 -05:00
|
|
|
&Variable{Name: "foo", Default: "bar"},
|
2014-07-20 19:17:03 -05:00
|
|
|
&Variable{Name: "foo"},
|
|
|
|
&Variable{Name: "bar"},
|
|
|
|
},
|
2017-08-31 13:47:10 -05:00
|
|
|
Locals: []*Local{
|
|
|
|
&Local{Name: "foo"},
|
|
|
|
&Local{Name: "foo"},
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
|
|
|
|
unknownKeys: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
|
|
|
|
false,
|
|
|
|
},
|
2016-12-13 23:48:59 -06:00
|
|
|
|
|
|
|
// Terraform block
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
RequiredVersion: "A",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
RequiredVersion: "A",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
RequiredVersion: "A",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
RequiredVersion: "A",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Provider alias
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
ProviderConfigs: []*ProviderConfig{
|
|
|
|
&ProviderConfig{Alias: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Variable type
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Variables: []*Variable{
|
|
|
|
&Variable{DeclaredType: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Output description
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Description: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Output depends_on
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{DependsOn: []string{"foo"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Output sensitive
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Outputs: []*Output{
|
|
|
|
&Output{Sensitive: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2017-03-22 08:01:54 -05:00
|
|
|
|
|
|
|
// terraform blocks are merged, not overwritten
|
|
|
|
{
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
RequiredVersion: "A",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
Backend: &Backend{
|
|
|
|
Type: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Config{
|
|
|
|
Terraform: &Terraform{
|
|
|
|
RequiredVersion: "A",
|
|
|
|
Backend: &Backend{
|
|
|
|
Type: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2014-07-20 19:17:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
2017-08-31 13:47:10 -05:00
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
actual, err := Merge(tc.c1, tc.c2)
|
|
|
|
if err != nil != tc.err {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, tc.result) {
|
|
|
|
t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(tc.result))
|
|
|
|
}
|
|
|
|
})
|
2014-07-20 19:17:03 -05:00
|
|
|
}
|
|
|
|
}
|