~maas-committers/maas/trunk

« back to all changes in this revision

Viewing changes to src/metadataserver/commissioning/tests/test_user_data.py

[r=allenap][bug=][author=jtv] Make commissioning nodes download their commissioning scripts (such as lshw) from the metadata service.  And componentize the big commissioning script.

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 generation of commissioning user data."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
import os.path
 
16
 
 
17
from maasserver.testing.factory import factory
 
18
from maasserver.testing.testcase import TestCase
 
19
from maastesting.matchers import ContainsAll
 
20
from metadataserver.commissioning.user_data import (
 
21
    generate_user_data,
 
22
    is_snippet,
 
23
    list_snippets,
 
24
    read_snippet,
 
25
    strip_name,
 
26
    )
 
27
 
 
28
 
 
29
class TestUserData(TestCase):
 
30
 
 
31
    def test_read_snippet_reads_snippet_file(self):
 
32
        contents = factory.getRandomString()
 
33
        snippet = self.make_file(contents=contents)
 
34
        self.assertEqual(
 
35
            contents,
 
36
            read_snippet(os.path.dirname(snippet), os.path.basename(snippet)))
 
37
 
 
38
    def test_strip_name_leaves_simple_names_intact(self):
 
39
        simple_name = factory.getRandomString()
 
40
        self.assertEqual(simple_name, strip_name(simple_name))
 
41
 
 
42
    def test_strip_name_replaces_dots(self):
 
43
        self.assertEqual('_x_y_', strip_name('.x.y.'))
 
44
 
 
45
    def test_is_snippet(self):
 
46
        are_snippets = {
 
47
            'snippet': True,
 
48
            'with-dash': True,
 
49
            'module.py': True,
 
50
            '.backup': False,
 
51
            'backup~': False,
 
52
            'module.pyc': False,
 
53
            '__init__.pyc': False,
 
54
        }
 
55
        self.assertEqual(
 
56
            are_snippets,
 
57
            {name: is_snippet(name) for name in are_snippets})
 
58
 
 
59
    def test_list_snippets(self):
 
60
        snippets_dir = self.make_dir()
 
61
        factory.make_file(snippets_dir, 'snippet')
 
62
        factory.make_file(snippets_dir, '.backup.pyc')
 
63
        self.assertItemsEqual(['snippet'], list_snippets(snippets_dir))
 
64
 
 
65
    def test_generate_user_data_produces_commissioning_script(self):
 
66
        # generate_user_data produces a commissioning script which contains
 
67
        # both definitions and use of various commands in python.
 
68
        self.assertThat(
 
69
            generate_user_data(), ContainsAll({
 
70
                'maas-get',
 
71
                'maas-signal',
 
72
                'def authenticate_headers',
 
73
                'def encode_multipart_data',
 
74
            }))