Best Python code snippet using tempest_python
test_availability_zone.py
Source:test_availability_zone.py  
...26            'cinder.db.service_get_all', autospec=True,27            return_value=[{'availability_zone': 'a', 'disabled': False,28                           'uuid': 'f838f35c-4035-464f-9792-ce60e390c13d'}])29    def test_list_availability_zones_cached(self):30        azs = self.volume_api.list_availability_zones(enable_cache=True)31        self.assertEqual([{"name": 'a', 'available': True}], list(azs))32        self.assertIsNotNone(self.volume_api.availability_zones_last_fetched)33        self.assertTrue(self.get_all.called)34        self.volume_api.list_availability_zones(enable_cache=True)35        self.assertEqual(1, self.get_all.call_count)36    def test_list_availability_zones_cached_and_refresh_on(self):37        azs = self.volume_api.list_availability_zones(enable_cache=True,38                                                      refresh_cache=True)39        self.assertEqual([{"name": 'a', 'available': True}], list(azs))40        time_before = self.volume_api.availability_zones_last_fetched41        self.assertIsNotNone(time_before)42        self.assertEqual(1, self.get_all.call_count)43        self.volume_api.list_availability_zones(enable_cache=True,44                                                refresh_cache=True)45        self.assertTrue(time_before !=46                        self.volume_api.availability_zones_last_fetched)47        self.assertEqual(2, self.get_all.call_count)48    def test_list_availability_zones_no_cached(self):49        azs = self.volume_api.list_availability_zones(enable_cache=False)50        self.assertEqual([{"name": 'a', 'available': True}], list(azs))51        self.assertIsNone(self.volume_api.availability_zones_last_fetched)52        self.get_all.return_value[0]['disabled'] = True53        azs = self.volume_api.list_availability_zones(enable_cache=False)54        self.assertEqual([{"name": 'a', 'available': False}], list(azs))55        self.assertIsNone(self.volume_api.availability_zones_last_fetched)56    @mock.patch('oslo_utils.timeutils.utcnow')57    def test_list_availability_zones_refetched(self, mock_utcnow):58        mock_utcnow.return_value = datetime.datetime.utcnow()59        azs = self.volume_api.list_availability_zones(enable_cache=True)60        self.assertEqual([{"name": 'a', 'available': True}], list(azs))61        self.assertIsNotNone(self.volume_api.availability_zones_last_fetched)62        last_fetched = self.volume_api.availability_zones_last_fetched63        self.assertTrue(self.get_all.called)64        self.volume_api.list_availability_zones(enable_cache=True)65        self.assertEqual(1, self.get_all.call_count)66        # The default cache time is 3600, push past that...67        mock_utcnow.return_value = (timeutils.utcnow() +68                                    datetime.timedelta(0, 3800))69        self.get_all.return_value = [70            {71                'availability_zone': 'a',72                'disabled': False,73                'uuid': 'a3a593da-7f8d-4bb7-8b4c-f2bc1e0b4824',74            },75            {76                'availability_zone': 'b',77                'disabled': False,78                'uuid': '4200b32b-0bf9-436c-86b2-0675f6ac218e',79            },80        ]81        azs = self.volume_api.list_availability_zones(enable_cache=True)82        azs = sorted([n['name'] for n in azs])83        self.assertEqual(['a', 'b'], azs)84        self.assertEqual(2, self.get_all.call_count)85        self.assertGreater(self.volume_api.availability_zones_last_fetched,86                           last_fetched)87        mock_utcnow.assert_called_with()88    def test_list_availability_zones_enabled_service(self):89        def sort_func(obj):90            return obj['name']91        self.get_all.return_value = [92            {'availability_zone': 'ping', 'disabled': 0,93             'uuid': 'a3a593da-7f8d-4bb7-8b4c-f2bc1e0b4824'},94            {'availability_zone': 'ping', 'disabled': 1,95             'uuid': '4200b32b-0bf9-436c-86b2-0675f6ac218e'},96            {'availability_zone': 'pong', 'disabled': 0,97             'uuid': '6d91e7f5-ca17-4e3b-bf4f-19ca77166dd7'},98            {'availability_zone': 'pung', 'disabled': 1,99             'uuid': '18417850-2ca9-43d1-9619-ae16bfb0f655'},100        ]101        volume_api = cinder.volume.api.API()102        azs = volume_api.list_availability_zones()103        azs = sorted(azs, key=sort_func)104        expected = sorted([105            {'name': 'pung', 'available': False},106            {'name': 'pong', 'available': True},107            {'name': 'ping', 'available': True},108        ], key=sort_func)...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!!
