From 6b0aac1855e31a7ff22d59042286ea349c97b0be Mon Sep 17 00:00:00 2001
From: Emmanuel Ormancey <emmanuel.ormancey@cern.ch>
Date: Fri, 8 Oct 2021 14:38:13 +0200
Subject: [PATCH] add groups

---
 Python/stress-testing/channel.py        | 16 ++++-----
 Python/stress-testing/config.py         | 11 ++++--
 Python/stress-testing/notification.py   |  2 +-
 Python/stress-testing/stress_testing.py | 46 +++++++++++++++++++------
 4 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/Python/stress-testing/channel.py b/Python/stress-testing/channel.py
index 2c96478..273f613 100644
--- a/Python/stress-testing/channel.py
+++ b/Python/stress-testing/channel.py
@@ -19,7 +19,7 @@ def create_channel(name, admingroup, description):
         #'incomingEgroup': egroup + '@cern.ch',
     }}
     #print(data)
-    r = requests.post(Config.BACKEND_URL + '/channels/', json=data, headers=Config.HEADER, verify=Config.VERIFY)
+    r = requests.post(Config.BACKEND_URL + '/channels/', json=data, headers=Config.HEADER(), verify=Config.VERIFY)
     if r.status_code != requests.codes.ok:
         print('error creating channel', r.json())
         sys.exit(2)
@@ -31,7 +31,7 @@ def create_channel(name, admingroup, description):
 # Delete Channel
 def delete_channel(channel_id):
     print('Deleting Channel', channel_id)
-    r = requests.delete(Config.BACKEND_URL + '/channels/' + channel_id, headers=Config.HEADER, verify=Config.VERIFY)
+    r = requests.delete(Config.BACKEND_URL + '/channels/' + channel_id, headers=Config.HEADER(), verify=Config.VERIFY)
     if r.status_code != requests.codes.ok:
         print('error deleting channel', r.json())
         sys.exit(2)
@@ -41,7 +41,7 @@ def delete_channel(channel_id):
 def add_user_to_channel(channel_id, username):
     print('Adding user to Channel members', username)
     data = { 'username': username }
-    r = requests.put(Config.BACKEND_URL + '/channels/' + channel_id + '/members', json=data, headers=Config.HEADER, verify=Config.VERIFY)
+    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())
         sys.exit(2)
@@ -53,7 +53,7 @@ def add_user_to_channel(channel_id, username):
 def add_group_to_channel(channel_id, group):
     print('Adding group to Channel members', group)
     data = { 'group': { 'groupIdentifier': group } }
-    r = requests.put(Config.BACKEND_URL + '/channels/' + channel_id + '/groups', json=data, headers=Config.HEADER, verify=Config.VERIFY)
+    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())
         sys.exit(2)
@@ -64,7 +64,7 @@ def add_group_to_channel(channel_id, group):
 # Remove ME from Members
 def remove_me_from_channel(channel_id):
     print('Removing ME from Channel members')
-    r = requests.get(Config.BACKEND_URL + '/me', headers=Config.HEADER, verify=Config.VERIFY)
+    r = requests.get(Config.BACKEND_URL + '/me', headers=Config.HEADER(), verify=Config.VERIFY)
     if r.status_code != requests.codes.ok:
         print('error removing ME from channel', r.json())
         sys.exit(2)
@@ -74,7 +74,7 @@ def remove_me_from_channel(channel_id):
         sys.exit(2)
 
     data = { 'userId': me['userId'] }
-    r = requests.delete(Config.BACKEND_URL + '/channels/' + channel_id + '/members', json=data, headers=Config.HEADER, verify=Config.VERIFY)
+    r = requests.delete(Config.BACKEND_URL + '/channels/' + channel_id + '/members', json=data, headers=Config.HEADER(), verify=Config.VERIFY)
     if r.status_code != requests.codes.ok:
         print('error removing ME from channel members', r.json())
         sys.exit(2)
@@ -86,7 +86,7 @@ def remove_me_from_channel(channel_id):
 def set_channel_owner(channel_id, username):
     print('Setting Channel owner to', username)
     data = { 'username': username }
-    r = requests.put(Config.BACKEND_URL + '/channels/' + channel_id + '/owner', json=data, headers=Config.HEADER, verify=Config.VERIFY)
+    r = requests.put(Config.BACKEND_URL + '/channels/' + channel_id + '/owner', json=data, headers=Config.HEADER(), verify=Config.VERIFY)
     if r.status_code != requests.codes.ok:
         print('error setting channel owner', r.json())
         sys.exit(2)
@@ -97,7 +97,7 @@ def set_channel_owner(channel_id, username):
 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)
