Random error message when op_failed
[clamp.git] / extra / docker / elk / tools / DMaaPServiceMocker / ds_mocker.py
index 05c1b98..bd6caec 100755 (executable)
@@ -18,11 +18,11 @@ def now_notification_time():
     return datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f+00:00")
 
 CONTROL_LOOP_NAMES = [
-    'ClosedLoop-vUSP-SIG-d925ed73-8231-4d02-9545-db4e101f88f8',
-    'ClosedLoop-vUSP-SIG-37b1c91e-fd6b-4abd-af15-771902d5fdb1',
-    'ClosedLoop-vUSP-SIG-c2597657-7113-4efb-a1f9-397a7a24c3e1',
-    'ClosedLoop-vUSP-SIG-a11318ba-4c61-46b8-a9da-bd40bec11d45',
-    'ClosedLoop-vUSP-SIG-5321c558-2254-4efb-ac24-99dd54edc94f',
+    'CL-vCPE-d925ed73',
+    'CL-vCPE-37b1c91e',
+    'CL-vCPE-c2597657',
+    'CL-vCPE-a11318ba',
+    'CL-vCPE-5321c558',
 ]
 
 TEMPLATES = {
@@ -39,6 +39,13 @@ TEMPLATES = {
     'notification_rejected_missing'  :'notification_rejected_missing.json',
 }
 
+ERROR_MESSAGES = [
+    ('APPC', 'APPC1 : timeout on restart','RESTART'),
+    ('APPC', 'APPC2 : cannot restart','RESTART'),
+    ('SO', 'SO1 : scale up failed', 'SCALEUP'),
+    ('SO', 'SO2 : scale down failed', 'SCALEDOWN'),
+]
+
 for key in TEMPLATES:
     with open(TEMPLATES[key]) as f:
         content = f.read()
@@ -110,7 +117,11 @@ class Notification(DMaaPMessage):
                 return FinalNotification.from_template('notification_final_success', **kwargs)
             @staticmethod
             def failed(**kwargs):
-                return FinalNotification.from_template('notification_final_failed', **kwargs)
+                msg = FinalNotification.from_template('notification_final_failed', **kwargs)
+                error = ERROR_MESSAGES[random.randint(0, len(ERROR_MESSAGES) - 1)]
+                h = msg['history'][-1]
+                h['actor'],h['message'],h['operation'] = error[0],error[1],error[2]
+                return msg
             @staticmethod
             def open(**kwargs):
                 return FinalNotification.from_template('notification_final_open', **kwargs)
@@ -149,7 +160,7 @@ class CLStatus(object):
     def __init__(self, dmaap_url=None,
                  missing=None, disabled=None, op_failure=None):
         self._stopped = False
-        def maybe(thing):
+        def maybe(thing):
             if thing is None:
                 thing = not luck(10)
             return thing
@@ -194,19 +205,53 @@ class CLStatus(object):
             yield Notification.final().success(**config)
         raise StopIteration()
 
-test_datas = [CLStatus(missing=False, disabled=False, op_failure=False) for i in range(45)]  \
+def print_usage():
+    print("""
+    ./ds_mocker.py <DMAAP_URL> <EVENT_TOPIC> [NOTIFICATION_TOPIC [REQUEST_TOPIC]]
+    """)
+    exit()
+
+def push(test_datas):
+    for current_i, status in enumerate(test_datas):
+        time.sleep(random.randint(0,3))
+        for s in status:
+            # print(s)
+            status_code = s.publish()
+            if status_code != 200:
+                print("Error when publishing : status_code={}".format(status_code))
+                exit(1)
+            time.sleep(random.randint(0,3))
+        print("%03d,missing:%5s,disabled:%5s,op_failure:%5s - %s" % (current_i, status._missing, status._disabled, status._op_failure, status._config))
+
+
+
+def generate_dataset_1():
+    test_datas = [CLStatus(missing=False, disabled=False, op_failure=False) for i in range(300)]  \
              + [CLStatus(missing=True, disabled=False, op_failure=False) for i in range(5)]  \
              + [CLStatus(missing=False, disabled=True, op_failure=False) for i in range(6)]  \
-             + [CLStatus(missing=False, disabled=False, op_failure=True) for i in range(7)]
-random.shuffle(test_datas)
-random.shuffle(test_datas)
-
-for current_i, status in enumerate(test_datas):
-    time.sleep(random.randint(0,6))
-    for s in status:
-        status_code = s.publish()
-        if status_code != 200:
-            print("Error when publishing : status_code={}".format(status_code))
-            exit(1)
-        time.sleep(random.randint(0,3))
-    print("%03d,missing:%5s,disabled:%5s,op_failure:%5s - %s" % (current_i, status._missing, status._disabled, status._op_failure, status._config))
+             + [CLStatus(missing=False, disabled=False, op_failure=True) for i in range(12)]
+    random.shuffle(test_datas)
+    return test_datas
+
+def generate_error_dataset_1():
+    test_datas = [CLStatus(missing=False, disabled=False, op_failure=True) for i in range(60)]
+    random.shuffle(test_datas)
+    return test_datas
+
+
+DATASETS = {
+    'dataset_1': generate_dataset_1,
+    'op_failure_1': generate_error_dataset_1,
+}
+
+if __name__ == "__main__":
+    import sys
+    if len(sys.argv) < 3:
+        print_usage()
+
+    DMaaPMessage.dmaap_host_url = sys.argv[1]
+    Event.topic = sys.argv[2]
+    Notification.topic = len(sys.argv) > 3 and sys.argv[3] or sys.argv[2]
+    # Request.topic = len(sys.argv) > 4 or Notification.topic
+    #push(DATASETS['op_failure_1']())
+    push(DATASETS['dataset_1']())