Ejemplo n.º 1
0
class PayloadInterfaceTestCase(TestCase):
    def setUp(self):
        """Set up the payload module."""
        self.payload_module = PayloadModule()
        self.payload_interface = PayloadInterface(self.payload_module)

    def kickstart_properties_test(self):
        """Test kickstart properties."""
        self.assertEqual(self.payload_interface.KickstartCommands, ['liveimg'])
        self.assertEqual(self.payload_interface.KickstartSections,
                         ["packages"])
        self.assertEqual(self.payload_interface.KickstartAddons, [])

    def no_handler_set_test(self):
        """Test empty string is returned when no handler is set."""
        self.assertEqual(self.payload_interface.GetActiveHandlerPath(), "")

    def generate_kickstart_without_handler_test(self):
        """Test kickstart parsing without handler set."""
        self.assertEqual(self.payload_interface.GenerateKickstart(), "")

    def process_kickstart_with_no_handler_test(self):
        """Test kickstart processing when no handler set or created based on KS data."""
        with self.assertLogs('anaconda.modules.payload.payload',
                             level="WARNING") as log:
            self.payload_interface.ReadKickstart("")

            self.assertTrue(
                any(map(lambda x: "No handler was created" in x, log.output)))

    @patch_dbus_publish_object
    def is_handler_set_test(self, publisher):
        """Test IsHandlerSet API."""
        self.assertFalse(self.payload_interface.IsHandlerSet())

        self.payload_interface.CreateHandler(HandlerType.DNF.value)
        self.assertTrue(self.payload_interface.IsHandlerSet())

    @patch_dbus_publish_object
    def create_dnf_handler_test(self, publisher):
        """Test creation and publishing of the DNF handler module."""
        self.payload_interface.CreateHandler(HandlerType.DNF.value)
        self.assertEqual(self.payload_interface.GetActiveHandlerPath(),
                         PAYLOAD_DEFAULT.object_path)
        # here the publisher is called twice because the Packages section is also published
        self.assertEqual(publisher.call_count, 2)

    @patch_dbus_publish_object
    def create_live_os_handler_test(self, publisher):
        """Test creation and publishing of the Live OS handler module."""
        self.payload_interface.CreateHandler(HandlerType.LIVE_OS.value)
        self.assertEqual(self.payload_interface.GetActiveHandlerPath(),
                         LIVE_OS_HANDLER.object_path)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_live_image_handler_test(self, publisher):
        """Test creation and publishing of the Live image handler module."""
        self.payload_interface.CreateHandler(HandlerType.LIVE_IMAGE.value)
        self.assertEqual(self.payload_interface.GetActiveHandlerPath(),
                         LIVE_IMAGE_HANDLER.object_path)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_invalid_handler_test(self, publisher):
        """Test creation of the not existing handler."""
        with self.assertRaises(ValueError):
            self.payload_interface.CreateHandler("NotAHandler")

    @patch_dbus_publish_object
    def create_multiple_handlers_test(self, publisher):
        """Test creating two handlers."""
        self.payload_interface.CreateHandler(HandlerType.DNF.value)
        self.payload_interface.CreateHandler(HandlerType.LIVE_OS.value)

        # The last one should win
        self.assertEqual(self.payload_interface.GetActiveHandlerPath(),
                         LIVE_OS_HANDLER.object_path)
        self.assertEqual(publisher.call_count, 3)
