summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestOptions/TestComponents.py
blob: 61b87de2a56fd887c6090bcfb1095e0201bf39c6 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""test component loading."""

import argparse
import os

from Bcfg2.Options import Option, BooleanOption, PathOption, ComponentAction, \
    get_parser, new_parser, Types, ConfigFileAction, Common

from testsuite.Testsrc.Testlib.TestOptions import make_config, One, Two, \
    OptionTestCase


# create a bunch of fake components for testing component loading options

class ChildOne(object):
    """fake component for testing component loading."""
    options = [Option("--child-one")]


class ChildTwo(object):
    """fake component for testing component loading."""
    options = [Option("--child-two")]


class ChildComponentAction(ComponentAction):
    """child component loader action."""
    islist = False
    mapping = {"one": ChildOne,
               "two": ChildTwo}


class ComponentOne(object):
    """fake component for testing component loading."""
    options = [BooleanOption("--one")]


class ComponentTwo(object):
    """fake component for testing component loading."""
    options = [Option("--child", default="one", action=ChildComponentAction)]


class ComponentThree(object):
    """fake component for testing component loading."""
    options = [BooleanOption("--three")]


class ConfigFileComponent(object):
    """fake component for testing component loading."""
    options = [Option("--config2", action=ConfigFileAction),
               Option(cf=("config", "test"), dest="config2_test",
                      default="bar")]


class PathComponent(object):
    """fake component for testing <repository> macros in child components."""
    options = [PathOption(cf=("test", "test_path")),
               PathOption(cf=("test", "test_path_default"),
                          default="<repository>/test/default")]


class ParentComponentAction(ComponentAction):
    """parent component loader action."""
    mapping = {"one": ComponentOne,
               "two": ComponentTwo,
               "three": ComponentThree,
               "config": ConfigFileComponent,
               "path": PathComponent}


class TestComponentOptions(OptionTestCase):
    """test cases for component loading."""

    def setUp(self):
        OptionTestCase.setUp(self)
        self.options = [
            Option("--parent", type=Types.comma_list,
                   default=["one", "two"], action=ParentComponentAction)]

        self.result = argparse.Namespace()
        new_parser()
        self.parser = get_parser(components=[self], namespace=self.result,
                                 description="component testing parser")

    @make_config()
    def test_loading_components(self, config_file):
        """load a single component during option parsing."""
        self.parser.parse(["-C", config_file, "--parent", "one"])
        self.assertEqual(self.result.parent, [ComponentOne])

    @make_config()
    def test_component_option(self, config_file):
        """use options from a component loaded during option parsing."""
        self.parser.parse(["--one", "-C", config_file, "--parent", "one"])
        self.assertEqual(self.result.parent, [ComponentOne])
        self.assertTrue(self.result.one)

    @make_config()
    def test_multi_component_load(self, config_file):
        """load multiple components during option parsing."""
        self.parser.parse(["-C", config_file, "--parent", "one,three"])
        self.assertEqual(self.result.parent, [ComponentOne, ComponentThree])

    @make_config()
    def test_multi_component_options(self, config_file):
        """use options from multiple components during option parsing."""
        self.parser.parse(["-C", config_file, "--three",
                           "--parent", "one,three", "--one"])
        self.assertEqual(self.result.parent, [ComponentOne, ComponentThree])
        self.assertTrue(self.result.one)
        self.assertTrue(self.result.three)

    @make_config()
    def test_component_default_not_loaded(self, config_file):
        """options from default but unused components not available."""
        self.assertRaises(
            SystemExit,
            self.parser.parse,
            ["-C", config_file, "--child", "one", "--parent", "one"])

    @make_config()
    def test_tiered_components(self, config_file):
        """load child component."""
        self.parser.parse(["-C", config_file, "--parent", "two",
                           "--child", "one"])
        self.assertEqual(self.result.parent, [ComponentTwo])
        self.assertEqual(self.result.child, ChildOne)

    @make_config()
    def test_options_tiered_components(self, config_file):
        """use options from child component."""
        self.parser.parse(["--child-one", "foo", "-C", config_file, "--parent",
                           "two", "--child", "one"])
        self.assertEqual(self.result.parent, [ComponentTwo])
        self.assertEqual(self.result.child, ChildOne)
        self.assertEqual(self.result.child_one, "foo")

    @make_config()
    def test_bogus_component(self, config_file):
        """error out with bad component name."""
        self.assertRaises(SystemExit,
                          self.parser.parse,
                          ["-C", config_file, "--parent", "blargle"])

    @make_config()
    @make_config({"config": {"test": "foo"}})
    def test_config_component(self, config1, config2):
        """load component with alternative config file."""
        self.parser.parse(["-C", config1, "--config2", config2,
                           "--parent", "config"])
        self.assertEqual(self.result.config2, config2)
        self.assertEqual(self.result.config2_test, "foo")

    @make_config()
    def test_config_component_no_file(self, config_file):
        """load component with missing alternative config file."""
        self.parser.parse(["-C", config_file, "--parent", "config"])
        self.assertEqual(self.result.config2, None)

    @make_config({"test": {"test_path": "<repository>/test"}})
    def test_macros_in_component_options(self, config_file):
        """fix up <repository> macros in component options."""
        self.parser.add_options([Common.repository])
        self.parser.parse(["-C", config_file, "-Q", "/foo/bar",
                           "--parent", "path"])
        self.assertEqual(self.result.test_path, "/foo/bar/test")
        self.assertEqual(self.result.test_path_default,
                         "/foo/bar/test/default")


class ImportComponentAction(ComponentAction):
    """action that imports real classes for testing."""
    islist = False
    bases = ["testsuite.Testsrc.Testlib.TestOptions"]


class ImportModuleAction(ImportComponentAction):
    """action that only imports modules for testing."""
    module = True


class TestImportComponentOptions(OptionTestCase):
    """test cases for component loading."""

    def setUp(self):
        self.options = [Option("--cls", action=ImportComponentAction),
                        Option("--module", action=ImportModuleAction)]

        self.result = argparse.Namespace()
        new_parser()
        self.parser = get_parser(components=[self], namespace=self.result)

    @make_config()
    def test_import_component(self, config_file):
        """load class components by importing."""
        self.parser.parse(["-C", config_file, "--cls", "One"])
        self.assertEqual(self.result.cls, One.One)

    @make_config()
    def test_import_module(self, config_file):
        """load module components by importing."""
        self.parser.parse(["-C", config_file, "--module", "One"])
        self.assertEqual(self.result.module, One)

    @make_config()
    def test_import_full_path(self, config_file):
        """load components by importing the full path."""
        self.parser.parse(["-C", config_file, "--cls", "os.path"])
        self.assertEqual(self.result.cls, os.path)

    @make_config()
    def test_import_bogus_class(self, config_file):
        """fail to load class component that cannot be imported."""
        self.assertRaises(SystemExit,
                          self.parser.parse,
                          ["-C", config_file, "--cls", "Three"])

    @make_config()
    def test_import_bogus_module(self, config_file):
        """fail to load module component that cannot be imported."""
        self.assertRaises(SystemExit,
                          self.parser.parse,
                          ["-C", config_file, "--module", "Three"])

    @make_config()
    def test_import_bogus_path(self, config_file):
        """fail to load component that cannot be imported by full path."""
        self.assertRaises(SystemExit,
                          self.parser.parse,
                          ["-C", config_file, "--cls", "Bcfg2.No.Such.Thing"])