From 598e4161ae9db833aefcafdc582a6c9a7904bc7f Mon Sep 17 00:00:00 2001 From: Ben Larabie Date: Fri, 24 Nov 2023 14:35:16 -0500 Subject: [PATCH 1/4] Smoke Test Loop Script --- Makefile | 4 ++ tests_smoke/smoke_loop.py | 100 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tests_smoke/smoke_loop.py diff --git a/Makefile b/Makefile index 732648e7dd..98482e95ef 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,10 @@ format: smoke-test: cd tests_smoke && poetry run python smoke_test.py +.PHONY: smoke-loop +smoke-loop: + cd tests_smoke && poetry run python smoke_loop.py $(ARGS) + .PHONY: run run: ## Run the web app flask run -p 6011 --host=0.0.0.0 diff --git a/tests_smoke/smoke_loop.py b/tests_smoke/smoke_loop.py new file mode 100644 index 0000000000..97526e9a1d --- /dev/null +++ b/tests_smoke/smoke_loop.py @@ -0,0 +1,100 @@ +import subprocess +import os +import time +from argparse import ArgumentParser +from smoke.common import Attachment_type, Config, Notification_type # type: ignore +from smoke.test_admin_csv import test_admin_csv # type: ignore +from smoke.test_admin_one_off import test_admin_one_off # type: ignore +from smoke.test_api_bulk import test_api_bulk # type: ignore +from smoke.test_api_one_off import test_api_one_off # type: ignore + +runCount = 0 +maxRuns = 0 + +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + +def init(): + + global maxRuns + + parser = ArgumentParser() + parser.add_argument("-m", "--max-runs", dest="maxruns", + help="Set the maximum number of runs you want", metavar="MAXRUNS") + parser.add_argument("-s", "--source-script", dest="source", + help="Pass a shell script to source environment variables from", metavar="SOURCE") + args = parser.parse_args() + + print(bcolors.OKBLUE) + + print("API Smoke test\n") + for key in ["API_HOST_NAME", "SERVICE_ID", "EMAIL_TEMPLATE_ID", "SMS_TEMPLATE_ID", "EMAIL_TO", "SMS_TO"]: + print(f"{key:>17}: {Config.__dict__[key]}") + print("") + + if args.maxruns != None: + print(f"Running smoke tests in a loop up to {args.maxruns} times") + maxRuns = int(args.maxruns) + else: + print(f"Running smoke tests in a loop indefinitely") + + os.chdir('../') + + if args.source != None: + print(f"Sourcing environment variables from external shell script {args.source}") + subprocess.run(["source", args.source],executable='/bin/bash') + + print(bcolors.ENDC) + +init() + + +while True: + + result = None + seconds = None + startTime = None + endTime = None + + try: + runCount +=1 + print(bcolors.OKBLUE) + print(f"Running smoke test #{str(runCount)}") + print(bcolors.ENDC) + startTime = time.time() + + for notification_type in [Notification_type.EMAIL, Notification_type.SMS]: + test_admin_one_off(notification_type) + test_admin_csv(notification_type) + test_api_one_off(notification_type) + test_api_bulk(notification_type) + test_api_one_off(Notification_type.EMAIL, Attachment_type.ATTACHED) + test_api_one_off(Notification_type.EMAIL, Attachment_type.LINK) + + print(subprocess.STDOUT) + endTime = time.time() + totalTime = endTime - startTime + print(bcolors.OKBLUE) + print(f"Smoke Test {str(runCount)} complete in {totalTime} seconds") + print(bcolors.ENDC) + if maxRuns != 0: + if maxRuns >= runCount: + print(bcolors.WARNING) + print(f"Run limit reached. Stopping") + print(bcolors.ENDC) + break + + except: + + print(bcolors.FAIL) + print(f"Smoke Test run {str(runCount)} failed") + print(bcolors.ENDC) + break From d6f6d05d7201cf5c83bfcd5faba1c50bd0a56dbd Mon Sep 17 00:00:00 2001 From: Ben Larabie Date: Fri, 24 Nov 2023 14:53:27 -0500 Subject: [PATCH 2/4] formatting --- .../notification_dao/test_notification_dao.py | 5 +- tests_smoke/smoke_loop.py | 89 +++++++++---------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 145915037e..9df1413cb6 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -1787,10 +1787,7 @@ def test_send_method_stats_by_service(sample_service, sample_organisation): assert NotificationHistory.query.count() == 5 - assert send_method_stats_by_service( - datetime.utcnow() - timedelta(days=7), - datetime.utcnow(), - ) == [ + assert send_method_stats_by_service(datetime.utcnow() - timedelta(days=7), datetime.utcnow(),) == [ ( sample_service.id, sample_service.name, diff --git a/tests_smoke/smoke_loop.py b/tests_smoke/smoke_loop.py index 97526e9a1d..fddfe156be 100644 --- a/tests_smoke/smoke_loop.py +++ b/tests_smoke/smoke_loop.py @@ -1,7 +1,8 @@ -import subprocess import os +import subprocess import time from argparse import ArgumentParser + from smoke.common import Attachment_type, Config, Notification_type # type: ignore from smoke.test_admin_csv import test_admin_csv # type: ignore from smoke.test_admin_one_off import test_admin_one_off # type: ignore @@ -11,7 +12,9 @@ runCount = 0 maxRuns = 0 + class bcolors: + HEADER = '\033[95m' OKBLUE = '\033[94m' OKCYAN = '\033[96m' @@ -22,79 +25,73 @@ class bcolors: BOLD = '\033[1m' UNDERLINE = '\033[4m' + def init(): - global maxRuns + global maxRuns parser = ArgumentParser() parser.add_argument("-m", "--max-runs", dest="maxruns", help="Set the maximum number of runs you want", metavar="MAXRUNS") parser.add_argument("-s", "--source-script", dest="source", - help="Pass a shell script to source environment variables from", metavar="SOURCE") + help="Pass a shell script to source environment variables from", metavar="SOURCE") args = parser.parse_args() print(bcolors.OKBLUE) - + print("API Smoke test\n") for key in ["API_HOST_NAME", "SERVICE_ID", "EMAIL_TEMPLATE_ID", "SMS_TEMPLATE_ID", "EMAIL_TO", "SMS_TO"]: print(f"{key:>17}: {Config.__dict__[key]}") - print("") + print("") - if args.maxruns != None: + if args.maxruns is not None: print(f"Running smoke tests in a loop up to {args.maxruns} times") maxRuns = int(args.maxruns) else: - print(f"Running smoke tests in a loop indefinitely") + print("Running smoke tests in a loop indefinitely") os.chdir('../') - if args.source != None: + if args.source is not None: print(f"Sourcing environment variables from external shell script {args.source}") - subprocess.run(["source", args.source],executable='/bin/bash') - - print(bcolors.ENDC) + subprocess.run(["source", args.source], executable='/bin/bash') + + print(bcolors.ENDC) + init() while True: - + result = None seconds = None startTime = None endTime = None - try: - runCount +=1 - print(bcolors.OKBLUE) - print(f"Running smoke test #{str(runCount)}") - print(bcolors.ENDC) - startTime = time.time() - - for notification_type in [Notification_type.EMAIL, Notification_type.SMS]: - test_admin_one_off(notification_type) - test_admin_csv(notification_type) - test_api_one_off(notification_type) - test_api_bulk(notification_type) - test_api_one_off(Notification_type.EMAIL, Attachment_type.ATTACHED) - test_api_one_off(Notification_type.EMAIL, Attachment_type.LINK) - - print(subprocess.STDOUT) - endTime = time.time() - totalTime = endTime - startTime - print(bcolors.OKBLUE) - print(f"Smoke Test {str(runCount)} complete in {totalTime} seconds") - print(bcolors.ENDC) - if maxRuns != 0: - if maxRuns >= runCount: - print(bcolors.WARNING) - print(f"Run limit reached. Stopping") - print(bcolors.ENDC) - break - - except: - - print(bcolors.FAIL) - print(f"Smoke Test run {str(runCount)} failed") - print(bcolors.ENDC) - break + runCount += 1 + print(bcolors.OKBLUE) + print(f"Running smoke test #{str(runCount)}") + print(bcolors.ENDC) + startTime = time.time() + + for notification_type in [Notification_type.EMAIL, Notification_type.SMS]: + test_admin_one_off(notification_type) + test_admin_csv(notification_type) + test_api_one_off(notification_type) + test_api_bulk(notification_type) + test_api_one_off(Notification_type.EMAIL, Attachment_type.ATTACHED) + test_api_one_off(Notification_type.EMAIL, Attachment_type.LINK) + + print(subprocess.STDOUT) + endTime = time.time() + totalTime = endTime - startTime + print(bcolors.OKBLUE) + print(f"Smoke Test {str(runCount)} complete in {totalTime} seconds") + print(bcolors.ENDC) + if maxRuns != 0: + if maxRuns >= runCount: + print(bcolors.WARNING) + print("Run limit reached. Stopping") + print(bcolors.ENDC) + break From c745b0eb2849db1065afedeae0df4d5a592c3e64 Mon Sep 17 00:00:00 2001 From: Ben Larabie Date: Fri, 24 Nov 2023 15:01:18 -0500 Subject: [PATCH 3/4] formatting? --- tests/app/dao/notification_dao/test_notification_dao.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 9df1413cb6..145915037e 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -1787,7 +1787,10 @@ def test_send_method_stats_by_service(sample_service, sample_organisation): assert NotificationHistory.query.count() == 5 - assert send_method_stats_by_service(datetime.utcnow() - timedelta(days=7), datetime.utcnow(),) == [ + assert send_method_stats_by_service( + datetime.utcnow() - timedelta(days=7), + datetime.utcnow(), + ) == [ ( sample_service.id, sample_service.name, From 59d81bc5d668a45835c41f332564a8293cb44721 Mon Sep 17 00:00:00 2001 From: Ben Larabie Date: Mon, 27 Nov 2023 13:58:54 -0500 Subject: [PATCH 4/4] Working on python best practices --- tests_smoke/smoke_loop.py | 79 +++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/tests_smoke/smoke_loop.py b/tests_smoke/smoke_loop.py index fddfe156be..8158ac06ad 100644 --- a/tests_smoke/smoke_loop.py +++ b/tests_smoke/smoke_loop.py @@ -9,10 +9,6 @@ from smoke.test_api_bulk import test_api_bulk # type: ignore from smoke.test_api_one_off import test_api_one_off # type: ignore -runCount = 0 -maxRuns = 0 - - class bcolors: HEADER = '\033[95m' @@ -25,11 +21,11 @@ class bcolors: BOLD = '\033[1m' UNDERLINE = '\033[4m' +def main(): -def init(): - - global maxRuns - + runCount = 0 + maxRuns = 0 + parser = ArgumentParser() parser.add_argument("-m", "--max-runs", dest="maxruns", help="Set the maximum number of runs you want", metavar="MAXRUNS") @@ -58,40 +54,35 @@ def init(): print(bcolors.ENDC) + while True: + + runCount += 1 + print(bcolors.OKBLUE) + print(f"Running smoke test #{str(runCount)}") + print(bcolors.ENDC) + startTime = time.time() + + for notification_type in [Notification_type.EMAIL, Notification_type.SMS]: + test_admin_one_off(notification_type) + test_admin_csv(notification_type) + test_api_one_off(notification_type) + test_api_bulk(notification_type) + test_api_one_off(Notification_type.EMAIL, Attachment_type.ATTACHED) + test_api_one_off(Notification_type.EMAIL, Attachment_type.LINK) + + print(subprocess.STDOUT) + endTime = time.time() + totalTime = endTime - startTime + print(bcolors.OKBLUE) + print(f"Smoke Test {str(runCount)} complete in {totalTime} seconds") + print(bcolors.ENDC) + if maxRuns != 0: + if maxRuns >= runCount: + print(bcolors.WARNING) + print("Run limit reached. Stopping") + print(bcolors.ENDC) + break + +if __name__ == "__main__": + main() -init() - - -while True: - - result = None - seconds = None - startTime = None - endTime = None - - runCount += 1 - print(bcolors.OKBLUE) - print(f"Running smoke test #{str(runCount)}") - print(bcolors.ENDC) - startTime = time.time() - - for notification_type in [Notification_type.EMAIL, Notification_type.SMS]: - test_admin_one_off(notification_type) - test_admin_csv(notification_type) - test_api_one_off(notification_type) - test_api_bulk(notification_type) - test_api_one_off(Notification_type.EMAIL, Attachment_type.ATTACHED) - test_api_one_off(Notification_type.EMAIL, Attachment_type.LINK) - - print(subprocess.STDOUT) - endTime = time.time() - totalTime = endTime - startTime - print(bcolors.OKBLUE) - print(f"Smoke Test {str(runCount)} complete in {totalTime} seconds") - print(bcolors.ENDC) - if maxRuns != 0: - if maxRuns >= runCount: - print(bcolors.WARNING) - print("Run limit reached. Stopping") - print(bcolors.ENDC) - break