+    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)
diff --git a/Python/stress-testing/config.py b/Python/stress-testing/config.py
index 2b114c2..f97b653 100644
--- a/Python/stress-testing/config.py
+++ b/Python/stress-testing/config.py
@@ -15,11 +15,15 @@ class Config:
         Config.ACCESS_TOKEN=get_api_token()
         return
 
+    @staticmethod
+    def HEADER():
+        return {"Authorization": "Bearer " + Config.ACCESS_TOKEN}
+
     # 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 = get_api_token()
-    HEADER = {"Authorization": "Bearer " + ACCESS_TOKEN}
+    ACCESS_TOKEN = '' #get_api_token()
+    #HEADER = {"Authorization": "Bearer " + ACCESS_TOKEN}
     VERIFY = False  # Verify SSL certificate for requests
 
     CHANNEL_NAME = "Stress Test Channel "
@@ -29,7 +33,10 @@ class Config:
         "<p><h3>Stress Test Notification</h3>This is a stress test notification</p>"
     )
 
+    # 50 notiftestxx users were created
     NOTIFTEST_USERS = ['notiftest' + f"{i:02d}" for i in range(1, 51)]
+    # 5 Grappa groups created, containing 10 notiftestxx users each
+    NOTIFTEST_GROUPS = ['group-notiftest' + f"{i:02d}" for i in range(1, 6)]
 
     PROBE_USERS = [
         "probe000@cern.ch",
diff --git a/Python/stress-testing/notification.py b/Python/stress-testing/notification.py
index c3d2342..d36e628 100644
--- a/Python/stress-testing/notification.py
+++ b/Python/stress-testing/notification.py
@@ -15,7 +15,7 @@ def send_notification(channel_id, summary):
         'target': channel_id,
     }}
     #print(data)
-    r = requests.post(Config.BACKEND_URL + '/notifications', json=data, headers=Config.HEADER, verify=Config.VERIFY)
+    r = requests.post(Config.BACKEND_URL + '/notifications', json=data, headers=Config.HEADER(), verify=Config.VERIFY)
     if r.status_code != requests.codes.ok:
         print('error sending notification', r.json())
         sys.exit(2)
diff --git a/Python/stress-testing/stress_testing.py b/Python/stress-testing/stress_testing.py
index deee55a..6d89e1a 100644
--- a/Python/stress-testing/stress_testing.py
+++ b/Python/stress-testing/stress_testing.py
@@ -44,10 +44,15 @@ def main(argv):
             countnotifications = int(arg)
         elif opt in ("-u", "--users"):
             countusers = int(arg)
+            if countusers > len(Config.NOTIFTEST_USERS):
+                countusers = len(Config.NOTIFTEST_USERS)
         elif opt in ("-d", "--delete"):
             delete_test_channels = True
 
     print("Stress testing Notifications", Config.BACKEND_URL)
+    
+    # Aquire token
+    Config.renew()
 
     print("Retrieving existing stress test channels if any")
     channels_reloaded = get_channels_by_prefix(Config.CHANNEL_NAME)
@@ -75,12 +80,6 @@ def main(argv):
         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
@@ -88,12 +87,37 @@ def main(argv):
         else:
             print("Error creating channel ", channelname)
 
+    # Add users to channel
+    if countusers > 0:
+        cnt = 0
+        for channel_id in channel_ids:
+            # Renew token
+            if (cnt % 1000) == 999:
+                Config.renew()
+            # Add one grappa gourp as member
+            try:
+                add_group_to_channel(channel_id, Config.NOTIFTEST_GROUPS[cnt % len(Config.NOTIFTEST_GROUPS)])
+                # Add notiftest users as member, except the ones in the group added above
+                for ucpt in range(0, countusers):
+                    if (int(ucpt / 10)) != (cnt % len(Config.NOTIFTEST_GROUPS)): 
+                        add_user_to_channel(channel_id, Config.NOTIFTEST_USERS[ucpt])
+            except:
+                print('error adding, skipping.')
+            # Add probeXXX users as member
+            #for username in Config.PROBE_USERS:
+            #   add_user_to_channel(channel_id, username)
+            cnt = cnt+1
+
     # Send Notifications
-    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))    
+    if countnotifications > 0:
+        cnt = 0
+        for channel_id in channel_ids:
+            # Renew token
+            if (cnt % 1000) == 999:
+                Config.renew()
+            for i in range(0, countnotifications):
+                send_notification(channel_id, Config.NOTIFICATION_SUMMARY + str(i)) 
+            cnt = cnt+1
 
     # 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.')
-- 
GitLab