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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
""" test_handler_apt_source
Testing various config variations of the apt_source config
"""
import os
import re
import shutil
import tempfile
try:
from unittest import mock
except ImportError:
import mock
from mock import call
from cloudinit.config import cc_apt_configure
from cloudinit import util
from ..helpers import TestCase
EXPECTEDKEY = """-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mI0ESuZLUgEEAKkqq3idtFP7g9hzOu1a8+v8ImawQN4TrvlygfScMU1TIS1eC7UQ
NUA8Qqgr9iUaGnejb0VciqftLrU9D6WYHSKz+EITefgdyJ6SoQxjoJdsCpJ7o9Jy
8PQnpRttiFm4qHu6BVnKnBNxw/z3ST9YMqW5kbMQpfxbGe+obRox59NpABEBAAG0
HUxhdW5jaHBhZCBQUEEgZm9yIFNjb3R0IE1vc2VyiLYEEwECACAFAkrmS1ICGwMG
CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAGILvPA2g/d3aEA/9tVjc10HOZwV29
OatVuTeERjjrIbxflO586GLA8cp0C9RQCwgod/R+cKYdQcHjbqVcP0HqxveLg0RZ
FJpWLmWKamwkABErwQLGlM/Hwhjfade8VvEQutH5/0JgKHmzRsoqfR+LMO6OS+Sm
S0ORP6HXET3+jC8BMG4tBWCTK/XEZw==
=ACB2
-----END PGP PUBLIC KEY BLOCK-----"""
def load_tfile_or_url(*args, **kwargs):
""" load_tfile_or_url
load file and return content after decoding
"""
return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents)
class TestAptSourceConfig(TestCase):
""" TestAptSourceConfig
Main Class to test apt_source configs
"""
def setUp(self):
super(TestAptSourceConfig, self).setUp()
self.tmp = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tmp)
self.aptlistfile = os.path.join(self.tmp, "single-deb.list")
self.aptlistfile2 = os.path.join(self.tmp, "single-deb2.list")
self.aptlistfile3 = os.path.join(self.tmp, "single-deb3.list")
self.join = os.path.join
# mock fallback filename into writable tmp dir
self.fallbackfn = os.path.join(self.tmp, "etc/apt/sources.list.d/",
"cloud_config_sources.list")
@staticmethod
def _get_default_params():
""" get_default_params
Get the most basic default mrror and release info to be used in tests
"""
params = {}
params['RELEASE'] = cc_apt_configure.get_release()
params['MIRROR'] = "http://archive.ubuntu.com/ubuntu"
return params
def myjoin(self, *args, **kwargs):
""" myjoin - redir into writable tmpdir"""
if (args[0] == "/etc/apt/sources.list.d/"
and args[1] == "cloud_config_sources.list"
and len(args) == 2):
return self.join(self.tmp, args[0].lstrip("/"), args[1])
else:
return self.join(*args, **kwargs)
def apt_source_basic(self, filename, cfg):
""" apt_source_basic
Test Fix deb source string, has to overwrite mirror conf in params
"""
params = self._get_default_params()
cc_apt_configure.add_sources(cfg, params)
self.assertTrue(os.path.isfile(filename))
contents = load_tfile_or_url(filename)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb", "http://archive.ubuntu.com/ubuntu",
"karmic-backports",
"main universe multiverse restricted"),
contents, flags=re.IGNORECASE))
def test_apt_source_basic(self):
""" test_apt_source_basic
Test Fix deb source string, has to overwrite mirror conf in params.
Test with a filename provided in config.
"""
cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu'
' karmic-backports'
' main universe multiverse restricted'),
'filename': self.aptlistfile}
self.apt_source_basic(self.aptlistfile, [cfg])
def test_apt_source_basic_triple(self):
""" test_apt_source_basic_triple
Test Fix three deb source string, has to overwrite mirror conf in
params. Test with filenames provided in config.
"""
cfg1 = {'source': ('deb http://archive.ubuntu.com/ubuntu'
' karmic-backports'
' main universe multiverse restricted'),
'filename': self.aptlistfile}
cfg2 = {'source': ('deb http://archive.ubuntu.com/ubuntu'
' precise-backports'
' main universe multiverse restricted'),
'filename': self.aptlistfile2}
cfg3 = {'source': ('deb http://archive.ubuntu.com/ubuntu'
' lucid-backports'
' main universe multiverse restricted'),
'filename': self.aptlistfile3}
self.apt_source_basic(self.aptlistfile, [cfg1, cfg2, cfg3])
# extra verify on two extra files of this test
contents = load_tfile_or_url(self.aptlistfile2)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb", "http://archive.ubuntu.com/ubuntu",
"precise-backports",
"main universe multiverse restricted"),
contents, flags=re.IGNORECASE))
contents = load_tfile_or_url(self.aptlistfile3)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb", "http://archive.ubuntu.com/ubuntu",
"lucid-backports",
"main universe multiverse restricted"),
contents, flags=re.IGNORECASE))
def test_apt_source_basic_nofn(self):
""" test_apt_source_basic_nofn
Test Fix deb source string, has to overwrite mirror conf in params.
Test without a filename provided in config and test for known fallback.
"""
cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu'
' karmic-backports'
' main universe multiverse restricted')}
with mock.patch.object(os.path, 'join', side_effect=self.myjoin):
self.apt_source_basic(self.fallbackfn, [cfg])
def apt_source_replacement(self, filename, cfg):
""" apt_source_replace
Test Autoreplacement of MIRROR and RELEASE in source specs
"""
params = self._get_default_params()
cc_apt_configure.add_sources(cfg, params)
self.assertTrue(os.path.isfile(filename))
contents = load_tfile_or_url(filename)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb", params['MIRROR'], params['RELEASE'],
"multiverse"),
contents, flags=re.IGNORECASE))
def test_apt_source_replace(self):
""" test_apt_source_replace
Test Autoreplacement of MIRROR and RELEASE in source specs with
Filename being set
"""
cfg = {'source': 'deb $MIRROR $RELEASE multiverse',
'filename': self.aptlistfile}
self.apt_source_replacement(self.aptlistfile, [cfg])
def test_apt_source_replace_triple(self):
""" test_apt_source_replace_triple
Test three autoreplacements of MIRROR and RELEASE in source specs with
Filename being set
"""
cfg1 = {'source': 'deb $MIRROR $RELEASE multiverse',
'filename': self.aptlistfile}
cfg2 = {'source': 'deb $MIRROR $RELEASE main',
'filename': self.aptlistfile2}
cfg3 = {'source': 'deb $MIRROR $RELEASE universe',
'filename': self.aptlistfile3}
self.apt_source_replacement(self.aptlistfile, [cfg1, cfg2, cfg3])
# extra verify on two extra files of this test
params = self._get_default_params()
contents = load_tfile_or_url(self.aptlistfile2)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb", params['MIRROR'], params['RELEASE'],
"main"),
contents, flags=re.IGNORECASE))
contents = load_tfile_or_url(self.aptlistfile3)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb", params['MIRROR'], params['RELEASE'],
"universe"),
contents, flags=re.IGNORECASE))
def test_apt_source_replace_nofn(self):
""" test_apt_source_replace_nofn
Test Autoreplacement of MIRROR and RELEASE in source specs with
No filename being set
"""
cfg = {'source': 'deb $MIRROR $RELEASE multiverse'}
with mock.patch.object(os.path, 'join', side_effect=self.myjoin):
self.apt_source_replacement(self.fallbackfn, [cfg])
def apt_source_keyid(self, filename, cfg, keynum):
""" apt_source_keyid
Test specification of a source + keyid
"""
params = self._get_default_params()
with mock.patch.object(util, 'subp',
return_value=('fakekey 1234', '')) as mockobj:
cc_apt_configure.add_sources(cfg, params)
# check if it added the right ammount of keys
calls = []
for i in range(keynum):
calls.append(call(('apt-key', 'add', '-'), 'fakekey 1234'))
mockobj.assert_has_calls(calls, any_order=True)
self.assertTrue(os.path.isfile(filename))
contents = load_tfile_or_url(filename)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb",
('http://ppa.launchpad.net/smoser/'
'cloud-init-test/ubuntu'),
"xenial", "main"),
contents, flags=re.IGNORECASE))
def test_apt_source_keyid(self):
""" test_apt_source_keyid
Test specification of a source + keyid with filename being set
"""
cfg = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial main'),
'keyid': "03683F77",
'filename': self.aptlistfile}
self.apt_source_keyid(self.aptlistfile, [cfg], 1)
def test_apt_source_keyid_triple(self):
""" test_apt_source_keyid_triple
Test specification of a source + keyid with filename being set
Setting three of such, check for content and keys
"""
cfg1 = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial main'),
'keyid': "03683F77",
'filename': self.aptlistfile}
cfg2 = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial universe'),
'keyid': "03683F77",
'filename': self.aptlistfile2}
cfg3 = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial multiverse'),
'keyid': "03683F77",
'filename': self.aptlistfile3}
self.apt_source_keyid(self.aptlistfile, [cfg1, cfg2, cfg3], 3)
contents = load_tfile_or_url(self.aptlistfile2)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb",
('http://ppa.launchpad.net/smoser/'
'cloud-init-test/ubuntu'),
"xenial", "universe"),
contents, flags=re.IGNORECASE))
contents = load_tfile_or_url(self.aptlistfile3)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb",
('http://ppa.launchpad.net/smoser/'
'cloud-init-test/ubuntu'),
"xenial", "multiverse"),
contents, flags=re.IGNORECASE))
def test_apt_source_keyid_nofn(self):
""" test_apt_source_keyid_nofn
Test specification of a source + keyid without filename being set
"""
cfg = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial main'),
'keyid': "03683F77"}
with mock.patch.object(os.path, 'join', side_effect=self.myjoin):
self.apt_source_keyid(self.fallbackfn, [cfg], 1)
def apt_source_key(self, filename, cfg):
""" apt_source_key
Test specification of a source + key
"""
params = self._get_default_params()
with mock.patch.object(util, 'subp') as mockobj:
cc_apt_configure.add_sources([cfg], params)
mockobj.assert_called_with(('apt-key', 'add', '-'), 'fakekey 4321')
self.assertTrue(os.path.isfile(filename))
contents = load_tfile_or_url(filename)
self.assertTrue(re.search(r"%s %s %s %s\n" %
("deb",
('http://ppa.launchpad.net/smoser/'
'cloud-init-test/ubuntu'),
"xenial", "main"),
contents, flags=re.IGNORECASE))
def test_apt_source_key(self):
""" test_apt_source_key
Test specification of a source + key with filename being set
"""
cfg = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial main'),
'key': "fakekey 4321",
'filename': self.aptlistfile}
self.apt_source_key(self.aptlistfile, cfg)
def test_apt_source_key_nofn(self):
""" test_apt_source_key_nofn
Test specification of a source + key without filename being set
"""
cfg = {'source': ('deb '
'http://ppa.launchpad.net/'
'smoser/cloud-init-test/ubuntu'
' xenial main'),
'key': "fakekey 4321"}
with mock.patch.object(os.path, 'join', side_effect=self.myjoin):
self.apt_source_key(self.fallbackfn, cfg)
def test_apt_source_keyonly(self):
""" test_apt_source_keyonly
Test specification key without source
"""
params = self._get_default_params()
cfg = {'key': "fakekey 4242",
'filename': self.aptlistfile}
with mock.patch.object(util, 'subp') as mockobj:
cc_apt_configure.add_sources([cfg], params)
mockobj.assert_called_once_with(('apt-key', 'add', '-'),
'fakekey 4242')
# filename should be ignored on key only
self.assertFalse(os.path.isfile(self.aptlistfile))
def test_apt_source_keyidonly(self):
""" test_apt_source_keyidonly
Test specification of a keyid without source
"""
params = self._get_default_params()
cfg = {'keyid': "03683F77",
'filename': self.aptlistfile}
with mock.patch.object(util, 'subp',
return_value=('fakekey 1212', '')) as mockobj:
cc_apt_configure.add_sources([cfg], params)
mockobj.assert_called_with(('apt-key', 'add', '-'), 'fakekey 1212')
# filename should be ignored on key only
self.assertFalse(os.path.isfile(self.aptlistfile))
def test_apt_source_keyid_real(self):
""" test_apt_source_keyid_real
Test specification of a keyid without source incl
up to addition of the key (nothing but add_key_raw mocked)
"""
keyid = "03683F77"
params = self._get_default_params()
cfg = {'keyid': keyid,
'filename': self.aptlistfile}
with mock.patch.object(cc_apt_configure, 'add_key_raw') as mockobj:
cc_apt_configure.add_sources([cfg], params)
mockobj.assert_called_with(EXPECTEDKEY)
# filename should be ignored on key only
self.assertFalse(os.path.isfile(self.aptlistfile))
def test_apt_source_longkeyid_real(self):
""" test_apt_source_keyid_real
Test specification of a long key fingerprint without source incl
up to addition of the key (nothing but add_key_raw mocked)
"""
keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77"
params = self._get_default_params()
cfg = {'keyid': keyid,
'filename': self.aptlistfile}
with mock.patch.object(cc_apt_configure, 'add_key_raw') as mockobj:
cc_apt_configure.add_sources([cfg], params)
mockobj.assert_called_with(EXPECTEDKEY)
# filename should be ignored on key only
self.assertFalse(os.path.isfile(self.aptlistfile))
def test_apt_source_ppa(self):
""" test_apt_source_ppa
Test specification of a ppa
"""
params = self._get_default_params()
cfg = {'source': 'ppa:smoser/cloud-init-test',
'filename': self.aptlistfile}
# default matcher needed for ppa
matcher = re.compile(r'^[\w-]+:\w').search
with mock.patch.object(util, 'subp') as mockobj:
cc_apt_configure.add_sources([cfg], params, aa_repo_match=matcher)
mockobj.assert_called_once_with(['add-apt-repository',
'ppa:smoser/cloud-init-test'])
# adding ppa should ignore filename (uses add-apt-repository)
self.assertFalse(os.path.isfile(self.aptlistfile))
# vi: ts=4 expandtab
|