Esempio n. 1
0
    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 = Mock()
        self.timezone_interface.PropertiesChanged.connect(self.callback)
 def setUp(self):
     """Set up the timezone module."""
     # Set up the timezone module.
     self.timezone_module = TimezoneService()
     self.timezone_interface = TimezoneInterface(self.timezone_module)
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)

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

    def test_kickstart_properties(self):
        """Test kickstart properties."""
        assert self.timezone_interface.KickstartCommands == [
            "timezone", "timesource"
        ]
        assert self.timezone_interface.KickstartSections == []
        assert self.timezone_interface.KickstartAddons == []

    def test_timezone_property(self):
        """Test the Timezone property."""
        self._check_dbus_property("Timezone", "Europe/Prague")

    def test_utc_property(self):
        """Test the IsUtc property."""
        self._check_dbus_property("IsUTC", True)

    def test_ntp_property(self):
        """Test the NTPEnabled property."""
        self._check_dbus_property("NTPEnabled", False)

    def test_time_sources_property(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.timezone_interface, ks_in, ks_out)

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

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

    def test_kickstart(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 test_kickstart2(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 test_kickstart3(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 test_kickstart_timesource_ntp_disabled(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 test_kickstart_timesource_ntp_server(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 test_kickstart_timesource_ntp_pool(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 test_kickstart_timesource_nts(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 test_kickstart_timesource_all(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 test_kickstart_timezone_timesource(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)

    def test_collect_requirements(self):
        """Test the requirements of the Timezone module."""
        # Check the default requirements.
        requirements = Requirement.from_structure_list(
            self.timezone_interface.CollectRequirements())
        assert len(requirements) == 1
        assert requirements[0].type == "package"
        assert requirements[0].name == "chrony"

        # Check requirements with disabled NTP service.
        self.timezone_interface.NTPEnabled = False
        requirements = Requirement.from_structure_list(
            self.timezone_interface.CollectRequirements())
        assert len(requirements) == 0

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

        # ConfigureHardwareClockTask
        obj = task_objs[0]
        assert obj.implementation._is_utc is False
        # ConfigureTimezoneTask
        obj = task_objs[1]
        assert obj.implementation._timezone == "America/New_York"
        assert obj.implementation._is_utc is False
        # ConfigureNTPTask
        obj = task_objs[2]
        assert obj.implementation._ntp_enabled is True
        assert obj.implementation._ntp_servers == []

    @patch_dbus_publish_object
    def test_install_with_tasks_configured(self, publisher):
        """Test install tasks - module in configured state."""
        self.timezone_interface.IsUTC = True
        self.timezone_interface.Timezone = "Asia/Tokyo"
        self.timezone_interface.NTPEnabled = 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.TimeSources = \
            TimeSourceData.to_structure_list([server, pool])

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

        # ConfigureHardwareClockTask
        obj = task_objs[0]
        assert obj.implementation._is_utc is True

        # ConfigureTimezoneTask
        obj = task_objs[1]
        assert obj.implementation._timezone == "Asia/Tokyo"
        assert obj.implementation._is_utc is True

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

    def test_deprecated_warnings(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."

        assert len(report.warning_messages) == 1
        assert report.warning_messages[0].message == warning

    @patch("pyanaconda.modules.timezone.timezone.GeolocationTask")
    def test_geoloc_interface(self, geoloc_mock):
        """Test geolocation-related interface and implementation of Timezone"""

        # without any actions, we should get empty GeolocationData
        new = GeolocationData.from_structure(
            self.timezone_interface.GeolocationResult)
        assert new.is_empty()

        # let's "run" geolocation
        self.timezone_interface.StartGeolocation()

        # the task should have been instantiated, signal connected, and started
        geoloc_mock.assert_called_once_with()
        mock_task = geoloc_mock.return_value
        mock_task.start.assert_called_once_with()
        mock_task.succeeded_signal.connect.assert_called_once_with(
            self.timezone_module._set_geolocation_result)

        # running the callback for finishing should also save result
        self.timezone_module._set_geolocation_result()
        mock_task.get_result.assert_called_once_with()
        assert self.timezone_module.geolocation_result == mock_task.get_result.return_value

        # try re-running, same as earlier
        geoloc_mock.reset_mock()
        self.timezone_interface.StartGeolocation()
        geoloc_mock.assert_called_once_with()
Esempio n. 4
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 = Mock()
        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 --isUtc --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)
Esempio n. 5
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 test_kickstart_properties(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 test_timezone_property(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 test_utc_property(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 test_ntp_property(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 test_time_sources_property(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 test_no_kickstart(self):
        """Test with no kickstart."""
        ks_in = None
        ks_out = """
        # System timezone
        timezone America/New_York
        """
        self._test_kickstart(ks_in, ks_out)

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

    def test_kickstart(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 test_kickstart2(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 test_kickstart3(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 test_kickstart_timesource_ntp_disabled(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 test_kickstart_timesource_ntp_server(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 test_kickstart_timesource_ntp_pool(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 test_kickstart_timesource_nts(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 test_kickstart_timesource_all(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 test_kickstart_timezone_timesource(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)

    def test_collect_requirements(self):
        """Test the requirements of the Timezone module."""
        # Check the default requirements.
        requirements = Requirement.from_structure_list(
            self.timezone_interface.CollectRequirements())
        self.assertEqual(len(requirements), 1)
        self.assertEqual(requirements[0].type, "package")
        self.assertEqual(requirements[0].name, "chrony")

        # Check requirements with disabled NTP service.
        self.timezone_interface.SetNTPEnabled(False)
        requirements = Requirement.from_structure_list(
            self.timezone_interface.CollectRequirements())
        self.assertEqual(len(requirements), 0)

    @patch_dbus_publish_object
    def test_install_with_tasks_default(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 test_install_with_tasks_configured(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))

    def test_deprecated_warnings(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)
Esempio n. 6
0
 def publish(self):
     """Publish the module."""
     TaskContainer.set_namespace(TIMEZONE.namespace)
     DBus.publish_object(TIMEZONE.object_path, TimezoneInterface(self))
     DBus.register_service(TIMEZONE.service_name)
Esempio n. 7
0
 def publish(self):
     """Publish the module."""
     DBus.publish_object(TimezoneInterface(self), MODULE_TIMEZONE_PATH)
     DBus.register_service(MODULE_TIMEZONE_NAME)
Esempio n. 8
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)
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."""
        tasks = self.timezone_interface.InstallWithTasks()
        timezone_task_path = tasks[0]
        ntp_task_path = tasks[1]

        publisher.assert_called()

        # timezone configuration
        timezone_object_path = publisher.call_args_list[0][0][0]
        tz_obj = publisher.call_args_list[0][0][1]
        self.assertEqual(timezone_task_path, timezone_object_path)
        self.assertIsInstance(tz_obj, TaskInterface)
        self.assertIsInstance(tz_obj.implementation, ConfigureTimezoneTask)
        self.assertEqual(tz_obj.implementation._timezone, "America/New_York")
        self.assertEqual(tz_obj.implementation._is_utc, False)

        # NTP configuration
        ntp_object_path = publisher.call_args_list[1][0][0]
        ntp_obj = publisher.call_args_list[1][0][1]
        self.assertEqual(ntp_task_path, ntp_object_path)
        self.assertIsInstance(ntp_obj, TaskInterface)
        self.assertIsInstance(ntp_obj.implementation, ConfigureNTPTask)
        self.assertEqual(ntp_obj.implementation._ntp_enabled, True)

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

        self.timezone_interface.SetNTPEnabled(False)
        self.timezone_interface.SetIsUTC(True)
        self.timezone_interface.SetTimezone("Asia/Tokyo")

        tasks = self.timezone_interface.InstallWithTasks()
        timezone_task_path = tasks[0]
        ntp_task_path = tasks[1]

        publisher.assert_called()

        # timezone configuration
        timezone_object_path = publisher.call_args_list[0][0][0]
        tz_obj = publisher.call_args_list[0][0][1]
        self.assertEqual(timezone_task_path, timezone_object_path)
        self.assertIsInstance(tz_obj, TaskInterface)
        self.assertIsInstance(tz_obj.implementation, ConfigureTimezoneTask)
        self.assertEqual(tz_obj.implementation._timezone, "Asia/Tokyo")
        self.assertEqual(tz_obj.implementation._is_utc, True)

        # NTP configuration
        ntp_object_path = publisher.call_args_list[1][0][0]
        ntp_obj = publisher.call_args_list[1][0][1]
        self.assertEqual(ntp_task_path, ntp_object_path)
        self.assertIsInstance(ntp_obj, TaskInterface)
        self.assertIsInstance(ntp_obj.implementation, ConfigureNTPTask)
        self.assertEqual(ntp_obj.implementation._ntp_enabled, False)
Esempio n. 10
0
 def publish(self):
     """Publish the module."""
     DBus.publish_object(TIMEZONE.object_path, TimezoneInterface(self))
     DBus.register_service(TIMEZONE.service_name)
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 = TimezoneModule()
        self.timezone_interface = TimezoneInterface(self.timezone_module)

        # Connect to the properties changed signal.
        self.callback = Mock()
        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(MODULE_TIMEZONE_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(MODULE_TIMEZONE_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(MODULE_TIMEZONE_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(
            MODULE_TIMEZONE_NAME, {'NTPServers': ["ntp.cesnet.cz"]}, [])

    def _test_kickstart(self, ks_in, ks_out):
        """Test the kickstart string."""
        # Remove extra spaces from the expected output.
        ks_output = "\n".join("".join(line.strip())
                              for line in ks_out.strip("\n").splitlines())

        # Read a kickstart,
        result = self.timezone_interface.ReadKickstart(ks_in)
        self.assertEqual({k: v.unpack()
                          for k, v in result.items()}, {"success": True})

        # Generate a kickstart.
        self.assertEqual(ks_output,
                         self.timezone_interface.GenerateKickstart())

        # Test the properties changed callback.
        self.callback.assert_any_call(DBUS_MODULE_NAMESPACE,
                                      {'Kickstarted': True}, [])

    def kickstart_test(self):
        """Test the timezone command."""
        ks_in = """
        timezone Europe/Prague
        """
        ks_out = """
        #version=DEVEL
        # 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 = """
        #version=DEVEL
        # System timezone
        timezone Europe/Prague --isUtc --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 = """
        #version=DEVEL
        # System timezone
        timezone Europe/Prague --ntpservers=ntp.cesnet.cz
        """
        self._test_kickstart(ks_in, ks_out)