Ejemplo n.º 1
0
 def test_get_systemd_version_should_return_a_version_number(self):
     with mock_cgroup_environment(self.tmp_dir):
         version_info = systemd.get_version()
         found = re.search(r"systemd \d+", version_info) is not None
         self.assertTrue(
             found, "Could not determine the systemd version: {0}".format(
                 version_info))
Ejemplo n.º 2
0
    def test_start_extension_command_should_return_the_command_output(self, _):
        original_popen = subprocess.Popen

        def mock_popen(command, *args, **kwargs):
            if command.startswith(
                    'systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3'
            ):
                command = "echo TEST_OUTPUT"
            return original_popen(command, *args, **kwargs)

        with mock_cgroup_environment(self.tmp_dir):
            with tempfile.TemporaryFile(dir=self.tmp_dir,
                                        mode="w+b") as output_file:
                with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen",
                           side_effect=mock_popen) as popen_patch:  # pylint: disable=unused-variable
                    command_output = SystemdCgroupsApi(
                    ).start_extension_command(
                        extension_name="Microsoft.Compute.TestExtension-1.2.3",
                        command="A_TEST_COMMAND",
                        shell=True,
                        timeout=300,
                        cwd=self.tmp_dir,
                        env={},
                        stdout=output_file,
                        stderr=output_file)

                    self.assertIn("[stdout]\nTEST_OUTPUT\n", command_output,
                                  "The test output was not captured")
Ejemplo n.º 3
0
    def test_start_extension_command_should_use_systemd_to_execute_the_command(
            self, _):
        with mock_cgroup_environment(self.tmp_dir):
            with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen",
                       wraps=subprocess.Popen) as popen_patch:
                SystemdCgroupsApi().start_extension_command(
                    extension_name="Microsoft.Compute.TestExtension-1.2.3",
                    command="the-test-extension-command",
                    timeout=300,
                    shell=True,
                    cwd=self.tmp_dir,
                    env={},
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)

                extension_calls = [
                    args[0] for (args, _) in popen_patch.call_args_list
                    if "the-test-extension-command" in args[0]
                ]

                self.assertEqual(
                    1, len(extension_calls),
                    "The extension should have been invoked exactly once")
                self.assertIn(
                    "systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3",
                    extension_calls[0],
                    "The extension should have been invoked using systemd")
Ejemplo n.º 4
0
    def test_get_cgroup2_controllers_should_return_the_v2_cgroup_controllers(self):
        with mock_cgroup_environment(self.tmp_dir):
            mount_point, controllers = SystemdCgroupsApi.get_cgroup2_controllers()

            self.assertEqual(mount_point, "/sys/fs/cgroup/unified", "Invalid mount point for V2 cgroups")
            self.assertIn("cpu", controllers, "The CPU controller is not in the list of V2 controllers")
            self.assertIn("memory", controllers, "The memory controller is not in the list of V2 controllers")
Ejemplo n.º 5
0
 def test_get_service_cgroup_paths_should_return_the_cgroup_mount_points(self):
     with mock_cgroup_environment(self.tmp_dir):
         cpu, memory = SystemdCgroupsApi().get_unit_cgroup_paths("extension.service")
         self.assertIn(cpu, '/sys/fs/cgroup/cpu,cpuacct/system.slice/extension.service',
                       "The mount point for the CPU controller is incorrect")
         self.assertIn(memory, '/sys/fs/cgroup/memory/system.slice/extension.service',
                       "The mount point for the memory controller is incorrect")
Ejemplo n.º 6
0
 def test_get_cpu_and_memory_mount_points_should_return_the_cgroup_mount_points(
         self):
     with mock_cgroup_environment(self.tmp_dir):
         cpu, memory = SystemdCgroupsApi().get_cgroup_mount_points()
         self.assertEqual(
             cpu, '/sys/fs/cgroup/cpu,cpuacct',
             "The mount point for the CPU controller is incorrect")
         self.assertEqual(
             memory, '/sys/fs/cgroup/memory',
             "The mount point for the memory controller is incorrect")
Ejemplo n.º 7
0
    def test_get_unit_property_should_return_the_value_of_the_given_property(
            self):
        with mock_cgroup_environment(self.tmp_dir):
            cpu_accounting = systemd.get_unit_property("walinuxagent.service",
                                                       "CPUAccounting")

            self.assertEqual(
                cpu_accounting, "no",
                "Property {0} of {1} is incorrect".format(
                    "CPUAccounting", "walinuxagent.service"))
Ejemplo n.º 8
0
 def test_get_cpu_and_memory_cgroup_relative_paths_for_process_should_return_the_cgroup_relative_paths(
         self):
     with mock_cgroup_environment(self.tmp_dir):
         cpu, memory = SystemdCgroupsApi.get_process_cgroup_relative_paths(
             'self')
         self.assertEqual(
             cpu, "system.slice/walinuxagent.service",
             "The relative path for the CPU cgroup is incorrect")
         self.assertEqual(
             memory, "system.slice/walinuxagent.service",
             "The relative memory for the CPU cgroup is incorrect")
Ejemplo n.º 9
0
    def test_start_extension_command_should_execute_the_command_in_a_cgroup(self, _):
        with mock_cgroup_environment(self.tmp_dir):
            SystemdCgroupsApi().start_extension_command(
                extension_name="Microsoft.Compute.TestExtension-1.2.3",
                command="test command",
                cmd_name="test",
                shell=False,
                timeout=300,
                cwd=self.tmp_dir,
                env={},
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)

            tracked = CGroupsTelemetry._tracked

            self.assertTrue(
                any(cg for cg in tracked.values() if cg.name == 'Microsoft.Compute.TestExtension-1.2.3' and 'cpu' in cg.path),
                "The extension's CPU is not being tracked")
 def _get_cgroup_configurator(self,
                              initialize=True,
                              enable=True,
                              mock_commands=None):
     CGroupConfigurator._instance = None
     configurator = CGroupConfigurator.get_instance()
     CGroupsTelemetry.reset()
     with mock_cgroup_environment(self.tmp_dir) as mock_environment:
         if mock_commands is not None:
             for command in mock_commands:
                 mock_environment.add_command(command)
         configurator.mocks = mock_environment
         if initialize:
             if not enable:
                 with patch.object(configurator, "enable"):
                     configurator.initialize()
             else:
                 configurator.initialize()
         yield configurator