test_RO: Removing deprecated tests and updating some examples
[osm/RO.git] / test / test_RO.py
index 1661ce0..932853c 100755 (executable)
@@ -308,7 +308,7 @@ class test_vimconn_new_network(test_base):
                      self.__class__.test_index, inspect.currentframe().f_code.co_name)
         self.__class__.test_index += 1
 
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                           net_type=network_type)
         self.__class__.network_id = network
         logger.debug("{}".format(network))
@@ -335,7 +335,7 @@ class test_vimconn_new_network(test_base):
         self.__class__.test_index += 1
         for net_type in network_types:
             self.__class__.network_name = _get_random_string(20)
-            network_id = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+            network_id, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                                                 net_type=net_type)
 
             delete_net_ids.append(network_id)
@@ -392,7 +392,7 @@ class test_vimconn_new_network(test_base):
                                                             self.__class__.test_index,
                                                 inspect.currentframe().f_code.co_name)
         self.__class__.test_index += 1
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                                            net_type='mgmt',
                                                                      ip_profile=ip_profile)
         self.__class__.network_id = network
@@ -417,7 +417,7 @@ class test_vimconn_new_network(test_base):
                                                             self.__class__.test_index,
                                                 inspect.currentframe().f_code.co_name)
         self.__class__.test_index += 1
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                                          net_type='bridge',
                                                                              shared=shared)
         self.__class__.network_id = network
@@ -442,7 +442,7 @@ class test_vimconn_new_network(test_base):
                                                             self.__class__.test_index,
                                                 inspect.currentframe().f_code.co_name)
         self.__class__.test_index += 1
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                                     net_type='unknowntype')
         self.__class__.network_id = network
         logger.debug("{}".format(network))
@@ -466,7 +466,7 @@ class test_vimconn_new_network(test_base):
         # creating new network
         network_name = _get_random_string(20)
         net_type = 'bridge'
-        network_id = test_config["vim_conn"].new_network(net_name=network_name,
+        network_id, _ = test_config["vim_conn"].new_network(net_name=network_name,
                                                           net_type=net_type)
         # refresh net status
         net_dict = test_config["vim_conn"].refresh_nets_status([network_id])
@@ -503,7 +503,7 @@ class test_vimconn_get_network_list(test_base):
         # creating new network
         self.__class__.network_name = _get_random_string(20)
         self.__class__.net_type = 'bridge'
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                           net_type=self.__class__.net_type)
         self.__class__.network_id = network
         logger.debug("{}".format(network))
@@ -645,7 +645,7 @@ class test_vimconn_get_network(test_base):
         # creating new network
         self.__class__.network_name = _get_random_string(20)
         self.__class__.net_type = 'bridge'
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                           net_type=self.__class__.net_type)
         self.__class__.network_id = network
         logger.debug("{}".format(network))
@@ -690,7 +690,7 @@ class test_vimconn_delete_network(test_base):
         # Creating network
         self.__class__.network_name = _get_random_string(20)
         self.__class__.net_type = 'bridge'
-        network = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        network, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                           net_type=self.__class__.net_type)
         self.__class__.network_id = network
         logger.debug("{}".format(network))
@@ -840,87 +840,87 @@ class test_vimconn_new_flavor(test_base):
 
         self.assertEqual((context.exception).http_code, 404)
 
