Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | |
| 17 | from tempest.common.utils.data_utils import rand_name |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 18 | from tempest import config |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 19 | from tempest.openstack.common import log as logging |
| 20 | from tempest.scenario import manager |
| 21 | from tempest.test import services |
| 22 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 25 | LOG = logging.getLogger(__name__) |
| 26 | |
| 27 | |
| 28 | class TestSwiftBasicOps(manager.OfficialClientTest): |
| 29 | """ |
| 30 | Test swift with the follow operations: |
| 31 | * get swift stat. |
| 32 | * create container. |
| 33 | * upload a file to the created container. |
| 34 | * list container's objects and assure that the uploaded file is present. |
| 35 | * delete object from container. |
| 36 | * list container's objects and assure that the deleted file is gone. |
| 37 | * delete a container. |
| 38 | * list containers and assure that the deleted container is gone. |
| 39 | """ |
| 40 | |
| 41 | @classmethod |
| 42 | def setUpClass(cls): |
Sylvain Afchain | 9206477 | 2014-01-16 02:45:57 +0100 | [diff] [blame] | 43 | cls.set_network_resources() |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 44 | super(TestSwiftBasicOps, cls).setUpClass() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 45 | if not CONF.service_available.swift: |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 46 | skip_msg = ("%s skipped as swift is not available" % |
| 47 | cls.__name__) |
| 48 | raise cls.skipException(skip_msg) |
| 49 | |
| 50 | def _get_swift_stat(self): |
| 51 | """get swift status for our user account.""" |
| 52 | self.object_storage_client.get_account() |
| 53 | LOG.debug('Swift status information obtained successfully') |
| 54 | |
| 55 | def _create_container(self, container_name=None): |
| 56 | name = container_name or rand_name('swift-scenario-container') |
| 57 | self.object_storage_client.put_container(name) |
| 58 | # look for the container to assure it is created |
| 59 | self._list_and_check_container_objects(name) |
| 60 | LOG.debug('Container %s created' % (name)) |
| 61 | return name |
| 62 | |
| 63 | def _delete_container(self, container_name): |
| 64 | self.object_storage_client.delete_container(container_name) |
| 65 | LOG.debug('Container %s deleted' % (container_name)) |
| 66 | |
| 67 | def _upload_object_to_container(self, container_name, obj_name=None): |
| 68 | obj_name = obj_name or rand_name('swift-scenario-object') |
| 69 | self.object_storage_client.put_object(container_name, obj_name, |
| 70 | rand_name('obj_data'), |
| 71 | content_type='text/plain') |
| 72 | return obj_name |
| 73 | |
| 74 | def _delete_object(self, container_name, filename): |
| 75 | self.object_storage_client.delete_object(container_name, filename) |
| 76 | self._list_and_check_container_objects(container_name, |
| 77 | not_present_obj=[filename]) |
| 78 | |
| 79 | def _list_and_check_container_objects(self, container_name, present_obj=[], |
| 80 | not_present_obj=[]): |
| 81 | """ |
| 82 | List objects for a given container and assert which are present and |
| 83 | which are not. |
| 84 | """ |
| 85 | meta, response = self.object_storage_client.get_container( |
| 86 | container_name) |
| 87 | # create a list with file name only |
| 88 | object_list = [obj['name'] for obj in response] |
| 89 | if present_obj: |
| 90 | for obj in present_obj: |
| 91 | self.assertIn(obj, object_list) |
| 92 | if not_present_obj: |
| 93 | for obj in not_present_obj: |
| 94 | self.assertNotIn(obj, object_list) |
| 95 | |
Matthew Treinish | 03c4f77 | 2014-02-02 13:37:44 -0500 | [diff] [blame] | 96 | @services('object_storage') |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 97 | def test_swift_basic_ops(self): |
| 98 | self._get_swift_stat() |
| 99 | container_name = self._create_container() |
| 100 | obj_name = self._upload_object_to_container(container_name) |
| 101 | self._list_and_check_container_objects(container_name, [obj_name]) |
| 102 | self._delete_object(container_name, obj_name) |
| 103 | self._delete_container(container_name) |