def test_handle_dhclient_restart_should_reconfigure_network_routes_when_dhcp_client_restarts(self):
        with patch("azurelinuxagent.common.dhcp.DhcpHandler.conf_routes") as mock_conf_routes:
            env_handler = EnvHandler()

            #
            # before the first call to handle_dhclient_restart, EnvHandler configures the network routes and initializes the DHCP PIDs
            #
            with patch.object(env_handler, "get_dhcp_client_pid", return_value=[123]):
                env_handler.dhcp_handler.conf_routes()
                env_handler.dhcp_id_list = env_handler.get_dhcp_client_pid()
                self.assertEquals(mock_conf_routes.call_count, 1)

            #
            # if the dhcp client has not been restarted then it should not reconfigure the network routes
            #
            def mock_check_pid_alive(pid):
                if pid == 123:
                    return True
                raise Exception("Unexpected PID: {0}".format(pid))

            with patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.check_pid_alive", side_effect=mock_check_pid_alive):
                with patch.object(env_handler, "get_dhcp_client_pid", side_effect=Exception("get_dhcp_client_pid should not have been invoked")):
                    env_handler.handle_dhclient_restart()
                    self.assertEquals(mock_conf_routes.call_count, 1)  # count did not change

            #
            # if the process was restarted then it should reconfigure the network routes
            #
            def mock_check_pid_alive(pid):
                if pid == 123:
                    return False
                raise Exception("Unexpected PID: {0}".format(pid))

            with patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.check_pid_alive", side_effect=mock_check_pid_alive):
                with patch.object(env_handler, "get_dhcp_client_pid", return_value=[456, 789]):
                    env_handler.handle_dhclient_restart()
                    self.assertEquals(mock_conf_routes.call_count, 2)  # count increased

            #
            # if the new dhcp client has not been restarted then it should not reconfigure the network routes
            #
            def mock_check_pid_alive(pid):
                if pid in [456, 789]:
                    return True
                raise Exception("Unexpected PID: {0}".format(pid))

            with patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.check_pid_alive", side_effect=mock_check_pid_alive):
                with patch.object(env_handler, "get_dhcp_client_pid", side_effect=Exception("get_dhcp_client_pid should not have been invoked")):
                    env_handler.handle_dhclient_restart()
                    self.assertEquals(mock_conf_routes.call_count, 2)  # count did not change
    def test_get_dhcp_client_pid_should_return_and_empty_list_and_log_an_error_when_an_invalid_command_is_used(self):
        with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", side_effect=lambda _: self.shellutil_run_command(["non-existing-command"])):
            with patch('azurelinuxagent.common.logger.Logger.error') as mock_error:
                pids = EnvHandler().get_dhcp_client_pid()

        self.assertEquals(pids, [])

        self.assertEquals(mock_error.call_count, 1)
        args, kwargs = mock_error.call_args
        self.assertIn("Failed to get the PID of the DHCP client", args[0])
        self.assertIn("No such file or directory", args[1])
    def test_get_dhcp_client_pid_should_return_an_empty_list_and_log_a_warning_when_dhcp_client_is_not_running(self):
        with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", side_effect=lambda _: self.shellutil_run_command(["pidof", "non-existing-process"])):
            with patch('azurelinuxagent.common.logger.Logger.warn') as mock_warn:
                pids = EnvHandler().get_dhcp_client_pid()

        self.assertEquals(pids, [])

        self.assertEquals(mock_warn.call_count, 1)
        args, kwargs = mock_warn.call_args
        message = args[0]
        self.assertEquals("Dhcp client is not running.", message)
