1
1
from copy import copy
4
6
from cloudinit.sources import DataSourceMAAS
5
7
from cloudinit import url_helper
6
from ..helpers import populate_dir
11
class TestMAASDataSource(mocker.MockerTestCase):
8
from ..helpers import TestCase, populate_dir
11
from unittest import mock
16
class TestMAASDataSource(TestCase):
14
19
super(TestMAASDataSource, self).setUp()
15
20
# Make a temp directoy for tests to use.
16
self.tmp = self.makeDir()
21
self.tmp = tempfile.mkdtemp()
22
self.addCleanup(shutil.rmtree, self.tmp)
18
24
def test_seed_dir_valid(self):
19
25
"""Verify a valid seeddir is read as such."""
94
100
def test_seed_url_valid(self):
95
101
"""Verify that valid seed_url is read as such."""
96
valid = {'meta-data/instance-id': 'i-instanceid',
103
'meta-data/instance-id': 'i-instanceid',
97
104
'meta-data/local-hostname': 'test-hostname',
98
105
'meta-data/public-keys': 'test-hostname',
99
'user-data': 'foodata'}
106
'user-data': 'foodata',
101
109
'meta-data/local-hostname',
102
110
'meta-data/instance-id',
103
111
'meta-data/public-keys',
106
114
my_seed = "http://example.com/xmeta"
107
115
my_ver = "1999-99-99"
108
116
my_headers = {'header1': 'value1', 'header2': 'value2'}
110
118
def my_headers_cb(url):
111
119
return my_headers
113
mock_request = self.mocker.replace(url_helper.readurl,
116
for key in valid_order:
117
url = "%s/%s/%s" % (my_seed, my_ver, key)
118
mock_request(url, headers=None, timeout=mocker.ANY,
119
data=mocker.ANY, sec_between=mocker.ANY,
120
ssl_details=mocker.ANY, retries=mocker.ANY,
121
headers_cb=my_headers_cb,
122
exception_cb=mocker.ANY)
123
resp = valid.get(key)
124
self.mocker.result(url_helper.StringResponse(resp))
127
(userdata, metadata) = DataSourceMAAS.read_maas_seed_url(my_seed,
128
header_cb=my_headers_cb, version=my_ver)
130
self.assertEqual("foodata", userdata)
131
self.assertEqual(metadata['instance-id'],
132
valid['meta-data/instance-id'])
133
self.assertEqual(metadata['local-hostname'],
134
valid['meta-data/local-hostname'])
121
# Each time url_helper.readurl() is called, something different is
122
# returned based on the canned data above. We need to build up a list
123
# of side effect return values, which the mock will return. At the
124
# same time, we'll build up a list of expected call arguments for
125
# asserting after the code under test is run.
129
for key in valid_order:
130
resp = valid.get(key)
131
url = "%s/%s/%s" % (my_seed, my_ver, key)
133
mock.call(url, headers=None, timeout=mock.ANY,
134
data=mock.ANY, sec_between=mock.ANY,
135
ssl_details=mock.ANY, retries=mock.ANY,
136
headers_cb=my_headers_cb,
137
exception_cb=mock.ANY))
138
yield url_helper.StringResponse(resp)
140
# Now do the actual call of the code under test.
141
with mock.patch.object(url_helper, 'readurl',
142
side_effect=side_effect()) as mockobj:
143
userdata, metadata = DataSourceMAAS.read_maas_seed_url(
144
my_seed, header_cb=my_headers_cb, version=my_ver)
146
self.assertEqual("foodata", userdata)
147
self.assertEqual(metadata['instance-id'],
148
valid['meta-data/instance-id'])
149
self.assertEqual(metadata['local-hostname'],
150
valid['meta-data/local-hostname'])
152
mockobj.has_calls(calls)
136
154
def test_seed_url_invalid(self):
137
155
"""Verify that invalid seed_url raises MAASSeedDirMalformed."""