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

« back to all changes in this revision

Viewing changes to src/metadataserver/tests/test_commissioningscript.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez, Raphaël Badin, Julian Edwards, Jeroen Vermeulen, Gavin Panella, Andres Rodriguez
  • Date: 2013-03-04 11:49:44 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20130304114944-81my0hho8arxa3ix
Tags: 1.3+bzr1452+dfsg-0ubuntu1
* New upstream release.
  - MAAS file storage mechanism is shifting from a single shared
    namespace to a per-user namespace. Operators of the majority
    of MAAS systems will not notice any change. However, operators
    of the most complex installations may find that a new
    "shared-environment" user is created, and that some resources
    are reassigned to it, such as API credentials and SSH public
    keys. This provides a transitional environment that mimics the
    behaviour of a shared namespace.

[ Raphaël Badin ]
* debian/control: maas-region-controller depends on bind9utils.
  (LP: #1103195)
* debian/maas-dns.postinst: Call write_dns_config.
  (LP: #1085865).
* debian/maas-cluster-controller.postinst: Fix the name of the config
  file (/etc/maas/pserv.yaml and not /etc/maas/pserv.conf)
  (LP: #1083542).
* debian/extras/99-maas-sudoers: Add 'SETENV:' to sudo rule
  to allow preserving the environment when running
  /usr/sbin/maas-import-pxe-files via sudo.
* debian/maas-dns.postinst: fix permissions and group ownership of
  file /etc/bind/maas/named.conf.rndc.maas. (LP: #1066935)
* debian/control: Remove the dependency of maas-cluster-controller
  on rabbitmq-server. (LP: #1072744)
* debian/extras/99-maas-sudoers: Add sudo rule for script
  /usr/sbin/maas-import-pxe-files.
* debian/maas-cluster-controller.install: Removed commissioning-user-data
  script.

[ Julian Edwards ]
* debian/maas-region-controller.install: Remove installation of maas-gc; it
  is no longer required as upstream no longer stores files in the filesystem.
  (LP: #1069734)
* debian/maas-cluster-controller.postinst: Ensure that /etc/maas/pserv.yaml
  is updated when reconfiguring. (LP: #1081212)

[ Jeroen Vermeulen ]
* debian/maas-cluster-controller.install: Install import scripts.
* debian/maas-cluster-controller.postinst: Configure tgt (the iSCSI server)
  so the import script can install files to it.
* debian/maas-cluster-controller.postrm: Clean up tgt config.
* debian/maas-region-controller.install: Move import scripts out to the
  cluster controller, and drop the maas-import-isos compatibility script.
* debian/maas-region-controller.postinst: Remove tgt config.
* debian/maas-region-controller.postrm: Remove tgt config cleanup.
* Bump code revision to include latest user_data.template fixes.

[ Gavin Panella ]
* debian/extras/99-maas: squashfs image download is no longer needed.
* debian/maas-cluster-controller.install: maas-import-squashfs and its
  configuration file are no longer part of upstream.
* debian/maas-cluster-controller.install: The maas-import-pxe-files cron
  task is no longer used.
* debian/maas-cluster-controller.postinst: Remove leading comment
  markers from the 'generator' line in pserv.yaml.

[ Andres Rodriguez ]
* debian/control:
  - maas-cluster-controller Conflicts with tftpd-hpa (LP: #1076028)
  - maas-dns: Conflicts with dnsmasq
  - maas-cluster-controller Conflicts/Replaces maas-region-controller as
    import scripts are no longer shipped in the region.
  - debian/control: Depends on distro-info for maas-cluster-controller
    instead of maas-region-controller (LP: #1103194)
* debian/maas-cluster-controller.config: If URL has been detected,
  add /MAAS if it doesn't contain it. This helps upgrades from versions
  where DEFAULT_MAAS_URL didn't use /MAAS.

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
"""Test custom commissioning scripts."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
from io import BytesIO
 
16
from math import (
 
17
    ceil,
 
18
    floor,
 
19
    )
 
20
import os.path
 
21
from random import randint
 
22
import tarfile
 
23
import time
 
24
 
 
25
from maasserver.testing.factory import factory
 
26
from maasserver.testing.testcase import TestCase
 
27
from maastesting.matchers import ContainsAll
 
28
from maastesting.utils import sample_binary_data
 
29
from metadataserver.fields import Bin
 
30
from metadataserver.models import (
 
31
    CommissioningScript,
 
32
    commissioningscript as cs_module,
 
33
    )
 
34
from metadataserver.models.commissioningscript import ARCHIVE_PREFIX
 
35
 
 
36
 
 
37
def open_tarfile(content):
 
38
    """Open tar file from raw binary data."""
 
39
    return tarfile.open(fileobj=BytesIO(content))
 
40
 
 
41
 
 
42
def make_script_name(base_name=None, number=None):
 
43
    """Make up a name for a commissioning script."""
 
44
    if base_name is None:
 
45
        base_name = 'script'
 
46
    if number is None:
 
47
        number = randint(0, 99)
 
48
    return factory.make_name(
 
49
        '%0.2d-%s' % (number, factory.make_name(base_name)))
 
50
 
 
51
 
 
52
class TestCommissioningScriptManager(TestCase):
 
53
 
 
54
    def test_get_archive_wraps_scripts_in_tar(self):
 
55
        script = factory.make_commissioning_script()
 
56
        path = os.path.join(ARCHIVE_PREFIX, script.name)
 
57
        archive = open_tarfile(CommissioningScript.objects.get_archive())
 
58
        self.assertTrue(archive.getmember(path).isfile())
 
59
        self.assertEqual(script.content, archive.extractfile(path).read())
 
60
 
 
61
    def test_get_archive_wraps_all_scripts(self):
 
62
        scripts = {factory.make_commissioning_script() for counter in range(3)}
 
63
        archive = open_tarfile(CommissioningScript.objects.get_archive())
 
64
        self.assertThat(
 
65
            archive.getnames(),
 
66
            ContainsAll({
 
67
                os.path.join(ARCHIVE_PREFIX, script.name)
 
68
                for script in scripts
 
69
                }))
 
70
 
 
71
    def test_get_archive_supports_binary_scripts(self):
 
72
        script = factory.make_commissioning_script(content=sample_binary_data)
 
73
        path = os.path.join(ARCHIVE_PREFIX, script.name)
 
74
        archive = open_tarfile(CommissioningScript.objects.get_archive())
 
75
        self.assertEqual(script.content, archive.extractfile(path).read())
 
76
 
 
77
    def test_get_archive_includes_builtin_scripts(self):
 
78
        name = factory.make_name('00-maas')
 
79
        path = os.path.join(ARCHIVE_PREFIX, name)
 
80
        content = factory.getRandomString().encode('ascii')
 
81
        self.patch(cs_module, 'BUILTIN_COMMISSIONING_SCRIPTS', {name: content})
 
82
        archive = open_tarfile(CommissioningScript.objects.get_archive())
 
83
        self.assertIn(path, archive.getnames())
 
84
        self.assertEqual(content, archive.extractfile(path).read())
 
85
 
 
86
    def test_get_archive_sets_sensible_mode(self):
 
87
        for counter in range(3):
 
88
            factory.make_commissioning_script()
 
89
        archive = open_tarfile(CommissioningScript.objects.get_archive())
 
90
        self.assertEqual({0755}, {info.mode for info in archive.getmembers()})
 
91
 
 
92
    def test_get_archive_initializes_file_timestamps(self):
 
93
        # The mtime on a file inside the tarball is reasonable.
 
94
        # It would otherwise default to the Epoch, and GNU tar warns
 
95
        # annoyingly about improbably old files.
 
96
        start_time = floor(time.time())
 
97
        script = factory.make_commissioning_script()
 
98
        path = os.path.join(ARCHIVE_PREFIX, script.name)
 
99
        archive = open_tarfile(CommissioningScript.objects.get_archive())
 
100
        timestamp = archive.getmember(path).mtime
 
101
        end_time = ceil(time.time())
 
102
        self.assertGreaterEqual(timestamp, start_time)
 
103
        self.assertLessEqual(timestamp, end_time)
 
104
 
 
105
 
 
106
class TestCommissioningScript(TestCase):
 
107
 
 
108
    def test_scripts_may_be_binary(self):
 
109
        name = make_script_name()
 
110
        CommissioningScript.objects.create(
 
111
            name=name, content=Bin(sample_binary_data))
 
112
        stored_script = CommissioningScript.objects.get(name=name)
 
113
        self.assertEqual(sample_binary_data, stored_script.content)