1#!/usr/bin/env python3
2# Copyright (c) 2024 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5"""
6Blackbox tests for twister's command line functions related to test configuration files.
7"""
8
9import importlib
10from unittest import mock
11import os
12import pytest
13import sys
14import json
15
16# pylint: disable=no-name-in-module
17from conftest import ZEPHYR_BASE, TEST_DATA, testsuite_filename_mock
18from twisterlib.testplan import TestPlan
19
20
21class TestConfig:
22    @classmethod
23    def setup_class(cls):
24        apath = os.path.join(ZEPHYR_BASE, 'scripts', 'twister')
25        cls.loader = importlib.machinery.SourceFileLoader('__main__', apath)
26        cls.spec = importlib.util.spec_from_loader(cls.loader.name, cls.loader)
27        cls.twister_module = importlib.util.module_from_spec(cls.spec)
28
29    @classmethod
30    def teardown_class(cls):
31        pass
32
33    @mock.patch.object(TestPlan, 'TESTSUITE_FILENAME', testsuite_filename_mock)
34    def test_alt_config_root(self, out_path):
35        test_platforms = ['qemu_x86', 'intel_adl_crb']
36        path = os.path.join(TEST_DATA, 'tests', 'dummy')
37        alt_config_root = os.path.join(TEST_DATA, 'alt-test-configs', 'dummy')
38        args = ['-i', '--outdir', out_path, '-T', path, '-y'] + \
39               ['--alt-config-root', alt_config_root] + \
40               ['--tag', 'alternate-config-root'] + \
41               [val for pair in zip(
42                   ['-p'] * len(test_platforms), test_platforms
43               ) for val in pair]
44
45        with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \
46                pytest.raises(SystemExit) as sys_exit:
47            self.loader.exec_module(self.twister_module)
48
49        with open(os.path.join(out_path, 'testplan.json')) as f:
50            j = json.load(f)
51        filtered_j = [
52           (ts['platform'], ts['name'], tc['identifier']) \
53               for ts in j['testsuites'] \
54               for tc in ts['testcases'] if 'reason' not in tc
55        ]
56
57        assert str(sys_exit.value) == '0'
58
59        assert len(filtered_j) == 4
60
61    @pytest.mark.parametrize(
62        'level, expected_tests',
63        [
64            ('smoke', 6),
65            ('acceptance', 7),
66        ],
67        ids=['smoke', 'acceptance']
68    )
69    @mock.patch.object(TestPlan, 'TESTSUITE_FILENAME', testsuite_filename_mock)
70    def test_level(self, out_path, level, expected_tests):
71        test_platforms = ['qemu_x86', 'intel_adl_crb']
72        path = os.path.join(TEST_DATA, 'tests', 'dummy')
73        config_path = os.path.join(TEST_DATA, 'test_config.yaml')
74        args = ['-i','--outdir', out_path, '-T', path, '--level', level, '-y',
75                '--test-config', config_path] + \
76               [val for pair in zip(
77                   ['-p'] * len(test_platforms), test_platforms
78               ) for val in pair]
79
80        with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \
81                pytest.raises(SystemExit) as sys_exit:
82            self.loader.exec_module(self.twister_module)
83        import pprint
84        with open(os.path.join(out_path, 'testplan.json')) as f:
85            j = json.load(f)
86            pprint.pprint(j)
87        filtered_j = [
88            (ts['platform'], ts['name'], tc['identifier']) \
89                for ts in j['testsuites'] \
90                for tc in ts['testcases'] if 'reason' not in tc
91        ]
92
93        assert str(sys_exit.value) == '0'
94
95        assert expected_tests == len(filtered_j)
96