~frankban/charms/precise/juju-gui/guiserver-optional-bundle-name

« back to all changes in this revision

Viewing changes to server/guiserver/tests/bundles/test_blocking.py

GUI server: Deployer and DeployMiddleware.

This branch includes the bundles support
base classes. They are already described
in the bundles package docstring.

The diff is a bit long, sorry about that.
Most of the new code are tests.
The next branch will integrate and enable
the bundle support in the GUI server.

Tests: `make unittest` from the branch root.

R=benji, bac
CC=
https://codereview.appspot.com/12802045

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tests for the bundles support blocking functions and objects."""
18
18
 
19
19
from contextlib import contextmanager
 
20
import os
 
21
import shutil
 
22
import tempfile
20
23
import unittest
21
24
 
22
25
import mock
92
95
        mock_env_instance.close.assert_called_once_with()
93
96
 
94
97
    @contextmanager
95
 
    def overlapping_services(self, mock_environment):
 
98
    def assert_overlapping_services(self, mock_environment):
96
99
        """Ensure a ValueError is raised in the context manager block.
97
100
 
98
101
        The given mock environment object is set up so that its status
123
126
    def test_overlapping_services(self, mock_environment):
124
127
        # The validation fails if the bundle includes a service name already
125
128
        # present in the Juju environment.
126
 
        with self.overlapping_services(mock_environment):
 
129
        with self.assert_overlapping_services(mock_environment):
127
130
            blocking.validate(self.apiurl, self.password, self.bundle)
128
131
 
129
132
 
130
133
@mock.patch('guiserver.bundles.blocking._Environment')
131
134
class TestImportBundle(DeployerFunctionsTestMixin, unittest.TestCase):
132
135
 
 
136
    @contextmanager
 
137
    def patch_juju_home(self):
 
138
        """Patch the value used by the bundle importer as Juju home."""
 
139
        base_dir = tempfile.mkdtemp()
 
140
        self.addCleanup(shutil.rmtree, base_dir)
 
141
        juju_home = os.path.join(base_dir, 'juju-home')
 
142
        with mock.patch('guiserver.bundles.blocking.JUJU_HOME', juju_home):
 
143
            yield juju_home
 
144
 
133
145
    @mock.patch('guiserver.bundles.blocking.Importer')
134
146
    def test_importing_bundle(self, mock_importer, mock_environment):
135
147
        # The juju-deployer importer is correctly set up and run.
136
 
        blocking.import_bundle(
137
 
            self.apiurl, self.password, self.name, self.bundle)
 
148
        with self.patch_juju_home():
 
149
            blocking.import_bundle(
 
150
                self.apiurl, self.password, self.name, self.bundle)
138
151
        # The environment is correctly instantiated and used.
139
152
        self.check_environment_life(mock_environment)
140
153
        # The importer is correctly instantiated.
156
169
    def test_overlapping_services(self, mock_environment):
157
170
        # The import fails if the bundle includes a service name already
158
171
        # present in the Juju environment.
159
 
        with self.overlapping_services(mock_environment):
 
172
        with self.assert_overlapping_services(mock_environment):
 
173
            with self.patch_juju_home():
 
174
                blocking.import_bundle(
 
175
                    self.apiurl, self.password, self.name, self.bundle)
 
176
 
 
177
    @mock.patch('guiserver.bundles.blocking.Importer')
 
178
    def test_juju_home(self, mock_importer, mock_environment):
 
179
        # A customized Juju home is created and used during the import process.
 
180
        with self.patch_juju_home() as juju_home:
 
181
            assert not os.path.isdir(juju_home), 'directory should not exist'
 
182
            # Ensure JUJU_HOME is included in the context when the Importer
 
183
            # instance is run.
 
184
            run = lambda: self.assertEqual(juju_home, os.getenv('JUJU_HOME'))
 
185
            mock_importer().run = run
160
186
            blocking.import_bundle(
161
187
                self.apiurl, self.password, self.name, self.bundle)
 
188
        # The JUJU_HOME directory has been created.
 
189
        self.assertTrue(os.path.isdir(juju_home))