Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers config_test.py Source File

config_test.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2017 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009 http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 
00018 import os
00019 import sys
00020 import json
00021 import pytest
00022 from mock import patch
00023 from hypothesis import given
00024 from hypothesis.strategies import sampled_from
00025 from os.path import join, isfile, dirname, abspath
00026 from tools.build_api import get_config
00027 from tools.targets import set_targets_json_location, Target, TARGET_NAMES
00028 from tools.config import ConfigException, Config
00029 
00030 def compare_config (cfg, expected):
00031     """Compare the output of config against a dictionary of known good results
00032 
00033     :param cfg: the configuration to check
00034     :param expected: what to expect in that config
00035     """
00036     try:
00037         for k in cfg:
00038             if cfg[k].value != expected[k]:
00039                 return "'%s': expected '%s', got '%s'" % (k, expected[k], cfg[k].value)
00040     except KeyError:
00041         return "Unexpected key '%s' in configuration data" % k
00042     for k in expected:
00043         if k not in ["expected_macros", "expected_features"] + list(cfg.keys()):
00044             return "Expected key '%s' was not found in configuration data" % k
00045     return ""
00046 
00047 def data_path (path):
00048     """The expected data file for a particular test
00049 
00050     :param path: the path to the test
00051     """
00052     return join(path, "test_data.json")
00053 
00054 def is_test (path):
00055     """Does a directory represent a test?
00056 
00057     :param path: the path to the test
00058     """
00059     return isfile(data_path(path))
00060 
00061 root_dir = abspath(dirname(__file__))
00062 
00063 @pytest.mark.parametrize("name", filter(lambda d: is_test(join(root_dir, d)),
00064                                         os.listdir(root_dir)))
00065 def test_config (name):
00066     """Run a particular configuration test
00067 
00068     :param name: test name (same as directory name)
00069     """
00070     test_dir = join(root_dir, name)
00071     test_data = json.load(open(data_path(test_dir)))
00072     targets_json = os.path.join(test_dir, "targets.json")
00073     set_targets_json_location(targets_json if isfile(targets_json) else None)
00074     for target, expected in test_data.items():
00075         try:
00076             cfg, macros, features = get_config(test_dir, target, "GCC_ARM")
00077             res = compare_config(cfg, expected)
00078             assert not(res), res
00079             expected_macros = expected.get("expected_macros", None)
00080             expected_features = expected.get("expected_features", None)
00081 
00082             if expected_macros is not None:
00083                 macros = Config.config_macros_to_macros(macros)
00084                 assert sorted(expected_macros) == sorted(macros)
00085             if expected_features is not None:
00086                 assert sorted(expected_features) == sorted(features)
00087         except ConfigException as e:
00088             err_msg = str(e)
00089             if "exception_msg" not in expected:
00090                 assert not(err_msg), "Unexpected Error: %s" % e
00091             else:
00092                 assert expected["exception_msg"] in err_msg
00093 
00094 
00095 @pytest.mark.parametrize("target", ["K64F"])
00096 def test_init_app_config (target):
00097     """
00098     Test that the initialisation correctly uses app_config
00099 
00100     :param target: The target to use
00101     """
00102     set_targets_json_location()
00103     with patch.object(Config, '_process_config_and_overrides'),\
00104          patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
00105         app_config = "app_config"
00106         mock_return = {'config': {'test': False}}
00107         mock_json_file_to_dict.return_value = mock_return
00108 
00109         config = Config(target, app_config=app_config)
00110 
00111         mock_json_file_to_dict.assert_any_call("app_config")
00112 
00113         assert config.app_config_data == mock_return
00114 
00115 
00116 @pytest.mark.parametrize("target", ["K64F"])
00117 def test_init_no_app_config (target):
00118     """
00119     Test that the initialisation works without app config
00120 
00121     :param target: The target to use
00122     """
00123     set_targets_json_location()
00124     with patch.object(Config, '_process_config_and_overrides'),\
00125          patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
00126         config = Config(target)
00127 
00128         mock_json_file_to_dict.assert_not_called()
00129         assert config.app_config_data == {}
00130 
00131 
00132 @pytest.mark.parametrize("target", ["K64F"])
00133 def test_init_no_app_config_with_dir (target):
00134     """
00135     Test that the initialisation works without app config and with a
00136     specified top level directory
00137 
00138     :param target: The target to use
00139     """
00140     set_targets_json_location()
00141     with patch.object(Config, '_process_config_and_overrides'),\
00142          patch('os.path.isfile') as mock_isfile, \
00143          patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
00144         directory = '.'
00145         path = os.path.join('.', 'mbed_app.json')
00146         mock_return = {'config': {'test': False}}
00147         mock_json_file_to_dict.return_value = mock_return
00148         mock_isfile.return_value = True
00149 
00150         config = Config(target, [directory])
00151 
00152         mock_isfile.assert_called_with(path)
00153         mock_json_file_to_dict.assert_any_call(path)
00154         assert config.app_config_data == mock_return
00155 
00156 
00157 @pytest.mark.parametrize("target", ["K64F"])
00158 def test_init_override_app_config (target):
00159     """
00160     Test that the initialisation uses app_config instead of top_level_dir
00161     when both are specified
00162 
00163     :param target: The target to use
00164     """
00165     set_targets_json_location()
00166     with patch.object(Config, '_process_config_and_overrides'),\
00167          patch('tools.config.json_file_to_dict') as mock_json_file_to_dict:
00168         app_config = "app_config"
00169         directory = '.'
00170         mock_return = {'config': {'test': False}}
00171         mock_json_file_to_dict.return_value = mock_return
00172 
00173         config = Config(target, [directory], app_config=app_config)
00174 
00175         mock_json_file_to_dict.assert_any_call(app_config)
00176         assert config.app_config_data == mock_return
00177 
00178 @pytest.mark.parametrize("target", ["K64F", "UBLOX_EVK_ODIN_W2"])
00179 @pytest.mark.parametrize("overrides", [
00180     {},
00181     {"restrict_size": "0x200"},
00182     {"mbed_app_start": "0x200"}
00183 ])
00184 def test_basic_regions (target, overrides):
00185     """
00186     Test that the region lists are sane with various configurations
00187     """
00188     set_targets_json_location()
00189     config = Config(target)
00190     for o, v in overrides.items():
00191         setattr(config.target, o, v)
00192     try:
00193         if config.has_regions:
00194             regions = list(config.regions)
00195             for r in regions:
00196                 assert r.size >= 0
00197     except ConfigException:
00198         pass