Exemplo n.º 4
0
    def test_purge_disk_cache(self, mock_conf, *args):
        names = [
            ("Prod", "agentsManifest"),
            ("Test", "agentsManifest"),
            ("FauxExtension1", "manifest.xml"),
            ("FauxExtension2", "manifest.xml"),
            ("GoalState", "xml"),
            ("ExtensionsConfig", "xml")
        ]

        env = EnvHandler()

        tmp_dir = tempfile.mkdtemp()
        mock_conf.return_value = tmp_dir

        # write incarnations 1-100
        for t in names:
            self._create_files(tmp_dir,
                               t[0],
                               t[1],
                               2 * MAXIMUM_CACHED_FILES,
                               with_sleep=0.001)

        # update incarnation 1 with the latest timestamp
        for t in names:
            f = os.path.join(tmp_dir, '.'.join((t[0], '1', t[1])))
            fileutil.write_file(f, "faux content")

        # ensure the expected number of files are created
        for t in names:
            p = os.path.join(tmp_dir, '{0}.*.{1}'.format(*t))
            self.assertEqual(2 * MAXIMUM_CACHED_FILES, len(glob.glob(p)))

        env.purge_disk_cache()

        # ensure the expected number of files remain
        for t in names:
            p = os.path.join(tmp_dir, '{0}.*.{1}'.format(*t))
            incarnation1 = os.path.join(tmp_dir, '{0}.1.{1}'.format(t[0], t[1]))
            incarnation2 = os.path.join(tmp_dir, '{0}.2.{1}'.format(t[0], t[1]))
            self.assertEqual(MAXIMUM_CACHED_FILES, len(glob.glob(p)))
            self.assertTrue(os.path.exists(incarnation1))
            self.assertFalse(os.path.exists(incarnation2))

        # write incarnation 101
        for t in names:
            f = os.path.join(tmp_dir, '.'.join((t[0], '101', t[1])))
            fileutil.write_file(f, "faux content")

        # call to purge should be ignored, since interval has not elapsed
        env.purge_disk_cache()

        for t in names:
            p = os.path.join(tmp_dir, '{0}.*.{1}'.format(*t))
            incarnation1 = os.path.join(tmp_dir, '{0}.1.{1}'.format(t[0], t[1]))
            self.assertEqual(MAXIMUM_CACHED_FILES + 1, len(glob.glob(p)))
            self.assertTrue(os.path.exists(incarnation1))
Exemplo n.º 5
0
    def test_get_dhcp_client_pid_should_not_log_consecutive_errors(self):
        env_handler = EnvHandler()

        with patch('azurelinuxagent.common.logger.Logger.warn') as mock_warn:

            def assert_warnings(count):
                self.assertEquals(mock_warn.call_count, count)

                for call_args in mock_warn.call_args_list:
                    args, kwargs = call_args
                    self.assertEquals("Dhcp client is not running.", args[0])

            with patch(
                    "azurelinuxagent.common.osutil.default.shellutil.run_command",
                    side_effect=lambda _: self.shellutil_run_command(
                        ["pidof", "non-existing-process"])):
                # it should log the first error
                pids = env_handler.get_dhcp_client_pid()
                self.assertEquals(pids, [])
                assert_warnings(1)

                # it should not log subsequent errors
                for i in range(0, 3):
                    pids = env_handler.get_dhcp_client_pid()
                    self.assertEquals(pids, [])
                    self.assertEquals(mock_warn.call_count, 1)

            with patch(
                    "azurelinuxagent.common.osutil.default.shellutil.run_command",
                    return_value="123"):
                # now it should succeed
                pids = env_handler.get_dhcp_client_pid()
                self.assertEquals(pids, [123])
                assert_warnings(1)

            with patch(
                    "azurelinuxagent.common.osutil.default.shellutil.run_command",
                    side_effect=lambda _: self.shellutil_run_command(
                        ["pidof", "non-existing-process"])):
                # it should log the new error
                pids = env_handler.get_dhcp_client_pid()
                self.assertEquals(pids, [])
                assert_warnings(2)

                # it should not log subsequent errors
                for i in range(0, 3):
                    pids = env_handler.get_dhcp_client_pid()
                    self.assertEquals(pids, [])
                    self.assertEquals(mock_warn.call_count, 2)
Exemplo n.º 6
0
 def test_get_dhcp_client_pid_should_return_a_sorted_list_of_pids(self):
     with patch("azurelinuxagent.common.utils.shellutil.run_command",
                return_value="11 9 5 22 4 6"):
         pids = EnvHandler().get_dhcp_client_pid()
         self.assertEquals(pids, [4, 5, 6, 9, 11, 22])