Best Python code snippet using tempest_python
test_volumes_list.py
Source:test_volumes_list.py
...37 for volume in fetched_list:38 for field in ('updated_at',):39 if field in volume:40 del volume[field]41 def _assert_volumes_in(self, fetched_list, expected_list, fields=None):42 """Check out the list.43 This function is aim at check out whether all of the volumes in44 expected_list are in fetched_list.45 """46 if fields:47 fieldsgetter = operator.itemgetter(*fields)48 expected_list = map(fieldsgetter, expected_list)49 fetched_list = [fieldsgetter(item) for item in fetched_list]50 # Hopefully the expected_list has already been cleaned.51 self._remove_volatile_fields(fetched_list)52 missing_vols = [v for v in expected_list if v not in fetched_list]53 if not missing_vols:54 return55 def str_vol(vol):56 return "%s:%s" % (vol['id'], vol['name'])57 raw_msg = "Could not find volumes %s in expected list %s; fetched %s"58 self.fail(raw_msg % ([str_vol(v) for v in missing_vols],59 [str_vol(v) for v in expected_list],60 [str_vol(v) for v in fetched_list]))61 @classmethod62 def resource_setup(cls):63 super(VolumesListTestJSON, cls).resource_setup()64 existing_volumes = cls.volumes_client.list_volumes()['volumes']65 cls.volume_id_list = [vol['id'] for vol in existing_volumes]66 # Create 3 test volumes67 cls.volume_list = []68 cls.metadata = {'Type': 'work'}69 for _ in range(3):70 volume = cls.create_volume(metadata=cls.metadata)71 volume = cls.volumes_client.show_volume(volume['id'])['volume']72 cls.volume_list.append(volume)73 cls.volume_id_list.append(volume['id'])74 cls._remove_volatile_fields(cls.volume_list)75 def _list_by_param_value_and_assert(self, params, with_detail=False):76 """list or list_details with given params and validates result"""77 if with_detail:78 fetched_vol_list = \79 self.volumes_client.list_volumes(detail=True,80 params=params)['volumes']81 else:82 fetched_vol_list = self.volumes_client.list_volumes(83 params=params)['volumes']84 # Validating params of fetched volumes85 if with_detail:86 for volume in fetched_vol_list:87 for key in params:88 msg = "Failed to list volumes %s by %s" % \89 ('details' if with_detail else '', key)90 if key == 'metadata':91 self.assertThat(92 volume[key].items(),93 matchers.ContainsAll(params[key].items()),94 msg)95 else:96 self.assertEqual(params[key], volume[key], msg)97 @decorators.attr(type='smoke')98 @decorators.idempotent_id('0b6ddd39-b948-471f-8038-4787978747c4')99 def test_volume_list(self):100 # Get a list of Volumes101 # Fetch all volumes102 fetched_list = self.volumes_client.list_volumes()['volumes']103 self._assert_volumes_in(fetched_list, self.volume_list,104 fields=self.VOLUME_FIELDS)105 @decorators.idempotent_id('adcbb5a7-5ad8-4b61-bd10-5380e111a877')106 def test_volume_list_with_details(self):107 # Get a list of Volumes with details108 # Fetch all Volumes109 fetched_list = self.volumes_client.list_volumes(detail=True)['volumes']110 self._assert_volumes_in(fetched_list, self.volume_list)111 @decorators.idempotent_id('a28e8da4-0b56-472f-87a8-0f4d3f819c02')112 def test_volume_list_by_name(self):113 volume = self.volume_list[data_utils.rand_int_id(0, 2)]114 params = {'name': volume['name']}115 fetched_vol = self.volumes_client.list_volumes(116 params=params)['volumes']117 self.assertEqual(1, len(fetched_vol), str(fetched_vol))118 self.assertEqual(fetched_vol[0]['name'], volume['name'])119 @decorators.idempotent_id('2de3a6d4-12aa-403b-a8f2-fdeb42a89623')120 def test_volume_list_details_by_name(self):121 volume = self.volume_list[data_utils.rand_int_id(0, 2)]122 params = {'name': volume['name']}123 fetched_vol = self.volumes_client.list_volumes(124 detail=True, params=params)['volumes']125 self.assertEqual(1, len(fetched_vol), str(fetched_vol))126 self.assertEqual(fetched_vol[0]['name'], volume['name'])127 @decorators.idempotent_id('39654e13-734c-4dab-95ce-7613bf8407ce')128 def test_volumes_list_by_status(self):129 params = {'status': 'available'}130 fetched_list = self.volumes_client.list_volumes(131 params=params)['volumes']132 self._list_by_param_value_and_assert(params)133 self._assert_volumes_in(fetched_list, self.volume_list,134 fields=self.VOLUME_FIELDS)135 @decorators.idempotent_id('2943f712-71ec-482a-bf49-d5ca06216b9f')136 def test_volumes_list_details_by_status(self):137 params = {'status': 'available'}138 fetched_list = self.volumes_client.list_volumes(139 detail=True, params=params)['volumes']140 for volume in fetched_list:141 self.assertEqual('available', volume['status'])142 self._assert_volumes_in(fetched_list, self.volume_list)143 @decorators.idempotent_id('2016a942-3020-40d7-95ce-7613bf8407ce')144 def test_volumes_list_by_bootable(self):145 """Check out volumes.146 This test function is aim at check out whether all of the volumes147 in volume_list are not a bootable volume.148 """149 params = {'bootable': 'false'}150 fetched_list = self.volumes_client.list_volumes(151 params=params)['volumes']152 self._list_by_param_value_and_assert(params)153 self._assert_volumes_in(fetched_list, self.volume_list,154 fields=self.VOLUME_FIELDS)155 @decorators.idempotent_id('2016a939-72ec-482a-bf49-d5ca06216b9f')156 def test_volumes_list_details_by_bootable(self):157 params = {'bootable': 'false'}158 fetched_list = self.volumes_client.list_volumes(159 detail=True, params=params)['volumes']160 for volume in fetched_list:161 self.assertEqual('false', volume['bootable'])162 self._assert_volumes_in(fetched_list, self.volume_list)163 @decorators.idempotent_id('c0cfa863-3020-40d7-b587-e35f597d5d87')164 def test_volumes_list_by_availability_zone(self):165 volume = self.volume_list[data_utils.rand_int_id(0, 2)]166 zone = volume['availability_zone']167 params = {'availability_zone': zone}168 fetched_list = self.volumes_client.list_volumes(169 params=params)['volumes']170 self._list_by_param_value_and_assert(params)171 self._assert_volumes_in(fetched_list, self.volume_list,172 fields=self.VOLUME_FIELDS)173 @decorators.idempotent_id('e1b80d13-94f0-4ba2-a40e-386af29f8db1')174 def test_volumes_list_details_by_availability_zone(self):175 volume = self.volume_list[data_utils.rand_int_id(0, 2)]176 zone = volume['availability_zone']177 params = {'availability_zone': zone}178 fetched_list = self.volumes_client.list_volumes(179 detail=True, params=params)['volumes']180 for volume in fetched_list:181 self.assertEqual(zone, volume['availability_zone'])182 self._assert_volumes_in(fetched_list, self.volume_list)183 @decorators.idempotent_id('b5ebea1b-0603-40a0-bb41-15fcd0a53214')184 def test_volume_list_with_param_metadata(self):185 # Test to list volumes when metadata param is given186 params = {'metadata': self.metadata}187 self._list_by_param_value_and_assert(params)188 @decorators.idempotent_id('1ca92d3c-4a8e-4b43-93f5-e4c7fb3b291d')189 def test_volume_list_with_detail_param_metadata(self):190 # Test to list volumes details when metadata param is given191 params = {'metadata': self.metadata}192 self._list_by_param_value_and_assert(params, with_detail=True)193 @decorators.idempotent_id('777c87c1-2fc4-4883-8b8e-5c0b951d1ec8')194 def test_volume_list_param_display_name_and_status(self):195 # Test to list volume when display name and status param is given196 volume = self.volume_list[data_utils.rand_int_id(0, 2)]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!