2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
# not use this file except in compliance with the License. You may obtain
4
# a copy of the License at
6
# http://www.apache.org/licenses/LICENSE-2.0
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
# License for the specific language governing permissions and limitations
18
from testtools import matchers
20
from heat.engine.clients.os import swift
21
from heat.tests import common
22
from heat.tests import utils
25
class SwiftClientPluginTestCase(common.HeatTestCase):
27
super(SwiftClientPluginTestCase, self).setUp()
28
self.swift_client = mock.Mock()
29
self.context = utils.dummy_context()
30
self.context.tenant_id = "demo"
31
c = self.context.clients
32
self.swift_plugin = c.client_plugin('swift')
33
self.swift_plugin._client = self.swift_client
36
class SwiftUtilsTests(SwiftClientPluginTestCase):
38
def test_is_valid_temp_url_path(self):
44
"/v1/AUTH_demo/c/pseudo_folder/o",
47
self.assertTrue(self.swift_plugin.is_valid_temp_url_path(url))
63
self.assertFalse(self.swift_plugin.is_valid_temp_url_path(url))
65
def test_get_temp_url(self):
66
self.swift_client.url = ("http://fake-host.com:8080/v1/"
68
self.swift_client.head_account = mock.Mock(return_value={
69
'x-account-meta-temp-url-key': '123456'})
70
self.swift_client.post_account = mock.Mock()
72
container_name = '1234' # from stack.id
75
obj_name = '%s-%s' % (stack_name, handle_name)
76
url = self.swift_plugin.get_temp_url(container_name, obj_name)
77
self.assertFalse(self.swift_client.post_account.called)
78
regexp = ("http://fake-host.com:8080/v1/AUTH_demo/%s"
79
"/%s\?temp_url_sig=[0-9a-f]{40}&"
80
"temp_url_expires=[0-9]{10}" %
81
(container_name, obj_name))
82
self.assertThat(url, matchers.MatchesRegex(regexp))
84
timeout = int(url.split('=')[-1])
85
self.assertTrue(timeout < swift.MAX_EPOCH)
87
def test_get_temp_url_no_account_key(self):
88
self.swift_client.url = ("http://fake-host.com:8080/v1/"
92
def post_account(data):
93
head_account.update(data)
95
self.swift_client.head_account = mock.Mock(return_value=head_account)
96
self.swift_client.post_account = post_account
98
container_name = '1234' # from stack.id
101
obj_name = '%s-%s' % (stack_name, handle_name)
103
self.assertNotIn('x-account-meta-temp-url-key', head_account)
104
self.swift_plugin.get_temp_url(container_name, obj_name)
105
self.assertIn('x-account-meta-temp-url-key', head_account)
107
def test_get_signal_url(self):
108
self.swift_client.url = ("http://fake-host.com:8080/v1/"
110
self.swift_client.head_account = mock.Mock(return_value={
111
'x-account-meta-temp-url-key': '123456'})
112
self.swift_client.post_account = mock.Mock()
114
container_name = '1234' # from stack.id
117
obj_name = '%s-%s' % (stack_name, handle_name)
118
url = self.swift_plugin.get_signal_url(container_name, obj_name)
119
self.assertTrue(self.swift_client.put_container.called)
120
self.assertTrue(self.swift_client.put_object.called)
121
regexp = ("http://fake-host.com:8080/v1/AUTH_demo/%s"
122
"/%s\?temp_url_sig=[0-9a-f]{40}&"
123
"temp_url_expires=[0-9]{10}" %
124
(container_name, obj_name))
125
self.assertThat(url, matchers.MatchesRegex(regexp))
127
def test_parse_last_modified(self):
128
self.assertIsNone(self.swift_plugin.parse_last_modified(None))
129
now = datetime.datetime(
130
2015, 2, 5, 1, 4, 40, 0, pytz.timezone('GMT'))
131
now_naive = datetime.datetime(
132
2015, 2, 5, 1, 4, 40, 0)
133
last_modified = now.strftime('%a, %d %b %Y %H:%M:%S %Z')
134
self.assertEqual('Thu, 05 Feb 2015 01:04:40 GMT', last_modified)
137
self.swift_plugin.parse_last_modified(last_modified))