42
43
'GLANCE_OS_REGION_NAME': 'glance-region',
43
44
'GLANCE_OS_TENANT_NAME': 'glance-tenant',
44
45
'GLANCE_OS_PASSWORD': 'glance-password',
46
'EPHEMERAL_CLOUD_NET_ID': 'ephemeral-cloud-net-id',
45
47
'OS_AUTH_URL': 'something.stack',
46
48
'CI_OAUTH_CONSUMER_KEY': 'key',
47
49
'CI_OAUTH_TOKEN': 'token',
220
222
self.assertTrue(os.path.exists(deployer_script))
225
class TestInstallKeys(unittest.TestCase):
228
super(TestInstallKeys, self).setUp()
229
fixtures.set_uniq_cwd(self)
231
def create_keys(self, keys_path):
232
"""Creates a 'keys' directory with the relevant key files."""
234
key_names = ['id_rsa', 'id_rsa.pub', 'gpg.sec', 'gpg.pub']
235
for key_name in key_names:
236
with open(os.path.join(keys_path, key_name), 'w') as fd:
239
@mock.patch('deploy._do_bzr_checkout')
240
@mock.patch('sys.stdout', new_callable=StringIO.StringIO)
241
def test_local_key_are_preferred(self, mocked_stdout, mocked_co):
242
# If there are valid keys in the working dir, the CI_KEYS_BRANCH
243
# is ignored and nothing is done. By definition, local keys are
244
# maintained manually.
245
fixtures.isolate_from_env(self, dict(CI_KEYS_BRANCH='A_BRANCH'))
246
working_dir = os.getcwd()
247
keys_dir = os.path.join(working_dir, 'keys')
248
self.create_keys(keys_dir)
250
deploy.install_keys(working_dir)
252
self.assertFalse(mocked_co.called)
253
self.assertEqual('', mocked_stdout.getvalue())
255
@mock.patch('deploy._do_bzr_checkout')
256
@mock.patch('sys.stdout', new_callable=StringIO.StringIO)
257
def test_keys_branch_provided(self, mocked_stdout, mocked_co):
258
# When CI_KEYS_BRANCH is provided and keys do not exist in the
259
# working dir, the keys branch is checked out.
260
keys_branch = 'A_BRANCH_URI'
261
fixtures.isolate_from_env(self, dict(CI_KEYS_BRANCH=keys_branch))
262
working_dir = os.getcwd()
264
deploy.install_keys(working_dir)
266
keys_dir = os.path.join(working_dir, 'keys')
267
mocked_co.assert_called_once_with(keys_branch, keys_dir)
269
'Installing keys from {}\n'.format(keys_branch),
270
mocked_stdout.getvalue())
272
@mock.patch('sys.stdout', new_callable=StringIO.StringIO)
273
def test_keys_branch_not_provided(self, mocked_stdout):
274
# When CI_KEYS_BRANCH is not provided and keys do not exist in
275
# the working dir, empty 'keys' are created to satisfy deployment
276
# configurations 'include's and a warning message is issued.
277
fixtures.isolate_from_env(self, dict(CI_KEYS_BRANCH=''))
278
working_dir = os.getcwd()
280
deploy.install_keys(working_dir)
282
keys_dir = os.path.join(working_dir, 'keys')
284
['gpg.pub', 'gpg.sec', 'id_rsa', 'id_rsa.pub'],
285
sorted(os.listdir(keys_dir)))
287
'Deployment keys are not available (CI_KEYS_BRANCH is not '
288
'defined and {} path does not exist). This deployment '
289
'will not be able to access LP resources that depend on it.'
290
'\n'.format(keys_dir),
291
mocked_stdout.getvalue())
223
294
class TestGenerateArgumentList(unittest.TestCase):
484
555
status = deploy.juju_status()
485
556
self.assertEqual('local', status['environment'])
558
@mock.patch('sys.stdout', new_callable=StringIO.StringIO)
559
def test_dump_juju_logs(self, mocked_out):
560
units = {'foo-service/0': {},
561
'foo-service/1': {'agent-state': 'error'}}
562
srv = {'foo-service': {'charm': 'foo', 'units': units},
563
'bar-service': {'charm': 'bar'}}
564
mocked = {'environment': 'local', 'machines': {}, 'services': srv}
565
mocked = yaml.safe_dump(mocked)
567
with mock.patch('subprocess.check_output', return_value=mocked):
568
with mock.patch('subprocess.check_call'):
569
deploy.dump_juju_logs()
571
expected = '/var/log/juju/unit-foo-service-1.log'
572
actual = mocked_out.getvalue().split('\n')
573
self.assertIn(expected, actual)
575
@mock.patch('sys.stderr', new_callable=StringIO.StringIO)
576
def test_dump_juju_logs_fail(self, mocked_err):
577
e = subprocess.CalledProcessError(-1, 'juju status')
578
with mock.patch('subprocess.check_output', side_effect=e):
579
deploy.dump_juju_logs()
580
actual = mocked_err.getvalue()
581
self.assertIn('Unable to get log files', actual)
583
u = {'foo-service/0': {'agent-state': 'error'}}
584
srv = {'foo-service': {'charm': 'foo', 'units': u}}
585
mocked = {'environment': 'local', 'machines': {}, 'services': srv}
586
mocked = yaml.safe_dump(mocked)
587
e = subprocess.CalledProcessError(-1, 'juju ssh')
588
with mock.patch('subprocess.check_output', return_value=mocked):
589
with mock.patch('subprocess.check_call', side_effect=e):
590
deploy.dump_juju_logs()
591
expected = 'Failed to capture juju log'
592
actual = mocked_err.getvalue()
593
self.assertIn(expected, actual)
488
596
class TestUpgrade(unittest.TestCase):