diff --git a/hooks/nova_compute_hooks.py b/hooks/nova_compute_hooks.py index d2f7d5c0..7c0e9d25 100755 --- a/hooks/nova_compute_hooks.py +++ b/hooks/nova_compute_hooks.py @@ -17,6 +17,7 @@ import platform import sys import uuid +import yaml import charmhelpers.core.unitdata as unitdata @@ -152,9 +153,12 @@ def config_changed(): do_openstack_upgrade(CONFIGS) send_remote_restart = True - sysctl_dict = config('sysctl') - if sysctl_dict: - create_sysctl(sysctl_dict, '/etc/sysctl.d/50-nova-compute.conf') + sysctl_settings = config('sysctl') + if sysctl_settings: + sysctl_dict = yaml.safe_load(sysctl_settings) + sysctl_dict['vm.swappiness'] = sysctl_dict.get('vm.swappiness', 1) + create_sysctl(yaml.dump(sysctl_dict), + '/etc/sysctl.d/50-nova-compute.conf') destroy_libvirt_network('default') diff --git a/unit_tests/test_nova_compute_hooks.py b/unit_tests/test_nova_compute_hooks.py index 171bc847..c968a02f 100644 --- a/unit_tests/test_nova_compute_hooks.py +++ b/unit_tests/test_nova_compute_hooks.py @@ -238,9 +238,24 @@ class NovaComputeRelationsTests(CharmTestCase): @patch.object(hooks, 'compute_joined') def test_config_changed_with_sysctl(self, compute_joined): self.git_install_requested.return_value = False - self.test_config.set('sysctl', '{ kernel.max_pid : "1337" }') + self.test_config.set( + 'sysctl', + '{ kernel.max_pid : "1337", vm.swappiness : 10 }') hooks.config_changed() - self.assertTrue(self.create_sysctl.called) + self.create_sysctl.assert_called_with( + "{kernel.max_pid: '1337', vm.swappiness: 10}\n", + '/etc/sysctl.d/50-nova-compute.conf') + + @patch.object(hooks, 'compute_joined') + def test_config_changed_with_sysctl_swappy_default(self, compute_joined): + self.git_install_requested.return_value = False + self.test_config.set( + 'sysctl', + '{ kernel.max_pid : "1337" }') + hooks.config_changed() + self.create_sysctl.assert_called_with( + "{kernel.max_pid: '1337', vm.swappiness: 1}\n", + '/etc/sysctl.d/50-nova-compute.conf') @patch.object(hooks, 'config_value_changed') def test_config_changed_git(self, config_val_changed):