Ejemplo n.º 1
0
class TimezoneInterfaceTestCase(unittest.TestCase):
    """Test DBus interface for the timezone module."""
    def setUp(self):
        """Set up the timezone module."""
        # Set up the timezone module.
        self.timezone_module = TimezoneService()
        self.timezone_interface = TimezoneInterface(self.timezone_module)

        # Connect to the properties changed signal.
        self.callback = PropertiesChangedCallback()
        self.timezone_interface.PropertiesChanged.connect(self.callback)

    def kickstart_properties_test(self):
        """Test kickstart properties."""
        self.assertEqual(self.timezone_interface.KickstartCommands,
                         ["timezone"])
        self.assertEqual(self.timezone_interface.KickstartSections, [])
        self.assertEqual(self.timezone_interface.KickstartAddons, [])
        self.callback.assert_not_called()

    def timezone_property_test(self):
        """Test the Timezone property."""
        self.timezone_interface.SetTimezone("Europe/Prague")
        self.assertEqual(self.timezone_interface.Timezone, "Europe/Prague")
        self.callback.assert_called_once_with(TIMEZONE.interface_name,
                                              {'Timezone': 'Europe/Prague'},
                                              [])

    def utc_property_test(self):
        """Test the IsUtc property."""
        self.timezone_interface.SetIsUTC(True)
        self.assertEqual(self.timezone_interface.IsUTC, True)
        self.callback.assert_called_once_with(TIMEZONE.interface_name,
                                              {'IsUTC': True}, [])

    def ntp_property_test(self):
        """Test the NTPEnabled property."""
        self.timezone_interface.SetNTPEnabled(False)
        self.assertEqual(self.timezone_interface.NTPEnabled, False)
        self.callback.assert_called_once_with(TIMEZONE.interface_name,
                                              {'NTPEnabled': False}, [])

    def ntp_servers_property_test(self):
        """Test the NTPServers property."""
        self.timezone_interface.SetNTPServers(["ntp.cesnet.cz"])
        self.assertEqual(self.timezone_interface.NTPServers, ["ntp.cesnet.cz"])
        self.callback.assert_called_once_with(
            TIMEZONE.interface_name, {'NTPServers': ["ntp.cesnet.cz"]}, [])

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

    def no_kickstart_test(self):
        """Test with no kickstart."""
        ks_in = None
        ks_out = """
        # System timezone
        timezone America/New_York
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_empty_test(self):
        """Test with empty string."""
        ks_in = ""
        ks_out = ""
        self._test_kickstart(ks_in, ks_out)

    def kickstart_test(self):
        """Test the timezone command."""
        ks_in = """
        timezone Europe/Prague
        """
        ks_out = """
        # System timezone
        timezone Europe/Prague
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart2_test(self):
        """Test the timezone command with flags."""
        ks_in = """
        timezone --utc --nontp Europe/Prague
        """
        ks_out = """
        # System timezone
        timezone Europe/Prague --utc --nontp
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart3_test(self):
        """Test the timezone command with ntp servers.."""
        ks_in = """
        timezone --ntpservers ntp.cesnet.cz Europe/Prague
        """
        ks_out = """
        # System timezone
        timezone Europe/Prague --ntpservers=ntp.cesnet.cz
        """
        self._test_kickstart(ks_in, ks_out)

    @patch_dbus_publish_object
    def install_with_tasks_default_test(self, publisher):
        """Test install tasks - module in default state."""
        task_classes = [
            ConfigureTimezoneTask,
            ConfigureNTPTask,
        ]
        task_paths = self.timezone_interface.InstallWithTasks()
        task_objs = check_task_creation_list(self, task_paths, publisher,
                                             task_classes)

        # ConfigureTimezoneTask
        obj = task_objs[0]
        self.assertEqual(obj.implementation._timezone, "America/New_York")
        self.assertEqual(obj.implementation._is_utc, False)
        # ConfigureNTPTask
        obj = task_objs[1]
        self.assertEqual(obj.implementation._ntp_enabled, True)
        self.assertEqual(obj.implementation._ntp_servers, [])

    @patch_dbus_publish_object
    def install_with_tasks_configured_test(self, publisher):
        """Test install tasks - module in configured state."""

        self.timezone_interface.SetIsUTC(True)
        self.timezone_interface.SetTimezone("Asia/Tokyo")
        self.timezone_interface.SetNTPEnabled(False)
        # --nontp and --ntpservers are mutually exclusive in kicstart but
        # there is no such enforcement in the module so for testing this is ok
        self.timezone_interface.SetNTPServers([
            "clock1.example.com",
            "clock2.example.com",
        ])

        task_classes = [
            ConfigureTimezoneTask,
            ConfigureNTPTask,
        ]
        task_paths = self.timezone_interface.InstallWithTasks()
        task_objs = check_task_creation_list(self, task_paths, publisher,
                                             task_classes)

        # ConfigureTimezoneTask
        obj = task_objs[0]
        self.assertEqual(obj.implementation._timezone, "Asia/Tokyo")
        self.assertEqual(obj.implementation._is_utc, True)

        # ConfigureNTPTask
        obj = task_objs[1]
        self.assertEqual(obj.implementation._ntp_enabled, False)
        self.assertEqual(obj.implementation._ntp_servers, [
            "clock1.example.com",
            "clock2.example.com",
        ])

    @patch_dbus_publish_object
    def configure_ntp_service_enablement_default_test(self, publisher):
        """Test ntp service enablement with default module state."""
        ntp_excluded = True
        task_path = self.timezone_interface.ConfigureNTPServiceEnablementWithTask(
            ntp_excluded)
        obj = check_task_creation(self, task_path, publisher,
                                  ConfigureNTPServiceEnablementTask)
        self.assertEqual(obj.implementation._ntp_enabled, True)
        self.assertEqual(obj.implementation._ntp_excluded, ntp_excluded)

    @patch_dbus_publish_object
    def configure_ntp_service_enablement_configured_test(self, publisher):
        """Test ntp service enablement with configured module state."""
        ntp_excluded = False
        self.timezone_interface.SetNTPEnabled(False)
        task_path = self.timezone_interface.ConfigureNTPServiceEnablementWithTask(
            ntp_excluded)
        obj = check_task_creation(self, task_path, publisher,
                                  ConfigureNTPServiceEnablementTask)
        self.assertEqual(obj.implementation._ntp_enabled, False)
        self.assertEqual(obj.implementation._ntp_excluded, ntp_excluded)

    def deprecated_warnings_test(self):
        response = self.timezone_interface.ReadKickstart(
            "timezone --isUtc Europe/Bratislava")
        report = KickstartReport.from_structure(response)

        warning = "The option --isUtc will be deprecated in future releases. " \
                  "Please modify your kickstart file to replace this option with " \
                  "its preferred alias --utc."

        self.assertEqual(len(report.warning_messages), 1)
        self.assertEqual(report.warning_messages[0].message, warning)
Ejemplo n.º 2
0
class TimezoneInterfaceTestCase(unittest.TestCase):
    """Test DBus interface for the timezone module."""

    def setUp(self):
        """Set up the timezone module."""
        # Set up the timezone module.
        self.timezone_module = TimezoneService()
        self.timezone_interface = TimezoneInterface(self.timezone_module)

        # Connect to the properties changed signal.
        self.callback = PropertiesChangedCallback()
        self.timezone_interface.PropertiesChanged.connect(self.callback)

    def _check_dbus_property(self, *args, **kwargs):
        check_dbus_property(
            self,
            TIMEZONE,
            self.timezone_interface,
            *args, **kwargs
        )

    def kickstart_properties_test(self):
        """Test kickstart properties."""
        self.assertEqual(self.timezone_interface.KickstartCommands, ["timezone", "timesource"])
        self.assertEqual(self.timezone_interface.KickstartSections, [])
        self.assertEqual(self.timezone_interface.KickstartAddons, [])
        self.callback.assert_not_called()

    def timezone_property_test(self):
        """Test the Timezone property."""
        self.timezone_interface.SetTimezone("Europe/Prague")
        self.assertEqual(self.timezone_interface.Timezone, "Europe/Prague")
        self.callback.assert_called_once_with(
            TIMEZONE.interface_name, {'Timezone': 'Europe/Prague'}, [])

    def utc_property_test(self):
        """Test the IsUtc property."""
        self.timezone_interface.SetIsUTC(True)
        self.assertEqual(self.timezone_interface.IsUTC, True)
        self.callback.assert_called_once_with(TIMEZONE.interface_name, {'IsUTC': True}, [])

    def ntp_property_test(self):
        """Test the NTPEnabled property."""
        self.timezone_interface.SetNTPEnabled(False)
        self.assertEqual(self.timezone_interface.NTPEnabled, False)
        self.callback.assert_called_once_with(TIMEZONE.interface_name, {'NTPEnabled': False}, [])

    def time_sources_property_test(self):
        """Test the TimeSources property."""
        server = {
            "type": get_variant(Str, TIME_SOURCE_SERVER),
            "hostname": get_variant(Str, "ntp.cesnet.cz"),
            "options": get_variant(List[Str], ["iburst"]),
        }

        pool = {
            "type": get_variant(Str, TIME_SOURCE_POOL),
            "hostname": get_variant(Str, "0.fedora.pool.ntp.org"),
            "options": get_variant(List[Str], []),
        }

        self._check_dbus_property(
            "TimeSources",
            [server, pool]
        )

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

    def no_kickstart_test(self):
        """Test with no kickstart."""
        ks_in = None
        ks_out = """
        # System timezone
        timezone America/New_York
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_empty_test(self):
        """Test with empty string."""
        ks_in = ""
        ks_out = ""
        self._test_kickstart(ks_in, ks_out)

    def kickstart_test(self):
        """Test the timezone command."""
        ks_in = """
        timezone Europe/Prague
        """
        ks_out = """
        # System timezone
        timezone Europe/Prague
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart2_test(self):
        """Test the timezone command with flags."""
        ks_in = """
        timezone --utc --nontp Europe/Prague
        """
        ks_out = """
        timesource --ntp-disable
        # System timezone
        timezone Europe/Prague --utc
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart3_test(self):
        """Test the timezone command with ntp servers."""
        ks_in = """
        timezone --ntpservers ntp.cesnet.cz Europe/Prague
        """
        ks_out = """
        timesource --ntp-server=ntp.cesnet.cz
        # System timezone
        timezone Europe/Prague
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_timesource_ntp_disabled_test(self):
        """Test the timesource command with ntp disabled."""
        ks_in = """
        timesource --ntp-disable
        """
        ks_out = """
        timesource --ntp-disable
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_timesource_ntp_server_test(self):
        """Test the timesource command with ntp servers."""
        ks_in = """
        timesource --ntp-server ntp.cesnet.cz
        """
        ks_out = """
        timesource --ntp-server=ntp.cesnet.cz
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_timesource_ntp_pool_test(self):
        """Test the timesource command with ntp pools."""
        ks_in = """
        timesource --ntp-pool ntp.cesnet.cz
        """
        ks_out = """
        timesource --ntp-pool=ntp.cesnet.cz
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_timesource_nts_test(self):
        """Test the timesource command with the nts option."""
        ks_in = """
        timesource --ntp-pool ntp.cesnet.cz --nts
        """
        ks_out = """
        timesource --ntp-pool=ntp.cesnet.cz --nts
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_timesource_all_test(self):
        """Test the timesource commands."""
        ks_in = """
        timesource --ntp-server ntp.cesnet.cz
        timesource --ntp-pool 0.fedora.pool.ntp.org
        """
        ks_out = """
        timesource --ntp-server=ntp.cesnet.cz
        timesource --ntp-pool=0.fedora.pool.ntp.org
        """
        self._test_kickstart(ks_in, ks_out)

    def kickstart_timezone_timesource_test(self):
        """Test the combination of timezone and timesource commands."""
        ks_in = """
        timezone --ntpservers ntp.cesnet.cz,0.fedora.pool.ntp.org Europe/Prague
        timesource --ntp-server ntp.cesnet.cz --nts
        timesource --ntp-pool 0.fedora.pool.ntp.org
        """
        ks_out = """
        timesource --ntp-server=ntp.cesnet.cz
        timesource --ntp-server=0.fedora.pool.ntp.org
        timesource --ntp-server=ntp.cesnet.cz --nts
        timesource --ntp-pool=0.fedora.pool.ntp.org
        # System timezone
        timezone Europe/Prague
        """
        self._test_kickstart(ks_in, ks_out)

    @patch_dbus_publish_object
    def install_with_tasks_default_test(self, publisher):
        """Test install tasks - module in default state."""
        task_classes = [
            ConfigureTimezoneTask,
            ConfigureNTPTask,
        ]
        task_paths = self.timezone_interface.InstallWithTasks()
        task_objs = check_task_creation_list(self, task_paths, publisher, task_classes)

        # ConfigureTimezoneTask
        obj = task_objs[0]
        self.assertEqual(obj.implementation._timezone, "America/New_York")
        self.assertEqual(obj.implementation._is_utc, False)
        # ConfigureNTPTask
        obj = task_objs[1]
        self.assertEqual(obj.implementation._ntp_enabled, True)
        self.assertEqual(obj.implementation._ntp_servers, [])

    @patch_dbus_publish_object
    def install_with_tasks_configured_test(self, publisher):
        """Test install tasks - module in configured state."""

        self.timezone_interface.SetIsUTC(True)
        self.timezone_interface.SetTimezone("Asia/Tokyo")
        self.timezone_interface.SetNTPEnabled(False)
        # --nontp and --ntpservers are mutually exclusive in kicstart but
        # there is no such enforcement in the module so for testing this is ok

        server = TimeSourceData()
        server.type = TIME_SOURCE_SERVER
        server.hostname = "clock1.example.com"
        server.options = ["iburst"]

        pool = TimeSourceData()
        pool.type = TIME_SOURCE_POOL
        pool.hostname = "clock2.example.com"

        self.timezone_interface.SetTimeSources(
            TimeSourceData.to_structure_list([server, pool])
        )

        task_classes = [
            ConfigureTimezoneTask,
            ConfigureNTPTask,
        ]
        task_paths = self.timezone_interface.InstallWithTasks()
        task_objs = check_task_creation_list(self, task_paths, publisher, task_classes)

        # ConfigureTimezoneTask
        obj = task_objs[0]
        self.assertEqual(obj.implementation._timezone, "Asia/Tokyo")
        self.assertEqual(obj.implementation._is_utc, True)

        # ConfigureNTPTask
        obj = task_objs[1]
        self.assertEqual(obj.implementation._ntp_enabled, False)
        self.assertEqual(len(obj.implementation._ntp_servers), 2)
        self.assertTrue(compare_data(obj.implementation._ntp_servers[0], server))
        self.assertTrue(compare_data(obj.implementation._ntp_servers[1], pool))

    @patch_dbus_publish_object
    def configure_ntp_service_enablement_default_test(self, publisher):
        """Test ntp service enablement with default module state."""
        ntp_excluded = True
        task_path = self.timezone_interface.ConfigureNTPServiceEnablementWithTask(ntp_excluded)
        obj = check_task_creation(self, task_path, publisher, ConfigureNTPServiceEnablementTask)
        self.assertEqual(obj.implementation._ntp_enabled, True)
        self.assertEqual(obj.implementation._ntp_excluded, ntp_excluded)

    @patch_dbus_publish_object
    def configure_ntp_service_enablement_configured_test(self, publisher):
        """Test ntp service enablement with configured module state."""
        ntp_excluded = False
        self.timezone_interface.SetNTPEnabled(False)
        task_path = self.timezone_interface.ConfigureNTPServiceEnablementWithTask(ntp_excluded)
        obj = check_task_creation(self, task_path, publisher, ConfigureNTPServiceEnablementTask)
        self.assertEqual(obj.implementation._ntp_enabled, False)
        self.assertEqual(obj.implementation._ntp_excluded, ntp_excluded)

    def deprecated_warnings_test(self):
        response = self.timezone_interface.ReadKickstart("timezone --isUtc Europe/Bratislava")
        report = KickstartReport.from_structure(response)

        warning = "The option --isUtc will be deprecated in future releases. " \
                  "Please modify your kickstart file to replace this option with " \
                  "its preferred alias --utc."

        self.assertEqual(len(report.warning_messages), 1)
        self.assertEqual(report.warning_messages[0].message, warning)