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_validate_and_digest(self):
     """do various tests of validate_and_digest"""
     params = {"section": {"bogus": "dict"}}
     errlist = kernel_settings.validate_and_digest(params)
     self.assertEqual(len(errlist), 1)
     self.assertEqual(
         errlist[0],
         ("Error: to remove the section [section] specify "
          "the value {'state': 'empty'}"),
     )
     params = {"section": 0}
     errlist = kernel_settings.validate_and_digest(params)
     self.assertEqual(len(errlist), 1)
     self.assertEqual(
         errlist[0],
         "Error: section [section] value must be a dict or a list")
     params = {"section": [0]}
     errlist = kernel_settings.validate_and_digest(params)
     self.assertEqual(len(errlist), 1)
     self.assertEqual(
         errlist[0],
         "Error: section [section] item value [0] is not a dict")
     params = {"section": [{"previous": "bogus"}]}
     errlist = kernel_settings.validate_and_digest(params)
     self.assertEqual(len(errlist), 1)
     self.assertEqual(
         errlist[0],
         "Error: section [section] item has invalid value for 'previous' [bogus]",
     )
     params = {"section": [{"name": "name", "value": "value"}]}
     errlist = kernel_settings.validate_and_digest(params)
     self.assertEqual(len(errlist), 0)
     params = {"section": [{"previous": "replaced"}]}
     errlist = kernel_settings.validate_and_digest(params)
     self.assertEqual(len(errlist), 0)
     self.assertTrue(params["__section_to_replace"]["section"])
 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)