~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/provisioningserver/pxe/tests/test_pxeconfig.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Tests for `provisioningserver.pxe`."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
import os
 
16
import re
 
17
 
 
18
from maastesting.factory import factory
 
19
from maastesting.testcase import TestCase
 
20
import provisioningserver.pxe.pxeconfig
 
21
from provisioningserver.pxe.pxeconfig import (
 
22
    PXEConfig,
 
23
    PXEConfigFail,
 
24
    )
 
25
from provisioningserver.pxe.tftppath import (
 
26
    compose_config_path,
 
27
    locate_tftp_path,
 
28
    )
 
29
import tempita
 
30
from testtools.matchers import (
 
31
    Contains,
 
32
    FileContains,
 
33
    MatchesRegex,
 
34
    )
 
35
 
 
36
 
 
37
class TestPXEConfig(TestCase):
 
38
    """Tests for PXEConfig."""
 
39
 
 
40
    def configure_templates_dir(self, path=None):
 
41
        """Configure PXE_TEMPLATES_DIR to `path`."""
 
42
        self.patch(
 
43
            provisioningserver.pxe.pxeconfig, 'PXE_TEMPLATES_DIR', path)
 
44
 
 
45
    def test_init_sets_up_paths(self):
 
46
        pxeconfig = PXEConfig("armhf", "armadaxp")
 
47
 
 
48
        expected_template = os.path.join(
 
49
            pxeconfig.template_basedir, 'maas.template')
 
50
        expected_target = os.path.dirname(locate_tftp_path(
 
51
            compose_config_path('armhf', 'armadaxp', 'default')))
 
52
        self.assertEqual(expected_template, pxeconfig.template)
 
53
        self.assertEqual(
 
54
            expected_target, os.path.dirname(pxeconfig.target_file))
 
55
 
 
56
    def test_init_with_no_subarch_makes_path_with_generic(self):
 
57
        pxeconfig = PXEConfig("i386")
 
58
        expected_target = os.path.dirname(locate_tftp_path(
 
59
                compose_config_path('i386', 'generic', 'default')))
 
60
        self.assertEqual(
 
61
            expected_target, os.path.dirname(pxeconfig.target_file))
 
62
 
 
63
    def test_init_with_no_mac_sets_default_filename(self):
 
64
        pxeconfig = PXEConfig("armhf", "armadaxp")
 
65
        expected_filename = locate_tftp_path(
 
66
            compose_config_path('armhf', 'armadaxp', 'default'))
 
67
        self.assertEqual(expected_filename, pxeconfig.target_file)
 
68
 
 
69
    def test_init_with_dodgy_mac(self):
 
70
        # !=5 colons is bad.
 
71
        bad_mac = "aa:bb:cc:dd:ee"
 
72
        exception = self.assertRaises(
 
73
            PXEConfigFail, PXEConfig, "armhf", "armadaxp", bad_mac)
 
74
        self.assertEqual(
 
75
            exception.message, "Expecting exactly five ':' chars, found 4")
 
76
 
 
77
    def test_init_with_mac_sets_filename(self):
 
78
        pxeconfig = PXEConfig("armhf", "armadaxp", mac="00:a1:b2:c3:e4:d5")
 
79
        expected_filename = locate_tftp_path(
 
80
            compose_config_path('armhf', 'armadaxp', '00-a1-b2-c3-e4-d5'))
 
81
        self.assertEqual(expected_filename, pxeconfig.target_file)
 
82
 
 
83
    def test_template_basedir_defaults_to_local_dir(self):
 
84
        self.configure_templates_dir()
 
85
        arch = factory.make_name('arch')
 
86
        self.assertEqual(
 
87
            os.path.join(
 
88
                os.path.dirname(os.path.dirname(__file__)), 'templates'),
 
89
            PXEConfig(arch).template_basedir)
 
90
 
 
91
    def test_template_basedir_prefers_configured_value(self):
 
92
        temp_dir = self.make_dir()
 
93
        self.configure_templates_dir(temp_dir)
 
94
        arch = factory.make_name('arch')
 
95
        self.assertEqual(
 
96
            temp_dir,
 
97
            PXEConfig(arch).template_basedir)
 
98
 
 
99
    def test_get_template_retrieves_template(self):
 
100
        self.configure_templates_dir()
 
101
        pxeconfig = PXEConfig("i386")
 
102
        template = pxeconfig.get_template()
 
103
        self.assertIsInstance(template, tempita.Template)
 
104
        self.assertThat(pxeconfig.template, FileContains(template.content))
 
105
 
 
106
    def test_get_template_looks_for_template_in_template_basedir(self):
 
107
        contents = factory.getRandomString()
 
108
        template = self.make_file(name='maas.template', contents=contents)
 
109
        self.configure_templates_dir(os.path.dirname(template))
 
110
        arch = factory.make_name('arch')
 
111
        self.assertEqual(contents, PXEConfig(arch).get_template().content)
 
112
 
 
113
    def test_render_template(self):
 
114
        pxeconfig = PXEConfig("i386")
 
115
        template = tempita.Template("template: {{kernelimage}}")
 
116
        rendered = pxeconfig.render_template(template, kernelimage="myimage")
 
117
        self.assertEqual("template: myimage", rendered)
 
118
 
 
119
    def test_render_template_raises_PXEConfigFail(self):
 
120
        # If not enough arguments are supplied to fill in template
 
121
        # variables then a PXEConfigFail is raised.
 
122
        pxeconfig = PXEConfig("i386")
 
123
        template_name = factory.getRandomString()
 
124
        template = tempita.Template(
 
125
            "template: {{kernelimage}}", name=template_name)
 
126
        exception = self.assertRaises(
 
127
            PXEConfigFail, pxeconfig.render_template, template)
 
128
        self.assertThat(
 
129
            exception.message, MatchesRegex(
 
130
                "name 'kernelimage' is not defined at line \d+ column \d+ "
 
131
                "in file %s" % re.escape(template_name)))
 
132
 
 
133
    def test_write_config_writes_config(self):
 
134
        # Ensure that a rendered template is written to the right place.
 
135
        tftproot = self.make_dir()
 
136
        pxeconfig = PXEConfig("armhf", "armadaxp", tftproot=tftproot)
 
137
        pxeconfig.write_config(
 
138
            menutitle="menutitle", kernelimage="/my/kernel", append="append")
 
139
 
 
140
        template = pxeconfig.get_template()
 
141
        expected = pxeconfig.render_template(
 
142
            template, menutitle="menutitle", kernelimage="/my/kernel",
 
143
            append="append")
 
144
 
 
145
        self.assertThat(pxeconfig.target_file, FileContains(expected))
 
146
 
 
147
    def test_write_config_overwrites_config(self):
 
148
        tftproot = self.make_dir()
 
149
        pxeconfig = PXEConfig("amd64", "generic", tftproot=tftproot)
 
150
        pxeconfig.write_config(
 
151
            menutitle="oldtitle", kernelimage="/old/kernel", append="append")
 
152
        pxeconfig = PXEConfig("amd64", "generic", tftproot=tftproot)
 
153
        pxeconfig.write_config(
 
154
            menutitle="newtitle", kernelimage="/new/kernel", append="append")
 
155
 
 
156
        self.assertThat(
 
157
            pxeconfig.target_file,
 
158
            FileContains(matcher=Contains('newtitle')))