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