diff --git a/Python/stress-testing/channel.py b/Python/stress-testing/channel.py index 4e51ef2879436347fbd260ba0337ec87f080b48a..733156d4a38da716a434049ec8f9c11f5de258c8 100644 --- a/Python/stress-testing/channel.py +++ b/Python/stress-testing/channel.py @@ -43,8 +43,11 @@ def add_user_to_channel(channel_id, username): data = { 'username': username } r = requests.put(Config.BACKEND_URL + '/channels/' + channel_id + '/members', json=data, headers=Config.HEADER(), verify=Config.VERIFY) if r.status_code != requests.codes.ok: - #print('error updating channel', r.json()) - raise FileExistsError("add_user_to_channel", r.json()) + if r.json()["name"] == 'ForbiddenError': + raise FileExistsError("add_user_to_channel", r.json()) + else: + print('error updating channel', r.json()["name"]) + sys.exit(2) updated_channel = r.json() return updated_channel['id'] @@ -55,8 +58,11 @@ def add_group_to_channel(channel_id, group): data = { 'group': { 'groupIdentifier': group } } r = requests.put(Config.BACKEND_URL + '/channels/' + channel_id + '/groups', json=data, headers=Config.HEADER(), verify=Config.VERIFY) if r.status_code != requests.codes.ok: - #print('error updating channel', r.json()) - raise FileExistsError("add_group_to_channel", r.json()) + if r.json()["name"] == 'ForbiddenError': + raise FileExistsError("add_group_to_channel", r.json()) + else: + print('error updating channel', r.json()["name"]) + sys.exit(2) updated_channel = r.json() return updated_channel['id'] diff --git a/Python/stress-testing/config.py b/Python/stress-testing/config.py index 4dddec62f29f5b5d3471df300421ddf1cc5e0975..c67db0939d5162f62b3b804de681ae6f16efae29 100644 --- a/Python/stress-testing/config.py +++ b/Python/stress-testing/config.py @@ -1,5 +1,6 @@ #from get_api_token import get_api_token import subprocess +import datetime # Comment this out on a kerberize machine and use import above def get_api_token(): @@ -9,10 +10,15 @@ def get_api_token(): class Config: """App configuration.""" + ACCESS_TOKEN_TIME=datetime.datetime.min @staticmethod def renew(): - Config.ACCESS_TOKEN=get_api_token() + time_delta = (datetime.datetime.now() - Config.ACCESS_TOKEN_TIME) + if (time_delta.total_seconds() / 60) > 8: + print("Bearer token older than 8 minutes, renewing...") + Config.ACCESS_TOKEN=get_api_token() + Config.ACCESS_TOKEN_TIME = datetime.datetime.now() return @staticmethod diff --git a/Python/stress-testing/stress_testing.py b/Python/stress-testing/stress_testing.py index b53d611a64005ceff3e83d111fabd779b57c7028..d7ad33e69ac0336aa9f1d282e6e57428ebdf37e4 100644 --- a/Python/stress-testing/stress_testing.py +++ b/Python/stress-testing/stress_testing.py @@ -19,6 +19,8 @@ def usage(): 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-s <start at count users> : continuating add of test users per channel, starting at count") + print("\t-t <start at count channels> : continuating add of test users at channel starting at count") print("\t-d : find and delete all previously created test Channels prefixed", Config.CHANNEL_NAME) @@ -27,10 +29,12 @@ def main(argv): countchannels = 0 countnotifications = 0 countusers = len(Config.NOTIFTEST_USERS) + startatcountusers = 0 + startatcountchannels = 0 adminGroup = Config.ADMIN_GROUP delete_test_channels = False try: - opts, args = getopt.getopt(argv, "hdlc:n:u:", ["channels=", "notifications=", "users="]) + opts, args = getopt.getopt(argv, "hdlc:n:u:s:t:", ["channels=", "notifications=", "users="]) except getopt.GetoptError: usage() sys.exit(2) @@ -46,6 +50,14 @@ def main(argv): countusers = int(arg) if countusers > (len(Config.NOTIFTEST_USERS) + len(Config.UNVERIFIED_USERS)): countusers = (len(Config.NOTIFTEST_USERS) + len(Config.UNVERIFIED_USERS)) + elif opt in ("-s", "--startatcountusers"): + startatcountusers = int(arg) + if startatcountusers > countusers: + startatcountusers = countusers-1 + elif opt in ("-t", "--startatcountchannels"): + startatcountchannels = int(arg) + if startatcountchannels > countchannels: + startatcountchannels = countchannels-1 elif opt in ("-d", "--delete"): delete_test_channels = True @@ -89,29 +101,32 @@ def main(argv): # Add users to channel if countusers > 0: - cnt = 0 - for channel_id in channel_ids: + for ucnl in range(startatcountchannels, len(channel_ids)): + channel_id = channel_ids[ucnl] print("Processing channel", channel_id) # Renew token - if (cnt % 100) == 99: + if (ucnl % 100) == 99: Config.renew() - if (cnt % 100) == 99: - print("================= Processed", cnt, "channels =================") + if (ucnl % 100) == 99: + print("================= Processed", ucnl, "channels =================") # Add one grappa gourp as member try: - add_group_to_channel(channel_id, Config.NOTIFTEST_GROUPS[cnt % len(Config.NOTIFTEST_GROUPS)]) + add_group_to_channel(channel_id, Config.NOTIFTEST_GROUPS[ucnl % len(Config.NOTIFTEST_GROUPS)]) except FileExistsError as ex: print("\tGroup already exists") pass # Add notiftest users as member, except the ones in the group added above - for ucpt in range(0, countusers): + for ucpt in range(startatcountusers, countusers): try: if ucpt < len(Config.NOTIFTEST_USERS): - if (int(ucpt / 10)) != (cnt % len(Config.NOTIFTEST_GROUPS)): + if (int(ucpt / 10)) != (ucnl % len(Config.NOTIFTEST_GROUPS)): add_user_to_channel(channel_id, Config.NOTIFTEST_USERS[ucpt]) else: add_user_to_channel(channel_id, Config.UNVERIFIED_USERS[ucpt - len(Config.NOTIFTEST_USERS)]) + # Renew token + if (ucpt % 100) == 99: + Config.renew() except FileExistsError as ex: print("\tUser already exists") pass @@ -119,7 +134,6 @@ def main(argv): # Add probeXXX users as member #for username in Config.PROBE_USERS: # add_user_to_channel(channel_id, username) - cnt = cnt+1 # Send Notifications if countnotifications > 0: