Merge "bugfix: /resources.json and /APIs cannot be accessed"
authorMorgan Richomme <morgan.richomme@orange.com>
Wed, 17 May 2017 06:43:34 +0000 (06:43 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Wed, 17 May 2017 06:43:34 +0000 (06:43 +0000)
16 files changed:
jjb/apex/apex-upload-artifact.sh
jjb/xci/bifrost-verify-jobs.yml
jjb/xci/bifrost-verify.sh
jjb/xci/xci-deploy.sh
prototypes/bifrost/playbooks/inventory/group_vars/baremetal [deleted file]
prototypes/xci/README.rst
prototypes/xci/config/user-vars
prototypes/xci/playbooks/configure-localhost.yml
prototypes/xci/playbooks/configure-opnfvhost.yml
prototypes/xci/playbooks/provision-vm-nodes.yml
prototypes/xci/playbooks/roles/remove-folders/tasks/main.yml
prototypes/xci/var/opnfv.yml
prototypes/xci/xci-deploy.sh
utils/test/testapi/opnfv_testapi/cmd/server.py
utils/test/testapi/opnfv_testapi/common/config.py
utils/test/testapi/opnfv_testapi/tests/unit/common/test_config.py

index 3ebb4a5..9d0b014 100755 (executable)
@@ -89,7 +89,7 @@ uploadimages () {
   export OPNFV_ARTIFACT_VERSION="dev${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}"
   echo "Uploading development build tarball"
   pushd $BUILD_DIRECTORY > /dev/null
-  tar czf apex-${OPNFV_ARTIFACT_VERSION}.tar.gz *.qcow2
+  tar czf apex-${OPNFV_ARTIFACT_VERSION}.tar.gz *.qcow2 *.vmlinuz *.initrd
   gsutil cp apex-${OPNFV_ARTIFACT_VERSION}.tar.gz gs://$GS_URL/apex-${OPNFV_ARTIFACT_VERSION}.tar.gz > gsutil.latest.log
   popd > /dev/null
 }
index 8068296..c6da9f4 100644 (file)
     project:
         - 'openstack':
             project-repo: 'https://git.openstack.org/openstack/bifrost'
-            clone-location: '/opt/bifrost'
+            clone-location: '$WORKSPACE/bifrost'
         - 'opnfv':
             project-repo: 'https://gerrit.opnfv.org/gerrit/releng'
-            clone-location: '/opt/releng'
+            clone-location: '$WORKSPACE/releng'
 #--------------------------------
 # distros
 #--------------------------------
index 18019a7..2e6f227 100755 (executable)
@@ -95,32 +95,32 @@ if [[ ! "$DISTRO" =~ (trusty|centos7|suse) ]]; then
 fi
 
 # remove previously cloned repos
-sudo /bin/rm -rf /opt/bifrost /opt/releng
+/bin/rm -rf $WORKSPACE/bifrost $WORKSPACE/releng
 
 # Fix up permissions
 fix_ownership
 
 # clone all the repos first and checkout the patch afterwards
-sudo git clone https://git.openstack.org/openstack/bifrost /opt/bifrost
-sudo git clone https://gerrit.opnfv.org/gerrit/releng /opt/releng
+git clone https://git.openstack.org/openstack/bifrost $WORKSPACE/bifrost
+git clone https://gerrit.opnfv.org/gerrit/releng $WORKSPACE/releng
 
 # checkout the patch
 cd $CLONE_LOCATION
-sudo git fetch $PROJECT_REPO $GERRIT_REFSPEC && sudo git checkout FETCH_HEAD
+git fetch $PROJECT_REPO $GERRIT_REFSPEC && sudo git checkout FETCH_HEAD
 
 # combine opnfv and upstream scripts/playbooks
-sudo /bin/cp -rf /opt/releng/prototypes/bifrost/* /opt/bifrost/
+/bin/cp -rf $WORKSPACE/releng/prototypes/bifrost/* $WORKSPACE/bifrost/
 
 # cleanup remnants of previous deployment
-cd /opt/bifrost
+cd $WORKSPACE/bifrost
 sudo -H -E ./scripts/destroy-env.sh
 
 # provision 3 VMs; xcimaster, controller, and compute
-cd /opt/bifrost
+cd $WORKSPACE/bifrost
 ./scripts/bifrost-provision.sh
 
 # list the provisioned VMs
-cd /opt/bifrost
+cd $WORKSPACE/bifrost
 source env-vars
 ironic node-list
 sudo -H -E virsh list
index b007b85..8ad6378 100755 (executable)
@@ -54,7 +54,7 @@ fi
 
 # proceed with the deployment
 cd $WORKSPACE/prototypes/xci
-sudo -E ./xci-deploy.sh
+./xci-deploy.sh
 
 if [[ "$JOB_NAME" =~ "periodic" && "$OPENSTACK_OSA_VERSION" == "master" ]]; then
     # if we arrived here without failing, it means we have something we can pin
diff --git a/prototypes/bifrost/playbooks/inventory/group_vars/baremetal b/prototypes/bifrost/playbooks/inventory/group_vars/baremetal
deleted file mode 100644 (file)
index 008b04d..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
----
-# The ironic API URL for bifrost operations.  Defaults to localhost.
-# ironic_url: "http://localhost:6385/"
-
-# The network interface that bifrost will be operating on.  Defaults
-# to virbr0 in roles, can be overridden here.
-# network_interface: "virbr0"
-
-# The path to the SSH key to be utilized for testing and burn-in
-# to configuration drives. When set, it should be set in both baremetal
-# and localhost groups, however this is only an override to the default.
-
-# workaround for opnfv ci until we can fix non-root use
-ssh_public_key_path: "/root/.ssh/id_rsa.pub"
-
-# Normally this user should be root, however if cirros is used,
-# a user may wish to define a specific user for testing VM
-# connectivity during a test sequence
-testing_user: root
-
-# The default port to download files via.  Required for IPA URL generation.
-# Presently the defaults are located in the roles, however if changed both
-# the localhost and baremetal group files must be updated.
-# file_url_port: 8080
-
-# IPA Image parameters.  If these are changed, they must be changed in
-# Both localhost and baremetal groups.  Presently the defaults
-# in each role should be sufficent for proper operation.
-# ipa_kernel: "{{http_boot_folder}}/coreos_production_pxe.vmlinuz"
-# ipa_ramdisk: "{{http_boot_folder}}/coreos_production_pxe_image-oem.cpio.gz"
-# ipa_kernel_url: "http://{{ hostvars[inventory_hostname]['ansible_' + network_interface]['ipv4']['address'] }}:{{file_url_port}}/coreos_production_pxe.vmlinuz"
-# ipa_ramdisk_url: "http://{{ hostvars[inventory_hostname]['ansible_' + network_interface]['ipv4']['address'] }}:{{file_url_port}}/coreos_production_pxe_image-oem.cpio.gz"
-
-# The http_boot_folder defines the root folder for the webserver.
-# If this setting is changed, it must be applied to both the baremetal
-# and localhost groups. Presently the role defaults are set to the value
-# below.
-# http_boot_folder: /httpboot
-
-# The settings for the name of the image to be deployed along with the
-# on disk location are below.  If changed, these settings must be applied
-# to both the baremetal and localhost groups.  If the file is already on
-# disk, then the image generation will not take place, otherwise an image
-# will be generated using diskimage-builder.
-# deploy_image_filename: "deployment_image.qcow2"
-# deploy_image: "{{http_boot_folder}}/{{deploy_image_filename}}"
-
-# Under normal circumstances, the os_ironic_node module does not wait for
-# the node to reach active state before continuing with the deployment
-# process.  This means we may have to timeout, to figure out a deployment
-# failed.  Change wait_for_node_deploy to true to cause bifrost to wait for
-# Ironic to show the instance in Active state.
-wait_for_node_deploy: false
index 8318cdb..6d7af0d 100644 (file)
@@ -148,13 +148,7 @@ set where the logs should be stored
 
 execute sandbox script
 
-    sudo -E ./xci-deploy.sh
-
-Warning::
-
-    Please encure you always execute the sandbox script using **sudo -E**
-    in order to make the environment variables you set available to the
-    sandbox script or you end up with the default settings.
+    ./xci-deploy.sh
 
 ===============
 User Variables
index d910405..f9de940 100755 (executable)
@@ -28,9 +28,11 @@ export XCI_FLAVOR=${XCI_FLAVOR:-aio}
 # OPNFV XCI Sandbox is not verified to be used as non-root user as of yet so
 # changing these paths might break things.
 #-------------------------------------------------------------------------------
-export OPNFV_RELENG_PATH=/opt/releng
-export OPENSTACK_BIFROST_PATH=/opt/bifrost
-export OPENSTACK_OSA_PATH=/opt/openstack-ansible
+export XCI_DEVEL_ROOT=${XCI_DEVEL_ROOT:-"/tmp/.xci-deploy-env"}
+export OPNFV_RELENG_PATH="${XCI_DEVEL_ROOT}/releng"
+export OPENSTACK_BIFROST_PATH="${XCI_DEVEL_ROOT}/bifrost"
+export OPENSTACK_OSA_PATH="${XCI_DEVEL_ROOT}/openstack-ansible"
+export OPNFV_SSH_HOST_KEYS_PATH="${XCI_DEVEL_ROOT}/ssh_host_keys"
 
 #-------------------------------------------------------------------------------
 # Set the playbook to use for OpenStack deployment
@@ -50,5 +52,5 @@ export OPNFV_OSA_PLAYBOOK=${OPNFV_OSA_PLAYBOOK:-"$OPENSTACK_OSA_PATH/playbooks/s
 # or
 #   ANSIBLE_VERBOSITY="-vvvv"
 export ANSIBLE_VERBOSITY=${ANSIBLE_VERBOSITY-""}
-export LOG_PATH=${LOG_PATH:-/opt/opnfv/logs}
+export LOG_PATH=${LOG_PATH:-${XCI_DEVEL_ROOT}/opnfv/logs}
 export RUN_TEMPEST=${RUN_TEMPEST:-false}
index 34b974c..b6d0fcc 100644 (file)
@@ -8,13 +8,35 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 - hosts: localhost
-  remote_user: root
+  connection: local
   vars_files:
     - ../var/{{ ansible_os_family }}.yml
     - ../var/opnfv.yml
   roles:
     - role: remove-folders
     - { role: clone-repository, project: "opnfv/releng", repo: "{{ OPNFV_RELENG_GIT_URL }}", dest: "{{ OPNFV_RELENG_PATH }}", version: "{{ OPNFV_RELENG_VERSION }}" }
+
+- hosts: localhost
+  connection: local
+  gather_facts: false
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
+  tasks:
+    - name: Synchronize local development releng repository to XCI paths
+      synchronize:
+        src: "{{ OPNFV_RELENG_DEV_PATH }}"
+        dest: "{{ OPNFV_RELENG_PATH }}"
+        recursive: yes
+        delete: yes
+      when:
+        - OPNFV_RELENG_DEV_PATH != ""
+
+- hosts: localhost
+  connection: local
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
   tasks:
     - name:  create log directory {{LOG_PATH}}
       file:
index 64fcef0..8656ff9 100644 (file)
     - role: remove-folders
     - { role: clone-repository, project: "opnfv/releng", repo: "{{ OPNFV_RELENG_GIT_URL }}", dest: "{{ OPNFV_RELENG_PATH }}", version: "{{ OPNFV_RELENG_VERSION }}" }
     - { role: clone-repository, project: "openstack/openstack-ansible", repo: "{{ OPENSTACK_OSA_GIT_URL }}", dest: "{{ OPENSTACK_OSA_PATH }}", version: "{{ OPENSTACK_OSA_VERSION }}" }
+
+- hosts: opnfv
+  remote_user: root
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
+  tasks:
+    - name: Synchronize local development releng repository to XCI paths
+      synchronize:
+        src: "{{ OPNFV_RELENG_DEV_PATH }}"
+        dest: "{{ OPNFV_RELENG_PATH }}"
+        recursive: yes
+        delete: yes
+      when:
+        - OPNFV_RELENG_DEV_PATH != ""
+    - name: Synchronize local development openstack-ansible repository to XCI paths
+      synchronize:
+        src: "{{ OPENSTACK_OSA_DEV_PATH }}"
+        dest: "{{ OPENSTACK_OSA_PATH }}"
+        recursive: yes
+        delete: yes
+      when:
+        - OPENSTACK_OSA_DEV_PATH != ""
+
+- hosts: opnfv
+  remote_user: root
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/flavor-vars.yml
+    - ../var/opnfv.yml
+  roles:
     # TODO: this only works for ubuntu/xenial and need to be adjusted for other distros
     - { role: configure-network, when: ansible_distribution_release == "xenial", src: "../template/opnfv.interface.j2", dest: "/etc/network/interfaces" }
   tasks:
       shell: ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -q -N ""
       args:
         creates: /root/.ssh/id_rsa
+    - name: ensure ssh key storage directory exists
+      file:
+        path: "{{ OPNFV_SSH_HOST_KEYS_PATH }}"
+        state: directory
     - name: fetch public key
-      fetch: src="/root/.ssh/id_rsa.pub" dest="/"
+      fetch: src="/root/.ssh/id_rsa.pub" dest="{{ OPNFV_SSH_HOST_KEYS_PATH }}"
     - name: copy flavor inventory
       shell: "/bin/cp -rf {{XCI_FLAVOR_ANSIBLE_FILE_PATH}}/inventory {{OPNFV_RELENG_PATH}}/prototypes/xci/playbooks"
     - name: copy flavor vars
         chdir: "{{OPENSTACK_OSA_PATH}}/scripts"
 - hosts: localhost
   remote_user: root
+  vars_files:
+    - ../var/opnfv.yml
   tasks:
     - name: Generate authorized_keys
-      shell: "/bin/cat /opnfv/root/.ssh/id_rsa.pub >> ../file/authorized_keys"
+      shell: "/bin/cat {{ OPNFV_SSH_HOST_KEYS_PATH }}/opnfv/root/.ssh/id_rsa.pub >> ../file/authorized_keys"
     - name: Append public keys to authorized_keys
-      shell: "/bin/cat /root/.ssh/id_rsa.pub >> ../file/authorized_keys"
+      shell: "/bin/cat {{ ansible_env.HOME }}/.ssh/id_rsa.pub >> ../file/authorized_keys"
index 9a32d0b..8be36c7 100644 (file)
@@ -8,7 +8,7 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 - hosts: localhost
-  remote_user: root
+  connection: local
   vars_files:
     - ../var/{{ ansible_os_family }}.yml
     - ../var/opnfv.yml
     - role: remove-folders
     - { role: clone-repository, project: "opnfv/releng", repo: "{{ OPNFV_RELENG_GIT_URL }}", dest: "{{ OPNFV_RELENG_PATH }}", version: "{{ OPNFV_RELENG_VERSION }}" }
     - { role: clone-repository, project: "opnfv/bifrost", repo: "{{ OPENSTACK_BIFROST_GIT_URL }}", dest: "{{ OPENSTACK_BIFROST_PATH }}", version: "{{ OPENSTACK_BIFROST_VERSION }}" }
+
+- hosts: localhost
+  connection: local
+  gather_facts: false
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
+  tasks:
+    - name: Synchronize local development bifrost repository to XCI paths
+      # command module is much faster than the copy module
+      synchronize:
+        src: "{{ OPENSTACK_BIFROST_DEV_PATH }}"
+        dest: "{{ OPENSTACK_BIFROST_PATH }}"
+        recursive: yes
+        delete: yes
+      when:
+        - OPENSTACK_BIFROST_DEV_PATH != ""
+    - name: Synchronize local development releng repository to XCI paths
+      synchronize:
+        src: "{{ OPNFV_RELENG_DEV_PATH }}"
+        dest: "{{ OPNFV_RELENG_PATH }}"
+        recursive: yes
+        delete: yes
+      when:
+        - OPNFV_RELENG_DEV_PATH != ""
+
+- hosts: localhost
+  connection: local
+  gather_facts: false
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
   tasks:
     - name: combine opnfv/releng and openstack/bifrost scripts/playbooks
       copy:
         src: "{{ OPNFV_RELENG_PATH }}/prototypes/bifrost/"
         dest: "{{ OPENSTACK_BIFROST_PATH }}"
+
+- hosts: localhost
+  connection: local
+  become: yes
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
+  tasks:
     - name: destroy VM nodes created by previous deployment
       command: "/bin/bash ./scripts/destroy-env.sh"
       args:
         chdir: "{{ OPENSTACK_BIFROST_PATH }}"
+
+- hosts: localhost
+  connection: local
+  vars_files:
+    - ../var/{{ ansible_os_family }}.yml
+    - ../var/opnfv.yml
+  tasks:
     - name: create and provision VM nodes for the flavor {{ XCI_FLAVOR }}
       command: "/bin/bash ./scripts/bifrost-provision.sh"
       args:
index ac8c0f7..425b8db 100644 (file)
@@ -18,3 +18,4 @@
     - "{{ OPENSTACK_OSA_PATH }}"
     - "{{ OPENSTACK_OSA_ETC_PATH }}"
     - "{{ LOG_PATH }} "
+    - "{{ OPNFV_SSH_HOST_KEYS_PATH }}"
index 12cb556..85f532a 100644 (file)
@@ -9,12 +9,15 @@
 ##############################################################################
 OPNFV_RELENG_GIT_URL: "{{ lookup('env','OPNFV_RELENG_GIT_URL') }}"
 OPNFV_RELENG_PATH: "{{ lookup('env','OPNFV_RELENG_PATH') }}"
+OPNFV_RELENG_DEV_PATH: "{{ lookup('env','OPNFV_RELENG_DEV_PATH') }}"
 OPNFV_RELENG_VERSION: "{{ lookup('env','OPNFV_RELENG_VERSION') }}"
 OPENSTACK_BIFROST_GIT_URL: "{{ lookup('env','OPENSTACK_BIFROST_GIT_URL') }}"
 OPENSTACK_BIFROST_PATH: "{{ lookup('env','OPENSTACK_BIFROST_PATH') }}"
+OPENSTACK_BIFROST_DEV_PATH: "{{ lookup('env','OPENSTACK_BIFROST_DEV_PATH') }}"
 OPENSTACK_BIFROST_VERSION: "{{ lookup('env','OPENSTACK_BIFROST_VERSION') }}"
 OPENSTACK_OSA_GIT_URL: "{{ lookup('env','OPENSTACK_OSA_GIT_URL') }}"
 OPENSTACK_OSA_PATH: "{{ lookup('env','OPENSTACK_OSA_PATH') }}"
+OPENSTACK_OSA_DEV_PATH: "{{ lookup('env','OPENSTACK_OSA_DEV_PATH') }}"
 OPENSTACK_OSA_VERSION: "{{ lookup('env','OPENSTACK_OSA_VERSION') }}"
 OPENSTACK_OSA_ETC_PATH: "{{ lookup('env','OPENSTACK_OSA_ETC_PATH') }}"
 XCI_ANSIBLE_PIP_VERSION: "{{ lookup('env','XCI_ANSIBLE_PIP_VERSION') }}"
@@ -23,3 +26,4 @@ XCI_FLAVOR_ANSIBLE_FILE_PATH: "{{ lookup('env','XCI_FLAVOR_ANSIBLE_FILE_PATH') }
 XCI_LOOP: "{{ lookup('env','XCI_LOOP') }}"
 LOG_PATH: "{{ lookup('env','LOG_PATH') }}"
 OPNFV_HOST_IP: "{{ lookup('env','OPNFV_HOST_IP') }}"
+OPNFV_SSH_HOST_KEYS_PATH: "{{ lookup('env', 'OPNFV_SSH_HOST_KEYS_PATH') }}"
index 718ed73..3a65983 100755 (executable)
@@ -4,11 +4,13 @@ set -o nounset
 set -o pipefail
 
 #-------------------------------------------------------------------------------
-# This script must run as root
+# This script should not be run as root
 #-------------------------------------------------------------------------------
-if [[ $(whoami) != "root" ]]; then
-    echo "Error: This script must be run as root!"
-    exit 1
+if [[ $(whoami) == "root" ]]; then
+    echo "WARNING: This script should not be run as root!"
+    echo "Elevated privileges are aquired automatically when necessary"
+    echo "Waiting 10s to give you a chance to stop the script (Ctrl-C)"
+    for x in $(seq 10 -1 1); do echo -n "$x..."; sleep 1; done
 fi
 
 #-------------------------------------------------------------------------------
@@ -137,7 +139,7 @@ echo "Info: Configured target hosts"
 #-------------------------------------------------------------------------------
 echo "Info: Setting up target hosts for openstack-ansible"
 echo "-----------------------------------------------------------------------"
-sudo -E /bin/sh -c "ssh root@$OPNFV_HOST_IP openstack-ansible \
+ssh root@$OPNFV_HOST_IP "openstack-ansible \
      $OPENSTACK_OSA_PATH/playbooks/setup-hosts.yml" | \
      tee $LOG_PATH/setup-hosts.log
 echo "-----------------------------------------------------------------------"
@@ -156,7 +158,7 @@ echo "Info: Set up target hosts for openstack-ansible successfuly"
 echo "Info: Setting up infrastructure"
 echo "-----------------------------------------------------------------------"
 echo "xci: running ansible playbook setup-infrastructure.yml"
-sudo -E /bin/sh -c "ssh root@$OPNFV_HOST_IP openstack-ansible \
+ssh root@$OPNFV_HOST_IP "openstack-ansible \
      $OPENSTACK_OSA_PATH/playbooks//setup-infrastructure.yml" | \
      tee $LOG_PATH/setup-infrastructure.log
 echo "-----------------------------------------------------------------------"
@@ -171,7 +173,7 @@ fi
 #-------------------------------------------------------------------------------
 echo "Info: Verifying database cluster"
 echo "-----------------------------------------------------------------------"
-sudo -E /bin/sh -c "ssh root@$OPNFV_HOST_IP ansible -i $OPENSTACK_OSA_PATH/playbooks/inventory/ \
+ssh root@$OPNFV_HOST_IP "ansible -i $OPENSTACK_OSA_PATH/playbooks/inventory/ \
            galera_container -m shell \
            -a "mysql -h localhost -e 'show status like \"%wsrep_cluster_%\";'"" \
            | tee $LOG_PATH/galera.log
@@ -190,7 +192,7 @@ echo "Info: Database cluster verification successful!"
 #-------------------------------------------------------------------------------
 echo "Info: Installing OpenStack on target hosts"
 echo "-----------------------------------------------------------------------"
-sudo -E /bin/sh -c "ssh root@$OPNFV_HOST_IP openstack-ansible \
+ssh root@$OPNFV_HOST_IP "openstack-ansible \
      $OPENSTACK_OSA_PATH/playbooks/setup-openstack.yml" | \
      tee $LOG_PATH/opnfv-setup-openstack.log
 echo "-----------------------------------------------------------------------"
index fa2b722..8b092b8 100644 (file)
@@ -48,7 +48,9 @@ def parse_config(argv=[]):
     parser.add_argument("-c", "--config-file", dest='config_file',
                         help="Config file location")
     args = parser.parse_args(argv)
-    CONF = config.APIConfig().parse(args.config_file)
+    if args.config_file:
+        config.Config.CONFIG = args.config_file
+    CONF = config.Config()
 
 
 def get_db():
@@ -60,8 +62,8 @@ def make_app():
     return swagger.Application(
         url_mappings.mappings,
         db=get_db(),
-        debug=CONF.api_debug_on,
-        auth=CONF.api_authenticate_on
+        debug=CONF.api_debug,
+        auth=CONF.api_authenticate
     )
 
 
index 362fca6..70d7bd6 100644 (file)
@@ -11,83 +11,41 @@ import ConfigParser
 import os
 
 
-class ParseError(Exception):
-    """
-    Custom exception class for config file
-    """
-
-    def __init__(self, message):
-        self.msg = message
-
-    def __str__(self):
-        return 'error parsing config file : %s' % self.msg
-
-
-class APIConfig(object):
-    """
-    The purpose of this class is to load values correctly from the config file.
-    Each key is declared as an attribute in __init__() and linked in parse()
-    """
+class Config(object):
+    CONFIG = None
 
     def __init__(self):
-        self._set_default_config()
-        self.mongo_url = None
-        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
+        self.file = self.CONFIG if self.CONFIG else self._default_config()
+        self._parse()
 
-    def _set_default_config(self):
-        venv = os.getenv('VIRTUAL_ENV')
-        self._default_config = os.path.join('/' if not venv else venv,
-                                            'etc/opnfv_testapi/config.ini')
+    def _parse(self):
+        if not os.path.exists(self.file):
+            raise Exception("%s not found" % self.file)
 
-    def _get_parameter(self, section, param):
-        try:
-            return self._parser.get(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("Not int: [%s.%s]" % (section, param))
+        config = ConfigParser.RawConfigParser()
+        config.read(self.file)
+        self._parse_section(config)
 
-    def _get_bool_parameter(self, section, param):
-        result = self._get_parameter(section, param)
-        if str(result).lower() == 'true':
-            return True
-        if str(result).lower() == 'false':
-            return False
+    def _parse_section(self, config):
+        [self._parse_item(config, section) for section in (config.sections())]
 
-        raise ParseError(
-            "Not boolean: [%s.%s : %s]" % (section, param, result))
+    def _parse_item(self, config, section):
+        [setattr(self, '{}_{}'.format(section, k), self._parse_value(v))
+         for k, v in config.items(section)]
 
     @staticmethod
-    def parse(config_location=None):
-        obj = APIConfig()
-
-        if config_location is None:
-            config_location = obj._default_config
-
-        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")
+    def _parse_value(value):
+        try:
+            value = int(value)
+        except:
+            if str(value).lower() == 'true':
+                value = True
+            elif str(value).lower() == 'false':
+                value = False
+        return value
 
-        return obj
+    @staticmethod
+    def _default_config():
+        is_venv = os.getenv('VIRTUAL_ENV')
+        return os.path.join('/' if not is_venv else is_venv,
+                            'etc/opnfv_testapi/config.ini')
index aaff6bb..446b944 100644 (file)
@@ -1,36 +1,16 @@
-import ConfigParser
 import os
 
-import pytest
-
 from opnfv_testapi.common import config
 
 
-@pytest.fixture()
-def config_dir():
-    return os.path.dirname(__file__)
-
-
-@pytest.mark.parametrize('exception, config_file, excepted', [
-    (config.ParseError, None, '/etc/opnfv_testapi/config.ini not found'),
-    (ConfigParser.NoSectionError, 'nosection.ini', 'No section:'),
-    (config.ParseError, 'noparam.ini', 'No parameter:'),
-    (config.ParseError, 'notint.ini', 'Not int:'),
-    (config.ParseError, 'notboolean.ini', 'Not boolean:')])
-def pytest_config_exceptions(config_dir, exception, config_file, excepted):
-    file = '{}/{}'.format(config_dir, config_file) if config_file else None
-    with pytest.raises(exception) as error:
-        config.APIConfig().parse(file)
-    assert excepted in str(error.value)
-
-
 def test_config_success():
-    config_dir = os.path.join(os.path.dirname(__file__),
-                              '../../../../etc/config.ini')
-    conf = config.APIConfig().parse(config_dir)
+    config_file = os.path.join(os.path.dirname(__file__),
+                               '../../../../etc/config.ini')
+    config.Config.CONFIG = config_file
+    conf = config.Config()
     assert conf.mongo_url == 'mongodb://127.0.0.1:27017/'
     assert conf.mongo_dbname == 'test_results_collection'
     assert conf.api_port == 8000
-    assert conf.api_debug_on is True
-    assert conf.api_authenticate_on is False
+    assert conf.api_debug is True
+    assert conf.api_authenticate is False
     assert conf.swagger_base_url == 'http://localhost:8000'