示例#1
0
def generic_test(function_name, parameter_name,  parameter_value, sleep_time=60, param_type='String'):
    
    dummy_value_updated = ''.join(random.choices(string.ascii_lowercase, k = 25))
    dummy_value_returned = dummy_value_updated
    revert_parameter_value = parameter_value
    if param_type == 'StringList':
        dummy_value_updated = ','.join(random.choices(string.ascii_lowercase, k = 5))
        dummy_value_returned = dummy_value_updated.split(',')
        revert_parameter_value = ','.join(parameter_value)

    value = get_message_from_lambda(function_name)
    assert value == parameter_value

    update_parameter(parameter_name, dummy_value_updated, param_type)
    value = get_message_from_lambda(function_name)
    assert value == parameter_value

    time.sleep(sleep_time)
    value = get_message_from_lambda(function_name)
    assert value == dummy_value_returned

    update_parameter(parameter_name, revert_parameter_value, param_type)
    value = get_message_from_lambda(function_name)
    assert value == dummy_value_returned

    time.sleep(sleep_time)
    value = get_message_from_lambda(function_name)
    assert value == parameter_value
示例#2
0
def parameter_assignment(parameter, parameter_value, parameter_type='String'):

    dummy_name = ''.join(random.choices(string.ascii_lowercase, k=8))

    if parameter_type == 'StringList':
        dummy_value = "d,e,f"
        dummy_value_return = dummy_value.split(",")
        parameter_value_return = parameter_value.split(",")
    else:
        dummy_value = 'get_dummy'
        dummy_value_return = dummy_value
        parameter_value_return = parameter_value

    ttl = 5

    param = ssm.get_entry(parameter=parameter, max_age_in_seconds=ttl)
    assert param == parameter_value_return

    update_parameter(parameter, dummy_value, parameter_type)
    param = ssm.get_entry(parameter=parameter, max_age_in_seconds=ttl)
    assert param == parameter_value_return

    time.sleep(ttl)
    param = ssm.get_entry(parameter=parameter, max_age_in_seconds=ttl)
    assert param == dummy_value_return
    param = ssm.get_entry(parameter=parameter, max_age_in_seconds=ttl)
    assert param == dummy_value_return

    time.sleep(ttl)
    update_parameter(parameter, parameter_value, parameter_type)
    param = ssm.get_entry(parameter=parameter, max_age_in_seconds=ttl)
    assert param == parameter_value_return
示例#3
0
def decorator_test(decorated_function, parameter_name, entry_name, parameter_value, max_age_in_seconds, param_type='String'):
    
    dummy_value_updated = ''.join(random.choices(string.ascii_lowercase, k = 25))
    dummy_value_returned = dummy_value_updated
    revert_parameter_value = parameter_value
    if param_type == 'StringList':
        dummy_value_updated = ','.join(random.choices(string.ascii_lowercase, k = 5))
        dummy_value_returned = dummy_value_updated.split(',')
        revert_parameter_value = ','.join(parameter_value)
    
    test_event = {'event_name': 'test'}
    test_context = LambdaContext()

    context = decorated_function(test_event, test_context)
    assert getattr(context, entry_name) == parameter_value
    
    # Update parameter but call before max_age_in_seconds
    update_parameter(parameter_name, dummy_value_updated, param_type=param_type)
    context = decorated_function(test_event, test_context)
    assert getattr(context, entry_name) == parameter_value

    # Wait max_age_in_seconds call again
    time.sleep(max_age_in_seconds)
    context = decorated_function(test_event, test_context)
    assert getattr(context, entry_name) == dummy_value_returned

    # Revert back to normal
    update_parameter(parameter_name, revert_parameter_value, param_type=param_type)
    time.sleep(max_age_in_seconds)
    context = decorated_function(test_event, test_context)
    assert getattr(context, entry_name) == parameter_value
示例#4
0
def test_invalidate_cache():
    
    updated_value = 'Dummy Value NEW!!'
    test_event = {}
    test_context = LambdaContext()
    refresh_event = {'refresh': True}

    context = invalidate_cache({}, test_context)
    assert getattr(context, ssm_parameter_default_name) == ssm_parameter_value
    
    # Update parameter and test within 5 seconds
    update_parameter(ssm_parameter, updated_value)
    time.sleep(5)
    context = invalidate_cache({}, test_context)
    assert getattr(context, ssm_parameter_default_name) == ssm_parameter_value

    # Wait 5 seconds call again, parameter should be refreshed
    time.sleep(5)
    context = invalidate_cache({}, test_context)
    assert getattr(context, ssm_parameter_default_name) == updated_value

    # Update parameter back to ssm_parameter_value, but call with invalidated cache
    update_parameter(ssm_parameter, ssm_parameter_value)
    context = invalidate_cache(refresh_event, test_context)
    assert getattr(context, ssm_parameter_default_name) == ssm_parameter_value
示例#5
0
def test_no_cache():
    test_event = {'event_name': 'test'}
    test_context = LambdaContext()
    new_value = "New Value"

    context = no_cache(test_event, test_context)
    assert getattr(context, ssm_parameter_default_name) == ssm_parameter_value
    
    update_parameter(ssm_parameter, new_value)
    context = no_cache(test_event, test_context)
    assert getattr(context, ssm_parameter_default_name) == new_value

    update_parameter(ssm_parameter, ssm_parameter_value)
    context = no_cache(test_event, test_context)
    assert getattr(context, ssm_parameter_default_name) == ssm_parameter_value
