1#!/usr/bin/env python3
2# Copyright (c) 2020 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5# pylint: disable=line-too-long
6"""
7Tests for testinstance class
8"""
9
10import os
11import sys
12import pytest
13
14ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
15sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister"))
16from twisterlib.testinstance import TestInstance
17from twisterlib.error import BuildError
18from twisterlib.runner import TwisterRunner
19from expr_parser import reserved
20
21
22TESTDATA_1 = [
23    (False, False, "console", "na", "qemu", False, [], (False, True)),
24    (False, False, "console", "native", "qemu", False, [], (False, True)),
25    (True, False, "console", "native", "nsim", False, [], (True, False)),
26    (True, True, "console", "native", "renode", False, [], (True, False)),
27    (False, False, "sensor", "native", "", False, [], (True, False)),
28    (False, False, "sensor", "na", "", False, [], (True, False)),
29    (False, True, "sensor", "native", "", True, [], (True, False)),
30]
31@pytest.mark.parametrize("build_only, slow, harness, platform_type, platform_sim, device_testing,fixture, expected", TESTDATA_1)
32def test_check_build_or_run(class_testplan, monkeypatch, all_testsuites_dict, platforms_list, build_only, slow, harness, platform_type, platform_sim, device_testing, fixture, expected):
33    """" Test to check the conditions for build_only and run scenarios
34    Scenario 1: Test when different parameters are passed, build_only and run are set correctly
35    Scenario 2: Test if build_only is enabled when the OS is Windows"""
36
37    class_testplan.testsuites = all_testsuites_dict
38    testsuite = class_testplan.testsuites.get('scripts/tests/twister/test_data/testsuites/tests/test_a/test_a.check_1')
39    print(testsuite)
40
41    class_testplan.platforms = platforms_list
42    platform = class_testplan.get_platform("demo_board_2")
43    platform.type = platform_type
44    platform.simulation = platform_sim
45    testsuite.harness = harness
46    testsuite.build_only = build_only
47    testsuite.slow = slow
48
49    testinstance = TestInstance(testsuite, platform, class_testplan.env.outdir)
50    run = testinstance.check_runnable(slow, device_testing, fixture)
51    _, r = expected
52    assert run == r
53
54    monkeypatch.setattr("os.name", "nt")
55    run = testinstance.check_runnable()
56    assert not run
57
58TESTDATA_2 = [
59    (True, True, True, ["demo_board_2"], "native", None, '\nCONFIG_COVERAGE=y\nCONFIG_COVERAGE_DUMP=y\nCONFIG_ASAN=y\nCONFIG_UBSAN=y'),
60    (True, False, True, ["demo_board_2"], "native", None, '\nCONFIG_COVERAGE=y\nCONFIG_COVERAGE_DUMP=y\nCONFIG_ASAN=y'),
61    (False, False, True, ["demo_board_2"], 'native', None, '\nCONFIG_COVERAGE=y\nCONFIG_COVERAGE_DUMP=y'),
62    (True, False, True, ["demo_board_2"], 'mcu', None, '\nCONFIG_COVERAGE=y\nCONFIG_COVERAGE_DUMP=y'),
63    (False, False, False, ["demo_board_2"], 'native', None, ''),
64    (False, False, True, ['demo_board_1'], 'native', None, ''),
65    (True, False, False, ["demo_board_2"], 'native', None, '\nCONFIG_ASAN=y'),
66    (False, True, False, ["demo_board_2"], 'native', None, '\nCONFIG_UBSAN=y'),
67    (False, False, False, ["demo_board_2"], 'native', ["CONFIG_LOG=y"], 'CONFIG_LOG=y'),
68    (False, False, False, ["demo_board_2"], 'native', ["arch:x86_demo:CONFIG_LOG=y"], 'CONFIG_LOG=y'),
69    (False, False, False, ["demo_board_2"], 'native', ["arch:arm_demo:CONFIG_LOG=y"], ''),
70    (False, False, False, ["demo_board_2"], 'native', ["platform:demo_board_2:CONFIG_LOG=y"], 'CONFIG_LOG=y'),
71    (False, False, False, ["demo_board_2"], 'native', ["platform:demo_board_1:CONFIG_LOG=y"], ''),
72]
73
74@pytest.mark.parametrize("enable_asan, enable_ubsan, enable_coverage, coverage_platform, platform_type, extra_configs, expected_content", TESTDATA_2)
75def test_create_overlay(class_testplan, all_testsuites_dict, platforms_list, enable_asan, enable_ubsan, enable_coverage, coverage_platform, platform_type, extra_configs, expected_content):
76    """Test correct content is written to testcase_extra.conf based on if conditions."""
77    class_testplan.testsuites = all_testsuites_dict
78    testcase = class_testplan.testsuites.get('scripts/tests/twister/test_data/testsuites/samples/test_app/sample_test.app')
79
80    if extra_configs:
81        testcase.extra_configs = extra_configs
82
83    class_testplan.platforms = platforms_list
84    platform = class_testplan.get_platform("demo_board_2")
85
86    testinstance = TestInstance(testcase, platform, class_testplan.env.outdir)
87    platform.type = platform_type
88    assert testinstance.create_overlay(platform, enable_asan, enable_ubsan, enable_coverage, coverage_platform) == expected_content
89
90def test_calculate_sizes(class_testplan, all_testsuites_dict, platforms_list):
91    """ Test Calculate sizes method for zephyr elf"""
92    class_testplan.testsuites = all_testsuites_dict
93    testcase = class_testplan.testsuites.get('scripts/tests/twister/test_data/testsuites/samples/test_app/sample_test.app')
94    class_testplan.platforms = platforms_list
95    platform = class_testplan.get_platform("demo_board_2")
96    testinstance = TestInstance(testcase, platform, class_testplan.env.outdir)
97
98    with pytest.raises(BuildError):
99        assert testinstance.calculate_sizes() == "Missing/multiple output ELF binary"
100
101TESTDATA_3 = [
102    ('CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE and CONFIG_TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE and not (CONFIG_TOOLCHAIN_ARCMWDT_SUPPORTS_THREAD_LOCAL_STORAGE and CONFIG_USERSPACE)', ['kconfig']),
103    ('(dt_compat_enabled("st,stm32-flash-controller") or dt_compat_enabled("st,stm32h7-flash-controller")) and dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions")', ['dts']),
104    ('((CONFIG_FLASH_HAS_DRIVER_ENABLED and not CONFIG_TRUSTED_EXECUTION_NONSECURE) and dt_label_with_parent_compat_enabled("storage_partition", "fixed-partitions")) or (CONFIG_FLASH_HAS_DRIVER_ENABLED and CONFIG_TRUSTED_EXECUTION_NONSECURE and dt_label_with_parent_compat_enabled("slot1_ns_partition", "fixed-partitions"))', ['dts', 'kconfig']),
105    ('((CONFIG_CPU_AARCH32_CORTEX_R or CONFIG_CPU_CORTEX_M) and CONFIG_CPU_HAS_FPU and TOOLCHAIN_HAS_NEWLIB == 1) or CONFIG_ARCH_POSIX', ['full'])
106]
107
108@pytest.mark.parametrize("filter_expr, expected_stages", TESTDATA_3)
109def test_which_filter_stages(filter_expr, expected_stages):
110    logic_keys = reserved.keys()
111    stages = TwisterRunner.get_cmake_filter_stages(filter_expr, logic_keys)
112    assert sorted(stages) == sorted(expected_stages)
113