1
# Copyright 2012-2014 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the install_pxe_bootloader command."""
6
from __future__ import (
19
from maastesting.factory import factory
20
from maastesting.testcase import MAASTestCase
21
from maastesting.utils import (
25
import provisioningserver.boot.install_bootloader
26
from provisioningserver.boot.install_bootloader import (
30
from provisioningserver.boot.tftppath import locate_tftp_path
31
from provisioningserver.testing.config import set_tftp_root
32
from provisioningserver.utils import MainScript
33
from testtools.matchers import (
40
class TestInstallBootloader(MAASTestCase):
42
def test_integration(self):
43
tftproot = self.make_dir()
44
config_fixture = self.useFixture(set_tftp_root(tftproot))
46
loader = self.make_file()
48
action = factory.make_name("action")
49
script = MainScript(action)
50
script.register(action, provisioningserver.boot.install_bootloader)
52
("--config-file", config_fixture.filename, action,
55
bootloader_filename = os.path.basename(loader)
58
bootloader_filename, tftproot=tftproot),
61
def test_make_destination_creates_directory_if_not_present(self):
62
tftproot = self.make_dir()
63
dest = make_destination(tftproot)
64
self.assertThat(dest, DirExists())
66
def test_make_destination_returns_existing_directory(self):
67
tftproot = self.make_dir()
68
make_destination(tftproot)
69
dest = make_destination(tftproot)
70
self.assertThat(dest, DirExists())
72
def test_install_bootloader_installs_new_bootloader(self):
73
contents = factory.getRandomString()
74
loader = self.make_file(contents=contents)
75
install_dir = self.make_dir()
76
dest = os.path.join(install_dir, factory.make_name('loader'))
77
install_bootloader(loader, dest)
78
self.assertThat(dest, FileContains(contents))
80
def test_install_bootloader_replaces_bootloader_if_changed(self):
81
contents = factory.getRandomString()
82
loader = self.make_file(contents=contents)
83
dest = self.make_file(contents="Old contents")
84
install_bootloader(loader, dest)
85
self.assertThat(dest, FileContains(contents))
87
def test_install_bootloader_skips_if_unchanged(self):
88
contents = factory.getRandomString()
89
dest = self.make_file(contents=contents)
91
original_write_time = get_write_time(dest)
92
loader = self.make_file(contents=contents)
93
install_bootloader(loader, dest)
94
self.assertThat(dest, FileContains(contents))
95
self.assertEqual(original_write_time, get_write_time(dest))
97
def test_install_bootloader_sweeps_aside_dot_new_if_any(self):
98
contents = factory.getRandomString()
99
loader = self.make_file(contents=contents)
100
dest = self.make_file(contents="Old contents")
101
temp_file = '%s.new' % dest
103
os.path.dirname(temp_file), name=os.path.basename(temp_file))
104
install_bootloader(loader, dest)
105
self.assertThat(dest, FileContains(contents))