Пример #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 --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)

    @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)
Пример #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 = 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)
Пример #3
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(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 == []
        self.callback.assert_not_called()

    def test_timezone_property(self):
        """Test the Timezone property."""
        self.timezone_interface.SetTimezone("Europe/Prague")
        assert 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)
        assert self.timezone_interface.IsUTC is 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)
        assert self.timezone_interface.NTPEnabled is 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.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.SetNTPEnabled(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 = [
            ConfigureTimezoneTask,
            ConfigureNTPTask,
        ]
        task_paths = self.timezone_interface.InstallWithTasks()
        task_objs = check_task_creation_list(task_paths, publisher,
                                             task_classes)

        # ConfigureTimezoneTask
        obj = task_objs[0]
        assert obj.implementation._timezone == "America/New_York"
        assert obj.implementation._is_utc is False
        # ConfigureNTPTask
        obj = task_objs[1]
        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.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(task_paths, publisher,
                                             task_classes)

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

        # ConfigureNTPTask
        obj = task_objs[1]
        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