2014-06-27 04:17:58 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2014-09-28 20:15:25 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/initialize"
|
2014-06-27 04:17:58 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergeCloudConfig(t *testing.T) {
|
2014-09-28 20:15:25 +04:00
|
|
|
simplecc := initialize.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-28 20:15:25 +04:00
|
|
|
udcc initialize.CloudConfig
|
|
|
|
mdcc initialize.CloudConfig
|
|
|
|
want initialize.CloudConfig
|
2014-06-27 04:17:58 +04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
// If mdcc is empty, udcc should be returned unchanged
|
|
|
|
simplecc,
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{},
|
2014-06-27 04:17:58 +04:00
|
|
|
simplecc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// If udcc is empty, mdcc should be returned unchanged(overridden)
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{},
|
2014-06-27 04:17:58 +04:00
|
|
|
simplecc,
|
|
|
|
simplecc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// user-data should override completely in the case of conflicts
|
|
|
|
simplecc,
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.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-28 20:15:25 +04:00
|
|
|
initialize.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-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
SSHAuthorizedKeys: []string{"woof", "qux"},
|
|
|
|
Hostname: "meta-hostname",
|
|
|
|
},
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.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-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "supercool",
|
|
|
|
},
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.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-28 20:15:25 +04:00
|
|
|
initialize.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-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
2014-09-28 20:15:25 +04:00
|
|
|
ManageEtcHosts: initialize.EtcHosts("lolz"),
|
2014-06-27 04:17:58 +04:00
|
|
|
},
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.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-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
2014-09-28 20:15:25 +04:00
|
|
|
ManageEtcHosts: initialize.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-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{
|
2014-06-27 04:17:58 +04:00
|
|
|
Hostname: "mememe",
|
|
|
|
},
|
2014-09-28 20:15:25 +04:00
|
|
|
initialize.CloudConfig{
|
|
|
|
ManageEtcHosts: initialize.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-28 20:15:25 +04:00
|
|
|
initialize.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|