Report: Configure 1901.1.13
[csit.git] / resources / tools / presentation / specification_parser.py
index 7b7d35b..b459bd3 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2017 Cisco and/or its affiliates.
+# Copyright (c) 2019 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
@@ -49,7 +49,6 @@ class Specification(object):
 
         self._specification = {"environment": dict(),
                                "configuration": dict(),
-                               "debug": dict(),
                                "static": dict(),
                                "input": dict(),
                                "output": dict(),
@@ -95,29 +94,32 @@ class Specification(object):
         return self._specification["static"]
 
     @property
-    def debug(self):
-        """Getter - debug
+    def mapping(self):
+        """Getter - Mapping.
 
-        :returns: Debug specification
+        :returns: Mapping of the old names of test cases to the new (actual)
+            one.
         :rtype: dict
         """
-        return self._specification["debug"]
+        return self._specification["configuration"]["mapping"]
 
     @property
-    def is_debug(self):
-        """Getter - debug mode
+    def ignore(self):
+        """Getter - Ignore list.
 
-        :returns: True if debug mode is on, otherwise False.
-        :rtype: bool
+        :returns: List of ignored test cases.
+        :rtype: list
         """
+        return self._specification["configuration"]["ignore"]
 
-        try:
-            if self.environment["configuration"]["CFG[DEBUG]"] == 1:
-                return True
-            else:
-                return False
-        except KeyError:
-            return False
+    @property
+    def alerting(self):
+        """Getter - Alerting.
+
+        :returns: Specification of alerts.
+        :rtype: dict
+        """
+        return self._specification["configuration"]["alerting"]
 
     @property
     def input(self):
@@ -368,7 +370,7 @@ class Specification(object):
 
         try:
             self._specification["environment"]["urls"] = \
-                self._replace_tags(self._cfg_yaml[idx]["urls"])
+                self._cfg_yaml[idx]["urls"]
         except KeyError:
             self._specification["environment"]["urls"] = None
 
@@ -390,6 +392,12 @@ class Specification(object):
         except KeyError:
             self._specification["environment"]["build-dirs"] = None
 
+        try:
+            self._specification["environment"]["testbeds"] = \
+                self._cfg_yaml[idx]["testbeds"]
+        except KeyError:
+            self._specification["environment"]["testbeds"] = None
+
         logging.info("Done.")
 
     def _parse_configuration(self):
@@ -412,6 +420,8 @@ class Specification(object):
 
         # Data sets: Replace ranges by lists
         for set_name, data_set in self.configuration["data-sets"].items():
+            if not isinstance(data_set, dict):
+                continue
             for job, builds in data_set.items():
                 if builds:
                     if isinstance(builds, dict):
@@ -423,44 +433,67 @@ class Specification(object):
                             build_nr = self._get_build_number(job, build_nr)
                         builds = [x for x in range(builds["start"], build_nr+1)]
                         self.configuration["data-sets"][set_name][job] = builds
-        logging.info("Done.")
-
-    def _parse_debug(self):
-        """Parse debug specification in the specification YAML file.
-        """
 
-        if int(self.environment["configuration"]["CFG[DEBUG]"]) != 1:
-            return None
-
-        logging.info("Parsing specification file: debug ...")
-
-        idx = self._get_type_index("debug")
-        if idx is None:
-            self.environment["configuration"]["CFG[DEBUG]"] = 0
-            return None
-
-        try:
-            for key, value in self._cfg_yaml[idx]["general"].items():
-                self._specification["debug"][key] = value
-
-            self._specification["input"]["builds"] = dict()
-            for job, builds in self._cfg_yaml[idx]["builds"].items():
-                if builds:
-                    self._specification["input"]["builds"][job] = list()
-                    for build in builds:
-                        self._specification["input"]["builds"][job].\
-                            append({"build": build["build"],
-                                    "status": "downloaded",
-                                    "file-name": self._replace_tags(
-                                        build["file"],
-                                        self.environment["paths"])})
-                else:
-                    logging.warning("No build is defined for the job '{}'. "
-                                    "Trying to continue without it.".
-                                    format(job))
+        # Data sets: add sub-sets to sets (only one level):
+        for set_name, data_set in self.configuration["data-sets"].items():
+            if isinstance(data_set, list):
+                new_set = dict()
+                for item in data_set:
+                    try:
+                        for key, val in self.configuration["data-sets"][item].\
+                                items():
+                            new_set[key] = val
+                    except KeyError:
+                        raise PresentationError(
+                            "Data set {0} is not defined in "
+                            "the configuration section.".format(item))
+                self.configuration["data-sets"][set_name] = new_set
+
+        # Mapping table:
+        mapping = None
+        mapping_file_name = self._specification["configuration"].\
+            get("mapping-file", None)
+        if mapping_file_name:
+            logging.debug("Mapping file: '{0}'".format(mapping_file_name))
+            try:
+                with open(mapping_file_name, 'r') as mfile:
+                    mapping = load(mfile)
+                logging.debug("Loaded mapping table:\n{0}".format(mapping))
+            except (YAMLError, IOError) as err:
+                raise PresentationError(
+                    msg="An error occurred while parsing the mapping file "
+                        "'{0}'.".format(mapping_file_name),
+                    details=repr(err))
+        # Make sure everything is lowercase
+        if mapping:
+            self._specification["configuration"]["mapping"] = \
+                {key.lower(): val.lower() for key, val in mapping.iteritems()}
+        else:
+            self._specification["configuration"]["mapping"] = dict()
+
+        # Ignore list:
+        ignore = None
+        ignore_list_name = self._specification["configuration"].\
+            get("ignore-list", None)
+        if ignore_list_name:
+            logging.debug("Ignore list file: '{0}'".format(ignore_list_name))
+            try:
+                with open(ignore_list_name, 'r') as ifile:
+                    ignore = load(ifile)
+                logging.debug("Loaded ignore list:\n{0}".format(ignore))
+            except (YAMLError, IOError) as err:
+                raise PresentationError(
+                    msg="An error occurred while parsing the ignore list file "
+                        "'{0}'.".format(ignore_list_name),
+                    details=repr(err))
+        # Make sure everything is lowercase
+        if ignore:
+            self._specification["configuration"]["ignore"] = \
+                [item.lower() for item in ignore]
+        else:
+            self._specification["configuration"]["ignore"] = list()
 
-        except KeyError:
-            raise PresentationError("No data to process.")
+        logging.info("Done.")
 
     def _parse_input(self):
         """Parse input specification in the specification YAML file.
@@ -489,10 +522,10 @@ class Specification(object):
                             # defined as a range <start, build_type>
                             build_nr = self._get_build_number(job, build_nr)
                         builds = [x for x in range(builds["start"], build_nr+1)]
-                        self._specification["input"]["builds"][job] = list()
-                        for build in builds:
-                            self._specification["input"]["builds"][job]. \
-                                append({"build": build, "status": None})
+                    self._specification["input"]["builds"][job] = list()
+                    for build in builds:
+                        self._specification["input"]["builds"][job]. \
+                            append({"build": build, "status": None})
 
                 else:
                     logging.warning("No build is defined for the job '{}'. "
@@ -561,6 +594,13 @@ class Specification(object):
             except KeyError:
                 pass
 
+            try:
+                element["input-file"] = self._replace_tags(
+                    element["input-file"],
+                    self._specification["environment"]["paths"])
+            except KeyError:
+                pass
+
             # add data sets to the elements:
             if isinstance(element.get("data", None), str):
                 data_set = element["data"]
@@ -579,6 +619,27 @@ class Specification(object):
                         self._specification["environment"]["paths"])
                 except KeyError:
                     pass
+
+                # add data sets
+                try:
+                    for item in ("reference", "compare"):
+                        if element.get(item, None):
+                            data_set = element[item].get("data", None)
+                            if isinstance(data_set, str):
+                                element[item]["data"] = \
+                                    self.configuration["data-sets"][data_set]
+
+                    if element.get("history", None):
+                        for i in range(len(element["history"])):
+                            data_set = element["history"][i].get("data", None)
+                            if isinstance(data_set, str):
+                                element["history"][i]["data"] = \
+                                    self.configuration["data-sets"][data_set]
+
+                except KeyError:
+                    raise PresentationError("Wrong data set used in {0}.".
+                                            format(element.get("title", "")))
+
                 self._specification["tables"].append(element)
                 count += 1
 
@@ -657,9 +718,7 @@ class Specification(object):
 
         self._parse_env()
         self._parse_configuration()
-        self._parse_debug()
-        if not self.debug:
-            self._parse_input()
+        self._parse_input()
         self._parse_output()
         self._parse_static()
         self._parse_elements()