diff --git a/cmd/control/config.go b/cmd/control/config.go index 95405012..d2512647 100644 --- a/cmd/control/config.go +++ b/cmd/control/config.go @@ -186,12 +186,12 @@ func configSet(c *cli.Context) { log.Fatal(err) } - cfg, err = cfg.Set(key, value) + cfgDiff, err := cfg.Set(key, value) if err != nil { log.Fatal(err) } - if err := cfg.Save(); err != nil { + if err := cfg.Save(cfgDiff); err != nil { log.Fatal(err) } } diff --git a/config/config.go b/config/config.go index f1d93cc0..959a124d 100644 --- a/config/config.go +++ b/config/config.go @@ -147,18 +147,14 @@ func (c *CloudConfig) GetIgnoreOmitEmpty(key string) (interface{}, error) { return v, nil } -func (c *CloudConfig) Set(key string, value interface{}) (*CloudConfig, error) { +func (c *CloudConfig) Set(key string, value interface{}) (map[interface{}]interface{}, error) { data := map[interface{}]interface{}{} - if err := util.Convert(c, &data); err != nil { - return c, err - } - _, data = getOrSetVal(key, data, value) - return c.Merge(data) + return data, nil } -func (c *CloudConfig) Save() error { +func (c *CloudConfig) Save(cfgDiffs ...map[interface{}]interface{}) error { files := append([]string{OsConfigFile, OemConfigFile}, CloudConfigDirFiles()...) files = util.FilterStrings(files, func(x string) bool { return x != CloudConfigPrivateFile }) exCfg, err := ChainCfgFuncs(nil, @@ -184,6 +180,12 @@ func (c *CloudConfig) Save() error { } data = util.MapsDifference(data, exData) + + // Apply any additional config diffs + for _, diff := range cfgDiffs { + data = util.MapsUnion(data, diff) + } + log.WithFields(log.Fields{"diff": data}).Debug("The diff we're about to save") if err := saveToDisk(data); err != nil { return err diff --git a/tests/integration/assets/test_14/cloud-config.yml b/tests/integration/assets/test_14/cloud-config.yml new file mode 100644 index 00000000..0586b0b8 --- /dev/null +++ b/tests/integration/assets/test_14/cloud-config.yml @@ -0,0 +1,25 @@ +#cloud-config +write_files: + - path: /var/lib/rancher/conf/cloud-config.d/cloud-config1.yml + permissions: "0444" + owner: root + content: | + hostname: hostname1 + rancher: + log: false + - path: /var/lib/rancher/conf/cloud-config.d/cloud-config2.yml + permissions: "0444" + owner: root + content: | + hostname: hostname2 + rancher: + debug: false + - path: /var/lib/rancher/conf/cloud-config.d/cloud-config3.yml + permissions: "0444" + owner: root + content: | + hostname: hostname3 + rancher: + log: true +ssh_authorized_keys: + - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC85w9stZyiLQp/DkVO6fqwiShYcj1ClKdtCqgHtf+PLpJkFReSFu8y21y+ev09gsSMRRrjF7yt0pUHV6zncQhVeqsZtgc5WbELY2DOYUGmRn/CCvPbXovoBrQjSorqlBmpuPwsStYLr92Xn+VVsMNSUIegHY22DphGbDKG85vrKB8HxUxGIDxFBds/uE8FhSy+xsoyT/jUZDK6pgq2HnGl6D81ViIlKecpOpWlW3B+fea99ADNyZNVvDzbHE5pcI3VRw8u59WmpWOUgT6qacNVACl8GqpBvQk8sw7O/X9DSZHCKafeD9G5k+GYbAUz92fKWrx/lOXfUXPS3+c8dRIF diff --git a/tests/integration/rostest/test_09_network.py b/tests/integration/rostest/test_09_network.py index 6e9a66e9..66a58f29 100644 --- a/tests/integration/rostest/test_09_network.py +++ b/tests/integration/rostest/test_09_network.py @@ -48,18 +48,3 @@ cat /etc/resolv.conf | grep "nameserver 208.67.220.123" SCRIPT sudo bash test-merge '''.strip()) - - -def test_network_dns_ros_set(qemu): - SSH(qemu).check_call(''' -set -x -e - -sudo ros config set rancher.network.dns.search '[a,b]' -if [ "$(sudo ros config get rancher.network.dns.search)" == "- a - - b - - " ]; then - sudo ros config get rancher.network.dns.search - exit 1 - fi - '''.strip()) diff --git a/tests/integration/rostest/test_14_ros_config.py b/tests/integration/rostest/test_14_ros_config.py new file mode 100644 index 00000000..2079ced6 --- /dev/null +++ b/tests/integration/rostest/test_14_ros_config.py @@ -0,0 +1,85 @@ +import pytest +import rostest.util as u +from rostest.util import SSH + +cloud_config_path = './tests/integration/assets/test_14/cloud-config.yml' + + +@pytest.fixture(scope="module") +def qemu(request): + q = u.run_qemu(request, run_args=['--cloud-config', cloud_config_path]) + u.flush_out(q.stdout) + return q + + +def test_ros_config_string(qemu): + SSH(qemu).check_call(''' +set -x -e + +if [ "$(sudo ros config get hostname)" == "hostname3 + " ]; then + sudo ros config get hostname + exit 1 + fi + +sudo ros config set hostname rancher-test +if [ "$(sudo ros config get hostname)" == "rancher-test + " ]; then + sudo ros config get hostname + exit 1 + fi + '''.strip()) + + +def test_ros_config_bool(qemu): + SSH(qemu).check_call(''' +set -x -e + +if [ "$(sudo ros config get rancher.log)" == "true + " ]; then + sudo ros config get rancher.log + exit 1 + fi + +sudo ros config set rancher.log false +if [ "$(sudo ros config get rancher.log)" == "false + " ]; then + sudo ros config get rancher.log + exit 1 + fi + +if [ "$(sudo ros config get rancher.debug)" == "false + " ]; then + sudo ros config get rancher.debug + exit 1 + fi + +sudo ros config set rancher.debug true +if [ "$(sudo ros config get rancher.debug)" == "true + " ]; then + sudo ros config get rancher.debug + exit 1 + fi + '''.strip()) + + +def test_ros_config_slice(qemu): + SSH(qemu).check_call(''' +set -x -e + +sudo ros config set rancher.network.dns.search '[a,b]' +if [ "$(sudo ros config get rancher.network.dns.search)" == "- a + - b + + " ]; then + sudo ros config get rancher.network.dns.search + exit 1 + fi + +sudo ros config set rancher.network.dns.search '[]' +if [ "$(sudo ros config get rancher.network.dns.search)" == "[] + " ]; then + sudo ros config get rancher.network.dns.search + exit 1 + fi + '''.strip())