2015-01-25 06:32:33 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:36:22 +04:00
|
|
|
|
2014-06-27 04:17:58 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-03-28 20:08:30 +03:00
|
|
|
"github.com/vtolstov/cloudinit/config"
|
|
|
|
"github.com/vtolstov/cloudinit/datasource"
|
2014-06-27 04:17:58 +04:00
|
|
|
)
|
|
|
|
|
2015-01-27 02:42:06 +03:00
|
|
|
func TestMergeConfigs(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
cc *config.CloudConfig
|
|
|
|
md datasource.Metadata
|
|
|
|
|
|
|
|
out config.CloudConfig
|
2014-06-27 04:17:58 +04:00
|
|
|
}{
|
|
|
|
{
|
2015-01-27 02:42:06 +03:00
|
|
|
// If md is empty and cc is nil, result should be empty
|
|
|
|
out: config.CloudConfig{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// If md and cc are empty, result should be empty
|
|
|
|
cc: &config.CloudConfig{},
|
|
|
|
out: config.CloudConfig{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// If cc is empty, cc should be returned unchanged
|
|
|
|
cc: &config.CloudConfig{SSHAuthorizedKeys: []string{"abc", "def"}, Hostname: "cc-host"},
|
|
|
|
out: config.CloudConfig{SSHAuthorizedKeys: []string{"abc", "def"}, Hostname: "cc-host"},
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
{
|
2015-01-27 02:42:06 +03:00
|
|
|
// If cc is empty, cc should be returned unchanged(overridden)
|
|
|
|
cc: &config.CloudConfig{},
|
|
|
|
md: datasource.Metadata{Hostname: "md-host", SSHPublicKeys: map[string]string{"key": "ghi"}},
|
|
|
|
out: config.CloudConfig{SSHAuthorizedKeys: []string{"ghi"}, Hostname: "md-host"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// If cc is nil, cc should be returned unchanged(overridden)
|
|
|
|
md: datasource.Metadata{Hostname: "md-host", SSHPublicKeys: map[string]string{"key": "ghi"}},
|
|
|
|
out: config.CloudConfig{SSHAuthorizedKeys: []string{"ghi"}, Hostname: "md-host"},
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// user-data should override completely in the case of conflicts
|
2015-01-27 02:42:06 +03:00
|
|
|
cc: &config.CloudConfig{SSHAuthorizedKeys: []string{"abc", "def"}, Hostname: "cc-host"},
|
|
|
|
md: datasource.Metadata{Hostname: "md-host"},
|
|
|
|
out: config.CloudConfig{SSHAuthorizedKeys: []string{"abc", "def"}, Hostname: "cc-host"},
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Mixed merge should succeed
|
2015-01-27 02:42:06 +03:00
|
|
|
cc: &config.CloudConfig{SSHAuthorizedKeys: []string{"abc", "def"}, Hostname: "cc-host"},
|
|
|
|
md: datasource.Metadata{Hostname: "md-host", SSHPublicKeys: map[string]string{"key": "ghi"}},
|
|
|
|
out: config.CloudConfig{SSHAuthorizedKeys: []string{"abc", "def", "ghi"}, Hostname: "cc-host"},
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Completely non-conflicting merge should be fine
|
2015-01-27 02:42:06 +03:00
|
|
|
cc: &config.CloudConfig{Hostname: "cc-host"},
|
|
|
|
md: datasource.Metadata{SSHPublicKeys: map[string]string{"zaphod": "beeblebrox"}},
|
|
|
|
out: config.CloudConfig{Hostname: "cc-host", SSHAuthorizedKeys: []string{"beeblebrox"}},
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Non-mergeable settings in user-data should not be affected
|
2015-01-27 02:42:06 +03:00
|
|
|
cc: &config.CloudConfig{Hostname: "cc-host", ManageEtcHosts: config.EtcHosts("lolz")},
|
|
|
|
md: datasource.Metadata{Hostname: "md-host"},
|
|
|
|
out: config.CloudConfig{Hostname: "cc-host", ManageEtcHosts: config.EtcHosts("lolz")},
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2015-01-27 02:42:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
out := mergeConfigs(tt.cc, tt.md)
|
|
|
|
if !reflect.DeepEqual(tt.out, out) {
|
|
|
|
t.Errorf("bad config (%d): want %#v, got %#v", i, tt.out, out)
|
2014-06-27 04:17:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|