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 customize_config."""
6
from __future__ import (
15
from argparse import ArgumentParser
16
from io import BytesIO
18
from subprocess import (
23
from textwrap import dedent
25
from maastesting.factory import factory
26
from maastesting.testcase import TestCase
27
import provisioningserver
28
from provisioningserver import customize_config
29
from provisioningserver.utils import maas_custom_config_markers
32
def locate_dev_root():
33
"""Return root of development source tree."""
35
os.path.dirname(provisioningserver.__file__),
39
class TestCustomizeConfig(TestCase):
41
def run_command(self, input_file, stdin):
42
self.patch(sys, 'stdin', BytesIO(stdin.encode('utf-8')))
43
self.patch(sys, 'stdout', BytesIO())
44
parser = ArgumentParser()
45
customize_config.add_arguments(parser)
46
parsed_args = parser.parse_args((input_file, ))
47
customize_config.run(parsed_args)
49
def test_runs_as_script(self):
50
original_text = factory.getRandomString()
51
original_file = self.make_file(original_text)
52
script = "%s/bin/maas-provision" % locate_dev_root()
54
[script, "customize-config", original_file],
55
stdin=PIPE, stdout=PIPE,
56
env=dict(PYTHONPATH=":".join(sys.path), LC_ALL='en_US.UTF-8'))
57
command.communicate(original_text)
58
self.assertEqual(0, command.returncode)
60
def test_produces_sensible_text(self):
61
header, footer = maas_custom_config_markers
62
original_file = self.make_file(contents="Original text here.")
64
self.run_command(original_file, stdin="Custom section here.")
67
expected = dedent("""\
72
""") % (header, footer)
73
output = sys.stdout.read()
74
self.assertEqual(expected, output.decode('utf-8'))
76
def test_does_not_modify_original(self):
77
original_text = factory.getRandomString().encode('ascii')
78
original_file = self.make_file(contents=original_text)
80
self.run_command(original_file, factory.getRandomString())
82
with open(original_file, 'rb') as reread_file:
83
contents_after = reread_file.read()
85
self.assertEqual(original_text, contents_after)