summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestOptions/TestConfigFiles.py
blob: 78acadf1fa2ad7b3b2b544aa25aa2e64b2fc962a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""test reading multiple config files."""

import argparse

import mock

from Bcfg2.Options import Option, PathOption, ConfigFileAction, get_parser, \
    new_parser
from testsuite.Testsrc.Testlib.TestOptions import make_config, OptionTestCase


class TestConfigFiles(OptionTestCase):
    def setUp(self):
        self.options = [
            PathOption(cf=("test", "config2"), action=ConfigFileAction),
            PathOption(cf=("test", "config3"), action=ConfigFileAction),
            Option(cf=("test", "foo")),
            Option(cf=("test", "bar")),
            Option(cf=("test", "baz"))]
        self.results = argparse.Namespace()
        new_parser()
        self.parser = get_parser(components=[self], namespace=self.results)

    @make_config({"test": {"baz": "baz"}})
    def test_config_files(self, config3):
        """read multiple config files."""
        # Because make_config() generates temporary files for the
        # configuration, we have to work backwards here.  first we
        # generate config3, then we generate config2 (which includes a
        # reference to config3), then we finally generate the main
        # config file, which contains a reference to config2.  oh how
        # I wish we could use context managers here...

        @make_config({"test": {"bar": "bar", "config3": config3}})
        def inner1(config2):
            @make_config({"test": {"foo": "foo", "config2": config2}})
            def inner2(config):
                self.parser.parse(["-C", config])
                self.assertEqual(self.results.foo, "foo")
                self.assertEqual(self.results.bar, "bar")
                self.assertEqual(self.results.baz, "baz")

            inner2()

        inner1()

    @mock.patch("os.path.exists", mock.Mock(return_value=False))
    def test_no_config_file(self):
        """fail to read config file."""
        self.assertRaises(SystemExit, self.parser.parse, [])