zephyr/scripts/tests/twister/test_twister.py
Tristan Honscheid 1eb0452fcb twister: Fields for Kconfig and DT overlay files in testcase.yaml
This is an implementation of issue #48334 and adds support for
specifying additional config and device tree overlays through fields in
the testcase.yaml file, which is more readable than having to cram these
in to `extra_args`.

Consider this example which shows the original and new ways to add
config and DT overlays:

```
common:
  extra_args: "CONF_FILE=a.conf;b.conf
DTC_OVERLAY_FILE=w.overlay;x.overlay OVERLAY_CONFIG=e.conf
UNRELATED=abc"
tests:
  utilities.base64:
    extra_conf_files:
      - "c.conf"
      - "d.conf"
    extra_overlay_confs:
      - "extra_overlay.conf"
    extra_dtc_overlay_files:
      - "y.overlay"
      - "z.overlay"
    extra_configs:
      - CONFIG_SAMPLE=y
    tags: base64
    type: unit
```

The new fields are `extra_conf_files`, `extra_overlay_confs,
`extra_dtc_overlay_files`. Files specified in these sections are appended
to any `CONF_FILE`, `OVERLAY_CONFIG`, or `DTC_OVERLAY_FILE` fields in
`extra_args`, causing the following args being passed in to
`self.run_cmake` at `runner.py:850`:

```
['-DUNRELATED=abc',
 '-DCONF_FILE=a.conf;b.conf;c.conf;d.conf',
 '-DDTC_OVERLAY_FILE=w.overlay;x.overlay;y.overlay;z.overlay',
 '-DOVERLAY_CONFIG=e.conf extra_overlay.conf '
 '<build_dir>/twister/testsuite_extra.conf']
```

These fields can be used in the common or scenario-specific YAML
sections and will be merged in order of least to most specific:

 1. config files extracted from common's extra_args
 2. files listed in common's {extra_conf_files or extra_overlay_confs
    or extra_dtc_overlay_files}
 3. config files extracted from test scenario's extra_args
 4. files listed in test scenario's {extra_conf_files or
    extra_overlay_confs or extra_dtc_overlay_files}

Specifying these files in extra_args now triggers a deprecation warning,
as the direct YAML fields are preferred for readability. They will still
function for now but support will be dropped in the future.  One
testcase.yaml
(`zephyr/tests/cmake/overlays/var_expansions/testcase.yaml`) is
converted to use the new fields. A follow-up PR will convert the
remaining files to the new format.

Signed-off-by: Tristan Honscheid <honscheid@google.com>
2023-03-01 16:52:01 -05:00

79 lines
3.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) 2020 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
"""
This test file contains foundational testcases for Twister tool
"""
import os
import sys
import pytest
from pathlib import Path
ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister"))
import scl
from twisterlib.testplan import TwisterConfigParser
def test_yamlload():
""" Test to check if loading the non-existent files raises the errors """
filename = 'testcase_nc.yaml'
with pytest.raises(FileNotFoundError):
scl.yaml_load(filename)
@pytest.mark.parametrize("filename, schema",
[("testsuite_correct_schema.yaml", "testsuite-schema.yaml"),
("platform_correct_schema.yaml", "platform-schema.yaml")])
def test_correct_schema(filename, schema, test_data):
""" Test to validate the testsuite schema"""
filename = test_data + filename
schema = scl.yaml_load(ZEPHYR_BASE +'/scripts/schemas/twister//' + schema)
data = TwisterConfigParser(filename, schema)
data.load()
assert data
@pytest.mark.parametrize("filename, schema",
[("testsuite_incorrect_schema.yaml", "testsuite-schema.yaml"),
("platform_incorrect_schema.yaml", "platform-schema.yaml")])
def test_incorrect_schema(filename, schema, test_data):
""" Test to validate the exception is raised for incorrect testsuite schema"""
filename = test_data + filename
schema = scl.yaml_load(ZEPHYR_BASE +'/scripts/schemas/twister//' + schema)
with pytest.raises(Exception) as exception:
scl.yaml_load_verify(filename, schema)
assert str(exception.value) == "Schema validation failed"
def test_testsuite_config_files():
""" Test to validate conf and overlay files are extracted properly """
filename = Path(ZEPHYR_BASE) / "scripts/tests/twister/test_data/testsuites/tests/test_config/test_data.yaml"
schema = scl.yaml_load(Path(ZEPHYR_BASE) / "scripts/schemas/twister/testsuite-schema.yaml")
data = TwisterConfigParser(filename, schema)
data.load()
# Load and validate the specific scenario from testcases.yaml
scenario = data.get_scenario("test_config.main")
assert scenario
# CONF_FILE, DTC_OVERLAY_FILE, OVERLAY_CONFIG fields should be stripped out
# of extra_args. Other fields should remain untouched.
assert scenario["extra_args"] == ["UNRELATED1=abc", "UNRELATED2=xyz"]
# Check that all conf files have been assembled in the correct order
assert ";".join(scenario["extra_conf_files"]) == \
"conf1;conf2;conf3;conf4;conf5;conf6;conf7;conf8"
# Check that all DTC overlay files have been assembled in the correct order
assert ";".join(scenario["extra_dtc_overlay_files"]) == \
"overlay1;overlay2;overlay3;overlay4;overlay5;overlay6;overlay7;overlay8"
# Check that all overlay conf files have been assembled in the correct order
assert scenario["extra_overlay_confs"] == \
["oc1.conf", "oc2.conf", "oc3.conf", "oc4.conf"]
# Check extra kconfig statements, too
assert scenario["extra_configs"] == ["CONFIG_FOO=y"]