def differences_between_devices_descriptions(cls, already_device_name, requested_device_def): """ Checks if two device description are the same. :param already_device_name: Name of device already created by Moler :param requested_device_def: Description od device provided to create. The name is the same as above. :return: Empty string if descriptions are the same, if not the string with differences. """ already_created_device = cls.get_device(already_device_name, establish_connection=False) already_device_def = copy_dict( DeviceFactory._devices_params[already_created_device.name], True) different_msg = "" already_full_class = already_device_def['class_fullname'] current_full_class = requested_device_def['DEVICE_CLASS'] if already_full_class == current_full_class: default_hops = dict() already_hops = already_device_def['constructor_parameters'][ 'sm_params'].get('CONNECTION_HOPS', default_hops) current_hops = requested_device_def.get('CONNECTION_HOPS', default_hops) diff = compare_objects(already_hops, current_hops) if diff: different_msg = "Device '{}' already created with SM parameters: '{}' but now requested with SM" \ " params: {}. \nDiff: {}".format(already_device_name, already_hops, current_hops, diff) else: different_msg = "Device '{}' already created as instance of class '{}' and now requested as instance of " \ "class '{}'".format(already_device_name, already_full_class, current_full_class) return different_msg
def _run_command_parsing_test(moler_cmd, creation_str, buffer_io, cmd_output, cmd_result, variant, base_class, observer_type): with buffer_io: # open it (autoclose by context-mngr) exclude_types = None if base_class is Event: moler_cmd.start() buffer_io.remote_inject([cmd_output]) result = moler_cmd.await_done(7) exclude_types = {datetime} elif base_class is Command: buffer_io.remote_inject_response([cmd_output]) result = moler_cmd() if sys.version_info < (3, 0): # workaround for python2.7 - convert str to unicode cmd_result = _convert_str_to_unicode(cmd_result) result = _convert_str_to_unicode(result) diff = compare_objects(cmd_result, result, significant_digits=6, exclude_types=exclude_types) if diff: expected_result = pformat(cmd_result, indent=4) real_result = pformat(result, indent=4) diff = pformat(diff, indent=4) error_msg = "{} {} {} (see {}{}):\n{}\n{}:\n{}\n{}\n{}".format( observer_type, creation_str, 'expected to return', '{}_RESULT'.format(observer_type), variant, expected_result, 'but returned', real_result, 'difference:', diff) return error_msg return ""
def configs_are_same(config1, config2): """ Utility function to check if two configs are identical (deep comparison) :param config1: first config to compare :param config2: second config to compare :return: bool """ diff = compare_objects(config1, config2) return not diff
def configs_are_same(config_list, config_to_find): """ Utility function to check if two configs are identical (deep comparison) :param config_list: list of configs to compare :param config_to_find: second config to compare :return: bool, True if config_to_find is in config_list, False otherwise. """ for config in config_list: diff = compare_objects(config, config_to_find) if not diff: return True return False
def test_convert_to_int(): from moler.helpers import convert_to_int, compare_objects sample_input = { 'KEY': [{ 'KEY1 ': { 'contextInfoList': ['sample', '2', '4'], 'someIds': '0' } }, { 'KEY2': { 'contextInfoList': [], 'num': '20', 'poolId': '1', 'user': { 'contextType': 'sample', 'numContexts': '3', 'num': '4' } } }] } expected_output = { 'KEY': [{ 'KEY1 ': { 'contextInfoList': ['sample', 2, 4], 'someIds': 0 } }, { 'KEY2': { 'contextInfoList': [], 'num': 20, 'poolId': 1, 'user': { 'contextType': 'sample', 'numContexts': 3, 'num': 4 } } }] } assert not compare_objects(convert_to_int(sample_input), expected_output)
def load_config(config=None, from_env_var=None, config_type='yaml'): """ Load Moler's configuration from config file :param config: either dict or config filename directly provided (overwrites 'from_env_var' if both given) :param from_env_var: name of environment variable storing config filename :param config_type: 'dict' ('config' param is dict) or 'yaml' ('config' is filename of file with YAML content) :return: None """ global loaded_config if loaded_config == "NOT_LOADED_YET": loaded_config = config elif not compare_objects(loaded_config, config): return else: # TODO: raise exception or no? MolerTest.error("Try to load '{}' config when '{}' config already loaded.\n" "Reload configuration under one Moler execution not supported!".format(config, loaded_config)) return assert (config_type == 'dict') or (config_type == 'yaml') # no other format supported yet if not config: if not from_env_var: raise AssertionError("Provide either 'config' or 'from_env_var' parameter (none given)") if from_env_var not in os.environ: raise KeyError("Environment variable '{}' is not set".format(from_env_var)) path = os.environ[from_env_var] config = read_yaml_configfile(path) elif config_type == 'yaml': assert isinstance(config, six.string_types) path = config config = read_yaml_configfile(path) elif config_type == 'dict': assert isinstance(config, dict) # TODO: check schema load_logger_from_config(config) load_connection_from_config(config) load_device_from_config(config)