17
17
from pipes import quote
18
from urlparse import urlparse
19
20
from django.conf import settings
20
21
from maasserver.enum import (
26
from maasserver.models import Config
24
27
from maasserver.preseed import (
25
28
compose_enlistment_preseed_url,
26
29
compose_preseed_url,
28
31
get_enlist_preseed,
32
get_hostname_and_path,
29
33
get_node_preseed_context,
31
35
get_preseed_context,
61
66
self.assertEqual(['amd64', 'test'], split_subarch('amd64/test'))
69
class TestGetHostnameAndPath(TestCase):
70
"""Tests for `get_hostname_and_path`."""
72
def test_get_hostname_and_path(self):
74
('http://name.domain/my/path', ('name.domain', '/my/path')),
75
('https://domain/path', ('domain', '/path')),
76
('http://domain/', ('domain', '/')),
77
('http://domain', ('domain', '')),
79
inputs = [input for input, _ in input_and_results]
80
results = [result for _, result in input_and_results]
81
self.assertEqual(results, map(get_hostname_and_path, inputs))
64
84
class TestGetPreseedFilenames(TestCase):
65
85
"""Tests for `get_preseed_filenames`."""
298
326
nodegroup = factory.make_node_group(maas_url=factory.getRandomString())
299
327
context = get_preseed_context(release, nodegroup)
300
328
self.assertItemsEqual(
301
['release', 'metadata_enlist_url', 'server_host', 'server_url'],
329
['release', 'metadata_enlist_url', 'server_host', 'server_url',
330
'main_archive_hostname', 'main_archive_directory',
331
'ports_archive_hostname', 'ports_archive_directory',
336
def test_get_preseed_context_archive_refs(self):
337
# urlparse lowercases the hostnames. That should not have any
338
# impact but for testing, create lower-case hostnames.
339
main_archive = make_url('main_archive')
340
ports_archive = make_url('ports_archive')
341
Config.objects.set_config('main_archive', main_archive)
342
Config.objects.set_config('ports_archive', ports_archive)
343
nodegroup = factory.make_node_group(maas_url=factory.getRandomString())
344
context = get_preseed_context(factory.make_node(), nodegroup)
345
parsed_main_archive = urlparse(main_archive)
346
parsed_ports_archive = urlparse(ports_archive)
349
parsed_main_archive.hostname,
350
parsed_main_archive.path,
351
parsed_ports_archive.hostname,
352
parsed_ports_archive.path,
355
context['main_archive_hostname'],
356
context['main_archive_directory'],
357
context['ports_archive_hostname'],
358
context['ports_archive_directory'],
305
362
class TestNodePreseedContext(TestCase):
306
363
"""Tests for `get_node_preseed_context`."""
377
434
preseed, MatchesAll(*[Contains(ng_url), Not(Contains(maas_url))]))
437
class TestRenderPreseedArchives(TestCase):
438
"""Test that the default preseed contains the default mirrors."""
440
def test_render_preseed_uses_default_archives_intel(self):
442
factory.make_node(architecture=ARCHITECTURE.i386),
443
factory.make_node(architecture=ARCHITECTURE.amd64),
446
"d-i mirror/http/hostname string archive.ubuntu.com",
447
"d-i mirror/http/directory string /ubuntu",
450
preseed = render_preseed(node, PRESEED_TYPE.DEFAULT, "precise")
451
self.assertThat(preseed, ContainsAll(default_snippets))
453
def test_render_preseed_uses_default_archives_arm(self):
454
node = factory.make_node(architecture=ARCHITECTURE.armhf_highbank)
456
"d-i mirror/http/hostname string ports.ubuntu.com",
457
"d-i mirror/http/directory string /ubuntu-ports",
459
preseed = render_preseed(node, PRESEED_TYPE.DEFAULT, "precise")
460
self.assertThat(preseed, ContainsAll(default_snippets))
463
class TestPreseedProxy(TestCase):
465
def test_preseed_uses_default_proxy(self):
466
server_host = factory.make_hostname()
467
url = 'http://%s:%d/%s' % (
468
server_host, factory.getRandomPort(), factory.getRandomString())
469
self.patch(settings, 'DEFAULT_MAAS_URL', url)
470
expected_proxy_statement = (
471
"mirror/http/proxy string http://%s:8000" % server_host)
472
preseed = render_preseed(
473
factory.make_node(), PRESEED_TYPE.DEFAULT, "precise")
474
self.assertIn(expected_proxy_statement, preseed)
476
def test_preseed_uses_configured_proxy(self):
477
http_proxy = 'http://%s:%d/%s' % (
478
factory.getRandomString(), factory.getRandomPort(),
479
factory.getRandomString())
480
Config.objects.set_config('http_proxy', http_proxy)
481
expected_proxy_statement = (
482
"mirror/http/proxy string %s" % http_proxy)
483
preseed = render_preseed(
484
factory.make_node(), PRESEED_TYPE.DEFAULT, "precise")
485
self.assertIn(expected_proxy_statement, preseed)
380
488
class TestPreseedMethods(TestCase):
381
489
"""Tests for `get_enlist_preseed` and `get_preseed`.