diff --git a/Python/stress-testing/channel.py b/Python/stress-testing/channel.py index bdfaf8a748cdda5a43fcfeb24d66fa0500ea7369..2c9647888cada2693b9a1b889b4e3bccc92d58dd 100644 --- a/Python/stress-testing/channel.py +++ b/Python/stress-testing/channel.py @@ -92,4 +92,16 @@ def set_channel_owner(channel_id, username): sys.exit(2) updated_channel = r.json() - return updated_channel['id'] \ No newline at end of file + return updated_channel['id'] + +def get_channels_by_prefix(prefix): + print('Get Channel By Prefix:', prefix) + data = { 'searchText': prefix, 'skip': 0, 'take': 10000 } + r = requests.get(Config.BACKEND_URL + '/channels', params=data, headers=Config.HEADER, verify=Config.VERIFY) + if r.status_code != requests.codes.ok: + print('error getting channels by prefix', r.json()) + sys.exit(2) + found_channels = r.json() + #print(found_channels) + #print(found_channels["channels"]) + return found_channels["channels"] diff --git a/Python/stress-testing/config.py b/Python/stress-testing/config.py index 2c25724ce10c12a150446a54086149e54afbeb89..2cf68ded6b2cc77906d25182339735550eae62c9 100644 --- a/Python/stress-testing/config.py +++ b/Python/stress-testing/config.py @@ -5,6 +5,11 @@ _ACCESS_TOKEN=get_api_token() class Config: """App configuration.""" + @staticmethod + def renew(): + _ACCESS_TOKEN=get_api_token() + return + # BACKEND_URL='https://api-notifications-dev.app.cern.ch' BACKEND_URL = "https://localhost:8080" ACCESS_TOKEN = _ACCESS_TOKEN diff --git a/Python/stress-testing/stress_testing.py b/Python/stress-testing/stress_testing.py index f4e556c59aa0516f1be2f960538869ddcf9931ea..d1b5e89dc6897a66aed40a6f7022cd1c0c547103 100644 --- a/Python/stress-testing/stress_testing.py +++ b/Python/stress-testing/stress_testing.py @@ -1,6 +1,6 @@ #!/usr/bin/python -#import json, os, re +import json, os, re import sys, getopt from config import Config from channel import ( @@ -10,22 +10,28 @@ from channel import ( set_channel_owner, add_user_to_channel, delete_channel, + get_channels_by_prefix, ) from notification import send_notification def usage(): - print("stress_testing.py -c <countchannels> -n <count notifications>") - print("\t-c <countchannels> : number of test channels to create") - print("\t-n <countnotifications> : number of test notifications to send per channel") + print("stress_testing.py -c <count channels> [-n <count notifications> -u <count users>-l -d]") + print("\t-c <count channels> : number of test channels to create") + print("\t-n <count notifications> : number of test notifications to send per channel") + print("\t-u <count users> : number of test users to add per channel, default and max =", len(Config.NOTIFTEST_USERS)) + print("\t-l : reload previously created test channels from file channel_ids.txt, will not recreate them") + print("\t-d : find and delete all previously created test Channels prefixed", Config.CHANNEL_NAME) # Main def main(argv): countchannels = 0 countnotifications = 0 + countusers = len(Config.NOTIFTEST_USERS) adminGroup = Config.ADMIN_GROUP + reload_channel_ids = False try: - opts, args = getopt.getopt(argv, "hc:n:", ["channels=", "notifications="]) + opts, args = getopt.getopt(argv, "hdlc:n:u:", ["channels=", "notifications=", "users="]) except getopt.GetoptError: usage() sys.exit(2) @@ -37,39 +43,63 @@ def main(argv): countchannels = int(arg) elif opt in ("-n", "--notifications"): countnotifications = int(arg) + elif opt in ("-u", "--users"): + countusers = int(arg) + elif opt in ("-l", "--load"): + reload_channel_ids = True + elif opt in ("-d", "--delete"): + print("Retrieving all stress test channels and deleting them") + channels = get_channels_by_prefix(Config.CHANNEL_NAME) + for channel in channels: + delete_channel(channel["id"]) + sys.exit() print("Stress testing Notifications", Config.BACKEND_URL) # Create Channels print("Creating ", countchannels, " test channels") - channel_ids = [] - for i in range(0, countchannels): - channelname = description = Config.CHANNEL_NAME + str(i) - channel_id = create_channel(channelname, adminGroup, description) - if channel_id: - channel_ids.append(channel_id) - # Add notiftest users as member - for username in Config.NOTIFTEST_USERS: - add_user_to_channel(channel_id, username) - # Add probeXXX users as member - #for username in Config.PROBE_USERS: - # add_user_to_channel(channel_id, username) - # Remove ME as member - #remove_me_from_channel(channel_id) - # Set new owner to one notiftestXX - #set_channel_owner(channel_id, Config.NOTIFTEST_USERS[i % 50]) - else: - print("Error creating channel ", channelname) + if reload_channel_ids is True: + print("Reloading channel_ids from file") + with open('channel_ids.txt') as json_file: + channel_ids = json.load(json_file) + else: + channel_ids = [] + for i in range(0, countchannels): + channelname = description = Config.CHANNEL_NAME + str(i) + channel_id = create_channel(channelname, adminGroup, description) + if channel_id: + channel_ids.append(channel_id) + # Add notiftest users as member + for ucpt in range(0, countusers): + add_user_to_channel(channel_id, Config.NOTIFTEST_USERS[ucpt]) + # Add probeXXX users as member + #for username in Config.PROBE_USERS: + # add_user_to_channel(channel_id, username) + # Remove ME as member + remove_me_from_channel(channel_id) + # Set new owner to one notiftestXX + set_channel_owner(channel_id, Config.NOTIFTEST_USERS[i % 50]) + else: + print("Error creating channel ", channelname) + + # Write IDs to file in case it breaks and we need to manually cleanup + with open('channel_ids.txt', 'w') as outfile: + json.dump(channel_ids, outfile) # Send Notifications - for i in range(0, countnotifications): - for channel_id in channel_ids: + for channel_id in channel_ids: + # Renew token + Config.renew() + for i in range(0, countnotifications): send_notification(channel_id, Config.NOTIFICATION_SUMMARY + str(i)) # Wait for checks to be done, and notifications to be delivered before exit and cleanup print('Please wait for notifications to be delivered, and check the system is stable before continuing.') - text = input("Press Enter to complete and remove all test channels...") + text = input("Press Enter to remove all test channels and exit, or CTRL+C to exit directly...") + + # Renew token + Config.renew() # Cleanup by deleting channels for channel_id in channel_ids: