~ubuntu-branches/ubuntu/wily/heat/wily

1.1.27 by Corey Bryant
Import upstream version 5.0.0~b2
1
#
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
5
#
6
#         http://www.apache.org/licenses/LICENSE-2.0
7
#
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
12
#    under the License.
13
14
import datetime
15
16
import mock
17
import pytz
18
from testtools import matchers
19
20
from heat.engine.clients.os import swift
21
from heat.tests import common
22
from heat.tests import utils
23
24
25
class SwiftClientPluginTestCase(common.HeatTestCase):
26
    def setUp(self):
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
34
35
36
class SwiftUtilsTests(SwiftClientPluginTestCase):
37
38
    def test_is_valid_temp_url_path(self):
39
40
        valids = [
41
            "/v1/AUTH_demo/c/o",
42
            "/v1/AUTH_demo/c/o/",
43
            "/v1/TEST_demo/c/o",
44
            "/v1/AUTH_demo/c/pseudo_folder/o",
45
        ]
46
        for url in valids:
47
            self.assertTrue(self.swift_plugin.is_valid_temp_url_path(url))
48
49
        invalids = [
50
            "/v2/AUTH_demo/c/o",
51
            "/v1/AUTH_demo/c//",
52
            "/v1/AUTH_demo/c/",
53
            "/AUTH_demo/c//",
54
            "//AUTH_demo/c/o",
55
            "//v1/AUTH_demo/c/o",
56
            "/v1/AUTH_demo/o",
57
            "/v1/AUTH_demo//o",
58
            "/v1/AUTH_d3mo//o",
59
            "/v1//c/o",
60
            "/v1/c/o",
61
        ]
62
        for url in invalids:
63
            self.assertFalse(self.swift_plugin.is_valid_temp_url_path(url))
64
65
    def test_get_temp_url(self):
66
        self.swift_client.url = ("http://fake-host.com:8080/v1/"
67
                                 "AUTH_demo")
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()
71
72
        container_name = '1234'  # from stack.id
73
        stack_name = 'test'
74
        handle_name = 'foo'
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))
83
84
        timeout = int(url.split('=')[-1])
85
        self.assertTrue(timeout < swift.MAX_EPOCH)
86
87
    def test_get_temp_url_no_account_key(self):
88
        self.swift_client.url = ("http://fake-host.com:8080/v1/"
89
                                 "AUTH_demo")
90
        head_account = {}
91
92
        def post_account(data):
93
            head_account.update(data)
94
95
        self.swift_client.head_account = mock.Mock(return_value=head_account)
96
        self.swift_client.post_account = post_account
97
98
        container_name = '1234'  # from stack.id
99
        stack_name = 'test'
100
        handle_name = 'foo'
101
        obj_name = '%s-%s' % (stack_name, handle_name)
102
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)
106
107
    def test_get_signal_url(self):
108
        self.swift_client.url = ("http://fake-host.com:8080/v1/"
109
                                 "AUTH_demo")
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()
113
114
        container_name = '1234'  # from stack.id
115
        stack_name = 'test'
116
        handle_name = 'foo'
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))
126
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)
135
        self.assertEqual(
136
            now_naive,
137
            self.swift_plugin.parse_last_modified(last_modified))