|
10 | 10 | from mfd_network_adapter.data_structures import State |
11 | 11 | from mfd_typing import OSName |
12 | 12 |
|
| 13 | +from mfd_host.feature.cpu import ESXiCPU |
| 14 | + |
13 | 15 |
|
14 | 16 | class TestESXiCPU: |
15 | 17 | cpu_attribute_output = dedent( |
@@ -78,3 +80,92 @@ def test_set_numa_affinity_disabled(self, host, mocker): |
78 | 80 | cmd = 'esxcli system settings advanced set -i 0 -o "/Numa/LocalityWeightActionAffinity"' |
79 | 81 | assert host.cpu.set_numa_affinity(State.DISABLED) is None |
80 | 82 | host.connection.execute_command.assert_called_once_with(cmd, custom_exception=CPUFeatureExecutionError) |
| 83 | + |
| 84 | + def test_start_cpu_measurement_returns_process_handle(self, host, mocker): |
| 85 | + mock_connection = mocker.Mock() |
| 86 | + mock_process = mocker.Mock() |
| 87 | + mock_connection.start_process.return_value = mock_process |
| 88 | + host.cpu._connection = mock_connection |
| 89 | + |
| 90 | + result = host.cpu.start_cpu_measurement() |
| 91 | + |
| 92 | + assert result == mock_process |
| 93 | + mock_connection.start_process.assert_called_once_with( |
| 94 | + command="esxtop -b -n 8 -d3", log_file=True |
| 95 | + ) |
| 96 | + |
| 97 | + def test_logs_debug_message_when_starting_cpu_measurement(self, host, mocker): |
| 98 | + mock_logger = mocker.patch("mfd_host.feature.cpu.esxi.logger") |
| 99 | + |
| 100 | + host.cpu.start_cpu_measurement() |
| 101 | + |
| 102 | + mock_logger.debug.assert_called_once_with( |
| 103 | + msg="Start CPU measurement on SUT Host" |
| 104 | + ) |
| 105 | + |
| 106 | + def test_parses_average_cpu_usage_from_last_4_samples(self, host, mocker): |
| 107 | + mock_connection = mocker.MagicMock() |
| 108 | + mock_process = mocker.MagicMock() |
| 109 | + mock_process.log_path = "/tmp/esxtop.log" |
| 110 | + mock_connection.modules().builtins.open.return_value.read.return_value = ( |
| 111 | + 'Header\n"10"\n"20"\n"30"\n"40"\n"50"\n' |
| 112 | + ) |
| 113 | + mock_connection.modules().builtins.open.return_value.__enter__.return_value = ( |
| 114 | + mock_connection.modules().builtins.open.return_value |
| 115 | + ) |
| 116 | + host.cpu._connection = mock_connection |
| 117 | + |
| 118 | + result = host.cpu.parse_cpu_usage("vm1", mock_process) |
| 119 | + assert result == 35 # (20+30+40+50)//4 |
| 120 | + |
| 121 | + def test_returns_zero_when_no_cpu_samples_found(self, host, mocker): |
| 122 | + mock_connection = mocker.MagicMock() |
| 123 | + mock_process = mocker.MagicMock() |
| 124 | + mock_process.log_path = "/tmp/esxtop.log" |
| 125 | + mock_connection.modules().builtins.open.return_value.read.return_value = ( |
| 126 | + "Header\n" |
| 127 | + ) |
| 128 | + mock_connection.modules().builtins.open.return_value.__enter__.return_value = ( |
| 129 | + mock_connection.modules().builtins.open.return_value |
| 130 | + ) |
| 131 | + host.cpu._connection = mock_connection |
| 132 | + |
| 133 | + result = host.cpu.parse_cpu_usage("vm1", mock_process) |
| 134 | + assert result == 0 |
| 135 | + |
| 136 | + def test_skips_lines_that_cannot_be_converted_to_float(self, host, mocker): |
| 137 | + mock_connection = mocker.MagicMock() |
| 138 | + mock_process = mocker.MagicMock() |
| 139 | + mock_process.log_path = "/tmp/esxtop.log" |
| 140 | + mock_connection.modules().builtins.open.return_value.read.return_value = ( |
| 141 | + 'Header\n"10"\n"invalid"\n"30"\n"40"\n"50"\n' |
| 142 | + ) |
| 143 | + mock_connection.modules().builtins.open.return_value.__enter__.return_value = ( |
| 144 | + mock_connection.modules().builtins.open.return_value |
| 145 | + ) |
| 146 | + host.cpu._connection = mock_connection |
| 147 | + |
| 148 | + result = host.cpu.parse_cpu_usage("vm1", mock_process) |
| 149 | + assert result == 32 # (10+30+40+50)//4 |
| 150 | + |
| 151 | + def test_raises_runtime_error_when_file_read_fails(self, host, mocker): |
| 152 | + mock_connection = mocker.MagicMock() |
| 153 | + mock_process = mocker.MagicMock() |
| 154 | + mock_process.log_path = "/tmp/esxtop.log" |
| 155 | + mock_connection.modules().builtins.open.side_effect = Exception("File error") |
| 156 | + host.cpu._connection = mock_connection |
| 157 | + |
| 158 | + with pytest.raises( |
| 159 | + RuntimeError, |
| 160 | + match="Failed to read parsed CPU usage output file due to - File error.", |
| 161 | + ): |
| 162 | + host.cpu.parse_cpu_usage("vm1", mock_process) |
| 163 | + |
| 164 | + def test_parses_cpu_usage_when_process_not_running(self, host, mocker): |
| 165 | + mock_process = mocker.Mock() |
| 166 | + mock_process.running = False |
| 167 | + mock_parse = mocker.patch.object(ESXiCPU, "parse_cpu_usage", return_value=7) |
| 168 | + host.cpu._connection = mocker.Mock() |
| 169 | + result = host.cpu.stop_cpu_measurement(mock_process, "vm4") |
| 170 | + assert result == 7 |
| 171 | + mock_parse.assert_called_once_with(vm_name="vm4", process=mock_process) |
0 commit comments