-class test_vimconn_new_image(test_base):
-
-    def test_000_new_image(self):
-        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
-                                                            self.__class__.test_index,
-                                                inspect.currentframe().f_code.co_name)
-        self.__class__.test_index += 1
-
-        image_path = test_config['image_path']
-        if image_path:
-            self.__class__.image_id = test_config["vim_conn"].new_image({ 'name': 'TestImage', 'location' : image_path, 'metadata': {'upload_location':None} })
-            time.sleep(20)
-
-            self.assertIsInstance(self.__class__.image_id, (str, unicode))
-            self.assertIsInstance(uuid.UUID(self.__class__.image_id), uuid.UUID)
-        else:
-            self.skipTest("Skipping test as image file not present at RO container")
-
-    def test_010_new_image_negative(self):
-        Non_exist_image_path = '/temp1/cirros.ovf'
-
-        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
-                                                            self.__class__.test_index,
-                                                inspect.currentframe().f_code.co_name)
-        self.__class__.test_index += 1
-
-        with self.assertRaises(Exception) as context:
-            test_config["vim_conn"].new_image({ 'name': 'TestImage', 'location' : Non_exist_image_path})
-
-        self.assertEqual((context.exception).http_code, 400)
-
-    def test_020_delete_image(self):
-        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
-                                                            self.__class__.test_index,
-                                                inspect.currentframe().f_code.co_name)
-        self.__class__.test_index += 1
-
-        image_id = test_config["vim_conn"].delete_image(self.__class__.image_id)
-
-        self.assertIsInstance(image_id, (str, unicode))
-
-    def test_030_delete_image_negative(self):
-        Non_exist_image_id = str(uuid.uuid4())
-
-        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
-                                                            self.__class__.test_index,
-                                                inspect.currentframe().f_code.co_name)
-        self.__class__.test_index += 1
-
-        with self.assertRaises(Exception) as context:
-            test_config["vim_conn"].delete_image(Non_exist_image_id)
-
-        self.assertEqual((context.exception).http_code, 404)
-
-class test_vimconn_get_image_id_from_path(test_base):
-
-    def test_000_get_image_id_from_path(self):
-        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
-                                                            self.__class__.test_index,
-                                                inspect.currentframe().f_code.co_name)
-        self.__class__.test_index += 1
-
-        image_path = test_config['image_path']
-        if image_path:
-            image_id = test_config["vim_conn"].get_image_id_from_path( image_path )
-            self.assertEqual(type(image_id),str)
-        else:
-            self.skipTest("Skipping test as image file not present at RO container")
-
-    def test_010_get_image_id_from_path_negative(self):
-        Non_exist_image_path = '/temp1/cirros.ovf'
-
-        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
-                                                            self.__class__.test_index,
-                                                inspect.currentframe().f_code.co_name)
-        self.__class__.test_index += 1
-
-        with self.assertRaises(Exception) as context:
-            test_config["vim_conn"].new_image({ 'name': 'TestImage', 'location' : Non_exist_image_path })
+# class test_vimconn_new_image(test_base):
+#
+#     def test_000_new_image(self):
+#         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+#                                                             self.__class__.test_index,
+#                                                 inspect.currentframe().f_code.co_name)
+#         self.__class__.test_index += 1
+#
+#         image_path = test_config['image_path']
+#         if image_path:
+#             self.__class__.image_id = test_config["vim_conn"].new_image({ 'name': 'TestImage', 'location' : image_path, 'metadata': {'upload_location':None} })
+#             time.sleep(20)
+#
+#             self.assertIsInstance(self.__class__.image_id, (str, unicode))
+#             self.assertIsInstance(uuid.UUID(self.__class__.image_id), uuid.UUID)
+#         else:
+#             self.skipTest("Skipping test as image file not present at RO container")
+#
+#     def test_010_new_image_negative(self):
+#         Non_exist_image_path = '/temp1/cirros.ovf'
+#
+#         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+#                                                             self.__class__.test_index,
+#                                                 inspect.currentframe().f_code.co_name)
+#         self.__class__.test_index += 1
+#
+#         with self.assertRaises(Exception) as context:
+#             test_config["vim_conn"].new_image({ 'name': 'TestImage', 'location' : Non_exist_image_path})
+#
+#         self.assertEqual((context.exception).http_code, 400)
+#
+#     def test_020_delete_image(self):
+#         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+#                                                             self.__class__.test_index,
+#                                                 inspect.currentframe().f_code.co_name)
+#         self.__class__.test_index += 1
+#
+#         image_id = test_config["vim_conn"].delete_image(self.__class__.image_id)
+#
+#         self.assertIsInstance(image_id, (str, unicode))
+#
+#     def test_030_delete_image_negative(self):
+#         Non_exist_image_id = str(uuid.uuid4())
+#
+#         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+#                                                             self.__class__.test_index,
+#                                                 inspect.currentframe().f_code.co_name)
+#         self.__class__.test_index += 1
+#
+#         with self.assertRaises(Exception) as context:
+#             test_config["vim_conn"].delete_image(Non_exist_image_id)
+#
+#         self.assertEqual((context.exception).http_code, 404)
 
