Fine-tuning the juju-epc codebase for better error-handling
[functest.git] / functest / opnfv_tests / vnf / router / cloudify_vrouter.py
index 07033ec..769c2a4 100644 (file)
@@ -14,6 +14,7 @@
 import logging
 import os
 import time
+import uuid
 
 from cloudify_rest_client import CloudifyClient
 from cloudify_rest_client.executions import Execution
@@ -22,11 +23,10 @@ from scp import SCPClient
 from functest.opnfv_tests.openstack.snaps import snaps_utils
 import functest.opnfv_tests.vnf.router.vrouter_base as vrouter_base
 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
-from functest.utils.constants import CONST
+from functest.utils import config
+from functest.utils import env
 from functest.utils import functest_utils
 
-from git import Repo
-
 from snaps.config.flavor import FlavorConfig
 from snaps.config.image import ImageConfig
 from snaps.config.keypair import KeypairConfig
@@ -34,6 +34,7 @@ from snaps.config.network import NetworkConfig, PortConfig, SubnetConfig
 from snaps.config.router import RouterConfig
 from snaps.config.security_group import (
     Direction, Protocol, SecurityGroupConfig, SecurityGroupRuleConfig)
+from snaps.config.user import UserConfig
 from snaps.config.vm_inst import FloatingIpConfig, VmInstanceConfig
 
 from snaps.openstack.create_flavor import OpenStackFlavor
@@ -43,6 +44,7 @@ from snaps.openstack.create_keypairs import OpenStackKeypair
 from snaps.openstack.create_network import OpenStackNetwork
 from snaps.openstack.create_security_group import OpenStackSecurityGroup
 from snaps.openstack.create_router import OpenStackRouter
+from snaps.openstack.create_user import OpenStackUser
 
 import snaps.openstack.utils.glance_utils as glance_utils
 from snaps.openstack.utils import keystone_utils
@@ -65,16 +67,12 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
 
         # Retrieve the configuration
         try:
-            self.config = CONST.__getattribute__(
-                'vnf_{}_config'.format(self.case_name))
+            self.config = getattr(
+                config.CONF, 'vnf_{}_config'.format(self.case_name))
         except Exception:
             raise Exception("VNF config file not found")
 
-        self.snaps_creds = ''
-        self.created_object = []
-
         self.cfy_manager_ip = ''
-        self.util_info = {}
         self.deployment_name = ''
 
         config_file = os.path.join(self.case_dir, self.config)
@@ -121,14 +119,21 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
             "tenant_images", config_file)
         self.__logger.info("Images needed for vrouter: %s", self.images)
 
+    @staticmethod
+    def run_blocking_ssh_command(ssh, cmd,
+                                 error_msg="Unable to run this command"):
+        """Command to run ssh command with the exit status."""
+        (_, stdout, stderr) = ssh.exec_command(cmd)
+        CloudifyVrouter.__logger.debug("SSH %s stdout: %s", cmd, stdout.read())
+        if stdout.channel.recv_exit_status() != 0:
+            CloudifyVrouter.__logger.error(
+                "SSH %s stderr: %s", cmd, stderr.read())
+            raise Exception(error_msg)
+
     def prepare(self):
         super(CloudifyVrouter, self).prepare()
-
         self.__logger.info("Additional pre-configuration steps")
-
         self.util.set_credentials(self.snaps_creds)
-
-        # needs some images
         self.__logger.info("Upload some OS images if it doesn't exist")
         for image_name, image_file in self.images.iteritems():
             self.__logger.info("image: %s, file: %s", image_name, image_file)
