Adds 3 additional tests to test_flavor.py script

Adds three additional scenarios to flavor-manage functionality.
The test cases are added to the script test_flavors.py present in
"/tempest/tempest/tests/compute/admin" folder. Also adds try and
finally blocks to the already existing test cases.
Modified the "create_flavor" method in flavors_client.py and
made extra data as optional paramters instead of mandatory ones.

Change-Id: I8791dfc0cf1df6c1788b5c7e05f99e6eda989823
Implements: blueprint add-tests-flavor-manage
diff --git a/tempest/services/compute/json/flavors_client.py b/tempest/services/compute/json/flavors_client.py
index 56546de..bd339b2 100644
--- a/tempest/services/compute/json/flavors_client.py
+++ b/tempest/services/compute/json/flavors_client.py
@@ -51,20 +51,23 @@
         body = json.loads(body)
         return resp, body['flavor']
 
-    def create_flavor(self, name, ram, vcpus, disk, ephemeral, flavor_id,
-                      swap, rxtx):
+    def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
         """Creates a new flavor or instance type."""
         post_body = {
             'name': name,
             'ram': ram,
             'vcpus': vcpus,
             'disk': disk,
-            'OS-FLV-EXT-DATA:ephemeral': ephemeral,
             'id': flavor_id,
-            'swap': swap,
-            'rxtx_factor': rxtx,
         }
-
+        if kwargs.get('ephemeral'):
+            post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
+        if kwargs.get('swap'):
+            post_body['swap'] = kwargs.get('swap')
+        if kwargs.get('rxtx'):
+            post_body['rxtx_factor'] = kwargs.get('rxtx')
+        if kwargs.get('is_public'):
+            post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
         post_body = json.dumps({'flavor': post_body})
         resp, body = self.post('flavors', post_body, self.headers)
 
@@ -74,3 +77,13 @@
     def delete_flavor(self, flavor_id):
         """Deletes the given flavor."""
         return self.delete("flavors/%s" % str(flavor_id))
+
+    def is_resource_deleted(self, id):
+        #Did not use get_flavor_details(id) for verification as it gives
+        #200 ok even for deleted id. LP #981263
+        #we can remove the loop here and use get by ID when bug gets sortedout
+        resp, flavors = self.list_flavors_with_detail()
+        for flavor in flavors:
+            if flavor['id'] == id:
+                return False
+        return True
diff --git a/tempest/services/compute/xml/flavors_client.py b/tempest/services/compute/xml/flavors_client.py
index 2a8ad0d..c011fe4 100644
--- a/tempest/services/compute/xml/flavors_client.py
+++ b/tempest/services/compute/xml/flavors_client.py
@@ -27,7 +27,9 @@
 
 
 XMLNS_OS_FLV_EXT_DATA = \
-        "http://docs.openstack.org/compute/ext/flavor_extra_data/api/v1.1"
+    "http://docs.openstack.org/compute/ext/flavor_extra_data/api/v1.1"
+XMLNS_OS_FLV_ACCESS = \
+    "http://docs.openstack.org/compute/ext/flavor_access/api/v1.1"
 
 
 class FlavorsClientXML(RestClientXML):
@@ -84,8 +86,7 @@
         flavor = self._format_flavor(body)
         return resp, flavor
 
-    def create_flavor(self, name, ram, vcpus, disk, ephemeral, flavor_id,
-                      swap, rxtx):
+    def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
         """Creates a new flavor or instance type."""
         flavor = Element("flavor",
                          xmlns=XMLNS_11,
@@ -93,12 +94,19 @@
                          vcpus=vcpus,
                          disk=disk,
                          id=flavor_id,
-                         swap=swap,
-                         rxtx_factor=rxtx,
                          name=name)
+        if kwargs.get('rxtx'):
+            flavor.add_attr('rxtx_factor', kwargs.get('rxtx'))
+        if kwargs.get('swap'):
+            flavor.add_attr('swap', kwargs.get('swap'))
+        if kwargs.get('ephemeral'):
+            flavor.add_attr('OS-FLV-EXT-DATA:ephemeral',
+                            kwargs.get('ephemeral'))
+        if kwargs.get('is_public'):
+            flavor.add_attr('os-flavor-access:is_public',
+                            kwargs.get('is_public'))
         flavor.add_attr('xmlns:OS-FLV-EXT-DATA', XMLNS_OS_FLV_EXT_DATA)