-        self.assertEqual((context.exception).http_code, 400)
+# class test_vimconn_get_image_id_from_path(test_base):
+#
+#     def test_000_get_image_id_from_path(self):
+#         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+#                                                             self.__class__.test_index,
+#                                                 inspect.currentframe().f_code.co_name)
+#         self.__class__.test_index += 1
+#
+#         image_path = test_config['image_path']
+#         if image_path:
+#             image_id = test_config["vim_conn"].get_image_id_from_path( image_path )
+#             self.assertEqual(type(image_id),str)
+#         else:
+#             self.skipTest("Skipping test as image file not present at RO container")
+#
+#     def test_010_get_image_id_from_path_negative(self):
+#         Non_exist_image_path = '/temp1/cirros.ovf'
+#
+#         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+#                                                             self.__class__.test_index,
+#                                                 inspect.currentframe().f_code.co_name)
+#         self.__class__.test_index += 1
+#
+#         with self.assertRaises(Exception) as context:
+#             test_config["vim_conn"].new_image({ 'name': 'TestImage', 'location' : Non_exist_image_path })
+#
+#         self.assertEqual((context.exception).http_code, 400)
 
 class test_vimconn_get_image_list(test_base):
     image_name = None
@@ -991,8 +991,21 @@ class test_vimconn_new_vminstance(test_base):
         self.__class__.network_name = _get_random_string(20)
         self.__class__.net_type = 'bridge'
 
-        self.__class__.network_id = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
+        self.__class__.network_id, _ = test_config["vim_conn"].new_network(net_name=self.__class__.network_name,
                                                                             net_type=self.__class__.net_type)
+        # find image name and image id
+        if test_config['image_name']:
+            image_list = test_config['vim_conn'].get_image_list({'name': test_config['image_name']})
+            if len(image_list) == 0:
+                raise Exception("Image {} is not found at VIM".format(test_config['image_name']))
+            else:
+                self.__class__.image_id = image_list[0]['id']
+        else:
+            image_list = test_config['vim_conn'].get_image_list()
+            if len(image_list) == 0:
+                raise Exception("Not found any image at VIM")
+            else:
+                self.__class__.image_id = image_list[0]['id']
 
     def tearDown(self):
         test_base.tearDown(self)
@@ -1012,19 +1025,6 @@ class test_vimconn_new_vminstance(test_base):
         # create new flavor
         flavor_id = test_config["vim_conn"].new_flavor(flavor_data)
 
-        # find image name and image id
-        if test_config['image_name']:
-            image_list = test_config['vim_conn'].get_image_list({'name': test_config['image_name']})
-            if len(image_list) == 0:
-                raise Exception("Image {} is not found at VIM".format(test_config['image_name']))
-            else:
-                self.__class__.image_id = image_list[0]['id']
-        else:
-            image_list = test_config['vim_conn'].get_image_list()
-            if len(image_list) == 0:
-                raise Exception("Not found any image at VIM")
-            else:
-                self.__class__.image_id = image_list[0]['id']
 
         self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
                                                             self.__class__.test_index,
@@ -1113,7 +1113,7 @@ class test_vimconn_new_vminstance(test_base):
             network_name = _get_random_string(20)
             net_type = 'data'
 
