~launchpad-pqm/launchpad/devel

11728.3.2 by Robert Collins
Create unique configs in the test runner.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
11728.3.3 by Robert Collins
And XXX For the namespace of canonical.config.
4
"""Fixtures related to configs.
5
6
XXX: Robert Collins 2010-10-20 bug=663454 this is in the wrong namespace.
7
"""
11728.3.2 by Robert Collins
Create unique configs in the test runner.
8
9
__metaclass__ = type
10
11
__all__ = [
12
    'ConfigFixture',
13
    'ConfigUseFixture',
14
    ]
15
16
import os.path
17
import shutil
11728.3.4 by Robert Collins
Used dedent in config.fixture/test_fixture, remove a stale if in layers.
18
from textwrap import dedent
11728.3.2 by Robert Collins
Create unique configs in the test runner.
19
20
from fixtures import Fixture
21
22
from canonical.config import config
23
24
25
class ConfigFixture(Fixture):
26
    """Create a unique launchpad config."""
27
11728.3.4 by Robert Collins
Used dedent in config.fixture/test_fixture, remove a stale if in layers.
28
    _extend_str = dedent("""\
29
        [meta]
30
        extends: ../%s/launchpad-lazr.conf
31
        """)
32
11728.3.2 by Robert Collins
Create unique configs in the test runner.
33
    def __init__(self, instance_name, copy_from_instance):
34
        """Create a ConfigFixture.
35
36
        :param instance_name: The name of the instance to create.
37
        :param copy_from_instance: An existing instance to clone.
38
        """
39
        self.instance_name = instance_name
40
        self.copy_from_instance = copy_from_instance
41
11737.2.3 by Robert Collins
Isolated test db's sufficient to run parallel tests of the db layer.
42
    def add_section(self, sectioncontent):
43
        """Add sectioncontent to the lazy config."""
44
        with open(self.absroot + '/launchpad-lazr.conf', 'ab') as out:
45
            out.write(sectioncontent)
13023.9.2 by Robert Collins
More explanation.
46
        # Trigger a refresh if and only if the config is in use at the moment
47
        # in order to make these new values available.
13023.9.1 by Robert Collins
Add a layer for RabbitMQ and bring that into the launchpad layers.
48
        if config.instance_name == self.instance_name:
49
            config._invalidateConfig()
11737.2.3 by Robert Collins
Isolated test db's sufficient to run parallel tests of the db layer.
50
11728.3.2 by Robert Collins
Create unique configs in the test runner.
51
    def setUp(self):
52
        super(ConfigFixture, self).setUp()
53
        root = 'configs/' + self.instance_name
54
        os.mkdir(root)
11737.2.3 by Robert Collins
Isolated test db's sufficient to run parallel tests of the db layer.
55
        self.absroot = os.path.abspath(root)
56
        self.addCleanup(shutil.rmtree, self.absroot)
11728.3.2 by Robert Collins
Create unique configs in the test runner.
57
        source = 'configs/' + self.copy_from_instance
58
        for basename in os.listdir(source):
59
            if basename == 'launchpad-lazr.conf':
11737.2.8 by Robert Collins
Merge unique config improvements.
60
                self.add_section(self._extend_str % self.copy_from_instance)
11728.3.2 by Robert Collins
Create unique configs in the test runner.
61
                continue
62
            with open(source + '/' + basename, 'rb') as input:
63
                with open(root + '/' + basename, 'wb') as out:
64
                    out.write(input.read())
65
66
67
class ConfigUseFixture(Fixture):
68
    """Use a config and restore the current config after."""
69
70
    def __init__(self, instance_name):
71
        self.instance_name = instance_name
72
73
    def setUp(self):
74
        super(ConfigUseFixture, self).setUp()
75
        self.addCleanup(config.setInstance, config.instance_name)
76
        config.setInstance(self.instance_name)