2014-10-18 02:36:22 +04:00
|
|
|
/*
|
|
|
|
Copyright 2014 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-06-27 04:17:58 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2014-09-22 06:22:13 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
2014-06-27 04:17:58 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergeCloudConfig(t *testing.T) {
|
2014-09-22 03:46:58 +04:00
|
|
|
simplecc := config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
SSHAuthorizedKeys: []string{"abc", "def"},
|
|
|
|
Hostname: "foobar",
|
|
|
|
NetworkConfigPath: "/path/somewhere",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
}
|
|
|
|
for i, tt := range []struct {
|
2014-09-22 03:46:58 +04:00
|
|
|
udcc config.CloudConfig
|
|
|
|
mdcc config.CloudConfig
|
|
|
|
want config.CloudConfig
|
2014-06-27 04:17:58 +04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
// If mdcc is empty, udcc should be returned unchanged
|
|
|
|
simplecc,
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{},
|
2014-06-27 04:17:58 +04:00
|
|
|
simplecc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// If udcc is empty, mdcc should be returned unchanged(overridden)
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{},
|
2014-06-27 04:17:58 +04:00
|
|
|
simplecc,
|
|
|
|
simplecc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// user-data should override completely in the case of conflicts
|
|
|
|
simplecc,
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "meta-hostname",
|
|
|
|
NetworkConfigPath: "/path/meta",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
simplecc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Mixed merge should succeed
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
SSHAuthorizedKeys: []string{"abc", "def"},
|
|
|
|
Hostname: "user-hostname",
|
|
|
|
NetworkConfigPath: "/path/somewhere",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
SSHAuthorizedKeys: []string{"woof", "qux"},
|
|
|
|
Hostname: "meta-hostname",
|
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
SSHAuthorizedKeys: []string{"abc", "def", "woof", "qux"},
|
|
|
|
Hostname: "user-hostname",
|
|
|
|
NetworkConfigPath: "/path/somewhere",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Completely non-conflicting merge should be fine
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "supercool",
|
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
SSHAuthorizedKeys: []string{"zaphod", "beeblebrox"},
|
|
|
|
NetworkConfigPath: "/dev/fun",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "supercool",
|
|
|
|
SSHAuthorizedKeys: []string{"zaphod", "beeblebrox"},
|
|
|
|
NetworkConfigPath: "/dev/fun",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Non-mergeable settings in user-data should not be affected
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
2014-09-22 06:22:13 +04:00
|
|
|
ManageEtcHosts: config.EtcHosts("lolz"),
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "youyouyou",
|
|
|
|
NetworkConfigPath: "meta-meta-yo",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
2014-09-22 06:22:13 +04:00
|
|
|
ManageEtcHosts: config.EtcHosts("lolz"),
|
2014-06-27 04:17:58 +04:00
|
|
|
NetworkConfigPath: "meta-meta-yo",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Non-mergeable (unexpected) settings in meta-data are ignored
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-09-22 06:22:13 +04:00
|
|
|
ManageEtcHosts: config.EtcHosts("lolz"),
|
2014-06-27 04:17:58 +04:00
|
|
|
NetworkConfigPath: "meta-meta-yo",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2014-09-22 03:46:58 +04:00
|
|
|
config.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
|
|
|
NetworkConfigPath: "meta-meta-yo",
|
2014-08-30 00:18:16 +04:00
|
|
|
NetworkConfig: `{"hostname":"test"}`,
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
got := mergeCloudConfig(tt.mdcc, tt.udcc)
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("case #%d: mergeCloudConfig mutated CloudConfig unexpectedly:\ngot:\n%s\nwant:\n%s", i, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|