~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/maasserver/tests/test_preseed.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-03-04 11:49:44 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: package-import@ubuntu.com-20130304114944-azcvu9anlf8mizpa
Tags: upstream-1.3+bzr1452+dfsg
ImportĀ upstreamĀ versionĀ 1.3+bzr1452+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
import httplib
16
16
import os
17
17
from pipes import quote
 
18
from urlparse import urlparse
18
19
 
19
20
from django.conf import settings
20
21
from maasserver.enum import (
 
22
    ARCHITECTURE,
21
23
    NODE_STATUS,
22
24
    PRESEED_TYPE,
23
25
    )
 
26
from maasserver.models import Config
24
27
from maasserver.preseed import (
25
28
    compose_enlistment_preseed_url,
26
29
    compose_preseed_url,
27
30
    GENERIC_FILENAME,
28
31
    get_enlist_preseed,
 
32
    get_hostname_and_path,
29
33
    get_node_preseed_context,
30
34
    get_preseed,
31
35
    get_preseed_context,
41
45
from maasserver.testing.factory import factory
42
46
from maasserver.testing.testcase import TestCase
43
47
from maasserver.utils import map_enum
 
48
from maastesting.matchers import ContainsAll
44
49
from testtools.matchers import (
45
50
    AllMatch,
46
51
    Contains,
61
66
        self.assertEqual(['amd64', 'test'], split_subarch('amd64/test'))
62
67
 
63
68
 
 
69
class TestGetHostnameAndPath(TestCase):
 
70
    """Tests for `get_hostname_and_path`."""
 
71
 
 
72
    def test_get_hostname_and_path(self):
 
73
        input_and_results = [
 
74
            ('http://name.domain/my/path',  ('name.domain', '/my/path')),
 
75
            ('https://domain/path',  ('domain', '/path')),
 
76
            ('http://domain/',  ('domain', '/')),
 
77
            ('http://domain',  ('domain', '')),
 
78
            ]
 
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))
 
82
 
 
83
 
64
84
class TestGetPreseedFilenames(TestCase):
65
85
    """Tests for `get_preseed_filenames`."""
66
86
 
290
310
            TemplateNotFoundError, template.substitute)
291
311
 
292
312
 
 
313
def make_url(name):
 
314
    """Create a fake archive URL."""
 
315
    return "http://%s.example.com/%s/" % (
 
316
        factory.make_name(name),
 
317
        factory.make_name('path'),
 
318
        )
 
319
 
 
320
 
293
321
class TestPreseedContext(TestCase):
294
322
    """Tests for `get_preseed_context`."""
295
323
 
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',
 
332
            'http_proxy',
 
333
            ],
302
334
            context)
303
335
 
 
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)
 
347
        self.assertEqual(
 
348
            (
 
349
                parsed_main_archive.hostname,
 
350
                parsed_main_archive.path,
 
351
                parsed_ports_archive.hostname,
 
352
                parsed_ports_archive.path,
 
353
            ),
 
354
            (
 
355
                context['main_archive_hostname'],
 
356
                context['main_archive_directory'],
 
357
                context['ports_archive_hostname'],
 
358
                context['ports_archive_directory'],
 
359
            ))
 
360
 
304
361
 
305
362
class TestNodePreseedContext(TestCase):
306
363
    """Tests for `get_node_preseed_context`."""
377
434
            preseed, MatchesAll(*[Contains(ng_url), Not(Contains(maas_url))]))
378
435
 
379
436
 
 
437
class TestRenderPreseedArchives(TestCase):
 
438
    """Test that the default preseed contains the default mirrors."""
 
439
 
 
440
    def test_render_preseed_uses_default_archives_intel(self):
 
441
        nodes = [
 
442
            factory.make_node(architecture=ARCHITECTURE.i386),
 
443
            factory.make_node(architecture=ARCHITECTURE.amd64),
 
444
            ]
 
445
        default_snippets = [
 
446
            "d-i     mirror/http/hostname string archive.ubuntu.com",
 
447
            "d-i     mirror/http/directory string /ubuntu",
 
448
            ]
 
449
        for node in nodes:
 
450
            preseed = render_preseed(node, PRESEED_TYPE.DEFAULT, "precise")
 
451
            self.assertThat(preseed, ContainsAll(default_snippets))
 
452
 
 
453
    def test_render_preseed_uses_default_archives_arm(self):
 
454
        node = factory.make_node(architecture=ARCHITECTURE.armhf_highbank)
 
455
        default_snippets = [
 
456
            "d-i     mirror/http/hostname string ports.ubuntu.com",
 
457
            "d-i     mirror/http/directory string /ubuntu-ports",
 
458
            ]
 
459
        preseed = render_preseed(node, PRESEED_TYPE.DEFAULT, "precise")
 
460
        self.assertThat(preseed, ContainsAll(default_snippets))
 
461
 
 
462
 
 
463
class TestPreseedProxy(TestCase):
 
464
 
 
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)
 
475
 
 
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)
 
486
 
 
487
 
380
488
class TestPreseedMethods(TestCase):
381
489
    """Tests for `get_enlist_preseed` and `get_preseed`.
382
490