Skip to content

Commit

Permalink
Update test_config_parser.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubielx authored Mar 27, 2024
1 parent 2d0e2af commit 0e0da09
Showing 1 changed file with 59 additions and 129 deletions.
188 changes: 59 additions & 129 deletions scripts/tests/twister/test_config_parser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
# Copyright (c) 2020 Intel Corporation
# Copyright (c) 2024 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
"""
Tests for config_parser.py
"""
from contextlib import nullcontext
import os
import pytest
import mock
Expand Down Expand Up @@ -91,140 +92,69 @@ def test_default_values(zephyr_base):
parser.load()

scenario_data = parser.get_scenario("scenario1")

Check warning on line 94 in scripts/tests/twister/test_config_parser.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

W0612

scripts/tests/twister/test_config_parser.py:94 Unused variable 'scenario_data' (unused-variable)
assert scenario_data["type"] == "integration"
assert scenario_data["extra_args"] == []
assert scenario_data["extra_configs"] == []
assert scenario_data["extra_conf_files"] == []
assert scenario_data["extra_overlay_confs"] == []
assert scenario_data["extra_dtc_overlay_files"] == []
assert scenario_data["required_snippets"] == []
assert scenario_data["build_only"] is False
assert scenario_data["build_on_all"] is False
assert scenario_data["skip"] is False
assert scenario_data["slow"] is False
assert scenario_data["timeout"] == 60
assert scenario_data["min_ram"] == 8
assert scenario_data["modules"] == []
assert scenario_data["depends_on"] == set()
assert scenario_data["min_flash"] == 32
assert scenario_data["arch_allow"] == set()
assert scenario_data["arch_exclude"] == set()
assert scenario_data["extra_sections"] == []
assert scenario_data["integration_platforms"] == []
assert scenario_data["ignore_faults"] is False
assert scenario_data["ignore_qemu_crash"] is False
assert scenario_data["testcases"] == []
assert scenario_data["platform_type"] == []
assert scenario_data["platform_exclude"] == set()
assert scenario_data["platform_allow"] == set()
assert scenario_data["platform_key"] == []
assert scenario_data["toolchain_exclude"] == set()
assert scenario_data["toolchain_allow"] == set()
assert scenario_data["filter"] == ""
assert scenario_data["levels"] == []
assert scenario_data["harness"] == "test"
assert scenario_data["harness_config"] == {}
assert scenario_data["seed"] == 0
assert scenario_data["sysbuild"] is False


def test_cast_string_to_string(zephyr_base):
value = "hello"
typestr = "str"

expected_scenario_data = { 'type': 'integration',
'extra_args': [],
'extra_configs': [],
'extra_conf_files': [],
'extra_overlay_confs': [],
'extra_dtc_overlay_files': [],
'required_snippets': [],
'build_only': False,
'build_on_all': False,
'skip': False, 'slow': False,
'timeout': 60,
'min_ram': 8,
'modules': [],
'depends_on': set(),
'min_flash': 32,
'arch_allow': set(),
'arch_exclude': set(),
'extra_sections': [],
'integration_platforms': [],
'ignore_faults': False,
'ignore_qemu_crash': False,
'testcases': [],
'platform_type': [],
'platform_exclude': set(),
'platform_allow': set(),
'platform_key': [],
'toolchain_exclude': set(),
'toolchain_allow': set(),
'filter': '',
'levels': [],
'harness': 'test',
'harness_config': {},
'seed': 0, 'sysbuild': False
}

assert expected_scenario_data.items() <= expected_scenario_data.items()

@pytest.mark.parametrize(
'value, typestr, expected',
[
(' hello ', 'str', 'hello'),
('3.14', 'float', 3.14),
('10', 'int', 10),
('True', 'bool', 'True'), # do-nothing cast
('key: val', 'map', 'key: val'), # do-nothing cast
('test', 'int', ValueError),
('test', 'unknown', ConfigurationError),
],
ids=['str to str', 'str to float', 'str to int', 'str to bool', 'str to map',
'invalid', 'to unknown']
)

def test_cast_value(zephyr_base, value, typestr, expected):
loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)
result = parser._cast_value(value, typestr)

assert result == "hello"


def test_cast_string_to_float(zephyr_base):
value = "3.14"
typestr = "float"

loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)
result = parser._cast_value(value, typestr)

assert result == 3.14


def test_cast_string_to_int(zephyr_base):
value = "10"
typestr = "int"

loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)
result = parser._cast_value(value, typestr)

assert result == 10


def test_cast_string_to_bool(zephyr_base):
value = True
typestr = "bool"

loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)
result = parser._cast_value(value, typestr)

assert result is True


def test_cast_string_to_map(zephyr_base):
value = "map1 map2"
typestr = "map"

loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)
result = parser._cast_value(value, typestr)

assert result == "map1 map2"


def test_raises_error_invalid_cast(zephyr_base):
value = "test"
typestr = "int"

loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)

with pytest.raises(ValueError):
parser._cast_value(value, typestr)


def test_cast_string_to_unknown_type(zephyr_base):
value = "test"
typestr = "unknown"

loaded_schema = scl.yaml_load(
os.path.join(zephyr_base, 'scripts', 'schemas', 'twister','testsuite-schema.yaml')
)

parser = TwisterConfigParser("config.yaml", loaded_schema)

with pytest.raises(ConfigurationError):
parser._cast_value(value, typestr)

with pytest.raises(expected) if \
isinstance(expected, type) and issubclass(expected, Exception) else nullcontext():
result = parser._cast_value(value, typestr)
assert result == expected

def test_load_invalid_test_config_yaml(zephyr_base):
filename = "test_data.yaml"
Expand Down

0 comments on commit 0e0da09

Please sign in to comment.