diff --git a/Python/stress-testing/config.py b/Python/stress-testing/config.py index 2cf68ded6b2cc77906d25182339735550eae62c9..2b114c2d6ec5a5b6db1b1268a18220f316a7759e 100644 --- a/Python/stress-testing/config.py +++ b/Python/stress-testing/config.py @@ -1,18 +1,24 @@ -from get_api_token import get_api_token +#from get_api_token import get_api_token +import subprocess -_ACCESS_TOKEN=get_api_token() +# Comment this out on a kerberize machine and use import above +def get_api_token(): + print("Requesting a new token via ssh") + result = subprocess.run(['sshpass -f ~/password ssh ormancey@lxplus.cern.ch "python notificationApiClient/get-api-token.py"'], stdout=subprocess.PIPE, shell=True) + return result.stdout.decode().replace("\n", "") class Config: """App configuration.""" @staticmethod def renew(): - _ACCESS_TOKEN=get_api_token() + Config.ACCESS_TOKEN=get_api_token() return # BACKEND_URL='https://api-notifications-dev.app.cern.ch' + # Drop SSL warnings with: export PYTHONWARNINGS="ignore:Unverified HTTPS request" BACKEND_URL = "https://localhost:8080" - ACCESS_TOKEN = _ACCESS_TOKEN + ACCESS_TOKEN = get_api_token() HEADER = {"Authorization": "Bearer " + ACCESS_TOKEN} VERIFY = False # Verify SSL certificate for requests diff --git a/Python/stress-testing/stress_testing.py b/Python/stress-testing/stress_testing.py index d1b5e89dc6897a66aed40a6f7022cd1c0c547103..deee55aa614493bfdd259c221633b5c757745f9d 100644 --- a/Python/stress-testing/stress_testing.py +++ b/Python/stress-testing/stress_testing.py @@ -15,11 +15,10 @@ from channel import ( from notification import send_notification def usage(): - print("stress_testing.py -c <count channels> [-n <count notifications> -u <count users>-l -d]") + print("stress_testing.py -c <count channels> [-n <count notifications> -u <count users> -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) @@ -29,7 +28,7 @@ def main(argv): countnotifications = 0 countusers = len(Config.NOTIFTEST_USERS) adminGroup = Config.ADMIN_GROUP - reload_channel_ids = False + delete_test_channels = False try: opts, args = getopt.getopt(argv, "hdlc:n:u:", ["channels=", "notifications=", "users="]) except getopt.GetoptError: @@ -45,47 +44,49 @@ def main(argv): 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() + delete_test_channels = True print("Stress testing Notifications", Config.BACKEND_URL) - # Create Channels - print("Creating ", countchannels, " test channels") - - 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) + print("Retrieving existing stress test channels if any") + channels_reloaded = get_channels_by_prefix(Config.CHANNEL_NAME) + if channels_reloaded: + channel_ids = [channel["id"] for channel in channels_reloaded] + print("\tfound " + str(len(channel_ids)) + " test channels, continuing...") 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) + # Delete all test channels + if delete_test_channels is True: + print("Deleting all test channels") + for channel_id in channel_ids: + delete_channel(channel_id) + sys.exit() + + # Create Channels + print("Creating ", countchannels, " test channels") + + for i in range(len(channel_ids), countchannels): + # Renew token + if (i % 1000) == 999: + Config.renew() + 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) # Send Notifications for channel_id in channel_ids: @@ -95,15 +96,13 @@ def main(argv): 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 remove all test channels and exit, or CTRL+C to exit directly...") - + #print('Please wait for notifications to be delivered, and check the system is stable before continuing.') + #text = input("Press Enter to remove all test channels and exit, or CTRL+C to exit directly...") # Renew token - Config.renew() - + #Config.renew() # Cleanup by deleting channels - for channel_id in channel_ids: - delete_channel(channel_id) + #for channel_id in channel_ids: + # delete_channel(channel_id) if __name__ == "__main__":