def test_sync(*args): resctrl_file_mock_simple_name = MagicMock() resctrl_file_mock_complex_name = MagicMock() open_mock = create_open_mock({ "/sys/fs/resctrl": "0", "/sys/fs/cgroup/cpu/ddd/tasks": "123", "/sys/fs/resctrl/mon_groups/ddd/tasks": resctrl_file_mock_simple_name, "/sys/fs/cgroup/cpu/ddd/ddd/tasks": "123", "/sys/fs/resctrl/mon_groups/ddd-ddd/tasks": resctrl_file_mock_complex_name, }) with patch('builtins.open', open_mock): cgroup_path = "/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() resctrl_file_mock_simple_name.assert_called_once_with( '/sys/fs/resctrl/mon_groups/ddd/tasks', 'w') resctrl_file_mock_simple_name.assert_has_calls( [call().__enter__().write('123')]) with patch('builtins.open', open_mock): cgroup_path = "/ddd/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() resctrl_file_mock_complex_name.assert_called_once_with( '/sys/fs/resctrl/mon_groups/ddd-ddd/tasks', 'w') resctrl_file_mock_complex_name.assert_has_calls( [call().__enter__().write('123')])
def test_create_open_mock_autocreated(): with patch('builtins.open', create_open_mock({ '/some/path.txt': 'foo', })) as mocks: assert open('/some/path.txt').read() == 'foo' open('/some/path.txt', 'w').write('bar') mocks['/some/path.txt'].assert_has_calls([call().write('bar')])
def test_create_open_mock_manually(): my_own_mock_open = mock_open(read_data='foo') with patch('builtins.open', create_open_mock({'/some/path.txt': my_own_mock_open})): assert open('/some/path.txt').read() == 'foo' open('/some/path.txt', 'w').write('bar') my_own_mock_open.assert_has_calls([call().write('bar')])
def test_sync_flush_exception(SetEffectiveRootUid_mock, makedirs_mock, exists_mock, log_warning_mock): resctrl_file_mock = Mock( # open function return_value=Mock( # handler write=Mock( # write side_effect=ProcessLookupError ) ) ) open_mock = create_open_mock({ "/sys/fs/resctrl": "0", "/sys/fs/cgroup/cpu/ddd/tasks": "123", "/sys/fs/resctrl/mon_groups/ddd/tasks": resctrl_file_mock, }) with patch('builtins.open', open_mock): cgroup_path = "/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() log_warning_mock.assert_any_call('sync: Unsuccessful synchronization attempts. Ignoring.')
# See the License for the specific language governing permissions and # limitations under the License. import errno from unittest.mock import call, MagicMock, patch, Mock import pytest from owca.resctrl import ResGroup, check_resctrl from owca.testing import create_open_mock @patch('builtins.open', new=create_open_mock({ "/sys/fs/resctrl": "0", "/sys/fs/resctrl/tasks": "0", "/sys/fs/resctrl/mon_data/mon_L3_00/mbm_total_bytes": "0", })) def test_check_resctrl(*mock): assert check_resctrl() @patch('owca.resctrl.log.warning') @patch('os.path.exists', return_value=True) @patch('os.makedirs') @patch('owca.resctrl.SetEffectiveRootUid') def test_sync(*args): resctrl_file_mock_simple_name = MagicMock() resctrl_file_mock_complex_name = MagicMock() open_mock = create_open_mock({ "/sys/fs/resctrl": "0",
prf._group_event_leaders = {} assert prf._read_events() == {} @patch('owca.perf._get_cgroup_fd') @patch('owca.perf.PerfCounters._open') def test_read_events_zero_values_one_cpu(_open_mock, _get_cgroup_fd_mock): prf = perf.PerfCounters('/mycgroup', []) # File descriptor mock for single cpu prf._group_event_leaders = {0: Mock()} assert prf._read_events() == {} @patch('owca.perf.SetEffectiveRootUid') @patch('builtins.open', new=create_open_mock({ "/dev/cpu/0/cpuid": b"\x16\x00\x00\x00\x47\x65\x6e\x75\x6e\x74\x65\x6c\x69\x6e\x65\x49" b"\x54\x06\x05\x00\x00\x08\x40\x00\xff\xfb\xfe\x7f\xff\xfb\xeb\xbf" })) def test_read_skylake_cpu_model(*args): assert pc.CPUModel.SKYLAKE == perf._get_cpu_model() @patch('owca.perf.SetEffectiveRootUid') @patch('builtins.open', new=create_open_mock({ "/dev/cpu/0/cpuid": b"\x14\x00\x00\x00\x47\x65\x6e\x75\x6e\x74\x65\x6c\x69\x6e\x65\x49" b"\xf1\x06\x04\x00\x00\x08\x20\x00\xff\xfb\xfe\x7f\xff\xfb\xeb\xbf" })) def test_read_broadwell_cpu_model(*args): assert pc.CPUModel.BROADWELL == perf._get_cpu_model() @patch('owca.perf.SetEffectiveRootUid')
) ] @mock.patch('sys.argv', ['owca', '-c', 'configs/see_yaml_config_variable_above.yaml', '-r', 'example.external_package:ExampleDetector', '-l', 'trace']) @mock.patch('os.rmdir') @mock.patch('owca.config.exists', return_value=True) @mock.patch('owca.config.open', mock.mock_open(read_data=yaml_config)) @mock.patch('owca.mesos.MesosNode.get_tasks', return_value=mesos_tasks_mocks) @mock.patch('owca.resctrl.ResGroup.sync') @mock.patch('owca.containers.PerfCounters') @mock.patch('owca.runner.DetectionRunner.wait_or_finish', return_value=False) @mock.patch('builtins.open', new=create_open_mock({ "/proc/sys/kernel/perf_event_paranoid": "0", "/sys/fs/cgroup/cpu/mesos/xxxx-yyy/cpuacct.usage": "10", "/proc/stat": proc_stat, "/proc/meminfo": proc_meminfo, "/sys/devices/system/cpu/cpu0/topology/physical_package_id": "0", "/sys/devices/system/cpu/cpu0/topology/core_id": "0", "/sys/fs/resctrl/tasks": "666", "/sys/fs/resctrl/mon_data/mon_L3_00/mbm_total_bytes": "1024", "/sys/fs/resctrl/mon_groups/mesos-xxxx-yyy/mon_data/cpu0/mbm_total_bytes": "512", "/sys/fs/resctrl/mon_groups/mesos-xxxx-yyy/mon_data/cpu0/llc_occupancy": "2048", })) @mock.patch('os.listdir', return_value=["cpu0"]) def test_main(*mocks): main.main() # restore 'silent' logging level init_logging('critical', 'owca')
7: 500 }) ]) def test_parse_proc_state(raw_proc_state_output, expected): assert parse_proc_stat(raw_proc_state_output) == expected @patch('builtins.open', new=create_open_mock({ "/sys/devices/system/cpu/cpu0/topology/physical_package_id": "0", "/sys/devices/system/cpu/cpu0/topology/core_id": "0", "/sys/devices/system/cpu/cpu1/topology/physical_package_id": "0", "/sys/devices/system/cpu/cpu1/topology/core_id": "0", "/sys/devices/system/cpu/cpu1/online": "1", "/sys/devices/system/cpu/cpu2/online": "0", "/sys/devices/system/cpu/cpu3/online": "0", })) @patch('os.listdir', return_value=[ 'cpu0', 'cpuidle', 'uevent', 'nohz_full', 'hotplug', 'cpu1', 'cpu2', 'possible', 'offline', 'present', 'power', 'microcode', 'cpu3', 'online', 'vulnerabilities', 'cpufreq', 'intel_pstate', 'isolated', 'kernel_max', 'modalias' ]) def test_collect_topology_information_2_cpus_in_1_core_offline_rest_online(