blob: 2ecfd089831e15d81d893c888fe3bece654f95e2 [file] [log] [blame]
Justin Shepherd0d9bbd12011-08-11 12:57:44 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2011 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18"""Functional test case for OpenStack Swift """
19
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050020import httplib2
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050021import os
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050022
23from pprint import pprint
24
Soren Hansenec3f7092011-09-08 13:03:42 +020025from kong import tests
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050026
27SMALL_OBJ = "include/swift_objects/swift_small"
28MED_OBJ = "include/swift_objects/swift_medium"
29LRG_OBJ = "include/swift_objects/swift_large"
30
31
32class TestSwift(tests.FunctionalTest):
33 def test_000_auth(self):
34 if self.swift['auth_ssl'] == "False":
35 prot = "http://"
36 else:
37 prot = "https://"
38
39 path = "%s%s:%s%s%s" % (prot, self.swift['auth_host'],
40 self.swift['auth_port'],
41 self.swift['auth_prefix'],
42 self.swift['ver'])
43
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050044 http = httplib2.Http(disable_ssl_certificate_validation=True)
45 self.swift['auth_user'] = '%s:%s' % (self.swift['account'],
46 self.swift['username'])
47 headers = {'X-Auth-User': '%s' % (self.swift['auth_user']),
48 'X-Auth-Key': '%s' % (self.swift['password'])}
49 response, content = http.request(path, 'GET', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -050050 self.assertEqual(response.status, 200)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050051 self.assertIsNotNone(response['x-auth-token'])
52 self.assertIsNotNone(response['x-storage-token'])
53 self.assertIsNotNone(response['x-storage-url'])
54
Justin Shepherd74a95b62011-08-18 11:52:02 -050055 # TODO: there has got to be a better way to do this (jshepher)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050056 for k, v in response.items():
57 if (k == 'x-auth-token'):
58 self.swift['x-auth-token'] = v
59 if (k == 'x-storage-token'):
60 self.swift['x-storage-token'] = v
61
62 # Since we don't have DNS this is a bit of a hack, but works
63 url = response['x-storage-url'].split('/')
64 self.swift['storage_url'] = "%s//%s:%s/%s/%s" % (url[0],
65 self.swift['auth_host'],
66 self.swift['auth_port'],
67 url[3],
68 url[4])
69 test_000_auth.tags = ['swift']
70
71 def test_001_create_container(self):
72 path = "%s/%s/" % (self.swift['storage_url'], "test_container")
73 http = httplib2.Http(disable_ssl_certificate_validation=True)
Soren Hansen826d5df2011-08-29 11:30:40 +020074 headers = {'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050075 response, content = http.request(path, 'PUT', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -050076 self.assertEqual(response.status, 201)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050077 test_001_create_container.tags = ['swift']
78
79 def test_002_list_containers(self):
80 http = httplib2.Http(disable_ssl_certificate_validation=True)
81 headers = {'X-Auth-Token': '%s' % (self.swift['x-auth-token'])}
82 response, content = http.request(self.swift['storage_url'], 'GET',
83 headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -050084 self.assertEqual(response.status, 200)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050085 self.assertLessEqual('1', response['x-account-container-count'])
86 test_002_list_containers.tags = ['swift']
87
88 def test_010_create_small_object(self):
89 md5 = self._md5sum_file(SMALL_OBJ)
90 path = "%s/%s/%s" % (self.swift['storage_url'],
91 "test_container",
92 "swift_small")
93 http = httplib2.Http(disable_ssl_certificate_validation=True)
94 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
95 self.swift['username']),
96 'X-Storage-Token': '%s' % (self.swift['x-storage-token']),
97 'ETag': '%s' % (md5),
98 'Content-Length': '%d' % os.path.getsize(SMALL_OBJ),
99 'Content-Type': 'application/octet-stream'}
100 upload = open(SMALL_OBJ, "rb")
101 response, content = http.request(path, 'PUT',
102 headers=headers,
103 body=upload)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500104 self.assertEqual(response.status, 201)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500105 test_010_create_small_object.tags = ['swift']
106
107 def test_011_create_medium_object(self):
108 md5 = self._md5sum_file(MED_OBJ)
109 path = "%s/%s/%s" % (self.swift['storage_url'],
110 "test_container",
111 "swift_medium")
112 http = httplib2.Http(disable_ssl_certificate_validation=True)
113 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
114 self.swift['username']),
115 'X-Storage-Token': '%s' % (self.swift['x-storage-token']),
116 'ETag': '%s' % (md5),
117 'Content-Length': '%d' % (os.path.getsize(MED_OBJ)),
118 'Content-Type': 'application/octet-stream',
119 'Content-Encoding': 'gzip'}
120 upload = ""
121 for chunk in self._read_in_chunks(MED_OBJ):
122 upload += chunk
123 response, content = http.request(path, 'PUT',
124 headers=headers,
125 body=upload)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500126 self.assertEqual(response.status, 201)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500127 test_011_create_medium_object.tags = ['swift']
128
129 def test_013_get_small_object(self):
130 path = "%s/%s/%s" % (self.swift['storage_url'],
131 "test_container",
132 "swift_small")
133 http = httplib2.Http(disable_ssl_certificate_validation=True)
134 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
135 self.swift['username']),
136 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
137 response, content = http.request(path, 'GET',
138 headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500139 self.assertEqual(response.status, 200)
140 self.assertEqual(response['etag'], self._md5sum_file(SMALL_OBJ))
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500141 test_013_get_small_object.tags = ['swift']
142
143 def test_017_delete_small_object(self):
144 path = "%s/%s/%s" % (self.swift['storage_url'], "test_container",
145 "swift_small")
146 http = httplib2.Http(disable_ssl_certificate_validation=True)
147 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
148 self.swift['username']),
149 'X-Storage-Token': '%s' % (
150 self.swift['x-storage-token'])}
151 response, content = http.request(path, 'DELETE', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500152 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500153 test_017_delete_small_object.tags = ['swift']
154
155 def test_018_delete_medium_object(self):
156 path = "%s/%s/%s" % (self.swift['storage_url'], "test_container",
157 "swift_medium")
158 http = httplib2.Http(disable_ssl_certificate_validation=True)
159 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
160 self.swift['username']),
161 'X-Storage-Token': '%s' % (
162 self.swift['x-storage-token'])}
163 response, content = http.request(path, 'DELETE', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500164 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500165 test_018_delete_medium_object.tags = ['swift']
166
167 def test_030_check_container_metadata(self):
168 path = "%s/%s" % (self.swift['storage_url'], "test_container")
169 http = httplib2.Http(disable_ssl_certificate_validation=True)
170 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
171 self.swift['username']),
172 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
173 response, content = http.request(path, 'HEAD', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500174 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500175 test_030_check_container_metadata.tags = ['swift']
176
177 def test_050_delete_container(self):
178 path = "%s/%s" % (self.swift['storage_url'], "test_container")
179 http = httplib2.Http(disable_ssl_certificate_validation=True)
180 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
181 self.swift['username']),
182 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
183 response, content = http.request(path, 'DELETE', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500184 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500185 test_050_delete_container.tags = ['swift']