add unittest of config.py
[releng.git] / utils / test / testapi / opnfv_testapi / common / config.py
index ecab88a..45e134f 100644 (file)
@@ -7,9 +7,8 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 # feng.xiaowei@zte.com.cn remove prepare_put_request            5-30-2016
 ##############################################################################
-
-
-from ConfigParser import SafeConfigParser, NoOptionError
+import ConfigParser
+import os
 
 
 class ParseError(Exception):
@@ -36,20 +35,21 @@ class APIConfig:
         self.mongo_dbname = None
         self.api_port = None
         self.api_debug_on = None
+        self.api_authenticate_on = None
         self._parser = None
         self.swagger_base_url = None
 
     def _get_parameter(self, section, param):
         try:
             return self._parser.get(section, param)
-        except NoOptionError:
-            raise ParseError("[%s.%s] parameter not found" % (section, param))
+        except ConfigParser.NoOptionError:
+            raise ParseError("No parameter: [%s.%s]" % (section, param))
 
     def _get_int_parameter(self, section, param):
         try:
             return int(self._get_parameter(section, param))
         except ValueError:
-            raise ParseError("[%s.%s] not an int" % (section, param))
+            raise ParseError("Not int: [%s.%s]" % (section, param))
 
     def _get_bool_parameter(self, section, param):
         result = self._get_parameter(section, param)
@@ -59,7 +59,7 @@ class APIConfig:
             return False
 
         raise ParseError(
-            "[%s.%s : %s] not a boolean" % (section, param, result))
+            "Not boolean: [%s.%s : %s]" % (section, param, result))
 
     @staticmethod
     def parse(config_location=None):
@@ -68,28 +68,21 @@ class APIConfig:
         if config_location is None:
             config_location = obj._default_config_location
 
-        obj._parser = SafeConfigParser()
-        obj._parser.read(config_location)
-        if not obj._parser:
+        if not os.path.exists(config_location):
             raise ParseError("%s not found" % config_location)
 
+        obj._parser = ConfigParser.SafeConfigParser()
+        obj._parser.read(config_location)
+
         # Linking attributes to keys from file with their sections
         obj.mongo_url = obj._get_parameter("mongo", "url")
         obj.mongo_dbname = obj._get_parameter("mongo", "dbname")
 
         obj.api_port = obj._get_int_parameter("api", "port")
         obj.api_debug_on = obj._get_bool_parameter("api", "debug")
+        obj.api_authenticate_on = obj._get_bool_parameter("api",
+                                                          "authenticate")
+
         obj.swagger_base_url = obj._get_parameter("swagger", "base_url")
 
         return obj
-
-    def __str__(self):
-        return "mongo_url = %s \n" \
-               "mongo_dbname = %s \n" \
-               "api_port = %s \n" \
-               "api_debug_on = %s \n" \
-               "swagger_base_url = %s \n" % (self.mongo_url,
-                                             self.mongo_dbname,
-                                             self.api_port,
-                                             self.api_debug_on,
-                                             self.swagger_base_url)