~larry-e-works/uci-engine/amqp-to-kombu

« back to all changes in this revision

Viewing changes to juju-deployer/test_deploy.py

  • Committer: Larry Works
  • Date: 2014-09-30 18:08:31 UTC
  • mfrom: (762.1.55 uci-engine)
  • Revision ID: larry.works@canonical.com-20140930180831-cpztlnifhc47ah69
Massive merge from trunk, no conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import mock
20
20
import tempfile
21
21
import shutil
 
22
import StringIO
22
23
import subprocess
23
24
import unittest
24
25
import yaml
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))
221
223
 
222
224
 
 
225
class TestInstallKeys(unittest.TestCase):
 
226
 
 
227
    def setUp(self):
 
228
        super(TestInstallKeys, self).setUp()
 
229
        fixtures.set_uniq_cwd(self)
 
230
 
 
231
    def create_keys(self, keys_path):
 
232
        """Creates a 'keys' directory with the relevant key files."""
 
233
        os.mkdir(keys_path)
 
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:
 
237
                fd.write('')
 
238
 
 
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)
 
249
 
 
250
        deploy.install_keys(working_dir)
 
251
 
 
252
        self.assertFalse(mocked_co.called)
 
253
        self.assertEqual('', mocked_stdout.getvalue())
 
254
 
 
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()
 
263
 
 
264
        deploy.install_keys(working_dir)
 
265
 
 
266
        keys_dir = os.path.join(working_dir, 'keys')
 
267
        mocked_co.assert_called_once_with(keys_branch, keys_dir)
 
268
        self.assertEqual(
 
269
            'Installing keys from {}\n'.format(keys_branch),
 
270
            mocked_stdout.getvalue())
 
271
 
 
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()
 
279
 
 
280
        deploy.install_keys(working_dir)
 
281
 
 
282
        keys_dir = os.path.join(working_dir, 'keys')
 
283
        self.assertEqual(
 
284
            ['gpg.pub', 'gpg.sec', 'id_rsa', 'id_rsa.pub'],
 
285
            sorted(os.listdir(keys_dir)))
 
286
        self.assertEqual(
 
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())
 
292
 
 
293
 
223
294
class TestGenerateArgumentList(unittest.TestCase):
224
295
 
225
296
    def setUp(self):
484
555
            status = deploy.juju_status()
485
556
            self.assertEqual('local', status['environment'])
486
557
 
 
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)
 
566
 
 
567
        with mock.patch('subprocess.check_output', return_value=mocked):
 
568
            with mock.patch('subprocess.check_call'):
 
569
                deploy.dump_juju_logs()
 
570
 
 
571
                expected = '/var/log/juju/unit-foo-service-1.log'
 
572
                actual = mocked_out.getvalue().split('\n')
 
573
                self.assertIn(expected, actual)
 
574
 
 
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)
 
582
 
 
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)
 
594
 
487
595
 
488
596
class TestUpgrade(unittest.TestCase):
489
597