blob: 3fa6d2c5cee047ee54fb6f89ace2ed09018eaf73 [file] [log] [blame]
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +00001# 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 Wang02abbf32014-06-12 11:50:35 +120016from tempest.common import http
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090017from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000018from tempest import config
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000019from tempest.openstack.common import log as logging
20from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090021from tempest import test
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000022
Matthew Treinish6c072292014-01-29 19:15:52 +000023CONF = config.CONF
24
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000025LOG = logging.getLogger(__name__)
26
27
Daisuke Morita5ec8de32014-08-18 14:52:31 +090028class TestSwiftBasicOps(manager.ScenarioTest):
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000029 """
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 Wang02abbf32014-06-12 11:50:35 +120035 * download the object and check the content
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000036 * 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 Wang02abbf32014-06-12 11:50:35 +120040 * change ACL of the container and make sure it works successfully
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000041 """
42
43 @classmethod
44 def setUpClass(cls):
Sylvain Afchain92064772014-01-16 02:45:57 +010045 cls.set_network_resources()
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000046 super(TestSwiftBasicOps, cls).setUpClass()
Matthew Treinish6c072292014-01-29 19:15:52 +000047 if not CONF.service_available.swift:
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000048 skip_msg = ("%s skipped as swift is not available" %
49 cls.__name__)
50 raise cls.skipException(skip_msg)
Daisuke Morita5ec8de32014-08-18 14:52:31 +090051 # 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. Rodriguese86ed042013-12-12 18:56:00 +000055
56 def _get_swift_stat(self):
57 """get swift status for our user account."""
Daisuke Morita5ec8de32014-08-18 14:52:31 +090058 self.account_client.list_account_containers()
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000059 LOG.debug('Swift status information obtained successfully')
60
61 def _create_container(self, container_name=None):
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090062 name = container_name or data_utils.rand_name(
63 'swift-scenario-container')
Daisuke Morita5ec8de32014-08-18 14:52:31 +090064 self.container_client.create_container(name)
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000065 # 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 Morita5ec8de32014-08-18 14:52:31 +090071 self.container_client.delete_container(container_name)
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000072 LOG.debug('Container %s deleted' % (container_name))
73
74 def _upload_object_to_container(self, container_name, obj_name=None):
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090075 obj_name = obj_name or data_utils.rand_name('swift-scenario-object')
Fei Long Wang02abbf32014-06-12 11:50:35 +120076 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. Rodriguese86ed042013-12-12 18:56:00 +000079
80 def _delete_object(self, container_name, filename):
Daisuke Morita5ec8de32014-08-18 14:52:31 +090081 self.object_client.delete_object(container_name, filename)
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000082 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 Morita5ec8de32014-08-18 14:52:31 +090091 _, object_list = self.container_client.list_container_contents(
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000092 container_name)
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +000093 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 Wang02abbf32014-06-12 11:50:35 +1200100 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 Igawa4ded9f02014-02-17 15:05:59 +0900112 @test.services('object_storage')
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +0000113 def test_swift_basic_ops(self):
114 self._get_swift_stat()
115 container_name = self._create_container()
Fei Long Wang02abbf32014-06-12 11:50:35 +1200116 obj_name, obj_data = self._upload_object_to_container(container_name)
Mauro S. M. Rodriguese86ed042013-12-12 18:56:00 +0000117 self._list_and_check_container_objects(container_name, [obj_name])
Fei Long Wang02abbf32014-06-12 11:50:35 +1200118 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. Rodriguese86ed042013-12-12 18:56:00 +0000141 self._delete_object(container_name, obj_name)
142 self._delete_container(container_name)