summaryrefslogtreecommitdiff
path: root/heat/tests/test_urlfetch.py
blob: b2d4e56aa28856759ffa2e156ab41597a40f60d1 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from oslo_config import cfg
import requests
from requests import exceptions
import six

from heat.common import urlfetch
from heat.tests import common


class Response(object):
    def __init__(self, buf=''):
        self.buf = buf

    def iter_content(self, chunk_size=1):
        while self.buf:
            yield self.buf[:chunk_size]
            self.buf = self.buf[chunk_size:]

    def raise_for_status(self):
        pass


class UrlFetchTest(common.HeatTestCase):
    def setUp(self):
        super(UrlFetchTest, self).setUp()
        self.m.StubOutWithMock(requests, 'get')

    def test_file_scheme_default_behaviour(self):
        self.m.ReplayAll()
        self.assertRaises(urlfetch.URLFetchError,
                          urlfetch.get, 'file:///etc/profile')
        self.m.VerifyAll()

    def test_file_scheme_supported(self):
        data = '{ "foo": "bar" }'
        url = 'file:///etc/profile'

        self.m.StubOutWithMock(six.moves.urllib.request, 'urlopen')
        six.moves.urllib.request.urlopen(url).AndReturn(
            six.moves.cStringIO(data))
        self.m.ReplayAll()

        self.assertEqual(data, urlfetch.get(url, allowed_schemes=['file']))
        self.m.VerifyAll()

    def test_file_scheme_failure(self):
        url = 'file:///etc/profile'

        self.m.StubOutWithMock(six.moves.urllib.request, 'urlopen')
        six.moves.urllib.request.urlopen(url).AndRaise(
            six.moves.urllib.error.URLError('oops'))
        self.m.ReplayAll()

        self.assertRaises(urlfetch.URLFetchError,
                          urlfetch.get, url, allowed_schemes=['file'])
        self.m.VerifyAll()

    def test_http_scheme(self):
        url = 'http://example.com/template'
        data = b'{ "foo": "bar" }'
        response = Response(data)
        requests.get(url, stream=True).AndReturn(response)
        self.m.ReplayAll()
        self.assertEqual(data, urlfetch.get(url))
        self.m.VerifyAll()

    def test_https_scheme(self):
        url = 'https://example.com/template'
        data = b'{ "foo": "bar" }'
        response = Response(data)
        requests.get(url, stream=True).AndReturn(response)
        self.m.ReplayAll()
        self.assertEqual(data, urlfetch.get(url))
        self.m.VerifyAll()

    def test_http_error(self):
        url = 'http://example.com/template'

        requests.get(url, stream=True).AndRaise(exceptions.HTTPError())
        self.m.ReplayAll()

        self.assertRaises(urlfetch.URLFetchError, urlfetch.get, url)
        self.m.VerifyAll()

    def test_non_exist_url(self):
        url = 'http://non-exist.com/template'

        requests.get(url, stream=True).AndRaise(exceptions.Timeout())
        self.m.ReplayAll()

        self.assertRaises(urlfetch.URLFetchError, urlfetch.get, url)
        self.m.VerifyAll()

    def test_garbage(self):
        self.m.ReplayAll()
        self.assertRaises(urlfetch.URLFetchError, urlfetch.get, 'wibble')
        self.m.VerifyAll()

    def test_max_fetch_size_okay(self):
        url = 'http://example.com/template'
        data = b'{ "foo": "bar" }'
        response = Response(data)
        cfg.CONF.set_override('max_template_size', 500, enforce_type=True)
        requests.get(url, stream=True).AndReturn(response)
        self.m.ReplayAll()
        urlfetch.get(url)
        self.m.VerifyAll()

    def test_max_fetch_size_error(self):
        url = 'http://example.com/template'
        data = b'{ "foo": "bar" }'
        response = Response(data)
        cfg.CONF.set_override('max_template_size', 5, enforce_type=True)
        requests.get(url, stream=True).AndReturn(response)
        self.m.ReplayAll()
        exception = self.assertRaises(urlfetch.URLFetchError,
                                      urlfetch.get, url)
        self.assertIn("Template exceeds", six.text_type(exception))
        self.m.VerifyAll()