Add assert statements in code contributed 43/7643/7
authorsantanude <santanu.de@xoriant.com>
Fri, 4 Feb 2022 19:06:14 +0000 (00:36 +0530)
committerMahesh Jethanandani <mjethanandani@gmail.com>
Tue, 8 Feb 2022 17:42:14 +0000 (17:42 +0000)
SMO-32

Signed-off-by: santanude <santanu.de@xoriant.com>
Change-Id: I7c708ad56d02613dd6028b94be374a78b1d98c2c
Signed-off-by: santanude <santanu.de@xoriant.com>
collector/evel-test-collector/code/collector/monitor.py
dmaapadapter/adapter/code/app_config.py
influxdb-connector/influxdb-connector/code/influxdb_connector.py

index 793ec20..62561e6 100755 (executable)
@@ -148,7 +148,9 @@ def listener(environ, start_response, schema):
                      'Against schema: {1}'.format(body, schema))
         try:
             decoded_body = json.loads(body)
-            jsonschema.validate(decoded_body, schema)
+            validate = jsonschema.validate(decoded_body, schema)
+            assert (validate is None), "Invalid event!"
+
             logger.info('Event is valid!')
             logger.info('Valid body decoded & checked against schema OK:\n'
                         '{0}'.format(json.dumps(decoded_body,
@@ -553,6 +555,7 @@ USAGE
         api_version = args.api_version
         config_file = args.config
         config_section = args.section
+        assert (config_file != ""), "Config file is missing"
 
         # ----------------------------------------------------------------------
         # Now read the config file, using command-line supplied values as
@@ -643,6 +646,11 @@ USAGE
         logger.addHandler(handler)
         logger.info('Started')
 
+
+        assert (log_file != ""), "Value of property 'log_file' is missing in config file"
+        assert (vel_schema_file != ""), "Value of property 'schema_file' is missing in config file"
+        assert (kafka_server != ""), "Value of property 'kafka_server' is missing in config file"
+
         # ---------------------------------------------------------------------
         # Log the details of the configuration.
         # ---------------------------------------------------------------------
index c2befe8..f64ab0b 100644 (file)
@@ -38,6 +38,7 @@ class AppConfig:
         args = parser.parse_args()
         config_file = args.config
         config_section = args.section
+        assert (config_file != ""), "Config file is missing"
 
         overrides = {}
         config = configparser.ConfigParser()
@@ -47,6 +48,10 @@ class AppConfig:
         log_file = config.get(config_section, 'log_file', vars=overrides)
         log_level = config.get(config_section, 'log_level', vars=overrides)
 
+        assert (self.kafka_broker != ""), "Value of property 'kafka_broker' is missing in config file"
+        assert (log_file != ""), "Value of property 'log_file' is missing in config file"
+        assert (log_level != ""), "Value of property 'log_level' is missing in config file"
+
         self.setLogger(log_file, log_level)
 
     def getKafkaBroker(self):
index d998824..2cb67a8 100644 (file)
@@ -37,8 +37,8 @@ def send_to_influxdb(event, pdata):
     logger.debug('Send {} to influxdb at {}: {}'.format(event, influxdb, pdata))
     r = requests.post(url, data=pdata, headers={'Content-Type': 'text/plain'})
     logger.info('influxdb return code {}'.format(r.status_code))
-    if r.status_code != 204:
-         logger.debug('*** Influxdb save failed, return code {} ***'.format(r.status_code))
+    assert (r.status_code == 204), logger.debug('*** Influxdb save failed, return code {} ***'.format(r.status_code))
+    
 
 def process_additional_measurements(val, domain, eventId, startEpochMicrosec, lastEpochMicrosec):
     for additionalMeasurements in val:
@@ -219,6 +219,10 @@ def save_event_in_db(body):
     domain = jobj['event']['commonEventHeader']['domain']
     eventTimestamp = jobj['event']['commonEventHeader']['startEpochMicrosec']
     agent = jobj['event']['commonEventHeader']['reportingEntityName'].upper()
+
+    assert (domain != ""), "'domain' in payload is empty"
+    assert(eventTimestamp != ""), "'eventTimestamp' in payload is empty"
+
     if "LOCALHOST" in agent:
         agent = "computehost"
         source = jobj['event']['commonEventHeader']['sourceId'].upper()
@@ -318,6 +322,7 @@ def main():
     verbose = args.verbose
     config_section = args.section
 
+    assert (config_file != ""), "Config file is missing"
     # ----------------------------------------------------------------------
     # Now read the config file, using command-line supplied values as
     # overrides.
@@ -338,8 +343,11 @@ def main():
 
     influxdb = config.get(config_section, 'influxdb', vars=overrides)
     log_file = config.get(config_section, 'log_file', vars=overrides)
-    kafka_server=config.get(config_section,'kafka_server',
-                                   vars=overrides)
+    kafka_server = config.get(config_section,'kafka_server', vars=overrides)
+
+    assert (influxdb != ""), "Value of property 'influxdb' is missing in config file"
+    assert (log_file != ""), "Value of property 'log_file' is missing in config file"
+    assert (kafka_server != ""), "Value of property 'kafka_server' is missing in config file"
 
     # ----------------------------------------------------------------------
     # Finally we have enough info to start a proper flow trace.
@@ -397,6 +405,7 @@ def main():
             if msg is None:
                 continue
             elif not msg.error():
+                assert (msg.value() is not None), "Invalid message"
                 logger.debug('Recived message from topic name {} and offset number {}'.format(msg.topic(), msg.offset()))
                 # saving data in influxdb
                 save_event_in_db(msg.value())