]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Completing Unit Tests
authorSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Thu, 25 Aug 2011 11:49:38 +0000 (12:49 +0100)
committerSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Thu, 25 Aug 2011 11:49:38 +0000 (12:49 +0100)
quantum/cli.py
quantum/cli_output.template
tests/unit/test_cli.py

index 15f64fab6fa267f4de4a4b079dad9248859d5676..5ebdde26830e287e1041484ef78f2a0d7808513b 100644 (file)
@@ -130,7 +130,9 @@ def list_ports(client, *args):
     try:
         ports = client.list_ports(network_id)
         LOG.debug("Operation 'list_ports' executed.")
-        output = prepare_output("list_ports", tenant_id, dict(ports=ports))
+        data = ports
+        data['network_id'] = network_id
+        output = prepare_output("list_ports", tenant_id, data)
         print output
     except Exception as ex:
         _handle_exception(ex)
@@ -167,7 +169,7 @@ def delete_port(client, *args):
 def detail_port(client, *args):
     tenant_id, network_id, port_id = args
     try:
-        port = client.list_port_details(network_id, port_id)["ports"]["port"]
+        port = client.list_port_details(network_id, port_id)["port"]
         LOG.debug("Operation 'list_port_details' executed.")
         #NOTE(salvatore-orland): current API implementation does not
         #return attachment with GET operation on port. Once API alignment
@@ -188,9 +190,9 @@ def set_port_state(client, *args):
         client.set_port_state(network_id, port_id, data)
         LOG.debug("Operation 'set_port_state' executed.")
         # Response has no body. Use data for populating output
-        data['id'] = port_id
-        output = prepare_output("set_port_state", tenant_id,
-                                dict(network_id=network_id, port=data))
+        data['network_id'] = network_id
+        data['port']['id'] = port_id
+        output = prepare_output("set_port_state", tenant_id, data)
         print output
     except Exception as ex:
         _handle_exception(ex)
index 1e1abbd2bdec6f4ee95fa29e1b33002a81737af0..e2857bdd21f812f532384007eba05f1fecd1e4c3 100644 (file)
@@ -30,6 +30,11 @@ for tenant: $tenant_id
 Deleted Logical Port with ID: $port_id
 on Virtual Network: $network_id
 for tenant: $tenant_id
+#elif $cmd == 'set_port_state'
+Updated state for Logical Port with ID: $port.id
+New state is: $port['port-state']
+on Virtual Network: $network_id
+for tenant: $tenant_id
 #elif $cmd == 'detail_port'
 Logical Port ID: $port.id
 On Virtual Network: $network_id
index f9aee4317ecc1ff12be4f969b863fd86750fe689..2b814eadabfb772b3ce64602b3be84ee75838632 100644 (file)
@@ -106,6 +106,73 @@ class CLITest(unittest.TestCase):
             # Verify!
             # Must add newline at the end to match effect of print call
             self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+            
+    def _verify_list_ports(self, network_id):
+            # Verification - get raw result from db
+            port_list = db.port_list(network_id)
+            ports = [dict(id=port.uuid, state=port.state)
+                     for port in port_list]
+            # Fill CLI template
+            output = cli.prepare_output('list_ports', self.tenant_id,
+                                        dict(network_id=network_id,
+                                             ports=ports))
+            # Verify!
+            # Must add newline at the end to match effect of print call
+            self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+            
+    def _verify_create_port(self, network_id):
+            # Verification - get raw result from db
+            port_list = db.port_list(network_id)
+            if len(port_list) != 1:
+                self.fail("No port created")
+            port_id = port_list[0].uuid
+            # Fill CLI template
+            output = cli.prepare_output('create_port', self.tenant_id,
+                                        dict(network_id=network_id,
+                                             port_id=port_id))
+            # Verify!
+            # Must add newline at the end to match effect of print call
+            self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+
+    def _verify_delete_port(self, network_id, port_id):
+            # Verification - get raw result from db
+            port_list = db.port_list(network_id)
+            if len(port_list) != 0:
+                self.fail("DB should not contain any port")
+            # Fill CLI template
+            output = cli.prepare_output('delete_port', self.tenant_id,
+                                        dict(network_id=network_id,
+                                             port_id=port_id))
+            # Verify!
+            # Must add newline at the end to match effect of print call
+            self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+            
+    def _verify_set_port_state(self, network_id, port_id):
+            # Verification - get raw result from db
+            port = db.port_get(port_id, network_id)
+            port_data = {'id': port.uuid, 'port-state': port.state}
+            # Fill CLI template
+            output = cli.prepare_output('set_port_state', self.tenant_id,
+                                        dict(network_id=network_id,
+                                             port=port_data))
+            # Verify!
+            # Must add newline at the end to match effect of print call
+            self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+
+    def _verify_detail_port(self, network_id, port_id):
+            # Verification - get raw result from db
+            # TODO(salvatore-orlando): Must resolve this issue with
+            # attachment in separate bug fix.
+            port = db.port_get(port_id, network_id)
+            port_data = {'id': port.uuid, 'state': port.state,
+                         'attachment':'<unavailable>'}
+            # Fill CLI template
+            output = cli.prepare_output('detail_port', self.tenant_id,
+                                        dict(network_id=network_id,
+                                             port=port_data))
+            # Verify!
+            # Must add newline at the end to match effect of print call
+            self.assertEquals(self.fake_stdout.make_string(), output + '\n')
 
     def test_list_networks(self):
         try:
