1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
from lib import BaseTest
import logging
import uuid
import os
try:
import swiftclient
if 'OS_USERNAME' in os.environ and 'OS_PASSWORD' in os.environ:
auth_username = os.environ.get('OS_USERNAME')
auth_password = os.environ.get('OS_PASSWORD')
# Using auth version 2 /v2.0/
auth_url = os.environ.get('OS_AUTH_URL')
auth_tenant = os.environ.get('OS_TENANT_NAME')
account_username = "%s:%s" % (auth_tenant, auth_username)
swift_conn = swiftclient.Connection(auth_url, account_username,
auth_password, auth_version=2)
elif 'ST_USER' in os.environ and 'ST_KEY' in os.environ:
auth_username = os.environ.get('ST_USER')
auth_password = os.environ.get('ST_KEY')
auth_url = os.environ.get('ST_AUTH')
# Using auth version 1 (/auth/v1.0)
swift_conn = swiftclient.Connection(auth_url, auth_username,
auth_password, auth_version=1)
else:
print("Swift tests disabled: OpenStack creds not found in the environment")
swift_conn = None
except ImportError as e:
print("Swift tests disabled: unable to import swiftclient", e)
swift_conn = None
class SwiftTest(BaseTest):
"""
BaseTest + support for Swift
"""
def fixture_available(self):
return super(SwiftTest, self).fixture_available() and swift_conn is not None
def prepare(self):
self.container_name = "aptly-sys-test-" + str(uuid.uuid1())
swift_conn.put_container(self.container_name)
self.configOverride = {"SwiftPublishEndpoints": {
"test1": {
"container": self.container_name,
}
}}
super(SwiftTest, self).prepare()
def _try_delete_container(self):
if not hasattr(self, "container_name"):
return
try:
for obj in swift_conn.get_container(self.container_name,
full_listing=True)[1]:
swift_conn.delete_object(self.container_name, obj.get("name"))
swift_conn.delete_container(self.container_name)
except swiftclient.ClientException:
logging.exception("Error shutting down Swift container")
def shutdown(self):
self._try_delete_container()
super(SwiftTest, self).shutdown()
def check_path(self, path):
if not hasattr(self, "container_contents"):
self.container_contents = [obj.get('name') for obj in
swift_conn.get_container(self.container_name)[1]]
if path in self.container_contents:
return True
if not path.endswith("/"):
path = path + "/"
for item in self.container_contents:
if item.startswith(path):
return True
return False
def check_exists(self, path):
if not self.check_path(path):
raise Exception("path %s doesn't exist" % (path, ))
def check_not_exists(self, path):
if self.check_path(path):
raise Exception("path %s exists" % (path, ))
def read_file(self, path):
hdrs, body = swift_conn.get_object(self.container_name, path)
return body
|