@@ -148,7 +153,6 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         network, security group, fip, VM creation
         """
         # network creation
-
         start_time = time.time()
         self.__logger.info("Creating keypair ...")
         kp_file = os.path.join(self.data_dir, "cloudify_vrouter.pem")
@@ -162,7 +166,8 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         self.__logger.info("Creating full network ...")
         subnet_settings = SubnetConfig(
             name='cloudify_vrouter_subnet-{}'.format(self.uuid),
-            cidr='10.67.79.0/24')
+            cidr='10.67.79.0/24',
+            dns_nameservers=[env.get('NAMESERVER')])
         network_settings = NetworkConfig(
             name='cloudify_vrouter_network-{}'.format(self.uuid),
             subnet_settings=[subnet_settings])
@@ -210,15 +215,13 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         flavor_settings = FlavorConfig(
             name=self.orchestrator['requirements']['flavor']['name'],
             ram=self.orchestrator['requirements']['flavor']['ram_min'],
-            disk=50,
-            vcpus=2)
+            disk=50, vcpus=2)
         flavor_creator = OpenStackFlavor(self.snaps_creds, flavor_settings)
         flavor_creator.create()
         self.created_object.append(flavor_creator)
         image_settings = ImageConfig(
             name=self.orchestrator['requirements']['os_image'],
-            image_user='centos',
-            exists=True)
+            image_user='centos', exists=True)
 
         port_settings = PortConfig(
             name='cloudify_manager_port-{}'.format(self.uuid),
@@ -243,16 +246,6 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         manager_creator.create()
         self.created_object.append(manager_creator)
 
-        public_auth_url = keystone_utils.get_endpoint(
-            self.snaps_creds, 'identity')
-
-        self.__logger.info("Set creds for cloudify manager")
-        cfy_creds = dict(
-            keystone_username=self.snaps_creds.username,
-            keystone_password=self.snaps_creds.password,
-            keystone_tenant_name=self.snaps_creds.project_name,
-            keystone_url=public_auth_url)
-
         cfy_client = CloudifyClient(
             host=manager_creator.get_floating_ip().ip,
             username='admin', password='admin', tenant='default_tenant')
@@ -267,10 +260,10 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         while str(cfy_status) != 'running' and retry:
             try:
                 cfy_status = cfy_client.manager.get_status()['status']
-                self.__logger.debug("The current manager status is %s",
-                                    cfy_status)
+                self.__logger.info(
+                    "The current manager status is %s", cfy_status)
             except Exception:  # pylint: disable=broad-except
-                self.__logger.exception(
+                self.__logger.info(
                     "Cloudify Manager isn't up and running. Retrying ...")
             retry = retry - 1
             time.sleep(30)
@@ -280,14 +273,6 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         else:
             raise Exception("Cloudify Manager isn't up and running")
 
-        self.__logger.info("Put OpenStack creds in manager")
-        secrets_list = cfy_client.secrets.list()
-        for k, val in cfy_creds.iteritems():
-            if not any(d.get('key', None) == k for d in secrets_list):
-                cfy_client.secrets.create(k, val)
-            else:
-                cfy_client.secrets.update(k, val)
-
         duration = time.time() - start_time
 
         self.__logger.info("Put private keypair in manager")
@@ -296,11 +281,11 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
             scp = SCPClient(ssh.get_transport(), socket_timeout=15.0)
             scp.put(kp_file, '~/')
             cmd = "sudo cp ~/cloudify_vrouter.pem /etc/cloudify/"
-            run_blocking_ssh_command(ssh, cmd)
+            self.run_blocking_ssh_command(ssh, cmd)
             cmd = "sudo chmod 444 /etc/cloudify/cloudify_vrouter.pem"
-            run_blocking_ssh_command(ssh, cmd)
+            self.run_blocking_ssh_command(ssh, cmd)
             cmd = "sudo yum install -y gcc python-devel"
-            run_blocking_ssh_command(
+            self.run_blocking_ssh_command(
                 ssh, cmd, "Unable to install packages on manager")
 
         self.details['orchestrator'].update(status='PASS', duration=duration)
@@ -317,55 +302,63 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         descriptor = self.vnf['descriptor']
         self.deployment_name = descriptor.get('name')
 
-        vrouter_blueprint_dir = os.path.join(self.data_dir,
-                                             self.util.blueprint_dir)
-        if not os.path.exists(vrouter_blueprint_dir):
-            Repo.clone_from(descriptor.get('url'),
-                            vrouter_blueprint_dir,
-                            branch=descriptor.get('version'))
-
-        cfy_client.blueprints.upload(vrouter_blueprint_dir +
-                                     self.util.blueprint_file_name,
-                                     descriptor.get('name'))
+        cfy_client.blueprints.upload(
+            descriptor.get('file_name'), descriptor.get('name'))
 
         self.__logger.info("Get or create flavor for vrouter")
         flavor_settings = FlavorConfig(
             name=self.vnf['requirements']['flavor']['name'],
             ram=self.vnf['requirements']['flavor']['ram_min'],
-            disk=25,
-            vcpus=1)
+            disk=25, vcpus=1)
         flavor_creator = OpenStackFlavor(self.snaps_creds, flavor_settings)
         flavor = flavor_creator.create()
         self.created_object.append(flavor_creator)
 
         # set image name
         glance = glance_utils.glance_client(self.snaps_creds)
-        image = glance_utils.get_image(glance,
-                                       "vyos1.1.7")
+        image = glance_utils.get_image(glance, "vyos1.1.7")
+
+        user_creator = OpenStackUser(
+            self.snaps_creds,
+            UserConfig(
+                name='cloudify_network_bug-{}'.format(self.uuid),
+                password=str(uuid.uuid4()),
+                project_name=self.tenant_name,
+                domain=self.snaps_creds.user_domain_name,
+                roles={'_member_': self.tenant_name}))
+        user_creator.create()
+        self.created_object.append(user_creator)
+        snaps_creds = user_creator.get_os_creds(self.snaps_creds.project_name)
+        self.__logger.debug("snaps creds: %s", snaps_creds)
+
         self.vnf['inputs'].update(dict(target_vnf_image_id=image.id))
         self.vnf['inputs'].update(dict(reference_vnf_image_id=image.id))
-
-        # set flavor id
         self.vnf['inputs'].update(dict(target_vnf_flavor_id=flavor.id))
         self.vnf['inputs'].update(dict(reference_vnf_flavor_id=flavor.id))
-
-        self.vnf['inputs'].update(dict(keystone_username=self.tenant_name))
-        self.vnf['inputs'].update(dict(keystone_password=self.tenant_name))
-        self.vnf['inputs'].update(dict(keystone_tenant_name=self.tenant_name))
-        self.vnf['inputs'].update(
-            dict(keystone_url=keystone_utils.get_endpoint(
-                self.snaps_creds, 'identity')))
+        self.vnf['inputs'].update(dict(
+            keystone_username=snaps_creds.username))
+        self.vnf['inputs'].update(dict(
+            keystone_password=snaps_creds.password))
+        self.vnf['inputs'].update(dict(
+            keystone_tenant_name=snaps_creds.project_name))
+        self.vnf['inputs'].update(dict(
+            keystone_user_domain_name=snaps_creds.user_domain_name))
+        self.vnf['inputs'].update(dict(
+            keystone_project_domain_name=snaps_creds.project_domain_name))
+        self.vnf['inputs'].update(dict(
+            region=snaps_creds.region_name))
+        self.vnf['inputs'].update(dict(
+            keystone_url=keystone_utils.get_endpoint(
+                snaps_creds, 'identity')))
 
         self.__logger.info("Create VNF Instance")
-        cfy_client.deployments.create(descriptor.get('name'),
-                                      descriptor.get('name'),
-                                      self.vnf.get('inputs'))
+        cfy_client.deployments.create(
+            descriptor.get('name'), descriptor.get('name'),
+            self.vnf.get('inputs'))
 
-        wait_for_execution(cfy_client,
-                           get_execution_id(
-                               cfy_client, descriptor.get('name')),
-                           self.__logger,
-                           timeout=7200)
+        wait_for_execution(
+            cfy_client, get_execution_id(cfy_client, descriptor.get('name')),
+            self.__logger, timeout=7200)
 
         self.__logger.info("Start the VNF Instance deployment")
         execution = cfy_client.executions.start(descriptor.get('name'),
@@ -386,11 +379,7 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
 
     def test_vnf(self):
         cfy_client = self.orchestrator['object']
-        credentials = {"snaps_creds": self.snaps_creds,
-                       "username": self.snaps_creds.username,
-                       "password": self.snaps_creds.password,
-                       "auth_url": self.snaps_creds.auth_url,
-                       "tenant_name": self.snaps_creds.project_name}
+        credentials = {"snaps_creds": self.snaps_creds}
 
         self.util_info = {"credentials": credentials,
                           "cfy": cfy_client,
@@ -403,15 +392,13 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
         duration = time.time() - start_time
 
         if result:
-            self.details['test_vnf'].update(status='PASS',
-                                            result='OK',
-                                            full_result=test_result_data,
-                                            duration=duration)
+            self.details['test_vnf'].update(
+                status='PASS', result='OK', full_result=test_result_data,
+                duration=duration)
         else:
-            self.details['test_vnf'].update(status='FAIL',
-                                            result='NG',
-                                            full_result=test_result_data,
-                                            duration=duration)
+            self.details['test_vnf'].update(
+                status='FAIL', result='NG', full_result=test_result_data,
+                duration=duration)
 
         return True
 
@@ -425,22 +412,20 @@ class CloudifyVrouter(vrouter_base.VrouterOnBoardingBase):
             for execution in exec_list:
                 if execution['status'] == "started":
                     try:
-                        cfy_client.executions.cancel(execution['id'],
-                                                     force=True)
+                        cfy_client.executions.cancel(
+                            execution['id'], force=True)
                     except Exception:  # pylint: disable=broad-except
                         self.__logger.warn("Can't cancel the current exec")
 
             execution = cfy_client.executions.start(
-                dep_name,
-                'uninstall',
-                parameters=dict(ignore_failure=True))
+                dep_name, 'uninstall', parameters=dict(ignore_failure=True))
 
             wait_for_execution(cfy_client, execution, self.__logger)
             cfy_client.deployments.delete(self.vnf['descriptor'].get('name'))
             cfy_client.blueprints.delete(self.vnf['descriptor'].get('name'))
         except Exception:  # pylint: disable=broad-except
-            self.__logger.warn("Some issue during the undeployment ..")
-            self.__logger.warn("Tenant clean continue ..")
+            self.__logger.exception("Some issue during the undeployment ..")
+
         super(CloudifyVrouter, self).clean()
 
     def get_vnf_info_list(self, target_vnf_name):
@@ -465,11 +450,8 @@ def wait_for_execution(client, execution, logger, timeout=7200, ):
     execution_ended = False
     while True:
         event_list = client.events.list(
-            execution_id=execution.id,
-            _offset=offset,
-            _size=batch_size,
-            include_logs=False,
-            sort='@timestamp').items
+            execution_id=execution.id, _offset=offset, _size=batch_size,
+            include_logs=False, sort='@timestamp').items
 
         offset = offset + len(event_list)
         for event in event_list:
@@ -509,10 +491,3 @@ def get_execution_id(client, deployment_id):
     raise RuntimeError('Failed to get create_deployment_environment '
                        'workflow execution.'
                        'Available executions: {0}'.format(executions))
-
-
-def run_blocking_ssh_command(ssh, cmd, error_msg="Unable to run this command"):
-    """Command to run ssh command with the exit status."""
-    (_, stdout, _) = ssh.exec_command(cmd)
-    if stdout.channel.recv_exit_status() != 0:
-        raise Exception(error_msg)