| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
| 2 | # Copyright 2013 IBM Corp. |
| 3 | # All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | from oslo_log import log |
| 18 | |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 19 | from tempest.common import waiters |
| 20 | from tempest import config |
| 21 | from tempest.lib.common.utils import data_utils |
| Michael Polenchuk | 4d0b92d | 2022-09-07 10:30:53 +0400 | [diff] [blame] | 22 | from tempest.lib.common.utils import test_utils |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 23 | from tempest.lib import exceptions as lib_exc |
| Ihar Hrachyshka | ecce1f6 | 2018-01-18 13:32:05 -0800 | [diff] [blame] | 24 | from tempest.scenario import manager |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 25 | |
| 26 | CONF = config.CONF |
| 27 | |
| 28 | LOG = log.getLogger(__name__) |
| 29 | |
| 30 | |
| Ihar Hrachyshka | 2d0cf0a | 2018-01-18 13:40:09 -0800 | [diff] [blame] | 31 | # we inherit from NetworkScenarioTest since some test cases need access to |
| 32 | # check_*_connectivity methods to validate instances are up and accessible |
| Pavlo Shchelokovskyy | c4b220a | 2023-08-09 13:23:48 +0300 | [diff] [blame^] | 33 | # we also inherit from ScenarioTestWithNetwork to get the networking resources |
| 34 | # pre-setup for us by setup_credentials (network, subnet with dhcp, router) |
| 35 | class ScenarioTest(manager.ScenarioTestWithNetwork, |
| 36 | manager.NetworkScenarioTest): |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 37 | """Base class for scenario tests. Uses tempest own clients. """ |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 38 | # ## Test functions library |
| 39 | # |
| 40 | # The create_[resource] functions only return body and discard the |
| 41 | # resp part which is not used in scenario tests |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 42 | |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 43 | def _image_create(self, name, fmt, path, |
| 44 | disk_format=None, properties=None): |
| 45 | if properties is None: |
| 46 | properties = {} |
| 47 | name = data_utils.rand_name('%s-' % name) |
| 48 | params = { |
| 49 | 'name': name, |
| 50 | 'container_format': fmt, |
| 51 | 'disk_format': disk_format or fmt, |
| Ghanshyam Mann | e5ed4b9 | 2023-08-06 12:03:34 -0700 | [diff] [blame] | 52 | 'visibility': 'private' |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 53 | } |
| Ghanshyam Mann | e5ed4b9 | 2023-08-06 12:03:34 -0700 | [diff] [blame] | 54 | # Additional properties are flattened out in the v2 API. |
| 55 | params.update(properties) |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 56 | body = self.image_client.create_image(**params) |
| 57 | image = body['image'] if 'image' in body else body |
| Michael Polenchuk | 4d0b92d | 2022-09-07 10:30:53 +0400 | [diff] [blame] | 58 | self.addCleanup(self.image_client.wait_for_resource_deletion, |
| 59 | image['id']) |
| 60 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
| 61 | self.image_client.delete_image, image['id']) |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 62 | self.assertEqual("queued", image['status']) |
| 63 | with open(path, 'rb') as image_file: |
| Ghanshyam Mann | e5ed4b9 | 2023-08-06 12:03:34 -0700 | [diff] [blame] | 64 | self.image_client.store_image_file(image['id'], image_file) |
| Marian Krcmarik | 1972c46 | 2020-11-20 01:33:02 +0100 | [diff] [blame] | 65 | |
| 66 | if CONF.image_feature_enabled.import_image: |
| 67 | available_stores = [] |
| 68 | try: |
| 69 | available_stores = self.image_client.info_stores()['stores'] |
| Luigi Toscano | 4e9303e | 2022-06-24 11:37:34 +0200 | [diff] [blame] | 70 | except lib_exc.NotFound: |
| Marian Krcmarik | 1972c46 | 2020-11-20 01:33:02 +0100 | [diff] [blame] | 71 | pass |
| 72 | available_import_methods = self.image_client.info_import()[ |
| 73 | 'import-methods']['value'] |
| 74 | if ('copy-image' in available_import_methods and |
| 75 | len(available_stores) > 1): |
| 76 | self.image_client.image_import(image['id'], |
| 77 | method='copy-image', |
| 78 | all_stores=True, |
| 79 | all_stores_must_succeed=False) |
| 80 | failed_stores = waiters.wait_for_image_copied_to_stores( |
| 81 | self.image_client, image['id']) |
| 82 | self.assertEqual(0, len(failed_stores), |
| 83 | "Failed to copy the following stores: %s" % |
| 84 | str(failed_stores)) |
| 85 | |
| Brianna Poulos | 011292a | 2017-03-15 16:24:38 -0400 | [diff] [blame] | 86 | return image['id'] |