def test_get_measurements(): cgroup = Cgroup('/some/foo1', platform_cpus=1) measurements = cgroup.get_measurements() assert measurements == { MetricName.CPU_USAGE_PER_TASK: 100, MetricName.MEM_USAGE_PER_TASK: 100 }
def test_cgroup_write(): cgroup = Cgroup('/some/foo1', platform=platform_mock) ctrl_file_mock = MagicMock() full_path = '/sys/fs/cgroup/cpu/some/foo1/some_ctrl_file' open_mock = create_open_mock({full_path: ctrl_file_mock}) with patch('builtins.open', open_mock): cgroup._write('some_ctrl_file', 5, CgroupType.CPU) ctrl_file_mock.assert_called_once_with(full_path, 'wb') ctrl_file_mock.assert_has_calls([call().__enter__().write(b'5')])
def test_set_normalized_shares(normalized_shares, allocation_configuration, expected_shares_write): with patch('wca.cgroups.Cgroup._write') as write_mock: cgroup = Cgroup('/some/foo1', platform=platform_mock, allocation_configuration=allocation_configuration) cgroup.set_shares(normalized_shares) write_mock.assert_called_with(CgroupResource.CPU_SHARES, expected_shares_write, CgroupType.CPU)
def test_get_measurements(): cgroup = Cgroup('/some/foo1', platform=platform_mock) measurements = cgroup.get_measurements() assert measurements == { MetricName.TASK_CPU_USAGE_SECONDS: 1, MetricName.TASK_MEM_USAGE_BYTES: 101, MetricName.TASK_MEM_MAX_USAGE_BYTES: 999, MetricName.TASK_MEM_LIMIT_BYTES: 2000, MetricName.TASK_MEM_SOFT_LIMIT_BYTES: 1500, MetricName.TASK_MEM_PAGE_FAULTS: 2730362811, MetricName.TASK_MEM_NUMA_PAGES: { 0: 123, 1: 234 } }
def test_set_normalized_cpuset(normalized_cpus, normalized_mems, platforms_cpu, platform_sockets, expected_cpus_write, expected_mems_write): with patch('wca.cgroups.Cgroup._write') as write_mock: cgroup = Cgroup('/some/foo1', platform_cpus=platforms_cpu, platform_sockets=platform_sockets) cgroup.set_cpuset(normalized_cpus, normalized_mems) write_mock.assert_has_calls([ call(CgroupResource.CPUSET_CPUS, expected_cpus_write, CgroupType.CPUSET) ]) write_mock.assert_has_calls([ call(CgroupResource.CPUSET_MEMS, expected_mems_write, CgroupType.CPUSET) ])
def test_set_normalized_quota(normalized_quota, cpu_quota_period, platforms_cpu, initial_period_value, expected_period_write, expected_quota_write): with patch('wca.cgroups.Cgroup._read', return_value=initial_period_value): with patch('wca.cgroups.Cgroup._write') as write_mock: cgroup = Cgroup('/some/foo1', platform_cpus=platforms_cpu, allocation_configuration=AllocationConfiguration( cpu_quota_period=cpu_quota_period)) cgroup.set_quota(normalized_quota) write_mock.assert_has_calls([ call(CgroupResource.CPU_QUOTA, expected_quota_write, CgroupType.CPU) ]) if expected_period_write: write_mock.assert_has_calls([ call(CgroupResource.CPU_PERIOD, expected_period_write, CgroupType.CPU) ])
def test_set_cpuset(cpus, mems, node_cpus, expected_cpus_write, expected_mems_write): platform_mock = Mock( spec=Platform, node_cpus=node_cpus, ) with patch('wca.cgroups.Cgroup._write') as write_mock: cgroup = Cgroup('/some/foo1', platform=platform_mock) cgroup.set_cpuset_cpus(cpus) write_mock.assert_has_calls([ call(CgroupResource.CPUSET_CPUS, expected_cpus_write, CgroupType.CPUSET) ]) cgroup.set_cpuset_mems(mems) write_mock.assert_has_calls([ call(CgroupResource.CPUSET_MEMS, expected_mems_write, CgroupType.CPUSET) ])
def test_get_normalized_shares(_read_mock): cgroup = Cgroup('/some/foo1', platform=platform_mock, allocation_configuration=AllocationConfiguration()) assert cgroup._get_normalized_shares() == pytest.approx(1, 0.01)
def test_cgroup_read(): cgroup = Cgroup('/some/foo1', platform=platform_mock) value = cgroup._read('some_ctr_file', CgroupType.CPU) assert value == 100
def test_get_measurements_not_found_cgroup(mock_file, mock_log_warning): cgroup = Cgroup('/some/foo1', platform=platform_mock) with pytest.raises(MissingMeasurementException): cgroup.get_measurements()
def test_cgroup_get_pids(): assert Cgroup('/some/foo1', platform=platform_mock).get_pids() == ['101', '102'] assert Cgroup('/foo2', platform=platform_mock).get_pids() == []
def test_get_normalized_quota(): cgroup = Cgroup('/some/foo1', platform=platform_mock, allocation_configuration=AllocationConfiguration()) assert cgroup._get_normalized_quota() == 1.0