def setup_typed_ssm_parameters(context): """parameters that will be deleted during the simulated disaster""" ssm_dict = context.ssm_dict = aws_ssm_dict() context.test_params = params = create_random_typed_params(ssm_dict) yield (True) aws_ssm_dict.remove_dictionary(ssm_dict, params) aws_ssm_dict.verify_deleted_dictionary(ssm_dict, params)
def test_store_returns_input_with_type(value, type_name, description): ssm_dict = aws_ssm_dict(return_type="dict") store_key_with_types(ssm_dict, fixed_key, value, type_name, description) max_retries = 10 count = 0 sleep_secs = 300 / 1000 sleep_mult = 1.4 while True: if count > max_retries: raise Exception( "Too many retries: description mismatch - probably update is broken" ) returned_param = ssm_dict[fixed_key] if returned_param["description"] != description: warn("description mismatch - sleep and try again") sleep(sleep_secs) count += 1 sleep_secs = sleep_secs * sleep_mult else: break assert isinstance(returned_param, dict) assert (returned_param["value"] == value and returned_param["type"] == type_name and returned_param["description"] == description), "return parameter mismatch"
def test_bad_key_raises_exception(key): ssm_dict = aws_ssm_dict() exception_thrown = False try: ssm_dict[key] = "the_value" except AttributeError: exception_thrown = True assert exception_thrown, "key: " + key + " failed to throw expected assertion"
def preexisting_ssm_parameters(context): """\ parameters that don't get deleted during disaster (or perhaps were recreated manually after disaster but before restore) """ ssm_dict = context.ssm_dict = aws_ssm_dict() context.preexist_params = params = create_random_params(ssm_dict) yield (True) aws_ssm_dict.remove_dictionary(ssm_dict, params) aws_ssm_dict.verify_deleted_dictionary(ssm_dict, params)
def backup_to_file(file): ssm_dict = aws_ssm_dict(return_type="dict") contents = {} for i in ssm_dict.keys(): try: contents[i] = ssm_dict[i] except KeyError: eprint("failed to find expected parameter:", i) raise try: with open(file, "w") as f: json.dump(contents, f) except TypeError: json.dump(contents, file)
def restore_from_file(file): ssm_dict = aws_ssm_dict() try: with open(file) as f: contents = json.load(f) except TypeError: contents = json.load(file) for i in contents.keys(): try: ssm_dict[i] = contents[i] except AttributeError as e: if "ParameterAlreadyExists" in str(e): logger.warning("Parameter " + i + " already exists!") else: raise
def test_empty_string_is_stored_as_none_and_returned_as_empty(): value = "empty desc demo" type_name = "SecureString" description = "" ssm_dict = aws_ssm_dict(return_type="dict") try: del ssm_dict[fixed_key] except KeyError: pass ssm_dict[fixed_key] = (type_name, value, "") returned_param = ssm_dict[fixed_key] assert isinstance(returned_param, dict) assert (returned_param["value"] == value and returned_param["type"] == type_name and returned_param["description"] == description), "return parameter mismatch"
def test_store_restore_keys_with_common_start(): key = "/test/fake/key_part" value = "that" key2 = key + "_too" type_name = "String" description = "two matching strings test parameter" ssm_dict = aws_ssm_dict(return_type="dict") try: ssm_dict[key2] = "this" except AttributeError as e: if "ParameterAlreadyExists" not in str(e): raise store_key_with_types(ssm_dict, key, value, type_name, description) max_retries = 10 count = 0 sleep_secs = 300 / 1000 sleep_mult = 1.4 while True: if count > max_retries: raise Exception( "Too many retries: description mismatch - probably update is broken" ) returned_param = ssm_dict[key] if returned_param["description"] != description: warn("description mismatch - sleep and try again") sleep(sleep_secs) count += 1 sleep_secs = sleep_secs * sleep_mult else: break assert isinstance(returned_param, dict) assert (returned_param["value"] == value and returned_param["type"] == type_name and returned_param["description"] == description), "return parameter mismatch"
def test_store_returns_input(value): ssm_dict = aws_ssm_dict() assert store_restore_key(ssm_dict, fixed_key, value) == value