-            network_id = test_config["vim_conn"].new_network(net_name=network_name,
+            network_id, _ = test_config["vim_conn"].new_network(net_name=network_name,
                                                                             net_type=net_type)
             net_list = [{'use': net_type, 'name': name, 'floating_ip': False, 'port_security': True,
                          'type': _type, 'net_id': network_id}]
@@ -1383,6 +1383,38 @@ class test_vimconn_new_vminstance(test_base):
         test_config["vim_conn"].delete_vminstance(self.__class__.instance_id)
         time.sleep(10)
 
+    def test_140_new_vminstance_sriov(self):
+        logger.info("Testing creation of sriov vm instance using {}".format(test_config['sriov_net_name']))
+        flavor_data = {'name': _get_random_string(20),'ram': 1024, 'vcpus': 2, 'disk': 10}
+        name = 'eth0'
+
+        # create new flavor
+        flavor_id = test_config["vim_conn"].new_flavor(flavor_data)
+
+        self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"],
+                                                            self.__class__.test_index,
+                                                inspect.currentframe().f_code.co_name)
+        self.__class__.test_index += 1
+
+        sriov_net_name = test_config['sriov_net_name']
+        new_network_list = test_config["vim_conn"].get_network_list({'name': sriov_net_name})
+        for list_item in new_network_list:
+            self.assertEqual(sriov_net_name, list_item.get('name'))
+            self.__class__.sriov_network_id = list_item.get('id')
+
+        net_list = [{'use': 'data', 'name': name, 'floating_ip': False, 'port_security': True, 'type': 'VF', 'net_id': self.__class__.sriov_network_id}]
+
+        instance_id, _ = test_config["vim_conn"].new_vminstance(name='Test1_sriov_vm', description='', start=False, image_id=self.__class__.image_id, flavor_id=flavor_id, net_list=net_list)
+
+        self.assertIsInstance(instance_id, (str, unicode))
+
+        logger.info("Waiting for created sriov-vm intance")
+        time.sleep(10)
+        # Deleting created vm instance
+        logger.info("Deleting created sriov-vm intance")
+        test_config["vim_conn"].delete_vminstance(instance_id)
+        time.sleep(10)
+
 class test_vimconn_get_tenant_list(test_base):
     tenant_id = None
 
@@ -1533,7 +1565,7 @@ class test_vimconn_vminstance_by_ip_address(test_base):
         # create network
         self.network_name = _get_random_string(20)
 
