1#!/usr/bin/env python3
2# Copyright (c) 2023 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5
6'''
7This test file contains tests for platform.py module of twister
8'''
9import sys
10import os
11import mock
12import pytest
13
14ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
15sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister"))
16
17from twisterlib.platform import Platform
18
19
20TESTDATA_1 = [
21    (
22"""\
23identifier: dummy empty
24arch: arch0
25""",
26        {
27            'name': 'dummy empty',
28            'arch': 'arch0',
29            'twister': True,
30            'ram': 128,
31            'timeout_multiplier': 1.0,
32            'ignore_tags': [],
33            'only_tags': [],
34            'default': False,
35            'binaries': [],
36            'flash': 512,
37            'supported': set(),
38            'vendor': '',
39            'tier': -1,
40            'type': 'na',
41            'simulation': 'na',
42            'simulation_exec': None,
43            'supported_toolchains': [],
44            'env': [],
45            'env_satisfied': True
46        },
47        '<dummy empty on arch0>'
48    ),
49    (
50"""\
51identifier: dummy full
52arch: riscv32
53twister: true
54ram: 1024
55testing:
56  timeout_multiplier: 2.0
57  ignore_tags:
58    - tag1
59    - tag2
60  only_tags:
61    - tag3
62  default: true
63  binaries:
64    - dummy.exe
65    - dummy.bin
66flash: 4096
67supported:
68  - ble
69  - netif:openthread
70  - gpio
71vendor: vendor1
72tier: 1
73type: unit
74simulation: nsim
75simulation_exec: nsimdrv
76toolchain:
77  - zephyr
78  - llvm
79env:
80  - dummynonexistentvar
81""",
82        {
83            'name': 'dummy full',
84            'arch': 'riscv32',
85            'twister': True,
86            'ram': 1024,
87            'timeout_multiplier': 2.0,
88            'ignore_tags': ['tag1', 'tag2'],
89            'only_tags': ['tag3'],
90            'default': True,
91            'binaries': ['dummy.exe', 'dummy.bin'],
92            'flash': 4096,
93            'supported': set(['ble', 'netif', 'openthread', 'gpio']),
94            'vendor': 'vendor1',
95            'tier': 1,
96            'type': 'unit',
97            'simulation': 'nsim',
98            'simulation_exec': 'nsimdrv',
99            'supported_toolchains': ['zephyr', 'llvm', 'cross-compile', 'xtools'],
100            'env': ['dummynonexistentvar'],
101            'env_satisfied': False
102        },
103        '<dummy full on riscv32>'
104    ),
105]
106
107@pytest.mark.parametrize(
108    'platform_text, expected_data, expected_repr',
109    TESTDATA_1,
110    ids=['almost empty specification', 'full specification']
111)
112def test_platform_load(platform_text, expected_data, expected_repr):
113    platform = Platform()
114
115    with mock.patch('builtins.open', mock.mock_open(read_data=platform_text)):
116        platform.load('dummy.yaml')
117
118    for k, v in expected_data.items():
119        if not hasattr(platform, k):
120            assert False, f'No key {k} in platform {platform}'
121        att = getattr(platform, k)
122        if isinstance(v, list) and not isinstance(att, list):
123            assert False, f'Value mismatch in key {k} in platform {platform}'
124        if isinstance(v, list):
125            assert sorted(att) == sorted(v)
126        else:
127            assert att == v
128
129    assert platform.__repr__() == expected_repr
130