Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 """
borlanic 0:02dd72d1d465 2 mbed SDK
borlanic 0:02dd72d1d465 3 Copyright (c) 2011-2017 ARM Limited
borlanic 0:02dd72d1d465 4
borlanic 0:02dd72d1d465 5 Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 6 you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 7 You may obtain a copy of the License at
borlanic 0:02dd72d1d465 8
borlanic 0:02dd72d1d465 9 http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 10
borlanic 0:02dd72d1d465 11 Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 12 distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 14 See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 15 limitations under the License.
borlanic 0:02dd72d1d465 16 """
borlanic 0:02dd72d1d465 17
borlanic 0:02dd72d1d465 18 import os
borlanic 0:02dd72d1d465 19 import sys
borlanic 0:02dd72d1d465 20 import json
borlanic 0:02dd72d1d465 21 import pytest
borlanic 0:02dd72d1d465 22 from mock import patch
borlanic 0:02dd72d1d465 23 from hypothesis import given
borlanic 0:02dd72d1d465 24 from hypothesis.strategies import sampled_from
borlanic 0:02dd72d1d465 25 from os.path import join, isfile, dirname, abspath
borlanic 0:02dd72d1d465 26 from tools.build_api import get_config
borlanic 0:02dd72d1d465 27 from tools.targets import set_targets_json_location, Target, TARGET_NAMES
borlanic 0:02dd72d1d465 28 from tools.config import ConfigException, Config
borlanic 0:02dd72d1d465 29
borlanic 0:02dd72d1d465 30 def compare_config(cfg, expected):
borlanic 0:02dd72d1d465 31 """Compare the output of config against a dictionary of known good results
borlanic 0:02dd72d1d465 32
borlanic 0:02dd72d1d465 33 :param cfg: the configuration to check
borlanic 0:02dd72d1d465 34 :param expected: what to expect in that config
borlanic 0:02dd72d1d465 35 """
borlanic 0:02dd72d1d465 36 try:
borlanic 0:02dd72d1d465 37 for k in cfg:
borlanic 0:02dd72d1d465 38 if cfg[k].value != expected[k]:
borlanic 0:02dd72d1d465 39 return "'%s': expected '%s', got '%s'" % (k, expected[k], cfg[k].value)
borlanic 0:02dd72d1d465 40 except KeyError:
borlanic 0:02dd72d1d465 41 return "Unexpected key '%s' in configuration data" % k
borlanic 0:02dd72d1d465 42 for k in expected:
borlanic 0:02dd72d1d465 43 if k not in ["expected_macros", "expected_features"] + list(cfg.keys()):
borlanic 0:02dd72d1d465 44 return "Expected key '%s' was not found in configuration data" % k
borlanic 0:02dd72d1d465 45 return ""
borlanic 0:02dd72d1d465 46
borlanic 0:02dd72d1d465 47 def data_path(path):
borlanic 0:02dd72d1d465 48 """The expected data file for a particular test
borlanic 0:02dd72d1d465 49
borlanic 0:02dd72d1d465 50 :param path: the path to the test
borlanic 0:02dd72d1d465 51 """
borlanic 0:02dd72d1d465 52 return join(path, "test_data.json")
borlanic 0:02dd72d1d465 53
borlanic 0:02dd72d1d465 54 def is_test(path):
borlanic 0:02dd72d1d465 55 """Does a directory represent a test?
borlanic 0:02dd72d1d465 56
borlanic 0:02dd72d1d465 57 :param path: the path to the test
borlanic 0:02dd72d1d465 58 """
borlanic 0:02dd72d1d465 59 return isfile(data_path(path))
borlanic 0:02dd72d1d465 60
borlanic 0:02dd72d1d465 61 root_dir = abspath(dirname(__file__))
borlanic 0:02dd72d1d465 62
borlanic 0:02dd72d1d465 63 @pytest.mark.parametrize("name", filter(lambda d: is_test(join(root_dir, d)),
borlanic 0:02dd72d1d465 64 os.listdir(root_dir)))
borlanic 0:02dd72d1d465 65 def test_config(name):
borlanic 0:02dd72d1d465 66 """Run a particular configuration test
borlanic 0:02dd72d1d465 67
borlanic 0:02dd72d1d465 68 :param name: test name (same as directory name)
borlanic 0:02dd72d1d465 69 """
borlanic 0:02dd72d1d465 70 test_dir = join(root_dir, name)
borlanic 0:02dd72d1d465 71 test_data = json.load(open(data_path(test_dir)))
borlanic 0:02dd72d1d465 72 targets_json = os.path.join(test_dir, "targets.json")
borlanic 0:02dd72d1d465 73 set_targets_json_location(targets_json if isfile(targets_json) else None)
borlanic 0:02dd72d1d465 74 for target, expected in test_data.items():
borlanic 0:02dd72d1d465 75 try:
borlanic 0:02dd72d1d465 76 cfg, macros, features = get_config(test_dir, target, "GCC_ARM")
borlanic 0:02dd72d1d465 77 res = compare_config(cfg, expected)
borlanic 0:02dd72d1d465 78 assert not(res), res
borlanic 0:02dd72d1d465 79 expected_macros = expected.get("expected_macros", None)
borlanic 0:02dd72d1d465 80 expected_features = expected.get("expected_features", None)
borlanic 0:02dd72d1d465 81
borlanic 0:02dd72d1d465 82 if expected_macros is not None:
borlanic 0:02dd72d1d465 83 macros = Config.config_macros_to_macros(macros)
borlanic 0:02dd72d1d465 84 assert sorted(expected_macros) == sorted(macros)
borlanic 0:02dd72d1d465 85 if expected_features is not None:
borlanic 0:02dd72d1d465 86 assert sorted(expected_features) == sorted(features)
borlanic 0:02dd72d1d465 87 except ConfigException as e:
borlanic 0:02dd72d1d465 88 err_msg = str(e)
borlanic 0:02dd72d1d465 89 if "exception_msg" not in expected:
borlanic 0:02dd72d1d465 90 assert not(err_msg), "Unexpected Error: %s" % e
borlanic 0:02dd72d1d465 91 else:
borlanic 0:02dd72d1d465 92 assert expected["exception_msg"] in err_msg
borlanic 0:02dd72d1d465 93
borlanic 0:02dd72d1d465 94
borlanic 0:02dd72d1d465 95 @pytest.mark.parametrize("target", ["K64F"])
borlanic 0:02dd72d1d465 96 def test_init_app_config(target):
borlanic 0:02dd72d1d465 97 """
borlanic 0:02dd72d1d465 98 Test that the initialisation correctly uses app_config
borlanic 0:02dd72d1d465 99
borlanic 0:02dd72d1d465 100 :param target: The target to use
borlanic 0:02dd72d1d465 101 """
borlanic 0:02dd72d1d465 102 set_targets_json_location()
borlanic 0:02dd72d1d465 103 with patch.object(Config, '_process_config_and_overrides'),\
borlanic 0:02dd72d1d465 104 patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
borlanic 0:02dd72d1d465 105 app_config = "app_config"
borlanic 0:02dd72d1d465 106 mock_return = {'config': {'test': False}}
borlanic 0:02dd72d1d465 107 mock_json_file_to_dict.return_value = mock_return
borlanic 0:02dd72d1d465 108
borlanic 0:02dd72d1d465 109 config = Config(target, app_config=app_config)
borlanic 0:02dd72d1d465 110
borlanic 0:02dd72d1d465 111 mock_json_file_to_dict.assert_any_call("app_config")
borlanic 0:02dd72d1d465 112
borlanic 0:02dd72d1d465 113 assert config.app_config_data == mock_return
borlanic 0:02dd72d1d465 114
borlanic 0:02dd72d1d465 115
borlanic 0:02dd72d1d465 116 @pytest.mark.parametrize("target", ["K64F"])
borlanic 0:02dd72d1d465 117 def test_init_no_app_config(target):
borlanic 0:02dd72d1d465 118 """
borlanic 0:02dd72d1d465 119 Test that the initialisation works without app config
borlanic 0:02dd72d1d465 120
borlanic 0:02dd72d1d465 121 :param target: The target to use
borlanic 0:02dd72d1d465 122 """
borlanic 0:02dd72d1d465 123 set_targets_json_location()
borlanic 0:02dd72d1d465 124 with patch.object(Config, '_process_config_and_overrides'),\
borlanic 0:02dd72d1d465 125 patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
borlanic 0:02dd72d1d465 126 config = Config(target)
borlanic 0:02dd72d1d465 127
borlanic 0:02dd72d1d465 128 mock_json_file_to_dict.assert_not_called()
borlanic 0:02dd72d1d465 129 assert config.app_config_data == {}
borlanic 0:02dd72d1d465 130
borlanic 0:02dd72d1d465 131
borlanic 0:02dd72d1d465 132 @pytest.mark.parametrize("target", ["K64F"])
borlanic 0:02dd72d1d465 133 def test_init_no_app_config_with_dir(target):
borlanic 0:02dd72d1d465 134 """
borlanic 0:02dd72d1d465 135 Test that the initialisation works without app config and with a
borlanic 0:02dd72d1d465 136 specified top level directory
borlanic 0:02dd72d1d465 137
borlanic 0:02dd72d1d465 138 :param target: The target to use
borlanic 0:02dd72d1d465 139 """
borlanic 0:02dd72d1d465 140 set_targets_json_location()
borlanic 0:02dd72d1d465 141 with patch.object(Config, '_process_config_and_overrides'),\
borlanic 0:02dd72d1d465 142 patch('os.path.isfile') as mock_isfile, \
borlanic 0:02dd72d1d465 143 patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
borlanic 0:02dd72d1d465 144 directory = '.'
borlanic 0:02dd72d1d465 145 path = os.path.join('.', 'mbed_app.json')
borlanic 0:02dd72d1d465 146 mock_return = {'config': {'test': False}}
borlanic 0:02dd72d1d465 147 mock_json_file_to_dict.return_value = mock_return
borlanic 0:02dd72d1d465 148 mock_isfile.return_value = True
borlanic 0:02dd72d1d465 149
borlanic 0:02dd72d1d465 150 config = Config(target, [directory])
borlanic 0:02dd72d1d465 151
borlanic 0:02dd72d1d465 152 mock_isfile.assert_called_with(path)
borlanic 0:02dd72d1d465 153 mock_json_file_to_dict.assert_any_call(path)
borlanic 0:02dd72d1d465 154 assert config.app_config_data == mock_return
borlanic 0:02dd72d1d465 155
borlanic 0:02dd72d1d465 156
borlanic 0:02dd72d1d465 157 @pytest.mark.parametrize("target", ["K64F"])
borlanic 0:02dd72d1d465 158 def test_init_override_app_config(target):
borlanic 0:02dd72d1d465 159 """
borlanic 0:02dd72d1d465 160 Test that the initialisation uses app_config instead of top_level_dir
borlanic 0:02dd72d1d465 161 when both are specified
borlanic 0:02dd72d1d465 162
borlanic 0:02dd72d1d465 163 :param target: The target to use
borlanic 0:02dd72d1d465 164 """
borlanic 0:02dd72d1d465 165 set_targets_json_location()
borlanic 0:02dd72d1d465 166 with patch.object(Config, '_process_config_and_overrides'),\
borlanic 0:02dd72d1d465 167 patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
borlanic 0:02dd72d1d465 168 app_config = "app_config"
borlanic 0:02dd72d1d465 169 directory = '.'
borlanic 0:02dd72d1d465 170 mock_return = {'config': {'test': False}}
borlanic 0:02dd72d1d465 171 mock_json_file_to_dict.return_value = mock_return
borlanic 0:02dd72d1d465 172
borlanic 0:02dd72d1d465 173 config = Config(target, [directory], app_config=app_config)
borlanic 0:02dd72d1d465 174
borlanic 0:02dd72d1d465 175 mock_json_file_to_dict.assert_any_call(app_config)
borlanic 0:02dd72d1d465 176 assert config.app_config_data == mock_return
borlanic 0:02dd72d1d465 177
borlanic 0:02dd72d1d465 178 @pytest.mark.parametrize("target", ["K64F", "UBLOX_EVK_ODIN_W2"])
borlanic 0:02dd72d1d465 179 @pytest.mark.parametrize("overrides", [
borlanic 0:02dd72d1d465 180 {},
borlanic 0:02dd72d1d465 181 {"restrict_size": "0x200"},
borlanic 0:02dd72d1d465 182 {"mbed_app_start": "0x200"}
borlanic 0:02dd72d1d465 183 ])
borlanic 0:02dd72d1d465 184 def test_basic_regions(target, overrides):
borlanic 0:02dd72d1d465 185 """
borlanic 0:02dd72d1d465 186 Test that the region lists are sane with various configurations
borlanic 0:02dd72d1d465 187 """
borlanic 0:02dd72d1d465 188 set_targets_json_location()
borlanic 0:02dd72d1d465 189 config = Config(target)
borlanic 0:02dd72d1d465 190 for o, v in overrides.items():
borlanic 0:02dd72d1d465 191 setattr(config.target, o, v)
borlanic 0:02dd72d1d465 192 try:
borlanic 0:02dd72d1d465 193 if config.has_regions:
borlanic 0:02dd72d1d465 194 regions = list(config.regions)
borlanic 0:02dd72d1d465 195 for r in regions:
borlanic 0:02dd72d1d465 196 assert r.size >= 0
borlanic 0:02dd72d1d465 197 except ConfigException:
borlanic 0:02dd72d1d465 198 pass