@@ -114,35 +181,38 @@ class CLITest(unittest.TestCase):
             db.network_create(self.tenant_id, self.network_name_2)
 
             cli.list_nets(self.client, self.tenant_id)
-            LOG.debug("Operation completed. Verifying result")
-            LOG.debug(self.fake_stdout.content)
-            self._verify_list_networks()
         except:
             LOG.exception("Exception caught: %s", sys.exc_info())
             self.fail("test_list_networks failed due to an exception")
 
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_list_networks()
+
     def test_create_network(self):
         try:
             cli.create_net(self.client, self.tenant_id, "test")
-            LOG.debug("Operation completed. Verifying result")
-            LOG.debug(self.fake_stdout.content)
-            self._verify_create_network()
         except:
             LOG.exception("Exception caught: %s", sys.exc_info())
             self.fail("test_create_network failed due to an exception")
 
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_create_network()
+
     def test_delete_network(self):
         try:
             db.network_create(self.tenant_id, self.network_name_1)
             network_id = db.network_list(self.tenant_id)[0]['uuid']
             cli.delete_net(self.client, self.tenant_id, network_id)
-            LOG.debug("Operation completed. Verifying result")
-            LOG.debug(self.fake_stdout.content)
-            self._verify_delete_network(network_id)
         except:
             LOG.exception("Exception caught: %s", sys.exc_info())
             self.fail("test_delete_network failed due to an exception")
 
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_delete_network(network_id)
+
     def test_detail_network(self):
         # Load some data into the datbase
         net = db.network_create(self.tenant_id, self.network_name_1)
@@ -157,7 +227,94 @@ class CLITest(unittest.TestCase):
             network_id = net['uuid']
             cli.rename_net(self.client, self.tenant_id,
                            network_id, self.network_name_2)
-            self._verify_rename_network()
         except:
             LOG.exception("Exception caught: %s", sys.exc_info())
             self.fail("test_rename_network failed due to an exception")
+
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_rename_network()
+            
+    def test_list_ports(self):
+        try:
+            # Pre-populate data for testing using db api
+            net = db.network_create(self.tenant_id, self.network_name_1)
+            network_id = net['uuid']
+            db.port_create(network_id)
+            db.port_create(network_id)
+            cli.list_ports(self.client, self.tenant_id, network_id)
+        except:
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_list_ports failed due to an exception")
+            
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_list_ports(network_id)    
+        
+    def test_create_port(self):
+        network_id = None
+        try:
+            # Pre-populate data for testing using db api
+            net = db.network_create(self.tenant_id, self.network_name_1)
+            network_id = net['uuid']
+            cli.create_port(self.client, self.tenant_id, network_id)
+        except:
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_create_port failed due to an exception")
+
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_create_port(network_id)
+        
+    def test_delete_port(self):
+        network_id = None
+        port_id = None
+        try:
+            # Pre-populate data for testing using db api
+            net = db.network_create(self.tenant_id, self.network_name_1)
+            network_id = net['uuid']
+            port = db.port_create(network_id)
+            port_id = port['uuid']
+            cli.delete_port(self.client, self.tenant_id, network_id, port_id)
+        except:
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_delete_port failed due to an exception")
+
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_delete_port(network_id, port_id)
+
+    def test_set_port_state(self):
+        try:
+            net = db.network_create(self.tenant_id, self.network_name_1)
+            network_id = net['uuid']
+            port = db.port_create(network_id)
+            port_id = port['uuid']
+            # Default state is DOWN - change to ACTIVE.
+            cli.set_port_state(self.client, self.tenant_id, network_id,
+                               port_id, 'ACTIVE')
+        except:
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_set_port_state failed due to an exception")
+
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_set_port_state(network_id, port_id)
+        
+    def test_detail_port(self):
+        network_id = None
+        port_id = None
+        try:
+            # Pre-populate data for testing using db api
+            net = db.network_create(self.tenant_id, self.network_name_1)
+            network_id = net['uuid']
+            port = db.port_create(network_id)
+            port_id = port['uuid']
+            cli.detail_port(self.client, self.tenant_id, network_id, port_id)
+        except:
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_detail_port failed due to an exception")
+
+        LOG.debug("Operation completed. Verifying result")
+        LOG.debug(self.fake_stdout.content)
+        self._verify_detail_port(network_id, port_id)