Ejemplo n.º 2
0
class PayloadInterfaceTestCase(unittest.TestCase):

    def setUp(self):
        """Set up the payload module."""
        # Set up the security module.
        self.payload_module = PayloadModule()
        self.payload_interface = PayloadInterface(self.payload_module)

        self.package_module = self.payload_module._payload_handler._packages_handler
        self.package_interface = PackagesHandlerInterface(self.package_module)

        self.callback = Mock()
        self.package_interface.PropertiesChanged.connect(self.callback)

    def _test_kickstart(self, ks_in, ks_out):
        check_kickstart_interface(self, self.payload_interface, ks_in, ks_out)

    def kickstart_properties_test(self):
        """Test kickstart properties."""
        self.assertEqual(self.payload_interface.KickstartCommands, [])
        self.assertEqual(self.payload_interface.KickstartSections, ["packages"])
        self.assertEqual(self.payload_interface.KickstartAddons, [])

    def packages_section_empty_kickstart_test(self):
        """Test the empty packages section."""
        ks_in = """
        %packages
        %end
        """
        ks_out = """
        %packages

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def packages_section_kickstart_test(self):
        """Test the packages section."""
        ks_in = """
        %packages
        package
        @group
        @module:10
        @module2:1.5/server
        @^environment
        %end
        """
        ks_out = """
        %packages
        @^environment
        @group
        @module2:1.5/server
        @module:10
        package

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def packages_section_complex_kickstart_test(self):
        """Test the packages section with duplicates."""
        ks_in = """
        %packages
        @^environment1
        package1
        @group1
        package2

        # Only this environment will stay (last specified wins)
        @^environment2
        @group2

        # duplicates
        package2
        @group2

        # modules
        @module:4
        @module:3.5/server

        %end
        """
        # The last specified environment wins, you can't specify two environments
        # Same package or group specified twice will be deduplicated
        ks_out = """
        %packages
        @^environment2
        @group1
        @group2
        @module:3.5/server
        @module:4
        package1
        package2

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def packages_section_with_attribute_kickstart_test(self):
        """Test the packages section with attribute."""
        ks_in = """
        %packages --nocore
        %end
        """
        ks_out = """
        %packages --nocore

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def packages_section_multiple_attributes_kickstart_test(self):
        """Test the packages section with multiple attributes."""
        ks_in = """
        %packages --nocore --multilib --instLangs en_US.UTF-8

        %end
        """
        ks_out = """
        %packages --nocore --instLangs=en_US.UTF-8 --multilib

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def packages_section_excludes_kickstart_test(self):
        """Test the packages section with excludes."""
        ks_in = """
        %packages
        -vim
        %end
        """
        ks_out = """
        %packages
        -vim

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def packages_section_complex_exclude_kickstart_test(self):
        """Test the packages section with complex exclude example."""
        ks_in = """
        %packages --nocore --ignoremissing --instLangs=
        @^environment1
        @group1
        package1
        -package2
        -@group2
        @group3
        package3
        %end
        """
        ks_out = """
        %packages --nocore --ignoremissing --instLangs=
        @^environment1
        @group1
        @group3
        package1
        package3
        -@group2
        -package2

        %end
        """
        self._test_kickstart(ks_in, ks_out)

    def core_group_enabled_properties_test(self):
        self.package_interface.SetCoreGroupEnabled(True)
        self.assertEqual(self.package_interface.CoreGroupEnabled, True)
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"CoreGroupEnabled": True}, [])

    def core_group_not_set_properties_test(self):
        self.assertEqual(self.package_interface.CoreGroupEnabled, True)

    def default_environment_not_set_properties_test(self):
        self.assertEqual(self.package_interface.DefaultEnvironment, False)

    def environment_properties_test(self):
        self.package_interface.SetEnvironment("TestEnv")
        self.assertEqual(self.package_interface.Environment, "TestEnv")
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"Environment": "TestEnv"}, [])

    def environment_not_set_properties_test(self):
        self.assertEqual(self.package_interface.Environment, "")

    def groups_properties_test(self):
        self.package_interface.SetGroups(["group1", "group2"])
        self.assertEqual(self.package_interface.Groups, ["group1", "group2"])
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"Groups": ["group1", "group2"]}, [])

    def groups_not_set_properties_test(self):
        self.assertEqual(self.package_interface.Groups, [])

    def groups_properties_from_kickstart_test(self):
        ks_in = """
        %packages
        @^environment
        @module:14
        @group1
        -@group1
        -@group2
        @group3
        @group4
        @module2:3/client
        %end
        """
        self.payload_interface.ReadKickstart(ks_in)
        self.assertEqual(self.package_interface.Groups, ["module:14",
                                                         "group3", "group4",
                                                         "module2:3/client"])

    def groups_properties_to_kickstart_test(self):
        ks_out = """
        %packages
        @group1
        @group2
        @module1:2.4/server
        @module2:33

        %end
        """
        self.package_interface.SetGroups(["group2", "group1",
                                          "module1:2.4/server", "module2:33"])
        self.assertEqual(self.payload_interface.GenerateKickstart().strip(),
                         dedent(ks_out).strip())

    def packages_properties_test(self):
        self.package_interface.SetPackages(["package1", "package2"])
        self.assertEqual(self.package_interface.Packages, ["package1", "package2"])
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"Packages": ["package1", "package2"]}, [])

    def packages_not_set_properties_test(self):
        self.assertEqual(self.package_interface.Packages, [])

    def excluded_groups_properties_test(self):
        self.package_interface.SetExcludedGroups(["group1", "group2"])
        self.assertEqual(self.package_interface.ExcludedGroups, ["group1", "group2"])
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"ExcludedGroups": ["group1", "group2"]}, [])

    def excluded_groups_not_set_properties_test(self):
        self.assertEqual(self.package_interface.ExcludedGroups, [])

    def excluded_groups_properties_from_kickstart_test(self):
        ks_in = """
        %packages
        @^environment1
        @group1
        -@group2
        @group3
        -@group3
        %end
        """
        self.payload_interface.ReadKickstart(ks_in)
        self.assertEqual(self.package_interface.ExcludedGroups, ["group2", "group3"])

    def excluded_groups_properties_to_kickstart_test(self):
        ks_out = """
        %packages
        -@group1
        -@group2

        %end
        """
        self.package_interface.SetExcludedGroups(["group2", "group1"])
        self.assertEqual(self.payload_interface.GenerateKickstart().strip(),
                         dedent(ks_out).strip())

    def excluded_packages_properties_test(self):
        self.package_interface.SetExcludedPackages(["package1", "package2"])
        self.assertEqual(self.package_interface.ExcludedPackages, ["package1", "package2"])
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"ExcludedPackages": ["package1", "package2"]}, [])

    def excluded_packages_not_set_properties_test(self):
        self.assertEqual(self.package_interface.ExcludedPackages, [])

    def docs_excluded_properties_test(self):
        self.package_interface.SetDocsExcluded(True)
        self.assertEqual(self.package_interface.DocsExcluded, True)
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"DocsExcluded": True}, [])

    def docs_excluded_not_set_properties_test(self):
        self.assertEqual(self.package_interface.DocsExcluded, False)

    def weakdeps_excluded_properties_test(self):
        self.package_interface.SetWeakdepsExcluded(True)
        self.assertEqual(self.package_interface.WeakdepsExcluded, True)
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"WeakdepsExcluded": True}, [])

    def weakdeps_excluded_not_set_properties_test(self):
        self.assertEqual(self.package_interface.WeakdepsExcluded, False)

    def missing_ignored_properties_test(self):
        self.package_interface.SetMissingIgnored(True)
        self.assertEqual(self.package_interface.MissingIgnored, True)
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"MissingIgnored": True}, [])

    def missing_ignored_not_set_properties_test(self):
        self.assertEqual(self.package_interface.MissingIgnored, False)

    def languages_properties_test(self):
        self.package_interface.SetLanguages("en, es")
        self.assertEqual(self.package_interface.Languages, "en, es")
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"Languages": "en, es"}, [])

    def languages_not_set_properties_test(self):
        self.assertEqual(self.package_interface.Languages, LANGUAGES_DEFAULT)

    def languages_incorrect_value_properties_test(self):
        with self.assertRaises(InvalidValueError):
            self.package_interface.SetLanguages("")

    def languages_none_properties_from_kickstart_test(self):
        ks_in = """
        %packages --instLangs=
        %end
        """
        self.payload_interface.ReadKickstart(ks_in)
        self.assertEqual(self.package_interface.Languages, LANGUAGES_NONE)

    def languages_all_properties_from_kickstart_test(self):
        ks_in = """
        %packages
        %end
        """
        self.payload_interface.ReadKickstart(ks_in)
        self.assertEqual(self.package_interface.Languages, LANGUAGES_DEFAULT)

    def multilib_policy_properties_test(self):
        self.package_interface.SetMultilibPolicy('all')
        self.assertEqual(self.package_interface.MultilibPolicy, 'all')
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"MultilibPolicy": 'all'}, [])

    def multilib_policy_not_set_properties_test(self):
        self.assertEqual(self.package_interface.MultilibPolicy, 'best')

    def timeout_properties_test(self):
        self.package_interface.SetTimeout(60)
        self.assertEqual(self.package_interface.Timeout, 60)
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"Timeout": 60}, [])

    def timeout_not_set_properties_test(self):
        self.assertEqual(self.package_interface.Timeout, TIMEOUT_UNSET)

    def retries_properties_test(self):
        self.package_interface.SetRetries(30)
        self.assertEqual(self.package_interface.Retries, 30)
        self.callback.assert_called_once_with(
            DNF_PACKAGES.interface_name, {"Retries": 30}, [])

    def retries_not_set_properties_test(self):
        self.assertEqual(self.package_interface.Retries, RETRIES_UNSET)