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 | |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 16 | from tempest.common import http |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 17 | from tempest.common.utils import data_utils |
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 |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 21 | from tempest import test |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 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 | |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 28 | class TestSwiftBasicOps(manager.ScenarioTest): |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 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. |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 35 | * download the object and check the content |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 36 | * delete object from container. |
| 37 | * list container's objects and assure that the deleted file is gone. |
| 38 | * delete a container. |
| 39 | * list containers and assure that the deleted container is gone. |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 40 | * change ACL of the container and make sure it works successfully |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 41 | """ |
| 42 | |
| 43 | @classmethod |
| 44 | def setUpClass(cls): |
Sylvain Afchain | 9206477 | 2014-01-16 02:45:57 +0100 | [diff] [blame] | 45 | cls.set_network_resources() |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 46 | super(TestSwiftBasicOps, cls).setUpClass() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 47 | if not CONF.service_available.swift: |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 48 | skip_msg = ("%s skipped as swift is not available" % |
| 49 | cls.__name__) |
| 50 | raise cls.skipException(skip_msg) |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 51 | # Clients for Swift |
| 52 | cls.account_client = cls.manager.account_client |
| 53 | cls.container_client = cls.manager.container_client |
| 54 | cls.object_client = cls.manager.object_client |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 55 | |
| 56 | def _get_swift_stat(self): |
| 57 | """get swift status for our user account.""" |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 58 | self.account_client.list_account_containers() |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 59 | LOG.debug('Swift status information obtained successfully') |
| 60 | |
| 61 | def _create_container(self, container_name=None): |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 62 | name = container_name or data_utils.rand_name( |
| 63 | 'swift-scenario-container') |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 64 | self.container_client.create_container(name) |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 65 | # look for the container to assure it is created |
| 66 | self._list_and_check_container_objects(name) |
| 67 | LOG.debug('Container %s created' % (name)) |
| 68 | return name |
| 69 | |
| 70 | def _delete_container(self, container_name): |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 71 | self.container_client.delete_container(container_name) |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 72 | LOG.debug('Container %s deleted' % (container_name)) |
| 73 | |
| 74 | def _upload_object_to_container(self, container_name, obj_name=None): |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 75 | obj_name = obj_name or data_utils.rand_name('swift-scenario-object') |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 76 | obj_data = data_utils.arbitrary_string() |
| 77 | self.object_client.create_object(container_name, obj_name, obj_data) |
| 78 | return obj_name, obj_data |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 79 | |
| 80 | def _delete_object(self, container_name, filename): |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 81 | self.object_client.delete_object(container_name, filename) |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 82 | self._list_and_check_container_objects(container_name, |
| 83 | not_present_obj=[filename]) |
| 84 | |
| 85 | def _list_and_check_container_objects(self, container_name, present_obj=[], |
| 86 | not_present_obj=[]): |
| 87 | """ |
| 88 | List objects for a given container and assert which are present and |
| 89 | which are not. |
| 90 | """ |
Daisuke Morita | 5ec8de3 | 2014-08-18 14:52:31 +0900 | [diff] [blame] | 91 | _, object_list = self.container_client.list_container_contents( |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 92 | container_name) |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 93 | if present_obj: |
| 94 | for obj in present_obj: |
| 95 | self.assertIn(obj, object_list) |
| 96 | if not_present_obj: |
| 97 | for obj in not_present_obj: |
| 98 | self.assertNotIn(obj, object_list) |
| 99 | |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 100 | def _change_container_acl(self, container_name, acl): |
| 101 | metadata_param = {'metadata_prefix': 'x-container-', |
| 102 | 'metadata': {'read': acl}} |
| 103 | self.container_client.update_container_metadata(container_name, |
| 104 | **metadata_param) |
| 105 | resp, _ = self.container_client.list_container_metadata(container_name) |
| 106 | self.assertEqual(resp['x-container-read'], acl) |
| 107 | |
| 108 | def _download_and_verify(self, container_name, obj_name, expected_data): |
| 109 | _, obj = self.object_client.get_object(container_name, obj_name) |
| 110 | self.assertEqual(obj, expected_data) |
| 111 | |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 112 | @test.services('object_storage') |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 113 | def test_swift_basic_ops(self): |
| 114 | self._get_swift_stat() |
| 115 | container_name = self._create_container() |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 116 | obj_name, obj_data = self._upload_object_to_container(container_name) |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 117 | self._list_and_check_container_objects(container_name, [obj_name]) |
Fei Long Wang | 02abbf3 | 2014-06-12 11:50:35 +1200 | [diff] [blame] | 118 | self._download_and_verify(container_name, obj_name, obj_data) |
| 119 | self._delete_object(container_name, obj_name) |
| 120 | self._delete_container(container_name) |
| 121 | |
| 122 | @test.services('object_storage') |
| 123 | def test_swift_acl_anonymous_download(self): |
| 124 | """This test will cover below steps: |
| 125 | 1. Create container |
| 126 | 2. Upload object to the new container |
| 127 | 3. Change the ACL of the container |
| 128 | 4. Check if the object can be download by anonymous user |
| 129 | 5. Delete the object and container |
| 130 | """ |
| 131 | container_name = self._create_container() |
| 132 | obj_name, _ = self._upload_object_to_container(container_name) |
| 133 | obj_url = '%s/%s/%s' % (self.object_client.base_url, |
| 134 | container_name, obj_name) |
| 135 | http_client = http.ClosingHttp() |
| 136 | resp, _ = http_client.request(obj_url, 'GET') |
| 137 | self.assertEqual(resp.status, 401) |
| 138 | self._change_container_acl(container_name, '.r:*') |
| 139 | resp, _ = http_client.request(obj_url, 'GET') |
| 140 | self.assertEqual(resp.status, 200) |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 141 | self._delete_object(container_name, obj_name) |
| 142 | self._delete_container(container_name) |