1
from argparse import Namespace
3
from tempfile import NamedTemporaryFile
5
from mock import patch, call
7
from run_download_juju import (
13
from jujupy import ensure_dir
17
from test_schedule_hetero_control import make_build_var_file
18
from tests import parse_error
19
from utility import temp_dir
22
class TestGetCandidateInfo(TestCase):
24
def test_get_candidate_info(self):
25
with temp_dir() as dir_path:
26
candidate_dir = os.path.join(dir_path, 'candidate')
27
ensure_dir(candidate_dir)
28
for ver, rev in [['1.24.3', '2870'], ['1.24.5', '2999']]:
29
ver_dir = os.path.join(candidate_dir, ver)
31
make_build_var_file(ver_dir, version=ver, revision_build=rev)
32
rev = get_revisions(dir_path)
33
self.assertEqual(sorted(rev), ['2870', '2999'])
35
def test_create_workspace_yaml(self):
36
with NamedTemporaryFile() as temp_file:
37
create_workspace_yaml("/my/home", "/script/path", temp_file)
38
with open(temp_file.name) as yaml_file:
39
content = yaml_file.read()
40
self.assertEqual(content, self.expected())
42
def test_create_workspace_yaml_with_rev(self):
43
with NamedTemporaryFile() as temp_file:
44
create_workspace_yaml(
45
"/my/home", "/script/path", temp_file, ['2870', '2999'])
46
with open(temp_file.name) as yaml_file:
47
content = yaml_file.read()
48
self.assertEqual(content, self.expected('-c 2870 2999'))
50
def test_parse_args_default(self):
51
os.environ['JUJU_HOME'] = '/juju/home'
53
expected_args = Namespace(
54
osx_host='jenkins@osx-slave.vapour.ws',
55
win_host='Administrator@win-slave.vapour.ws',
56
juju_home='/juju/home')
57
self.assertEqual(args, expected_args)
59
def test_parse_args(self):
60
args = parse_args(['-o', 'j@my-osx-host', '-w', 'j@my-win-host',
62
expected_args = Namespace(
63
osx_host='j@my-osx-host', win_host='j@my-win-host',
64
juju_home='/juju/home')
65
self.assertEqual(args, expected_args)
67
def test_parse_args_juju_home_unset(self):
68
with parse_error(self) as stderr:
70
self.assertRegexpMatches(stderr.getvalue(), 'Invalid JUJU_HOME value')
73
with temp_dir() as home:
74
os.environ['HOME'] = home
75
juju_home = os.path.join(home, 'juju')
77
os.environ['JUJU_HOME'] = juju_home
78
temp_file = NamedTemporaryFile(delete=False)
79
with patch('run_download_juju.NamedTemporaryFile', autospec=True,
80
return_value=temp_file) as ntf_mock:
81
with patch('subprocess.check_output') as sco_mock:
84
call(['workspace-run', temp_file.name,
85
'Administrator@win-slave.vapour.ws']),
86
call(['workspace-run', temp_file.name,
87
'jenkins@osx-slave.vapour.ws'])]
88
self.assertEqual(sco_mock.call_args_list, calls)
89
ntf_mock.assert_called_once_with()
91
def test_main_expected_arguments(self):
92
os.environ['HOME'] = '/home/fake-juju-user'
93
with temp_dir() as juju_home:
94
os.environ['JUJU_HOME'] = juju_home
95
temp_file = NamedTemporaryFile(delete=False)
96
with patch('run_download_juju.get_revisions',
97
autospec=True) as gr_mock:
98
with patch('run_download_juju.create_workspace_yaml',
99
autospec=True) as cwy_mock:
100
with patch('run_download_juju.NamedTemporaryFile',
102
return_value=temp_file) as ntf_mock:
103
with patch('subprocess.check_output') as sco_mock:
106
call(['workspace-run', temp_file.name,
107
'Administrator@win-slave.vapour.ws']),
108
call(['workspace-run', temp_file.name,
109
'jenkins@osx-slave.vapour.ws'])]
112
('C:\\\\Users\\\\Administrator\\\\juju-ci-tools'
113
'\\\\download_juju.py'),
114
temp_file, gr_mock.return_value),
115
call(juju_home, '$HOME/juju-ci-tools/download_juju.py',
116
temp_file, gr_mock.return_value)]
117
self.assertEqual(sco_mock.call_args_list, sco_calls)
118
ntf_mock.assert_called_once_with()
119
gr_mock.assert_called_once_with(os.environ['HOME'])
120
self.assertEqual(cwy_mock.call_args_list, cwy_calls)
122
def expected(self, rev="''"):
123
return ("command: [python, /script/path, cloud-city, -r, -v, {}]\n"
125
" cloud-city: [/my/home/ec2rc]\n".format(rev))