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 the install_pxe_bootloader command."""
6
from __future__ import (
17
from django.core.management import call_command
18
from maasserver.management.commands.install_pxe_bootloader import (
22
from maastesting.factory import factory
23
from maastesting.testcase import TestCase
24
from maastesting.utils import (
28
from provisioningserver.pxe.tftppath import (
29
compose_bootloader_path,
32
from testtools.matchers import (
40
class TestInstallPXEBootloader(TestCase):
42
def test_integration(self):
43
loader = self.make_file()
44
tftproot = self.make_dir()
45
arch = factory.make_name('arch')
46
subarch = factory.make_name('subarch')
49
'install_pxe_bootloader', arch=arch, subarch=subarch,
50
loader=loader, tftproot=tftproot)
54
compose_bootloader_path(arch, subarch), tftproot=tftproot),
56
self.assertThat(loader, Not(FileExists()))
58
def test_make_destination_creates_directory_if_not_present(self):
59
tftproot = self.make_dir()
60
arch = factory.make_name('arch')
61
subarch = factory.make_name('subarch')
62
dest = make_destination(tftproot, arch, subarch)
63
self.assertThat(os.path.dirname(dest), DirExists())
65
def test_make_destination_returns_existing_directory(self):
66
tftproot = self.make_dir()
67
arch = factory.make_name('arch')
68
subarch = factory.make_name('subarch')
69
make_destination(tftproot, arch, subarch)
70
dest = make_destination(tftproot, arch, subarch)
71
self.assertThat(os.path.dirname(dest), DirExists())
73
def test_install_bootloader_installs_new_bootloader(self):
74
contents = factory.getRandomString()
75
loader = self.make_file(contents=contents)
76
install_dir = self.make_dir()
77
dest = os.path.join(install_dir, factory.make_name('loader'))
78
install_bootloader(loader, dest)
79
self.assertThat(dest, FileContains(contents))
81
def test_install_bootloader_replaces_bootloader_if_changed(self):
82
contents = factory.getRandomString()
83
loader = self.make_file(contents=contents)
84
dest = self.make_file(contents="Old contents")
85
install_bootloader(loader, dest)
86
self.assertThat(dest, FileContains(contents))
88
def test_install_bootloader_skips_if_unchanged(self):
89
contents = factory.getRandomString()
90
dest = self.make_file(contents=contents)
92
original_write_time = get_write_time(dest)
93
loader = self.make_file(contents=contents)
94
install_bootloader(loader, dest)
95
self.assertThat(dest, FileContains(contents))
96
self.assertEqual(original_write_time, get_write_time(dest))
98
def test_install_bootloader_sweeps_aside_dot_new_if_any(self):
99
contents = factory.getRandomString()
100
loader = self.make_file(contents=contents)
101
dest = self.make_file(contents="Old contents")
102
temp_file = '%s.new' % dest
104
os.path.dirname(temp_file), name=os.path.basename(temp_file))
105
install_bootloader(loader, dest)
106
self.assertThat(dest, FileContains(contents))