-        self.network_id = test_config["vim_conn"].new_network(net_name=self.network_name,
+        self.network_id, _ = test_config["vim_conn"].new_network(net_name=self.network_name,
                                                                        net_type='bridge')
 
     def tearDown(self):
@@ -1668,7 +1700,7 @@ class test_vimconn_vminstance_by_adding_10_nics(test_base):
         i = 0
         for i in range(10):
             self.network_name = _get_random_string(20)
-            network_id = test_config["vim_conn"].new_network(net_name=self.network_name,
+            network_id, _ = test_config["vim_conn"].new_network(net_name=self.network_name,
                                                                       net_type='bridge')
             self.net_ids.append(network_id)
 
@@ -1720,7 +1752,7 @@ class test_vimconn_vminstance_by_existing_disk(test_base):
     def setUp(self):
         # create network
         self.network_name = _get_random_string(20)
-        self.network_id = test_config["vim_conn"].new_network(net_name=self.network_name,
+        self.network_id, _ = test_config["vim_conn"].new_network(net_name=self.network_name,
                                                                        net_type='bridge')
 
     def tearDown(self):
@@ -1831,7 +1863,7 @@ class test_vimconn_vminstance_by_affinity_anti_affinity(test_base):
     def setUp(self):
         # create network
         self.network_name = _get_random_string(20)
-        self.network_id = test_config["vim_conn"].new_network(net_name=self.network_name,
+        self.network_id, _ = test_config["vim_conn"].new_network(net_name=self.network_name,
                                                                        net_type='bridge')
 
     def tearDown(self):
@@ -1881,7 +1913,7 @@ class test_vimconn_vminstance_by_numa_affinity(test_base):
     def setUp(self):
         # create network
         self.network_name = _get_random_string(20)
-        self.network_id = test_config["vim_conn"].new_network(net_name=self.network_name,
+        self.network_id, _ = test_config["vim_conn"].new_network(net_name=self.network_name,
                                                                        net_type='bridge')
 
     def tearDown(self):
@@ -2096,6 +2128,7 @@ def test_vimconnector(args):
         vim_url = args.endpoint_url
         test_config['image_path'] = args.image_path
         test_config['image_name'] = args.image_name
+        test_config['sriov_net_name'] = args.sriov_net_name
 
         # vmware connector obj
         test_config['vim_conn'] = vim.vimconnector(name=org_name, tenant_name=tenant_name, user=org_user,passwd=org_passwd, url=vim_url, config=config_params)
@@ -2115,6 +2148,7 @@ def test_vimconnector(args):
         vim_url = args.endpoint_url
         test_config['image_path'] = args.image_path
         test_config['image_name'] = args.image_name
+        test_config['sriov_net_name'] = args.sriov_net_name
 
         # openstack connector obj
         vim_persistent_info = {}
@@ -2253,6 +2287,72 @@ def test_vim(args):
     return executed, failed
 
 
+def test_wim(args):
+    global test_config
+    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/osm_ro")
+    import openmanoclient
+    executed = 0
+    failed = 0
+    test_config["client"] = openmanoclient.openmanoclient(
+        endpoint_url=args.endpoint_url,
+        tenant_name=args.tenant_name,
+        datacenter_name=args.datacenter,
+        debug=args.debug, logger=test_config["logger_name"])
+    clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
+    # If only want to obtain a tests list print it and exit
+    if args.list_tests:
+        tests_names = []
+        for cls in clsmembers:
+            if cls[0].startswith('test_WIM'):
+                tests_names.append(cls[0])
+
+        msg = "The 'wim' set tests are:\n\t" + ', '.join(sorted(tests_names)) +\
+              "\nNOTE: The test test_VIM_tenant_operations will fail in case the used datacenter is type OpenStack " \
+              "unless RO has access to the admin endpoint. Therefore this test is excluded by default"
+        print(msg)
+        logger.info(msg)
+        sys.exit(0)
+
+    # Create the list of tests to be run
+    code_based_tests = []
+    if args.tests:
+        for test in args.tests:
+            for t in test.split(','):
+                matches_code_based_tests = [item for item in clsmembers if item[0] == t]
+                if len(matches_code_based_tests) > 0:
+                    code_based_tests.append(matches_code_based_tests[0][1])
+                else:
+                    logger.critical("Test '{}' is not among the possible ones".format(t))
+                    sys.exit(1)
+    if not code_based_tests:
+        # include all tests
+        for cls in clsmembers:
+            # We exclude 'test_VIM_tenant_operations' unless it is specifically requested by the user
+            if cls[0].startswith('test_VIM') and cls[0] != 'test_VIM_tenant_operations':
+                code_based_tests.append(cls[1])
+
+    logger.debug("tests to be executed: {}".format(code_based_tests))
+
+    # TextTestRunner stream is set to /dev/null in order to avoid the method to directly print the result of tests.
+    # This is handled in the tests using logging.
+    stream = open('/dev/null', 'w')
+
+    # Run code based tests
+    basic_tests_suite = unittest.TestSuite()
+    for test in code_based_tests:
+        basic_tests_suite.addTest(unittest.makeSuite(test))
+    result = unittest.TextTestRunner(stream=stream, failfast=failfast).run(basic_tests_suite)
+    executed += result.testsRun
+    failed += len(result.failures) + len(result.errors)
+    if failfast and failed:
+        sys.exit(1)
+    if len(result.failures) > 0:
+        logger.debug("failures : {}".format(result.failures))
+    if len(result.errors) > 0:
+        logger.debug("errors : {}".format(result.errors))
+    return executed, failed
+
+
 def test_deploy(args):
     global test_config
     sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/osm_ro")
@@ -2378,6 +2478,7 @@ if __name__=="__main__":
     vimconn_parser.add_argument('-n', '--image-name', dest='image_name', help="Provide image name for test")
     # TODO add optional arguments for vimconn tests
     # vimconn_parser.add_argument("-i", '--image-name', dest='image_name', help='<HELP>'))
+    vimconn_parser.add_argument('-s', '--sriov-net-name', dest='sriov_net_name', help="Provide SRIOV network name for test")
 
     # Datacenter test set
     # -------------------
@@ -2392,6 +2493,19 @@ if __name__=="__main__":
     vimconn_parser.add_argument('-u', '--url', dest='endpoint_url', default='http://localhost:9090/openmano',
                                help="Set the openmano server url. By default 'http://localhost:9090/openmano'")
 
+    # WIM test set
+    # -------------------
+    vimconn_parser = subparsers.add_parser('wim', parents=[parent_parser], help="test wim")
+    vimconn_parser.set_defaults(func=test_wim)
+
+    # Mandatory arguments
+    mandatory_arguments = vimconn_parser.add_argument_group('mandatory arguments')
+    mandatory_arguments.add_argument('-d', '--datacenter', required=True, help='Set the datacenter to test')
+
+    # Optional arguments
+    vimconn_parser.add_argument('-u', '--url', dest='endpoint_url', default='http://localhost:9090/openmano',
+                                help="Set the openmano server url. By default 'http://localhost:9090/openmano'")
+
     argcomplete.autocomplete(parser)
     args = parser.parse_args()
     # print str(args)