Ejemplo n.º 1
0
def test_parse_dmidecode():
    with open(relative_module_path(__file__, 'fixtures/dmidecode_memory.txt')) as f:
        dmidecode_raw = f.read()
    assert _parse_dmidecode_output(dmidecode_raw) == (0, 12, 0, 196608000000.0, '2666')

    with open(relative_module_path(__file__, 'fixtures/dmidecode_nvm.txt')) as f:
        dmidecode_raw = f.read()

    assert _parse_dmidecode_output(dmidecode_raw) == (4, 12,
                                                      1033984000000.0, 384000000000.0, '2666')
def test_parse_cpu_info(filename, expected_cpus, expected_cpu):
    with patch('builtins.open',
               new=create_open_mock(
                   {"/proc/cpuinfo": open(relative_module_path(__file__, filename)).read()})
               ):
        got_data = _parse_cpuinfo()
        assert len(got_data) == expected_cpus
        assert _is_dict_match(got_data[0], expected_cpu), 'some keys do not match!'
Ejemplo n.º 3
0
def test_config_strict_mode_errors():
    config.register(Item, strict_mode=True)

    test_config_path = testing.relative_module_path(__file__,
                                                    'test_config.yaml')

    with pytest.raises(ConfigLoadError, match='missing type declaration'):
        config.load_config(test_config_path)
Ejemplo n.º 4
0
def test_config_with_env_class():
    test_config_path = testing.relative_module_path(__file__,
                                                    'test_config_env.yaml')
    data = config.load_config(test_config_path)

    user = data['from_env']
    assert user == os.environ["USER"]
    assert user.strip() != ''

    assert data['unset_env'] == ''
def test_collect_topology_information(filename, expected_cpus, expected_cores,
                                      expected_sockets):
    with patch('builtins.open',
               new=create_open_mock(
                   {"/proc/cpuinfo": open(relative_module_path(__file__, filename)).read()})
               ):
        cpuinfo = _parse_cpuinfo()
        got_cpus, got_cores, got_sockets, got_topology = collect_topology_information(cpuinfo)
        assert got_cpus == expected_cpus
        assert got_cores == expected_cores
        assert got_sockets == expected_sockets
Ejemplo n.º 6
0
def test_config_unsafe_object_creation():
    from ruamel import yaml
    import calendar

    test_config_path = testing.relative_module_path(__file__,
                                                    'test_config_unsafe.yaml')

    # Unsafe default loader allows any python object initialization
    data = yaml.load(open(test_config_path), Loader=yaml.Loader)
    assert 'time' in data
    assert isinstance(data['time'], calendar.Calendar)

    # With use safe version only to allow construct previously registered objects
    with pytest.raises(config.ConfigLoadError,
                       match='could not determine a constructor'):
        config.load_config(test_config_path)
Ejemplo n.º 7
0
def test_dataclass():
    test_config_path = testing.relative_module_path(__file__,
                                                    'test_dataclasses.yaml')
    data = config.load_config(test_config_path)

    dc1 = data['dc1']
    assert dc1.x == 5
    assert dc1.y is None
    assert dc1.z == [1, 2, 3]
    assert dc1.d == 'asd'
    assert dc1.e == '/abc/def'
    assert dc1.foo == 'foobaz'

    dc2 = data['dc2']
    assert dc2.x == 1
    assert dc2.y == 'newble'
    assert dc2.z == [3, 4, 5]
    assert dc2.d == 4
    assert dc2.foo == FooEnum.BAR
Ejemplo n.º 8
0
def test_config_with_simple_classes():
    # Another method for registering items (other than using decorator).
    config.register(Item, strict_mode=False)

    test_config_path = testing.relative_module_path(__file__,
                                                    'test_config.yaml')

    data = config.load_config(test_config_path)

    foo_with_defaults = data['foo_with_defaults']
    assert foo_with_defaults.f == 1

    empty_boo = data['empty_boo']
    assert empty_boo.foo is None

    foo = data['foo']
    boo = data['boo']

    assert foo.s == 'some_string'
    assert foo.f == 2.5

    assert boo.foo is foo
    assert len(boo.items) == 2
    assert isinstance(boo.items[0], Item)
Ejemplo n.º 9
0
                           "MemFree:        18245956 kB\n"
                           "MemAvailable:   24963992 kB\n"
                           "Buffers:         1190812 kB\n"
                           "Cached:          6971960 kB\n"
                           "SwapCached:            0 kB\n"
                           "Active:          8808464 kB\n"
                           "Inactive:        4727816 kB\n"
                           "Active(anon):    5376088 kB\n", 6406972 * 1024)])
def test_parse_proc_meminfo(raw_meminfo_output, expected):
    assert parse_proc_meminfo(raw_meminfo_output) == expected


@patch('builtins.open',
       new=create_open_mock({
           "/proc/vmstat":
           open(relative_module_path(__file__,
                                     'fixtures/proc-vmstat.txt')).read(),
       }))
def test_parse_proc_vmstat(*mocks):
    assert parse_proc_vmstat() == {
        MetricName.PLATFORM_VMSTAT_NUMA_HINT_FAULTS: 97909,
        MetricName.PLATFORM_VMSTAT_NUMA_HINT_FAULTS_LOCAL: 97909,
        MetricName.PLATFORM_VMSTAT_NUMA_PAGES_MIGRATED: 14266,
        MetricName.PLATFORM_VMSTAT_PGFAULTS: 2911936,
        MetricName.PLATFORM_VMSTAT_PGMIGRATE_FAIL: 5633,
        MetricName.PLATFORM_VMSTAT_PGMIGRATE_SUCCESS: 14266
    }


