def __write_file_with_separator_conversion(self, contents): contents = serialize_json( self.__convert_separators(parse_json(contents))) fp = open(self.__config_file, 'w') fp.write(contents) fp.close()
def __write_config_fragment_file_with_separator_conversion(self, file_path, contents): contents = serialize_json(self.__convert_separators(parse_json(contents))) full_path = os.path.join(self.__config_fragments_dir, file_path) fp = open(full_path, "w") fp.write(contents) fp.close()
def __write_config_fragment_file_with_separator_conversion(self, file_path, contents): contents = serialize_json(self.__convert_separators(parse_json(contents))) full_path = os.path.join(self.__config_fragments_dir, file_path) fp = open(full_path, 'w') fp.write(contents) fp.close()
def __write_file_with_separator_conversion(self, contents): contents = serialize_json(self.__convert_separators(parse_json(contents))) fp = open(self.__config_file, "w") fp.write(contents) fp.close()
def _test_environment_aware_global_params(self, uppercase): """Tests config params that have environment variable overrides as follows: 1. Ensure params are "environment variable aware" -- meaning code exists to look for corresponding environment variable to override with. 2. Generate fake environment variable values and ensure they propagate to the configuration object. Fake value is guaranteed different from config-file (or default) value. 3. Repeat test for lower-case environment variables (we support both fully upper or lower case). """ field_types = {} config_file_dict = { "logs": [ {"path": "/var/log/tomcat6/$DIR_VAR.log"} ], "api_key": "abcd1234", "use_unsafe_debugging": False, } self._write_file_with_separator_conversion(serialize_json(JsonObject(config_file_dict))) config = self._create_test_configuration_instance() # Parse config files once to capture all environment-aware variables in Configuration._environment_aware_map config.parse() expected_aware_fields = config._environment_aware_map # Currently, all environment-aware global config params are primitives. # (In the future, we will need to intercept the json_array, json_object, ArrayOfString methods) original_verify_or_set_optional_bool = config._Configuration__verify_or_set_optional_bool original_verify_or_set_optional_int = config._Configuration__verify_or_set_optional_int original_verify_or_set_optional_float = config._Configuration__verify_or_set_optional_float original_verify_or_set_optional_string = config._Configuration__verify_or_set_optional_string @patch.object(config, '_Configuration__verify_or_set_optional_bool') @patch.object(config, '_Configuration__verify_or_set_optional_int') @patch.object(config, '_Configuration__verify_or_set_optional_float') @patch.object(config, '_Configuration__verify_or_set_optional_string') def patch_and_start_test(p3, p2, p1, p0): # Decorate the Configuration.__verify_or_set_optional_xxx methods as follows: # 1) capture fields that are environment-aware # 2) allow setting of the corresponding environment variable def capture_aware_field(field_type): def wrapper(*args, **kwargs): field = args[1] field_types[field] = field_type envar_val = function_lookup['get_environment'](field) if envar_val: env_name = expected_aware_fields[field] if not env_name: env_name = 'SCALYR_%s' % field if uppercase: env_name = env_name.upper() else: env_name = env_name.lower() os.environ[env_name] = envar_val if field_type == bool: return original_verify_or_set_optional_bool(*args, **kwargs) elif field_type == int: return original_verify_or_set_optional_int(*args, **kwargs) elif field_type == float: return original_verify_or_set_optional_float(*args, **kwargs) elif field_type == str: return original_verify_or_set_optional_string(*args, **kwargs) return wrapper p0.side_effect = capture_aware_field(bool) p1.side_effect = capture_aware_field(int) p2.side_effect = capture_aware_field(float) p3.side_effect = capture_aware_field(str) # Build the Configuration object tree, also populating the field_types lookup in the process # This first iteration does not set any environment variables def no_set_values(field): return None function_lookup = {'get_environment': no_set_values} config.parse() # --------------------------------------------------------------------------------------------------------- # Ensure each field can be overridden (by faking environment variables) # --------------------------------------------------------------------------------------------------------- fake_env = {} config_obj = config._Configuration__get_config() # prepare fake environment variable values that differ from existing config object FAKE_INT = 1234567890 FAKE_FLOAT = 1234567.89 FAKE_STRING = str(FAKE_INT) for field in expected_aware_fields: field_type = field_types[field] if field_type == bool: # fake value should be different from config-file value fake_env[field] = not config_obj.get_bool(field, none_if_missing=True) elif field_type == int: # special case : debug_level cannot be arbitrary. Set it to config file value + 1 if field == 'debug_level': existing_level = config_obj.get_int(field, none_if_missing=True) fake_env[field] = existing_level + 1 else: self.assertNotEquals(FAKE_INT, config_obj.get_int(field, none_if_missing=True)) fake_env[field] = FAKE_INT elif field_type == float: self.assertNotEquals(FAKE_FLOAT, config_obj.get_float(field, none_if_missing=True)) fake_env[field] = FAKE_FLOAT elif field_type == str: self.assertNotEquals(FAKE_STRING, config_obj.get_string(field, none_if_missing=True)) fake_env[field] = FAKE_STRING def fake_environment_value(field): if field not in fake_env: return None return str(fake_env[field]).lower() function_lookup = {'get_environment': fake_environment_value} config.parse() self.assertGreater(len(expected_aware_fields), 1) for field, env_varname in expected_aware_fields.items(): field_type = field_types[field] if field_type == bool: value = config._Configuration__get_config().get_bool(field) elif field_type == int: value = config._Configuration__get_config().get_int(field) elif field_type == float: value = config._Configuration__get_config().get_float(field) elif field_type == str: value = config._Configuration__get_config().get_string(field) config_file_value = config_file_dict.get(field) if field in config_file_dict: # Config params defined in the config file must not take on the fake environment values. self.assertNotEquals(value, fake_env[field]) self.assertEquals(value, config_file_value) else: # But those not defined in config file will take on environment values. self.assertEquals(value, fake_env[field]) self.assertNotEquals(value, config_file_value) patch_and_start_test()