92
95
mock_env_instance.close.assert_called_once_with()
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.
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)
130
133
@mock.patch('guiserver.bundles.blocking._Environment')
131
134
class TestImportBundle(DeployerFunctionsTestMixin, unittest.TestCase):
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):
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)
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
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))