@pytest.mark.parametrize("raw_proc_state_output,expected", [
    ("cpu  8202889 22275 2138696 483384497 138968 853793 184852 0 0 0\n"
     "cpu0 100 100 100 100 100 100 100 0 0 0\n"
Ejemplo n.º 10
0
def test_dataclass_validation_invalid_dict():
    test_config_path = testing.relative_module_path(
        __file__, 'test_dataclasses_validation_invalid_dict.yaml')
    with pytest.raises(ConfigLoadError, match='wrong_value'):
        config.load_config(test_config_path)
Ejemplo n.º 11
0
def test_dataclass_validation():
    test_config_path = testing.relative_module_path(
        __file__, 'test_dataclasses_validation.yaml')
    data = config.load_config(test_config_path)
    assert data['dc1'].rr == [[2, 3], []]
Ejemplo n.º 12
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from unittest.mock import patch

from tests.testing import create_open_mock, relative_module_path
from wca import sched_stats
from wca.metrics import MetricName


@patch('builtins.open',
       new=create_open_mock({
           "/proc/1/sched":
           open(relative_module_path(__file__,
                                     'fixtures/proc-1-sched.txt')).read(),
           "/proc/2/sched":
           open(relative_module_path(__file__,
                                     'fixtures/proc-2-sched.txt')).read(),
       }))
def test_parse_proc_vmstat_keys(*mocks):
    pattern = re.compile('.*numa')
    measurements = sched_stats.get_pids_sched_measurements([1, 2], pattern)
    assert measurements == {
        MetricName.TASK_SCHED_STAT: {
            'mm->numa_scan_seq': 27,
            'numa_pages_migrated': 10.5,
            'numa_preferred_nid': -3.0,
            'total_numa_faults': 0,
        },
        MetricName.TASK_SCHED_STAT_NUMA_FAULTS: {
Ejemplo n.º 13
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from unittest.mock import patch

from tests.testing import create_open_mock, relative_module_path
from wca.metrics import MetricName
from wca.zoneinfo import get_zoneinfo_measurements, DEFAULT_REGEXP


@patch('builtins.open',
       new=create_open_mock({
           "/proc/zoneinfo":
           open(relative_module_path(__file__,
                                     'fixtures/proc-zoneinfo.txt')).read(),
       }))
def test_parse_proc_zoneinfo(*mocks):
    assert get_zoneinfo_measurements(re.compile(DEFAULT_REGEXP)) == {
        MetricName.PLATFORM_ZONEINFO: {
            '0': {
                'DMA': {
                    'high': 6.0,
                    'hmem_autonuma_promote_dst': 0.0,
                    'hmem_autonuma_promote_src': 0.0,
                    'hmem_reclaim_demote_dst': 0.0,
                    'hmem_reclaim_demote_src': 0.0,
                    'hmem_reclaim_promote_dst': 0.0,
                    'hmem_reclaim_promote_src': 0.0,
                    'hmem_swapcache_promote_dst': 0.0,
                    'hmem_swapcache_promote_src': 0.0,
Ejemplo n.º 14
0
def test_invalid_dataclass():
    test_config_path = testing.relative_module_path(
        __file__, 'test_dataclasses_invalid.yaml')
    with pytest.raises(config.ConfigLoadError,
                       match="field 'x'.*Invalid type"):
        config.load_config(test_config_path)
Ejemplo n.º 15
0
def test_invalid_dataclass_union():
    test_config_path = testing.relative_module_path(
        __file__, 'test_dataclasses_invalid_union.yaml')
    with pytest.raises(config.ConfigLoadError,
                       match="field 'd'.*improper type from union"):
        config.load_config(test_config_path)
Ejemplo n.º 16
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import patch

from tests.testing import create_open_mock, relative_module_path
from wca import vmstats


@patch(
    'builtins.open',
    new=create_open_mock({
        '/sys/devices/system/node/node0/vmstat':
        open(relative_module_path(__file__,
                                  'fixtures/proc-vmstat-simple.txt')).read(),
        '/sys/devices/system/node/node1/vmstat':
        open(relative_module_path(__file__,
                                  'fixtures/proc-vmstat-simple.txt')).read()
    }))
@patch('os.listdir', return_value=['node0', 'node1'])
def test_parse_node_meminfo(*mocks):
    measurements = vmstats.parse_node_vmstat_keys(None)
    assert measurements == {
        'platform_node_vmstat': {
            0: {
                'nr_bar': 30,
                'nr_foo': 20
            },
            1: {
                'nr_bar': 30,
Ejemplo n.º 17
0
def test_dataclass_exceed_maximum():
    test_config_path = testing.relative_module_path(
        __file__, 'test_dataclasses_exceed_maximum.yaml')
    with pytest.raises(config.ConfigLoadError,
                       match="field 'x'.*Maximum value is 5. Got 123."):
        config.load_config(test_config_path)
Ejemplo n.º 18
0
def test_dataclass_invalid_field():
    test_config_path = testing.relative_module_path(
        __file__, 'test_dataclasses_invalid_field.yaml')
    with pytest.raises(config.ConfigLoadError, match="constructor signature"):
        config.load_config(test_config_path)