|
1 | 1 | import mock
|
| 2 | +import os |
2 | 3 | import pathlib
|
| 4 | +import pytest |
3 | 5 | import sqlite3
|
4 | 6 |
|
5 | 7 |
|
6 |
| -@mock.patch('pytest_monitor.sys_utils.psutil.cpu_freq', return_value=None) |
7 |
| -def test_when_cpu_freq_cannot_fetch_frequency(cpu_freq_mock, testdir): |
8 |
| - """Make sure that pytest-monitor does the job when we have issue in collecing context resources""" |
| 8 | +CPU_FREQ_PATH = 'pytest_monitor.sys_utils.psutil.cpu_freq' |
| 9 | + |
| 10 | +TEST_CONTENT = """ |
| 11 | +import time |
| 12 | +
|
| 13 | +
|
| 14 | +def test_ok(): |
| 15 | + time.sleep(0.5) |
| 16 | + x = ['a' * i for i in range(100)] |
| 17 | + assert len(x) == 100 |
| 18 | +""" |
| 19 | + |
| 20 | +def get_nb_metrics_with_cpu_freq(path): |
| 21 | + pymon_path = pathlib.Path(str(path)) / '.pymon' |
| 22 | + db = sqlite3.connect(path.as_posix()) |
| 23 | + cursor = db.cursor() |
| 24 | + cursor.execute('SELECT ITEM FROM TEST_METRICS;') |
| 25 | + nb_metrics = len(cursor.fetchall()) |
| 26 | + cursor = db.cursor() |
| 27 | + cursor.execute('SELECT CPU_FREQUENCY_MHZ FROM EXECUTION_CONTEXTS;') |
| 28 | + rows = cursor.fetchall() |
| 29 | + assert 1 == len(rows) |
| 30 | + cpu_freq = rows[0][0] |
| 31 | + return nb_metrics, cpu_freq |
| 32 | + |
| 33 | + |
| 34 | +def test_force_cpu_freq_set_0_use_psutil(testdir): |
| 35 | + """Test that when force mode is set, we do not call psutil to fetch CPU's frequency""" |
| 36 | + |
9 | 37 | # create a temporary pytest test module
|
10 |
| - testdir.makepyfile(""" |
11 |
| - import time |
| 38 | + testdir.makepyfile(TEST_CONTENT) |
12 | 39 |
|
| 40 | + with mock.patch(CPU_FREQ_PATH, return_value=1500) as cpu_freq_mock: |
| 41 | + os.environ['PYTEST_MONITOR_FORCE_CPU_FREQ'] = '0' |
| 42 | + os.environ['PYTEST_MONITOR_CPU_FREQ'] = '3000' |
| 43 | + # run pytest with the following cmd args |
| 44 | + result = testdir.runpytest('-vv') |
| 45 | + del os.environ['PYTEST_MONITOR_FORCE_CPU_FREQ'] |
| 46 | + del os.environ['PYTEST_MONITOR_CPU_FREQ'] |
| 47 | + cpu_freq_mock.assert_called() |
13 | 48 |
|
14 |
| - def test_ok(): |
15 |
| - time.sleep(0.5) |
16 |
| - x = ['a' * i for i in range(100)] |
17 |
| - assert len(x) == 100 |
| 49 | + # fnmatch_lines does an assertion internally |
| 50 | + result.stdout.fnmatch_lines(['*::test_ok PASSED*']) |
| 51 | + # make sure that that we get a '0' exit code for the test suite |
| 52 | + result.assert_outcomes(passed=1) |
18 | 53 |
|
19 |
| -""") |
| 54 | + assert 1, 3000 == get_nb_metrics_with_cpu_freq(testdir) |
20 | 55 |
|
21 |
| - # run pytest with the following cmd args |
22 |
| - result = testdir.runpytest('-vv') |
| 56 | + |
| 57 | +def test_force_cpu_freq(testdir): |
| 58 | + """Test that when force mode is set, we do not call psutil to fetch CPU's frequency""" |
| 59 | + |
| 60 | + # create a temporary pytest test module |
| 61 | + testdir.makepyfile(TEST_CONTENT) |
| 62 | + |
| 63 | + with mock.patch(CPU_FREQ_PATH, return_value=1500) as cpu_freq_mock: |
| 64 | + os.environ['PYTEST_MONITOR_FORCE_CPU_FREQ'] = '1' |
| 65 | + os.environ['PYTEST_MONITOR_CPU_FREQ'] = '3000' |
| 66 | + # run pytest with the following cmd args |
| 67 | + result = testdir.runpytest('-vv') |
| 68 | + del os.environ['PYTEST_MONITOR_FORCE_CPU_FREQ'] |
| 69 | + del os.environ['PYTEST_MONITOR_CPU_FREQ'] |
| 70 | + cpu_freq_mock.assert_not_called() |
23 | 71 |
|
24 | 72 | # fnmatch_lines does an assertion internally
|
25 | 73 | result.stdout.fnmatch_lines(['*::test_ok PASSED*'])
|
| 74 | + # make sure that that we get a '0' exit code for the test suite |
| 75 | + result.assert_outcomes(passed=1) |
| 76 | + |
| 77 | + assert 1, 3000 == get_nb_metrics_with_cpu_freq(testdir) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize('effect', [AttributeError, NotImplementedError, FileNotFoundError]) |
| 81 | +def test_when_cpu_freq_cannot_fetch_frequency_set_freq_by_using_fallback(effect, testdir): |
| 82 | + """Make sure that pytest-monitor fallback takes value of CPU FREQ from special env var""" |
| 83 | + # create a temporary pytest test module |
| 84 | + testdir.makepyfile(TEST_CONTENT) |
26 | 85 |
|
27 |
| - pymon_path = pathlib.Path(str(testdir)) / '.pymon' |
28 |
| - assert pymon_path.exists() |
| 86 | + with mock.patch(CPU_FREQ_PATH, side_effect=effect) as cpu_freq_mock: |
| 87 | + os.environ['PYTEST_MONITOR_CPU_FREQ'] = '3000' |
| 88 | + # run pytest with the following cmd args |
| 89 | + result = testdir.runpytest('-vv') |
| 90 | + del os.environ['PYTEST_MONITOR_CPU_FREQ'] |
| 91 | + cpu_freq_mock.assert_called() |
29 | 92 |
|
| 93 | + # fnmatch_lines does an assertion internally |
| 94 | + result.stdout.fnmatch_lines(['*::test_ok PASSED*']) |
30 | 95 | # make sure that that we get a '0' exit code for the test suite
|
31 | 96 | result.assert_outcomes(passed=1)
|
32 | 97 |
|
33 |
| - db = sqlite3.connect(str(pymon_path)) |
34 |
| - cursor = db.cursor() |
35 |
| - cursor.execute('SELECT ITEM FROM TEST_METRICS;') |
36 |
| - assert 1 == len(cursor.fetchall()) # current test |
37 |
| - cursor = db.cursor() |
38 |
| - cursor.execute('SELECT CPU_FREQUENCY_MHZ FROM EXECUTION_CONTEXTS;') |
39 |
| - rows = cursor.fetchall() |
40 |
| - assert 1 == len(rows) |
41 |
| - assert rows[0][0] == 0 |
| 98 | + assert 1, 3000 == get_nb_metrics_with_cpu_freq(testdir) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize('effect', [AttributeError, NotImplementedError, FileNotFoundError]) |
| 102 | +def test_when_cpu_freq_cannot_fetch_frequency_set_freq_to_0(effect, testdir): |
| 103 | + """Make sure that pytest-monitor's fallback mechanism is efficient enough. """ |
| 104 | + # create a temporary pytest test module |
| 105 | + testdir.makepyfile(TEST_CONTENT) |
| 106 | + |
| 107 | + with mock.patch(CPU_FREQ_PATH, side_effect=effect) as cpu_freq_mock: |
| 108 | + # run pytest with the following cmd args |
| 109 | + result = testdir.runpytest('-vv') |
| 110 | + cpu_freq_mock.assert_called() |
| 111 | + |
| 112 | + # fnmatch_lines does an assertion internally |
| 113 | + result.stdout.fnmatch_lines(['*::test_ok PASSED*']) |
| 114 | + # make sure that that we get a '0' exit code for the test suite |
| 115 | + result.assert_outcomes(passed=1) |
| 116 | + |
| 117 | + assert 1, 0 == get_nb_metrics_with_cpu_freq(testdir) |
42 | 118 |
|
| 119 | + |
| 120 | +@mock.patch('pytest_monitor.sys_utils.psutil.cpu_freq', return_value=None) |
| 121 | +def test_when_cpu_freq_cannot_fetch_frequency(cpu_freq_mock, testdir): |
| 122 | + """Make sure that pytest-monitor does the job when we have issue in collecing context resources""" |
| 123 | + # create a temporary pytest test module |
| 124 | + testdir.makepyfile(TEST_CONTENT) |
| 125 | + |
| 126 | + # run pytest with the following cmd args |
| 127 | + result = testdir.runpytest('-vv') |
| 128 | + |
| 129 | + # fnmatch_lines does an assertion internally |
| 130 | + result.stdout.fnmatch_lines(['*::test_ok PASSED*']) |
| 131 | + # make sure that that we get a '0' exit code for the test suite |
| 132 | + result.assert_outcomes(passed=1) |
| 133 | + |
| 134 | + assert 1, 0 == get_nb_metrics_with_cpu_freq(testdir) |
| 135 | + |
0 commit comments