Search

小例子分析tempest处理流程

继续搞定昨晚tempest的问题,依旧是那个用例

1:关于flavor创建的用例

vim tempest/tempest/api/compute/admin/test_flavors.py
88     @test.attr(type='gate')
89     def test_create_flavor_with_int_id(self):
90         flavor_id = data_utils.rand_int_id(start=10000)
91         new_flavor_id = self._create_flavor(flavor_id)
92         self.assertEqual(new_flavor_id, str(flavor_id))

2:这个用例实际上就为了测试_create_flavor里面的create_flavor

 56     def _create_flavor(self, flavor_id):
 57         # Create a flavor and ensure it is listed
 58         # This operation requires the user to have 'admin' role
 59         flavor_name = data_utils.rand_name(self.flavor_name_prefix)
 60
 61         # Create the flavor
 62         resp, flavor = self.client.create_flavor(flavor_name,
 63                                                  self.ram, self.vcpus,
 64                                                  self.disk,
 65                                                  flavor_id,
 66                                                  ephemeral=self.ephemeral,
 67                                                  swap=self.swap,
 68                                                  rxtx=self.rxtx)
 69         self.addCleanup(self.flavor_clean_up, flavor['id'])
 70         self.assertEqual(200, resp.status)
 71         self.assertEqual(flavor['name'], flavor_name)
 72         self.assertEqual(flavor['vcpus'], self.vcpus)
 73         self.assertEqual(flavor['disk'], self.disk)
 74         self.assertEqual(flavor['ram'], self.ram)
 75         self.assertEqual(flavor['swap'], self.swap)
 76         self.assertEqual(flavor['rxtx_factor'], self.rxtx)
 77         self.assertEqual(flavor['OS-FLV-EXT-DATA:ephemeral'],
 78                          self.ephemeral)
 79         self.assertEqual(flavor['os-flavor-access:is_public'], True)
 80
 81         # Verify flavor is retrieved
 82         resp, flavor = self.client.get_flavor_details(flavor['id'])
 83         self.assertEqual(resp.status, 200)
 84         self.assertEqual(flavor['name'], flavor_name)
 85
 86         return flavor['id']

3:昨晚随意找了找,没找到中间还有这行,self.client也是其它地方传进来的

 26 class FlavorsAdminTestJSON(base.BaseV2ComputeAdminTest):
 27
 28     """
 29     Tests Flavors API Create and Delete that require admin privileges
 30     """
 31
 32     _interface = 'json'
 33
 34     @classmethod
 35     def setUpClass(cls):
 36         super(FlavorsAdminTestJSON, cls).setUpClass()
 37         if not test.is_extension_enabled('FlavorExtraData', 'compute'):
 38             msg = "FlavorExtraData extension not enabled."
 39             raise cls.skipException(msg)
 40
 41         cls.client = cls.os_adm.flavors_client

4:os_adm显然是继承的某个基类的成员,create_flavor就需要从cls.os_adm.flavors_client中找,先看看第一个基类

vim tempest/tempest/api/compute/base.py
236 class BaseV2ComputeAdminTest(BaseV2ComputeTest):
237     """Base test case class for Compute Admin V2 API tests."""
238
239     @classmethod
240     def setUpClass(cls):
241         super(BaseV2ComputeAdminTest, cls).setUpClass()
242         admin_username = cls.config.compute_admin.username
243         admin_password = cls.config.compute_admin.password
244         admin_tenant = cls.config.compute_admin.tenant_name
245         if not (admin_username and admin_password and admin_tenant):
246             msg = ("Missing Compute Admin API credentials "
247                    "in configuration.")
248             raise cls.skipException(msg)
249         if (cls.config.compute.allow_tenant_isolation or
250             cls.force_tenant_isolation is True):
251             creds = cls.isolated_creds.get_admin_creds()
252             admin_username, admin_tenant_name, admin_password = creds
253             cls.os_adm = clients.Manager(username=admin_username,
254                                          password=admin_password,
255                                          tenant_name=admin_tenant_name,
256                                          interface=cls._interface)
257         else:
258             cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)

5:os_adm来自clients.Manager

vim tempest/tempest/clients.py
196 class Manager(object):
197
198     """
199     Top level manager for OpenStack Compute clients
200     """

6:最终想要create_flavor就需要得到clients.Manager.flavors_client,在Manager里找到self.flavors_client,这里就看json输出格式

305         elif interface == 'json':
306             self.certificates_client = CertificatesClientJSON(*client_args)
307             self.certificates_v3_client = CertificatesV3ClientJSON(
308                 *client_args)
309             self.baremetal_client = BaremetalClientJSON(*client_args)
310             self.servers_client = ServersClientJSON(*client_args)
311             self.servers_v3_client = ServersV3ClientJSON(*client_args)
312             self.limits_client = LimitsClientJSON(*client_args)
313             self.images_client = ImagesClientJSON(*client_args)
314             self.keypairs_v3_client = KeyPairsV3ClientJSON(*client_args)
315             self.keypairs_client = KeyPairsClientJSON(*client_args)
316             self.keypairs_v3_client = KeyPairsV3ClientJSON(*client_args)
317             self.quotas_client = QuotasClientJSON(*client_args)
318             self.quotas_v3_client = QuotasV3ClientJSON(*client_args)
319             self.flavors_client = FlavorsClientJSON(*client_args)

7:self.flavors_client来自FlavorsClientJSON,从import中可以看到

 33 from tempest.services.compute.json.flavors_client import FlavorsClientJSON

8:拐了这么多弯,终于确信tempest.services.compute.json.flavors_client就是要找的地方

vim tempest/tempest/services/compute/json/flavors_client.py
 54     def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
 55         """Creates a new flavor or instance type."""
 56         post_body = {
 57             'name': name,
 58             'ram': ram,
 59             'vcpus': vcpus,
 60             'disk': disk,
 61             'id': flavor_id,
 62         }
 63         if kwargs.get('ephemeral'):
 64             post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
 65         if kwargs.get('swap'):
 66             post_body['swap'] = kwargs.get('swap')
 67         if kwargs.get('rxtx'):
 68             post_body['rxtx_factor'] = kwargs.get('rxtx')
 69         if kwargs.get('is_public'):
 70             post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
 71         post_body = json.dumps({'flavor': post_body})
 72         resp, body = self.post('flavors', post_body, self.headers)
 73
 74         body = json.loads(body)
 75         return resp, body['flavor']

 

最终找到了测试的函数的出处,与昨天直接搜索的地方一致

发表评论

浙ICP备16024533号

浙公网安备 33010802007459号