~ubuntu-branches/ubuntu/precise/maas/precise-updates

« back to all changes in this revision

Viewing changes to src/provisioningserver/dhcp/tests/test_writer.py

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

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
"""Tests for `provisioningserver.dhcp.writer`."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
from argparse import ArgumentParser
 
16
from io import BytesIO
 
17
import os
 
18
from subprocess import (
 
19
    PIPE,
 
20
    Popen,
 
21
    )
 
22
import sys
 
23
 
 
24
from maastesting.matchers import ContainsAll
 
25
from maastesting.testcase import TestCase
 
26
import provisioningserver
 
27
from provisioningserver.dhcp import writer
 
28
from testtools.matchers import MatchesStructure
 
29
 
 
30
 
 
31
class TestScript(TestCase):
 
32
    """Test the DHCP configuration writer."""
 
33
 
 
34
    test_args = (
 
35
        '--subnet', 'subnet',
 
36
        '--subnet-mask', 'subnet-mask',
 
37
        '--broadcast-ip', 'broadcast-ip',
 
38
        '--dns-servers', 'dns-servers',
 
39
        '--router-ip', 'router-ip',
 
40
        '--ip-range-low', 'ip-range-low',
 
41
        '--ip-range-high', 'ip-range-high',
 
42
        '--omapi-key', 'omapi-key',
 
43
        )
 
44
 
 
45
    def test_script_executable(self):
 
46
        dev_root = os.path.join(
 
47
            os.path.dirname(provisioningserver.__file__),
 
48
            os.pardir, os.pardir)
 
49
        script = ["%s/bin/maas-provision" % dev_root, "generate-dhcp-config"]
 
50
        script.extend(self.test_args)
 
51
        cmd = Popen(
 
52
            script, stdout=PIPE, env=dict(PYTHONPATH=":".join(sys.path)))
 
53
        output, err = cmd.communicate()
 
54
        contains_all_params = ContainsAll(
 
55
            ['subnet', 'subnet-mask', 'broadcast-ip',
 
56
             'omapi-key', 'dns-servers', 'router-ip',
 
57
             'ip-range-low', 'ip-range-high'])
 
58
        self.assertThat(output, contains_all_params)
 
59
 
 
60
    def test_arg_setup(self):
 
61
        parser = ArgumentParser()
 
62
        writer.add_arguments(parser)
 
63
        args = parser.parse_args(self.test_args)
 
64
        self.assertThat(
 
65
            args, MatchesStructure.byEquality(
 
66
                subnet='subnet',
 
67
                subnet_mask='subnet-mask',
 
68
                broadcast_ip='broadcast-ip',
 
69
                dns_servers='dns-servers',
 
70
                router_ip='router-ip',
 
71
                omapi_key='omapi-key',
 
72
                ip_range_low='ip-range-low',
 
73
                ip_range_high='ip-range-high'))
 
74
 
 
75
    def test_run(self):
 
76
        self.patch(sys, "stdout", BytesIO())
 
77
        parser = ArgumentParser()
 
78
        writer.add_arguments(parser)
 
79
        args = parser.parse_args(self.test_args)
 
80
        writer.run(args)
 
81
        output = sys.stdout.getvalue()
 
82
        contains_all_params = ContainsAll([
 
83
            'subnet',
 
84
            'subnet-mask',
 
85
            'broadcast-ip',
 
86
            'omapi-key',
 
87
            'dns-servers',
 
88
            'router-ip',
 
89
            'ip-range-low',
 
90
            'ip-range-high',
 
91
            ])
 
92
        self.assertThat(output, contains_all_params)
 
93
 
 
94
    def test_run_save_to_file(self):
 
95
        parser = ArgumentParser()
 
96
        writer.add_arguments(parser)
 
97
        outfile = os.path.join(self.make_dir(), "outfile.txt")
 
98
        args = parser.parse_args(
 
99
            self.test_args + ("--outfile", outfile))
 
100
        writer.run(args)
 
101
        with open(outfile, "rb") as stream:
 
102
            output = stream.read()
 
103
        contains_all_params = ContainsAll([
 
104
            'subnet',
 
105
            'subnet-mask',
 
106
            'broadcast-ip',
 
107
            'omapi-key',
 
108
            'dns-servers',
 
109
            'router-ip',
 
110
            'ip-range-low',
 
111
            'ip-range-high',
 
112
            ])
 
113
        self.assertThat(output, contains_all_params)