~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_jujuci.py

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import json
4
4
import os
5
5
from StringIO import StringIO
 
6
from unittest import TestCase
6
7
import urllib2
7
8
 
8
9
from mock import patch
37
38
)
38
39
import jujupy
39
40
from utility import temp_dir
40
 
from tests import (
41
 
    FakeHomeTestCase,
42
 
    parse_error,
43
 
    TestCase,
44
 
)
 
41
from tests import parse_error
45
42
 
46
43
 
47
44
def make_build_data(number='lastSuccessfulBuild'):
199
196
    }
200
197
 
201
198
 
202
 
class JujuCITestCase(FakeHomeTestCase):
 
199
class JujuCITestCase(TestCase):
203
200
 
204
201
    def test_get_credentials(self):
205
202
        self.assertEqual(
220
217
            get_credentials(Namespace(user='jrandom', password=None))
221
218
 
222
219
    def test_main_list_options(self):
223
 
        print_list = []
224
 
        with patch('jujuci.print_now', side_effect=print_list.append):
225
 
            with patch('jujuci.list_artifacts') as mock:
226
 
                main(['-d', '-v', 'list', '-b', '1234', 'foo', '*.tar.gz',
227
 
                      '--user', 'jrandom', '--password', '1password'])
228
 
        args, kwargs = mock.call_args
229
 
        self.assertEqual((Credentials('jrandom', '1password'), 'foo',
230
 
                         '1234', '*.tar.gz'), args)
231
 
        self.assertTrue(kwargs['verbose'])
232
 
        self.assertEqual(print_list, ['Done.'])
 
220
        with patch('jujuci.list_artifacts') as mock:
 
221
            main(['-d', '-v', 'list', '-b', '1234', 'foo', '*.tar.gz',
 
222
                  '--user', 'jrandom', '--password', '1password'])
 
223
            args, kwargs = mock.call_args
 
224
            self.assertEqual((Credentials('jrandom', '1password'), 'foo',
 
225
                             '1234', '*.tar.gz'), args)
 
226
            self.assertTrue(kwargs['verbose'])
233
227
 
234
228
    def test_main_get_options(self):
235
 
        print_list = []
236
 
        with patch('jujuci.print_now', side_effect=print_list.append):
237
 
            with patch('jujuci.get_artifacts') as mock:
238
 
                main(['-d', '-v',
239
 
                      'get', '-a', '-b', '1234', 'foo', '*.tar.gz', 'bar',
240
 
                      '--user', 'jrandom', '--password', '1password'])
241
 
        args, kwargs = mock.call_args
242
 
        self.assertEqual((Credentials('jrandom', '1password'), 'foo',
243
 
                         '1234', '*.tar.gz', 'bar'), args)
244
 
        self.assertTrue(kwargs['archive'])
245
 
        self.assertTrue(kwargs['verbose'])
246
 
        self.assertTrue(kwargs['dry_run'])
247
 
        self.assertEqual(print_list, ['Done.'])
 
229
        with patch('jujuci.get_artifacts') as mock:
 
230
            main(['-d', '-v',
 
231
                  'get', '-a', '-b', '1234', 'foo', '*.tar.gz', 'bar',
 
232
                  '--user', 'jrandom', '--password', '1password'])
 
233
            args, kwargs = mock.call_args
 
234
            self.assertEqual((Credentials('jrandom', '1password'), 'foo',
 
235
                             '1234', '*.tar.gz', 'bar'), args)
 
236
            self.assertTrue(kwargs['archive'])
 
237
            self.assertTrue(kwargs['verbose'])
 
238
            self.assertTrue(kwargs['dry_run'])
248
239
 
249
240
    def test_main_setup_workspace_options(self):
250
 
        print_list = []
251
 
        with patch('jujuci.print_now', side_effect=print_list.append):
252
 
            with patch('jujuci.setup_workspace', autospec=True) as mock:
253
 
                main(['-d', '-v', 'setup-workspace', '--clean-env', 'bar',
254
 
                      './foo'])
255
 
        args, kwargs = mock.call_args
256
 
        self.assertEqual(('./foo', ), args)
257
 
        self.assertEqual('bar', kwargs['env'])
258
 
        self.assertTrue(kwargs['dry_run'])
259
 
        self.assertTrue(kwargs['verbose'])
260
 
        self.assertEqual(print_list, ['Done.'])
 
241
        with patch('jujuci.setup_workspace', autospec=True) as mock:
 
242
            main(
 
243
                ['-d', '-v', 'setup-workspace', '--clean-env', 'bar', './foo'])
 
244
            args, kwargs = mock.call_args
 
245
            self.assertEqual(('./foo', ), args)
 
246
            self.assertEqual('bar', kwargs['env'])
 
247
            self.assertTrue(kwargs['dry_run'])
 
248
            self.assertTrue(kwargs['verbose'])
261
249
 
262
250
    def test_main_get_buildvars(self):
263
 
        print_list = []
264
 
        with patch('jujuci.print_now', side_effect=print_list.append):
265
 
            with patch('jujuci.get_buildvars', autospec=True,
266
 
                       return_value='sample buildvars') as mock:
267
 
                main(
268
 
                    ['get-build-vars', '--env', 'foo', '--summary',
269
 
                     '--revision-build', '--version', '--short-branch',
270
 
                     '--short-revision', '--branch', '--revision', '123',
271
 
                     '--user', 'jrandom', '--password', '1password'])
 
251
        with patch('jujuci.get_buildvars', autospec=True) as mock:
 
252
            main(
 
253
                ['get-build-vars', '--env', 'foo', '--summary',
 
254
                 '--revision-build', '--version', '--short-branch',
 
255
                 '--short-revision', '--branch', '--revision', '123',
 
256
                 '--user', 'jrandom', '--password', '1password'])
272
257
        args, kwargs = mock.call_args
273
258
        self.assertEqual((Credentials('jrandom', '1password'), '123'), args)
274
259
        self.assertEqual('foo', kwargs['env'])
279
264
        self.assertTrue(kwargs['short_branch'])
280
265
        self.assertTrue(kwargs['branch'])
281
266
        self.assertTrue(kwargs['revision'])
282
 
        self.assertEqual(print_list, ['sample buildvars'])
283
267
 
284
268
    def test_parse_arg_buildvars_common_options(self):
285
269
        args, credentials = parse_args(
545
529
 
546
530
    def test_get_artifacts(self):
547
531
        build_data = make_build_data(1234)
548
 
        print_list = []
549
 
        with patch('jujuci.print_now', side_effect=print_list.append):
550
 
            with patch('jujuci.get_build_data', return_value=build_data):
551
 
                with patch('urllib.urlretrieve') as uo_mock:
 
532
        with patch('jujuci.get_build_data', return_value=build_data):
 
533
            with patch('urllib.urlretrieve') as uo_mock:
 
534
                with patch('jujuci.print_now') as pn_mock:
552
535
                    found = get_artifacts(
553
536
                        Credentials('jrandom', '1password'), 'foo', '1234',
554
537
                        '*.bash', './', verbose=True)
563
546
        auth_location = location.replace('http://',
564
547
                                         'http://jrandom:1password@')
565
548
        self.assertEqual((auth_location, local_path), args)
 
549
        messages = sorted(call[1][0] for call in pn_mock.mock_calls)
 
550
        self.assertEqual(1, len(messages))
 
551
        message = messages[0]
566
552
        self.assertEqual(
567
 
            print_list,
568
 
            ['Retrieving %s => %s' % (location, local_path)]
569
 
        )
 
553
            'Retrieving %s => %s' % (location, local_path),
 
554
            message)
570
555
 
571
556
    def test_get_artifacts_with_dry_run(self):
572
557
        build_data = make_build_data(1234)
573
 
        print_list = []
574
 
        with patch('jujuci.print_now', side_effect=print_list.append):
575
 
            with patch('jujuci.get_build_data', return_value=build_data):
576
 
                with patch('urllib.urlretrieve') as uo_mock:
577
 
                    get_artifacts(
578
 
                        Credentials('jrandom', '1password'), 'foo', '1234',
579
 
                        '*.bash', './', dry_run=True)
 
558
        with patch('jujuci.get_build_data', return_value=build_data):
 
559
            with patch('urllib.urlretrieve') as uo_mock:
 
560
                get_artifacts(
 
561
                    Credentials('jrandom', '1password'), 'foo', '1234',
 
562
                    '*.bash', './', dry_run=True)
580
563
        self.assertEqual(0, uo_mock.call_count)
581
 
        self.assertEqual(print_list, ['buildvars.bash'])
582
564
 
583
565
    def test_get_artifacts_with_archive(self):
584
566
        build_data = make_build_data(1234)
585
 
        print_list = []
586
 
        with temp_dir() as base_dir:
587
 
            path = os.path.join(base_dir, 'foo')
588
 
            os.mkdir(path)
589
 
            old_file_path = os.path.join(path, 'old_file.txt')
590
 
            with open(old_file_path, 'w') as old_file:
591
 
                old_file.write('old')
592
 
            with patch('jujuci.print_now', side_effect=print_list.append):
593
 
                with patch('jujuci.get_build_data', return_value=build_data):
594
 
                    with patch('urllib.urlretrieve'):
595
 
                        get_artifacts(
596
 
                            Credentials('jrandom', '1password'), 'foo', '1234',
597
 
                            '*.bash', path, archive=True)
598
 
            self.assertFalse(os.path.isfile(old_file_path))
599
 
            self.assertTrue(os.path.isdir(path))
600
 
        self.assertEqual(print_list, ['buildvars.bash'])
 
567
        with patch('jujuci.get_build_data', return_value=build_data):
 
568
            with patch('urllib.urlretrieve'):
 
569
                with temp_dir() as base_dir:
 
570
                    path = os.path.join(base_dir, 'foo')
 
571
                    os.mkdir(path)
 
572
                    old_file_path = os.path.join(path, 'old_file.txt')
 
573
                    with open(old_file_path, 'w') as old_file:
 
574
                        old_file.write('old')
 
575
                    get_artifacts(
 
576
                        Credentials('jrandom', '1password'), 'foo', '1234',
 
577
                        '*.bash', path, archive=True)
 
578
                    self.assertFalse(os.path.isfile(old_file_path))
 
579
                    self.assertTrue(os.path.isdir(path))
601
580
 
602
581
    def test_get_artifacts_with_archive_error(self):
603
582
        build_data = make_build_data(1234)
615
594
            os.makedirs(foo_dir)
616
595
            with open(os.path.join(workspace_dir, 'old.txt'), 'w') as of:
617
596
                of.write('old')
618
 
            print_list = []
619
 
            with patch('jujuci.print_now', side_effect=print_list.append):
620
 
                setup_workspace(workspace_dir, dry_run=False, verbose=False)
 
597
            setup_workspace(workspace_dir, dry_run=False, verbose=False)
621
598
            self.assertEqual(['artifacts'], os.listdir(workspace_dir))
622
599
            artifacts_dir = os.path.join(workspace_dir, 'artifacts')
623
600
            self.assertEqual(['empty'], os.listdir(artifacts_dir))
624
 
            self.assertEqual(
625
 
                print_list,
626
 
                ['Removing old.txt', 'Removing foo', 'Creating artifacts dir.']
627
 
            )
628
601
 
629
602
    def test_setup_workspace_with_env(self):
630
603
        with temp_dir() as base_dir:
631
604
            workspace_dir = os.path.join(base_dir, 'workspace')
632
605
            os.makedirs(workspace_dir)
633
 
            print_list = []
634
 
            with patch('jujuci.print_now', side_effect=print_list.append):
635
 
                with patch('jujuci.clean_environment', autospec=True) as mock:
636
 
                    setup_workspace(
637
 
                        workspace_dir, env='foo', dry_run=False, verbose=False)
 
606
            with patch('jujuci.clean_environment', autospec=True) as mock:
 
607
                setup_workspace(
 
608
                    workspace_dir, env='foo', dry_run=False, verbose=False)
638
609
            mock.assert_called_once_with('foo', verbose=False)
639
 
            self.assertEqual(print_list, ['Creating artifacts dir.'])
640
610
 
641
611
    def test_clean_environment_not_dirty(self):
642
612
        config = {'environments': {'local': {'type': 'local'}}}
643
613
        with jujupy._temp_env(config, set_home=True):
644
 
            with patch('jujuci.destroy_environment', autospec=True) as mock_de:
645
 
                with patch.object(jujupy.EnvJujuClient, 'get_version'):
646
 
                    with patch('jujupy.get_client_class') as gcc_mock:
647
 
                        gcc_mock.return_value = jujupy.EnvJujuClient
648
 
                        dirty = clean_environment('foo', verbose=False)
 
614
            with patch('jujuci.destroy_environment') as mock:
 
615
                dirty = clean_environment('foo', verbose=False)
649
616
        self.assertFalse(dirty)
650
 
        self.assertEqual(0, mock_de.call_count)
 
617
        self.assertEqual(0, mock.call_count)
651
618
 
652
619
    def test_clean_environment_dirty(self):
653
620
        config = {'environments': {'foo': {'type': 'local'}}}
654
621
        with jujupy._temp_env(config, set_home=True):
655
 
            with patch('jujuci.destroy_environment', autospec=True) as mock_de:
656
 
                with patch.object(jujupy.EnvJujuClient, 'get_version'):
657
 
                    with patch('jujupy.get_client_class') as gcc_mock:
658
 
                        factory = gcc_mock.return_value
659
 
                        factory.return_value = jujupy.EnvJujuClient(
660
 
                            None, None, None)
661
 
                        with patch.object(jujupy.EnvJujuClient,
662
 
                                          'get_full_path'):
663
 
                            with patch.object(jujupy.JujuData, 'load_yaml'):
664
 
                                dirty = clean_environment('foo', verbose=False)
 
622
            with patch('jujuci.destroy_environment', autospec=True) as mock:
 
623
                dirty = clean_environment('foo', verbose=False)
665
624
        self.assertTrue(dirty)
666
 
        self.assertEqual(1, mock_de.call_count)
667
 
        args, kwargs = mock_de.call_args
 
625
        self.assertEqual(1, mock.call_count)
 
626
        args, kwargs = mock.call_args
668
627
        self.assertIsInstance(args[0], jujupy.EnvJujuClient)
669
628
        self.assertEqual('foo', args[1])
670
629