220
217
get_credentials(Namespace(user='jrandom', password=None))
222
219
def test_main_list_options(self):
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'])
234
228
def test_main_get_options(self):
236
with patch('jujuci.print_now', side_effect=print_list.append):
237
with patch('jujuci.get_artifacts') as mock:
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:
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'])
249
240
def test_main_setup_workspace_options(self):
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',
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:
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'])
262
250
def test_main_get_buildvars(self):
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:
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:
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'])
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(
568
['Retrieving %s => %s' % (location, local_path)]
553
'Retrieving %s => %s' % (location, local_path),
571
556
def test_get_artifacts_with_dry_run(self):
572
557
build_data = make_build_data(1234)
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:
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:
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'])
583
565
def test_get_artifacts_with_archive(self):
584
566
build_data = make_build_data(1234)
586
with temp_dir() as base_dir:
587
path = os.path.join(base_dir, 'foo')
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'):
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')
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')
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))
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:
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))
626
['Removing old.txt', 'Removing foo', 'Creating artifacts dir.']
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)
634
with patch('jujuci.print_now', side_effect=print_list.append):
635
with patch('jujuci.clean_environment', autospec=True) as mock:
637
workspace_dir, env='foo', dry_run=False, verbose=False)
606
with patch('jujuci.clean_environment', autospec=True) as mock:
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.'])
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)
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(
661
with patch.object(jujupy.EnvJujuClient,
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])