blob: 0c16da13fb80596830552ba7c7ce808a24f7a0e8 [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
20import hashlib
21import httplib2
22import json
23import os
24import tempfile
25import time
26import unittest
27import urllib
28
29from pprint import pprint
30
31import tests
32
33SMALL_OBJ = "include/swift_objects/swift_small"
34MED_OBJ = "include/swift_objects/swift_medium"
35LRG_OBJ = "include/swift_objects/swift_large"
36
37
38class TestSwift(tests.FunctionalTest):
39 def test_000_auth(self):
40 if self.swift['auth_ssl'] == "False":
41 prot = "http://"
42 else:
43 prot = "https://"
44
45 path = "%s%s:%s%s%s" % (prot, self.swift['auth_host'],
46 self.swift['auth_port'],
47 self.swift['auth_prefix'],
48 self.swift['ver'])
49
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050050 http = httplib2.Http(disable_ssl_certificate_validation=True)
51 self.swift['auth_user'] = '%s:%s' % (self.swift['account'],
52 self.swift['username'])
53 headers = {'X-Auth-User': '%s' % (self.swift['auth_user']),
54 'X-Auth-Key': '%s' % (self.swift['password'])}
55 response, content = http.request(path, 'GET', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -050056 self.assertEqual(response.status, 200)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050057 self.assertIsNotNone(response['x-auth-token'])
58 self.assertIsNotNone(response['x-storage-token'])
59 self.assertIsNotNone(response['x-storage-url'])
60
Justin Shepherd74a95b62011-08-18 11:52:02 -050061 # TODO: there has got to be a better way to do this (jshepher)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050062 for k, v in response.items():
63 if (k == 'x-auth-token'):
64 self.swift['x-auth-token'] = v
65 if (k == 'x-storage-token'):
66 self.swift['x-storage-token'] = v
67
68 # Since we don't have DNS this is a bit of a hack, but works
69 url = response['x-storage-url'].split('/')
70 self.swift['storage_url'] = "%s//%s:%s/%s/%s" % (url[0],
71 self.swift['auth_host'],
72 self.swift['auth_port'],
73 url[3],
74 url[4])
75 test_000_auth.tags = ['swift']
76
77 def test_001_create_container(self):
78 path = "%s/%s/" % (self.swift['storage_url'], "test_container")
79 http = httplib2.Http(disable_ssl_certificate_validation=True)
80 headers = { 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
81 response, content = http.request(path, 'PUT', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -050082 self.assertEqual(response.status, 201)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050083 test_001_create_container.tags = ['swift']
84
85 def test_002_list_containers(self):
86 http = httplib2.Http(disable_ssl_certificate_validation=True)
87 headers = {'X-Auth-Token': '%s' % (self.swift['x-auth-token'])}
88 response, content = http.request(self.swift['storage_url'], 'GET',
89 headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -050090 self.assertEqual(response.status, 200)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050091 self.assertLessEqual('1', response['x-account-container-count'])
92 test_002_list_containers.tags = ['swift']
93
94 def test_010_create_small_object(self):
95 md5 = self._md5sum_file(SMALL_OBJ)
96 path = "%s/%s/%s" % (self.swift['storage_url'],
97 "test_container",
98 "swift_small")
99 http = httplib2.Http(disable_ssl_certificate_validation=True)
100 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
101 self.swift['username']),
102 'X-Storage-Token': '%s' % (self.swift['x-storage-token']),
103 'ETag': '%s' % (md5),
104 'Content-Length': '%d' % os.path.getsize(SMALL_OBJ),
105 'Content-Type': 'application/octet-stream'}
106 upload = open(SMALL_OBJ, "rb")
107 response, content = http.request(path, 'PUT',
108 headers=headers,
109 body=upload)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500110 self.assertEqual(response.status, 201)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500111 test_010_create_small_object.tags = ['swift']
112
113 def test_011_create_medium_object(self):
114 md5 = self._md5sum_file(MED_OBJ)
115 path = "%s/%s/%s" % (self.swift['storage_url'],
116 "test_container",
117 "swift_medium")
118 http = httplib2.Http(disable_ssl_certificate_validation=True)
119 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
120 self.swift['username']),
121 'X-Storage-Token': '%s' % (self.swift['x-storage-token']),
122 'ETag': '%s' % (md5),
123 'Content-Length': '%d' % (os.path.getsize(MED_OBJ)),
124 'Content-Type': 'application/octet-stream',
125 'Content-Encoding': 'gzip'}
126 upload = ""
127 for chunk in self._read_in_chunks(MED_OBJ):
128 upload += chunk
129 response, content = http.request(path, 'PUT',
130 headers=headers,
131 body=upload)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500132 self.assertEqual(response.status, 201)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500133 test_011_create_medium_object.tags = ['swift']
134
135 def test_013_get_small_object(self):
136 path = "%s/%s/%s" % (self.swift['storage_url'],
137 "test_container",
138 "swift_small")
139 http = httplib2.Http(disable_ssl_certificate_validation=True)
140 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
141 self.swift['username']),
142 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
143 response, content = http.request(path, 'GET',
144 headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500145 self.assertEqual(response.status, 200)
146 self.assertEqual(response['etag'], self._md5sum_file(SMALL_OBJ))
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500147 test_013_get_small_object.tags = ['swift']
148
149 def test_017_delete_small_object(self):
150 path = "%s/%s/%s" % (self.swift['storage_url'], "test_container",
151 "swift_small")
152 http = httplib2.Http(disable_ssl_certificate_validation=True)
153 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
154 self.swift['username']),
155 'X-Storage-Token': '%s' % (
156 self.swift['x-storage-token'])}
157 response, content = http.request(path, 'DELETE', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500158 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500159 test_017_delete_small_object.tags = ['swift']
160
161 def test_018_delete_medium_object(self):
162 path = "%s/%s/%s" % (self.swift['storage_url'], "test_container",
163 "swift_medium")
164 http = httplib2.Http(disable_ssl_certificate_validation=True)
165 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
166 self.swift['username']),
167 'X-Storage-Token': '%s' % (
168 self.swift['x-storage-token'])}
169 response, content = http.request(path, 'DELETE', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500170 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500171 test_018_delete_medium_object.tags = ['swift']
172
173 def test_030_check_container_metadata(self):
174 path = "%s/%s" % (self.swift['storage_url'], "test_container")
175 http = httplib2.Http(disable_ssl_certificate_validation=True)
176 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
177 self.swift['username']),
178 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
179 response, content = http.request(path, 'HEAD', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500180 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500181 test_030_check_container_metadata.tags = ['swift']
182
183 def test_050_delete_container(self):
184 path = "%s/%s" % (self.swift['storage_url'], "test_container")
185 http = httplib2.Http(disable_ssl_certificate_validation=True)
186 headers = {'X-Auth-User': '%s:%s' % (self.swift['account'],
187 self.swift['username']),
188 'X-Storage-Token': '%s' % (self.swift['x-storage-token'])}
189 response, content = http.request(path, 'DELETE', headers=headers)
Justin Shepherd74a95b62011-08-18 11:52:02 -0500190 self.assertEqual(response.status, 204)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500191 test_050_delete_container.tags = ['swift']