1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for `provisioningserver.pxe`."""
6
from __future__ import (
18
from maastesting.factory import factory
19
from maastesting.testcase import TestCase
20
import provisioningserver.pxe.pxeconfig
21
from provisioningserver.pxe.pxeconfig import (
25
from provisioningserver.pxe.tftppath import (
30
from testtools.matchers import (
37
class TestPXEConfig(TestCase):
38
"""Tests for PXEConfig."""
40
def configure_templates_dir(self, path=None):
41
"""Configure PXE_TEMPLATES_DIR to `path`."""
43
provisioningserver.pxe.pxeconfig, 'PXE_TEMPLATES_DIR', path)
45
def test_init_sets_up_paths(self):
46
pxeconfig = PXEConfig("armhf", "armadaxp")
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)
54
expected_target, os.path.dirname(pxeconfig.target_file))
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')))
61
expected_target, os.path.dirname(pxeconfig.target_file))
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)
69
def test_init_with_dodgy_mac(self):
71
bad_mac = "aa:bb:cc:dd:ee"
72
exception = self.assertRaises(
73
PXEConfigFail, PXEConfig, "armhf", "armadaxp", bad_mac)
75
exception.message, "Expecting exactly five ':' chars, found 4")
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)
83
def test_template_basedir_defaults_to_local_dir(self):
84
self.configure_templates_dir()
85
arch = factory.make_name('arch')
88
os.path.dirname(os.path.dirname(__file__)), 'templates'),
89
PXEConfig(arch).template_basedir)
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')
97
PXEConfig(arch).template_basedir)
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))
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)
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)
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)
129
exception.message, MatchesRegex(
130
"name 'kernelimage' is not defined at line \d+ column \d+ "
131
"in file %s" % re.escape(template_name)))
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")
140
template = pxeconfig.get_template()
141
expected = pxeconfig.render_template(
142
template, menutitle="menutitle", kernelimage="/my/kernel",
145
self.assertThat(pxeconfig.target_file, FileContains(expected))
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")
157
pxeconfig.target_file,
158
FileContains(matcher=Contains('newtitle')))