From 5a790d0b4255c118f579d3f6656af6af3f490c30 Mon Sep 17 00:00:00 2001 From: Adrian Hornsby Date: Wed, 10 Jul 2019 14:16:03 +0100 Subject: [PATCH] Fix the behavior of the decorator when notEnabled + some speliing mistakes. --- lambda_function.py | 24 +++++++++++++----------- python/chaos_lib.py | 13 ++++++++++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lambda_function.py b/lambda_function.py index 93b23d0..ce7ed61 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -1,8 +1,10 @@ from chaos_lib import corrupt_delay -from chaos_lib import corrupt_expection +from chaos_lib import corrupt_exception from chaos_lib import corrupt_statuscode from chaos_lib import SessionWithDelay +# from chaos_lib import * + # def dummy1(): # session = SessionWithDelay(delay=300) @@ -15,15 +17,7 @@ # pass -# @corrupt_expection -# def lambda_handler(event, context): -# return { -# 'statusCode': 200, -# 'body': 'Hello from Lambda!' -# } - - -# @corrupt_delay +# @corrupt_exception # def lambda_handler(event, context): # return { # 'statusCode': 200, @@ -31,9 +25,17 @@ # } -@corrupt_statuscode +@corrupt_delay def lambda_handler(event, context): return { 'statusCode': 200, 'body': 'Hello from Lambda!' } + + +# @corrupt_statuscode +# def lambda_handler(event, context): +# return { +# 'statusCode': 200, +# 'body': 'Hello from Lambda!' +# } diff --git a/python/chaos_lib.py b/python/chaos_lib.py index 18c0abb..65184b0 100644 --- a/python/chaos_lib.py +++ b/python/chaos_lib.py @@ -28,7 +28,7 @@ def get_config(config_key): try: value = json.loads(param.value) if not value["isEnabled"]: - return 0 + return 0, 1 return value[config_key], value.get('rate', 1) except InvalidParameterError as e: # key does not exist in SSM @@ -41,10 +41,13 @@ def get_config(config_key): def corrupt_delay(func): def latency(*args, **kw): delay, rate = get_config('delay') + if not delay: + return func(*args, **kw) print("delay: {0}, rate: {1}".format(delay, rate)) - start = time.time() # if delay and rate exist, delaying with that value at that rate + start = time.time() if delay > 0 and rate >= 0: + # add latency approx rate% of the time if random.random() <= rate: time.sleep(delay / 1000.0) @@ -60,10 +63,12 @@ def latency(*args, **kw): return latency -def corrupt_expection(func): +def corrupt_exception(func): def wrapper(*args, **kwargs): result = func(*args, **kwargs) exception_msg, rate = get_config('exception_msg') + if not exception_msg: + return result print("exception_msg from config {0} with a rate of {1}".format(exception_msg, rate)) # add injection approx rate% of the time if random.random() <= rate: @@ -78,6 +83,8 @@ def corrupt_statuscode(func): def wrapper(*args, **kwargs): result = func(*args, **kwargs) error_code, rate = get_config('error_code') + if not error_code: + return result print("Error from config {0} at a rate of {1}".format(error_code, rate)) # add injection approx rate% of the time if random.random() <= rate: