def test_apply_params_to_empty_profile(self): """test applying params to empty profile""" params = { "sysctl": [{ "name": "fs.epoll.max_user_watches", "value": 785592 }], "sysfs": [{ "name": "/sys/kernel/debug/x86/pti_enabled", "value": 0 }], "vm": [{ "name": "transparent_hugepages", "value": "never" }], "bootloader": [{ "name": "cmdline", "value": [ { "name": "spectre_v2", "value": "off" }, { "name": "nopti" }, { "name": "panic", "value": 10001 }, { "name": "splash" }, ], }], } # pylint: disable=blacklisted-name tuned_app, _ = kernel_settings.load_current_profile( self.tuned_config, "kernel_settings", self.logger) current_profile = tuned_app.daemon.profile errlist = kernel_settings.validate_and_digest(params) self.assertEqual(len(errlist), 0) changestatus, reboot_required = kernel_settings.apply_params_to_profile( params, current_profile, False) self.assertEqual(kernel_settings.CHANGES, changestatus) sysctl = {"fs.epoll.max_user_watches": "785592"} self.assertEqual(sysctl, dict(current_profile.units["sysctl"].options)) sysfs = {"/sys/kernel/debug/x86/pti_enabled": "0"} self.assertEqual(sysfs, dict(current_profile.units["sysfs"].options)) vm = {"transparent_hugepages": "never"} self.assertEqual(vm, current_profile.units["vm"].options) cmdline = {"cmdline": "spectre_v2=off nopti panic=10001 splash"} self.assertEqual(cmdline, dict(current_profile.units["bootloader"].options)) self.assertTrue(reboot_required) # test idempotency changestatus, reboot_required = kernel_settings.apply_params_to_profile( params, current_profile, False) self.assertEqual(kernel_settings.NOCHANGES, changestatus) self.assertFalse(reboot_required)
def test_load_profile(self): """test the code that loads profiles""" tuned_app, errmsg = kernel_settings.load_current_profile( self.tuned_config, "junk", self.logger) self.assertIsNone(tuned_app) self.assertRegex(errmsg, "Profile does not exist") tuned_app, errmsg = kernel_settings.load_current_profile( self.tuned_config, "no_profile_file", self.logger) self.assertIsNone(tuned_app) self.assertRegex(errmsg, "Profile does not exist") tuned_app, errmsg = kernel_settings.load_current_profile( self.tuned_config, "empty_profile_file", self.logger) self.assertIsNone(tuned_app) self.assertRegex(errmsg, "Error loading tuned profile") tuned_app, errmsg = kernel_settings.load_current_profile( self.tuned_config, "bogus", self.logger) self.assertIsNone(tuned_app) self.assertRegex(errmsg, "Error loading tuned profile") # pylint: disable=blacklisted-name tuned_app, _ = kernel_settings.load_current_profile( self.tuned_config, "kernel_settings", self.logger) self.assertEqual("kernel settings", tuned_app.daemon.profile.options["summary"]) self.assertEqual(0, len(tuned_app.daemon.profile.units)) # pylint: disable=blacklisted-name tuned_app, _ = kernel_settings.load_current_profile( self.tuned_config, "basic_settings", self.logger) self.assertEqual("kernel settings", tuned_app.daemon.profile.options["summary"]) units = tuned_app.daemon.profile.units self.assertIn("sysctl", units) self.assertIn("sysfs", units) self.assertIn("vm", units) self.assertIn("bootloader", units) self.assertEqual(4, len(units["sysctl"].options)) self.assertEqual(4, len(units["sysfs"].options)) self.assertEqual(1, len(units["vm"].options)) self.assertEqual(1, len(units["bootloader"].options))
def test_write_profile(self): """test applying params and writing new profile""" params = { "sysctl": [{ "name": "fs.epoll.max_user_watches", "value": 785592 }], "sysfs": [{ "name": "/sys/kernel/debug/x86/pti_enabled", "value": 0 }], "vm": [{ "name": "transparent_hugepages", "value": "never" }], "bootloader": [{ "name": "cmdline", "value": [ { "name": "spectre_v2", "value": "off" }, { "name": "nopti" }, { "name": "panic", "value": 10001 }, { "name": "splash" }, ], }], } errlist = kernel_settings.validate_and_digest(params) self.assertEqual(len(errlist), 0) tuned_app, _ = kernel_settings.load_current_profile( self.tuned_config, "kernel_settings", self.logger) current_profile = tuned_app.daemon.profile changestatus, reboot_required = kernel_settings.apply_params_to_profile( params, current_profile, False) self.assertEqual(kernel_settings.CHANGES, changestatus) self.assertTrue(reboot_required) kernel_settings.write_profile(current_profile) fname = os.path.join(tuned.consts.LOAD_DIRECTORIES[-1], "kernel_settings", "tuned.conf") expected_lines = [ "# File managed by Ansible - DO NOT EDIT", "[main]", "summary = kernel settings", "[sysctl]", "fs.epoll.max_user_watches = 785592", "[sysfs]", "/sys/kernel/debug/x86/pti_enabled = 0", "[vm]", "transparent_hugepages = never", "[bootloader]", "cmdline = spectre_v2=off nopti panic=10001 splash", ] expected = ConfigObj(expected_lines) actual = None with open(fname, "r") as ff: actual = ConfigObj(ff) self.assertEqual(expected, actual) self.assertEqual(expected.initial_comment, actual.initial_comment)
def test_apply_params_and_ops_to_profile(self): """test applying params and operations to a profile""" params = { "sysctl": [ { "name": "fs.epoll.max_user_watches", "value": 785592 }, { "name": "kernel.threads-max", "state": "absent" }, { "name": "fs.file-max", "state": "absent" }, ], "sysfs": [ { "previous": "replaced" }, { "name": "/sys/kernel/debug/x86/pti_enabled", "value": 0 }, ], "vm": { "state": "empty" }, "bootloader": [{ "name": "cmdline", "value": [ { "name": "someother", "value": "value" }, { "name": "spectre_v2", "value": "off" }, { "name": "nopti" }, { "name": "panic", "state": "absent" }, { "name": "splash", "state": "absent" }, ], }], } paramsorig = copy.deepcopy(params) tuned_app, _ = kernel_settings.load_current_profile( self.tuned_config, "basic_settings", self.logger) current_profile = tuned_app.daemon.profile errlist = kernel_settings.validate_and_digest(params) self.assertEqual(len(errlist), 0) changestatus, reboot_required = kernel_settings.apply_params_to_profile( params, current_profile, False) self.assertEqual(kernel_settings.CHANGES, changestatus) sysctl = { "fs.epoll.max_user_watches": "785592", "vm.max_map_count": "65530" } self.assertEqual(sysctl, dict(current_profile.units["sysctl"].options)) sysfs = {"/sys/kernel/debug/x86/pti_enabled": "0"} self.assertEqual(sysfs, dict(current_profile.units["sysfs"].options)) self.assertNotIn("vm", current_profile.units) cmdline = {"cmdline": "spectre_v2=off nopti someother=value"} self.assertEqual(cmdline, dict(current_profile.units["bootloader"].options)) self.assertTrue(reboot_required) # idempotency errlist = kernel_settings.validate_and_digest(paramsorig) self.assertEqual(len(errlist), 0) changestatus, reboot_required = kernel_settings.apply_params_to_profile( paramsorig, current_profile, False) self.assertEqual(kernel_settings.NOCHANGES, changestatus) self.assertFalse(reboot_required)