-        flavor.add_attr('OS-FLV-EXT-DATA:ephemeral', ephemeral)
-
+        flavor.add_attr('xmlns:os-flavor-access', XMLNS_OS_FLV_ACCESS)
         resp, body = self.post('flavors', str(Document(flavor)), self.headers)
         body = xml_to_json(etree.fromstring(body))
         flavor = self._format_flavor(body)
@@ -107,3 +115,13 @@
     def delete_flavor(self, flavor_id):
         """Deletes the given flavor."""
         return self.delete("flavors/%s" % str(flavor_id), self.headers)
+
+    def is_resource_deleted(self, id):
+        #Did not use get_flavor_details(id) for verification as it gives
+        #200 ok even for deleted id. LP #981263
+        #we can remove the loop here and use get by ID when bug gets sortedout
+        resp, flavors = self.list_flavors_with_detail()
+        for flavor in flavors:
+            if flavor['id'] == id:
+                return False
+        return True
diff --git a/tempest/tests/compute/admin/test_flavors.py b/tempest/tests/compute/admin/test_flavors.py
index b5ee13a..8172bd4 100644
--- a/tempest/tests/compute/admin/test_flavors.py
+++ b/tempest/tests/compute/admin/test_flavors.py
@@ -29,8 +29,8 @@
     Tests Flavors API Create and Delete that require admin privileges
     """
 
-    @staticmethod
-    def setUpClass(cls):
+    @classmethod
+    def setUpClass(self, cls):
         if not compute.FLAVOR_EXTRA_DATA_ENABLED:
             msg = "FlavorExtraData extension not enabled."
             raise nose.SkipTest(msg)
@@ -43,61 +43,77 @@
         cls.ephemeral = 10
         cls.new_flavor_id = 1234
         cls.swap = 1024
-        cls.rxtx = 1
+        cls.rxtx = 2
 
     @attr(type='positive')
     def test_create_flavor(self):
         # Create a flavor and ensure it is listed
         # This operation requires the user to have 'admin' role
-        #Create the flavor
-        resp, flavor = self.client.create_flavor(self.flavor_name,
-                                                 self.ram, self.vcpus,
-                                                 self.disk,
-                                                 self.ephemeral,
-                                                 self.new_flavor_id,
-                                                 self.swap, self.rxtx)
-        self.assertEqual(200, resp.status)
-        self.assertEqual(flavor['name'], self.flavor_name)
-        self.assertEqual(flavor['vcpus'], self.vcpus)
-        self.assertEqual(flavor['disk'], self.disk)
-        self.assertEqual(flavor['ram'], self.ram)
-        self.assertEqual(int(flavor['id']), self.new_flavor_id)
-        self.assertEqual(flavor['swap'], self.swap)
-        self.assertEqual(flavor['rxtx_factor'], self.rxtx)
-        self.assertEqual(flavor['OS-FLV-EXT-DATA:ephemeral'], self.ephemeral)
+        try:
+            #Create the flavor
+            resp, flavor = self.client.create_flavor(self.flavor_name,
+                                                     self.ram, self.vcpus,
+                                                     self.disk,
+                                                     self.new_flavor_id,
+                                                     ephemeral=self.ephemeral,
+                                                     swap=self.swap,
+                                                     rxtx=self.rxtx)
+            self.assertEqual(200, resp.status)
+            self.assertEqual(flavor['name'], self.flavor_name)
+            self.assertEqual(flavor['vcpus'], self.vcpus)
+            self.assertEqual(flavor['disk'], self.disk)
+            self.assertEqual(flavor['ram'], self.ram)
+            self.assertEqual(int(flavor['id']), self.new_flavor_id)
+            self.assertEqual(flavor['swap'], self.swap)
+            self.assertEqual(flavor['rxtx_factor'], self.rxtx)
+            self.assertEqual(flavor['OS-FLV-EXT-DATA:ephemeral'],
+                             self.ephemeral)
+            if self._interface == "xml":
+                XMLNS_OS_FLV_ACCESS = "http://docs.openstack.org/compute/ext/"\
+                    "flavor_access/api/v2"
+                key = "{" + XMLNS_OS_FLV_ACCESS + "}is_public"
+                self.assertEqual(flavor[key], "True")
+            if self._interface == "json":
+                self.assertEqual(flavor['os-flavor-access:is_public'], True)
 
-        #Verify flavor is retrieved
-        resp, flavor = self.client.get_flavor_details(self.new_flavor_id)
-        self.assertEqual(resp.status, 200)
-        self.assertEqual(flavor['name'], self.flavor_name)
+            #Verify flavor is retrieved
+            resp, flavor = self.client.get_flavor_details(self.new_flavor_id)
+            self.assertEqual(resp.status, 200)
+            self.assertEqual(flavor['name'], self.flavor_name)
 
-        #Delete the flavor
-        resp, body = self.client.delete_flavor(flavor['id'])
-        self.assertEqual(resp.status, 202)
+        finally:
+            #Delete the flavor
+            resp, body = self.client.delete_flavor(self.new_flavor_id)
+            self.assertEqual(resp.status, 202)
+            self.client.wait_for_resource_deletion(self.new_flavor_id)
 
     @attr(type='positive')
     def test_create_flavor_verify_entry_in_list_details(self):
         # Create a flavor and ensure it's details are listed
         # This operation requires the user to have 'admin' role
-        #Create the flavor
-        resp, flavor = self.client.create_flavor(self.flavor_name,
-                                                 self.ram, self.vcpus,
-                                                 self.disk,
-                                                 self.ephemeral,
-                                                 self.new_flavor_id,
-                                                 self.swap, self.rxtx)
-        flag = False
-        #Verify flavor is retrieved
-        resp, flavors = self.client.list_flavors_with_detail()
-        self.assertEqual(resp.status, 200)
-        for flavor in flavors:
-            if flavor['name'] == self.flavor_name:
-                flag = True
-        self.assertTrue(flag)
+        try:
+            #Create the flavor
+            resp, flavor = self.client.create_flavor(self.flavor_name,
+                                                     self.ram, self.vcpus,
+                                                     self.disk,
+                                                     self.new_flavor_id,
+                                                     ephemeral=self.ephemeral,
+                                                     swap=self.swap,
+                                                     rxtx=self.rxtx)
+            flag = False
+            #Verify flavor is retrieved
+            resp, flavors = self.client.list_flavors_with_detail()
+            self.assertEqual(resp.status, 200)
+            for flavor in flavors:
+                if flavor['name'] == self.flavor_name:
+                    flag = True
+            self.assertTrue(flag)
 
-        #Delete the flavor
-        resp, body = self.client.delete_flavor(self.new_flavor_id)
-        self.assertEqual(resp.status, 202)
+        finally:
+            #Delete the flavor
+            resp, body = self.client.delete_flavor(self.new_flavor_id)
+            self.assertEqual(resp.status, 202)
+            self.client.wait_for_resource_deletion(self.new_flavor_id)
 
     @attr(type='negative')
     def test_get_flavor_details_for_deleted_flavor(self):
@@ -106,9 +122,10 @@
         resp, flavor = self.client.create_flavor(self.flavor_name,
                                                  self.ram,
                                                  self.vcpus, self.disk,
-                                                 self.ephemeral,
                                                  self.new_flavor_id,
-                                                 self.swap, self.rxtx)
+                                                 ephemeral=self.ephemeral,
+                                                 swap=self.swap,
+                                                 rxtx=self.rxtx)
         self.assertEquals(200, resp.status)
 
         # Delete the flavor
@@ -129,20 +146,118 @@
                 flag = False
         self.assertTrue(flag)
 
+    def test_create_list_flavor_without_extra_data(self):
+        #Create a flavor and ensure it is listed
+        #This operation requires the user to have 'admin' role
+        try:
+            #Create the flavor
+            resp, flavor = self.client.create_flavor(self.flavor_name,
+                                                     self.ram, self.vcpus,
+                                                     self.disk,
+                                                     self.new_flavor_id)
+            self.assertEqual(200, resp.status)
+            self.assertEqual(flavor['name'], self.flavor_name)
+            self.assertEqual(flavor['ram'], self.ram)
+            self.assertEqual(flavor['vcpus'], self.vcpus)
+            self.assertEqual(flavor['disk'], self.disk)
+            self.assertEqual(int(flavor['id']), self.new_flavor_id)
+            self.assertEqual(flavor['swap'], '')
+            self.assertEqual(int(flavor['rxtx_factor']), 1)
+            self.assertEqual(int(flavor['OS-FLV-EXT-DATA:ephemeral']), 0)
+            if self._interface == "xml":
+                XMLNS_OS_FLV_ACCESS = "http://docs.openstack.org/compute/ext/"\
+                    "flavor_access/api/v2"
+                key = "{" + XMLNS_OS_FLV_ACCESS + "}is_public"
+                self.assertEqual(flavor[key], "True")
+            if self._interface == "json":
+                self.assertEqual(flavor['os-flavor-access:is_public'], True)
+
+            #Verify flavor is retrieved
+            resp, flavor = self.client.get_flavor_details(self.new_flavor_id)
+            self.assertEqual(resp.status, 200)
+            self.assertEqual(flavor['name'], self.flavor_name)
+            #Check if flavor is present in list
+            resp, flavors = self.client.list_flavors_with_detail()
+            self.assertEqual(resp.status, 200)
+            for flavor in flavors:
+                if flavor['name'] == self.flavor_name:
+                    flag = True
+            self.assertTrue(flag)
+
+        finally:
+            #Delete the flavor
+            resp, body = self.client.delete_flavor(self.new_flavor_id)
+            self.assertEqual(resp.status, 202)
+            self.client.wait_for_resource_deletion(self.new_flavor_id)
+
+    @attr(type='positive')
+    def test_flavor_not_public_verify_entry_not_in_list_details(self):
+        #Create a flavor with os-flavor-access:is_public false should not
+        #be present in list_details.
+        #This operation requires the user to have 'admin' role
+        try:
+            #Create the flavor
+            resp, flavor = self.client.create_flavor(self.flavor_name,
+                                                     self.ram, self.vcpus,
+                                                     self.disk,
+                                                     self.new_flavor_id,
+                                                     is_public="False")
+            flag = False
+            #Verify flavor is retrieved
+            resp, flavors = self.client.list_flavors_with_detail()
+            self.assertEqual(resp.status, 200)
+            for flavor in flavors:
+                if flavor['name'] == self.flavor_name:
+                    flag = True
+            self.assertFalse(flag)
+        finally:
+            #Delete the flavor
+            resp, body = self.client.delete_flavor(self.new_flavor_id)
+            self.assertEqual(resp.status, 202)
+
+    def test_list_public_flavor_with_other_user(self):
+        #Create a Flavor with public access.
+        #Try to List/Get flavor with another user
+        try:
+            #Create the flavor
+            resp, flavor = self.client.create_flavor(self.flavor_name,
+                                                     self.ram, self.vcpus,
+                                                     self.disk,
+                                                     self.new_flavor_id,
+                                                     is_public="True")
+            flag = False
+            self.new_client = self.flavors_client
+            #Verify flavor is retrieved with new user
+            resp, flavors = self.new_client.list_flavors_with_detail()
+            self.assertEqual(resp.status, 200)
+            for flavor in flavors:
+                if flavor['name'] == self.flavor_name:
+                    flag = True
+            self.assertTrue(flag)
+        finally:
+            #Delete the flavor
+            resp, body = self.client.delete_flavor(self.new_flavor_id)
+            self.assertEqual(resp.status, 202)
+            self.client.wait_for_resource_deletion(self.new_flavor_id)
+
 
 class FlavorsAdminTestXML(base.BaseComputeAdminTestXML,
+                          base.BaseComputeTestXML,
                           FlavorsAdminTestBase):
 
     @classmethod
     def setUpClass(cls):
         super(FlavorsAdminTestXML, cls).setUpClass()
+        base.BaseComputeTestXML.setUpClass()
         FlavorsAdminTestBase.setUpClass(cls)
 
 
 class FlavorsAdminTestJSON(base.BaseComputeAdminTestJSON,
+                           base.BaseComputeTestJSON,
                            FlavorsAdminTestBase):
 
     @classmethod
     def setUpClass(cls):
         super(FlavorsAdminTestJSON, cls).setUpClass()
+        base.BaseComputeTestJSON.setUpClass()
         FlavorsAdminTestBase.setUpClass(cls)