示例#6
0
def test_lambda_assign_parameter():
    test_initialize()
    function_name = f"{service}-{stage}-assign_parameter"
    return_value = {"param_1": ssm_parameter_value, 
                    "param_2": ssm_parameter_2_value,
                    "param_3": string_list_value.split(','),
                    "param_4": secure_parameter_value}
    
    new_value = ''.join(random.choices(string.ascii_lowercase, k = 8))
    
    value = get_message_from_lambda(function_name)
    assert value == return_value

    update_parameter(ssm_parameter, new_value)
    update_parameter(ssm_parameter_2, new_value)
    update_parameter(secure_parameter, new_value)
    value = get_message_from_lambda(function_name)
    assert value == return_value

    time.sleep(20) # after 20 seconds
    return_value['param_1'] = new_value
    value = get_message_from_lambda(function_name)
    assert value == return_value

    time.sleep(10) # after 30 seconds
    return_value['param_2'] = new_value
    value = get_message_from_lambda(function_name)
    assert value == return_value

    time.sleep(30) # after 60 seconds
    return_value['param_4'] = new_value
    value = get_message_from_lambda(function_name)
    assert value == return_value
示例#7
0
def test_lambda_multi_handler():
    function_name = f"{service}-{stage}-multi_handler"
    return_value = {"param_1": ssm_parameter_value, 
                    "param_2": ssm_parameter_2_value,
                    "param_3": string_list_value.split(','),
                    "param_4": secure_parameter_value}
    
    new_value = ''.join(random.choices(string.ascii_lowercase, k = 8))

    value = get_message_from_lambda(function_name)
    assert value == return_value
    
    update_parameter(ssm_parameter, new_value)
    update_parameter(ssm_parameter_2, new_value)
    value = get_message_from_lambda(function_name)
    assert value == return_value

    time.sleep(10)
    value = get_message_from_lambda(function_name)
    return_value["param_1"] = new_value
    return_value["param_2"] = new_value
    assert value == return_value
示例#8
0
def test_multi_parameters():

    dummy_string = "__"
    dummy_list = "-,--,---"
    test_event = {}
    test_context = LambdaContext()

    context = multi_parameters(test_event, test_context)
    assert getattr(context, default_entry_name).get(
        ssm_parameter_default_name) == ssm_parameter_value
    assert getattr(context, default_entry_name).get(
        ssm_parameter_2_default_name) == ssm_parameter_2_value
    assert getattr(context, default_entry_name).get(
        string_list_default_name) == string_list_value.split(',')
    assert getattr(context, default_entry_name).get(
        secure_parameter_default_name) == secure_parameter_value

    update_parameter(ssm_parameter, dummy_string)
    update_parameter(ssm_parameter_2, dummy_string)
    update_parameter(secure_parameter, dummy_string, param_type='SecureString')
    update_parameter(string_list_parameter,
                     dummy_list,
                     param_type='StringList')
    context = multi_parameters(test_event, test_context)
    assert getattr(context, default_entry_name).get(
        ssm_parameter_default_name) == ssm_parameter_value
    assert getattr(context, default_entry_name).get(
        ssm_parameter_2_default_name) == ssm_parameter_2_value
    assert getattr(context, default_entry_name).get(
        string_list_default_name) == string_list_value.split(',')
    assert getattr(context, default_entry_name).get(
        secure_parameter_default_name) == secure_parameter_value

    time.sleep(10)
    context = multi_parameters(test_event, test_context)
    assert getattr(
        context,
        default_entry_name).get(ssm_parameter_default_name) == dummy_string
    assert getattr(
        context,
        default_entry_name).get(ssm_parameter_2_default_name) == dummy_string
    assert getattr(context, default_entry_name).get(
        string_list_default_name) == dummy_list.split(',')
    assert getattr(
        context,
        default_entry_name).get(secure_parameter_default_name) == dummy_string

    test_initialize()
    context = multi_parameters(test_event, test_context)
    assert getattr(
        context,
        default_entry_name).get(ssm_parameter_default_name) == dummy_string
    assert getattr(
        context,
        default_entry_name).get(ssm_parameter_2_default_name) == dummy_string
    assert getattr(context, default_entry_name).get(
        string_list_default_name) == dummy_list.split(',')
    assert getattr(
        context,
        default_entry_name).get(secure_parameter_default_name) == dummy_string

    time.sleep(10)
    context = multi_parameters(test_event, test_context)
    assert getattr(context, default_entry_name).get(
        ssm_parameter_default_name) == ssm_parameter_value
    assert getattr(context, default_entry_name).get(
        ssm_parameter_2_default_name) == ssm_parameter_2_value
    assert getattr(context, default_entry_name).get(
        string_list_default_name) == string_list_value.split(',')
    assert getattr(context, default_entry_name).get(
        secure_parameter_default_name) == secure_parameter_value
示例#9
0
def test_initialize():
    delete_parameters([ssm_parameter, ssm_parameter_2, secure_parameter, string_list_parameter])
    update_parameter(ssm_parameter, ssm_parameter_value)
    update_parameter(ssm_parameter_2, ssm_parameter_2_value)
    update_parameter(secure_parameter, secure_parameter_value, param_type='SecureString')
    update_parameter(string_list_parameter,string_list_value, param_type='StringList')
示例#10
0
def test_initialize():
    update_parameter(ssm_parameter, ssm_parameter_value)
    update_parameter(ssm_parameter_2, ssm_parameter_2_value)
    update_parameter(secure_parameter, secure_parameter_value, param_type='SecureString')
    update_parameter(long_name_parameter, long_name_value)
    update_parameter(string_list_parameter,string_list_value, param_type='StringList')