1#!/usr/bin/env python3
2# Copyright (c) 2020 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5
6'''Common fixtures for use in testing the twister tool.'''
7import logging
8import os
9import sys
10import pytest
11
12pytest_plugins = ["pytester"]
13logging.getLogger("twister").setLevel(logging.DEBUG)  # requires for testing twister
14
15ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
16sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister"))
17sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts"))
18from twisterlib.testplan import TestPlan, TestConfiguration
19from twisterlib.testinstance import TestInstance
20from twisterlib.environment import TwisterEnv, add_parse_arguments, parse_arguments
21
22def new_get_toolchain(*args, **kwargs):
23    return 'zephyr'
24
25TestPlan.get_toolchain = new_get_toolchain
26
27@pytest.fixture(name='test_data')
28def _test_data():
29    """ Pytest fixture to load the test data directory"""
30    data = ZEPHYR_BASE + "/scripts/tests/twister/test_data/"
31    return data
32
33@pytest.fixture(name='zephyr_base')
34def zephyr_base_directory():
35    return ZEPHYR_BASE
36
37@pytest.fixture(name='testsuites_dir')
38def testsuites_directory():
39    """ Pytest fixture to load the test data directory"""
40    return ZEPHYR_BASE + "/scripts/tests/twister/test_data/testsuites"
41
42@pytest.fixture(name='class_env')
43def tesenv_obj(test_data, testsuites_dir, tmpdir_factory):
44    """ Pytest fixture to initialize and return the class TestPlan object"""
45    parser = add_parse_arguments()
46    options = parse_arguments(parser, [])
47    options.detailed_test_id = True
48    env = TwisterEnv(options)
49    env.board_roots = [os.path.join(test_data, "board_config", "1_level", "2_level")]
50    env.test_roots = [os.path.join(testsuites_dir, 'tests', testsuites_dir, 'samples')]
51    env.test_config = os.path.join(test_data, "test_config.yaml")
52    env.outdir = tmpdir_factory.mktemp("sanity_out_demo")
53    return env
54
55
56@pytest.fixture(name='class_testplan')
57def testplan_obj(test_data, class_env, testsuites_dir, tmpdir_factory):
58    """ Pytest fixture to initialize and return the class TestPlan object"""
59    env = class_env
60    env.board_roots = [test_data +"board_config/1_level/2_level/"]
61    env.test_roots = [testsuites_dir + '/tests', testsuites_dir + '/samples']
62    env.outdir = tmpdir_factory.mktemp("sanity_out_demo")
63    plan = TestPlan(env)
64    plan.test_config = TestConfiguration(config_file=env.test_config)
65    return plan
66
67@pytest.fixture(name='all_testsuites_dict')
68def testsuites_dict(class_testplan):
69    """ Pytest fixture to call add_testcase function of
70	Testsuite class and return the dictionary of testsuites"""
71    class_testplan.SAMPLE_FILENAME = 'test_sample_app.yaml'
72    class_testplan.TESTSUITE_FILENAME = 'test_data.yaml'
73    class_testplan.add_testsuites()
74    return class_testplan.testsuites
75
76@pytest.fixture(name='platforms_list')
77def all_platforms_list(test_data, class_testplan):
78    """ Pytest fixture to call add_configurations function of
79	Testsuite class and return the Platforms list"""
80    class_testplan.env.board_roots = [os.path.abspath(os.path.join(test_data, "board_config"))]
81    plan = TestPlan(class_testplan.env)
82    plan.test_config = TestConfiguration(config_file=class_testplan.env.test_config)
83    plan.add_configurations()
84    return plan.platforms
85
86@pytest.fixture
87def instances_fixture(class_testplan, platforms_list, all_testsuites_dict, tmpdir_factory):
88    """ Pytest fixture to call add_instances function of Testsuite class
89    and return the instances dictionary"""
90    class_testplan.outdir = tmpdir_factory.mktemp("sanity_out_demo")
91    class_testplan.platforms = platforms_list
92    platform = class_testplan.get_platform("demo_board_2")
93    instance_list = []
94    for _, testcase in all_testsuites_dict.items():
95        instance = TestInstance(testcase, platform, 'zephyr', class_testplan.outdir)
96        instance_list.append(instance)
97    class_testplan.add_instances(instance_list)
98    return class_testplan.instances
99