~lutostag/ubuntu/trusty/maas/1.5.2+packagefix

« back to all changes in this revision

Viewing changes to src/provisioningserver/boot/tests/test_install_bootloader.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2014-03-28 10:43:53 UTC
  • mto: This revision was merged to the branch mainline in revision 57.
  • Revision ID: package-import@ubuntu.com-20140328104353-ekpolg0pm5xnvq2s
Tags: upstream-1.5+bzr2204
ImportĀ upstreamĀ versionĀ 1.5+bzr2204

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012-2014 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Tests for the install_pxe_bootloader command."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
str = None
 
13
 
 
14
__metaclass__ = type
 
15
__all__ = []
 
16
 
 
17
import os.path
 
18
 
 
19
from maastesting.factory import factory
 
20
from maastesting.testcase import MAASTestCase
 
21
from maastesting.utils import (
 
22
    age_file,
 
23
    get_write_time,
 
24
    )
 
25
import provisioningserver.boot.install_bootloader
 
26
from provisioningserver.boot.install_bootloader import (
 
27
    install_bootloader,
 
28
    make_destination,
 
29
    )
 
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 (
 
34
    DirExists,
 
35
    FileContains,
 
36
    FileExists,
 
37
    )
 
38
 
 
39
 
 
40
class TestInstallBootloader(MAASTestCase):
 
41
 
 
42
    def test_integration(self):
 
43
        tftproot = self.make_dir()
 
44
        config_fixture = self.useFixture(set_tftp_root(tftproot))
 
45
 
 
46
        loader = self.make_file()
 
47
 
 
48
        action = factory.make_name("action")
 
49
        script = MainScript(action)
 
50
        script.register(action, provisioningserver.boot.install_bootloader)
 
51
        script.execute(
 
52
            ("--config-file", config_fixture.filename, action,
 
53
             "--loader", loader))
 
54
 
 
55
        bootloader_filename = os.path.basename(loader)
 
56
        self.assertThat(
 
57
            locate_tftp_path(
 
58
                bootloader_filename, tftproot=tftproot),
 
59
            FileExists())
 
60
 
 
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())
 
65
 
 
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())
 
71
 
 
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))
 
79
 
 
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))
 
86
 
 
87
    def test_install_bootloader_skips_if_unchanged(self):
 
88
        contents = factory.getRandomString()
 
89
        dest = self.make_file(contents=contents)
 
90
        age_file(dest, 100)
 
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))
 
96
 
 
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
 
102
        factory.make_file(
 
103
            os.path.dirname(temp_file), name=os.path.basename(temp_file))
 
104
        install_bootloader(loader, dest)
 
105
        self.assertThat(dest, FileContains(contents))