Merge lp:~sseman/juju-ci-tools/download-juju into lp:juju-ci-tools

Proposed by Seman
Status: Merged
Merged at revision: 1109
Proposed branch: lp:~sseman/juju-ci-tools/download-juju
Merge into: lp:juju-ci-tools
Diff against target: 238 lines (+216/-2)
3 files modified
run_download_juju.py (+70/-0)
test_run_download_juju.py (+144/-0)
tests/test_schedule_hetero_control.py (+2/-2)
To merge this branch: bzr merge lp:~sseman/juju-ci-tools/download-juju
Reviewer Review Type Date Requested Status
John George (community) Approve
Review via email: mp+271906@code.launchpad.net

Description of the change

This branch creates run_download_juju.py script. Using the Workspace Runner, it runs the download_juju.py script on the OS X and Windows machines.

Once this branch is approved, I will create a new Jenkins job called prepare-for-client-server-test. The job will execute this script (run_download_juju.py) and prepares the OS X and Widows machine for the client-server test by downloading all released and candidate Juju to the local file systems. When we run the client-server tests on OS X or Windows, we are going to assume the released and candidate Juju are already exist on local drive.

To post a comment you must log in.
Revision history for this message
John George (jog) wrote :

Approve but I left one comment in-line suggesting you also test two additional calls made from main.

review: Approve
1097. By Seman

Added unit test.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'run_download_juju.py'
2--- run_download_juju.py 1970-01-01 00:00:00 +0000
3+++ run_download_juju.py 2015-09-22 22:39:06 +0000
4@@ -0,0 +1,70 @@
5+#!/usr/bin/python
6+from argparse import ArgumentParser
7+import os
8+from tempfile import NamedTemporaryFile
9+import yaml
10+
11+from utility import run_command
12+
13+from schedule_hetero_control import get_candidate_info
14+from utility import find_candidates
15+
16+
17+def get_revisions(root_dir):
18+ revisions = []
19+ for candidate in find_candidates(root_dir):
20+ _, rev = get_candidate_info(candidate)
21+ revisions.append(rev)
22+ return revisions
23+
24+
25+def create_workspace_yaml(juju_home, script_path, stream, rev=None):
26+ rev = '-c {}'.format(" ".join(rev)) if rev else ''
27+ yl = {
28+ "install": {
29+ "cloud-city": [os.path.join(juju_home, 'ec2rc')]},
30+ "command": [
31+ "python",
32+ script_path,
33+ "cloud-city", "-r", "-v", rev]
34+ }
35+ yaml.safe_dump(yl, stream)
36+
37+
38+def parse_args(args=None):
39+ parser = ArgumentParser(
40+ "Run download_juju script on the OS X and Windows machines.")
41+ parser.add_argument('-o', '--osx-host',
42+ default='jenkins@osx-slave.vapour.ws',
43+ help="OS X machine's username and hostname.")
44+ parser.add_argument('-w', '--win-host',
45+ default='Administrator@win-slave.vapour.ws',
46+ help="Windows' username and hostname.")
47+ parser.add_argument('-j', '--juju-home',
48+ default=os.environ.get('JUJU_HOME'),
49+ help="Juju home directory (cloud-city dir).")
50+ parsed_args = parser.parse_args(args)
51+ if parsed_args.juju_home is None:
52+ parser.error(
53+ 'Invalid JUJU_HOME value: either set $JUJU_HOME env variable or '
54+ 'use --juju-home option to set the value.')
55+
56+ return parsed_args
57+
58+
59+def main(argv=None):
60+ args = parse_args(argv)
61+ juju_home = args.juju_home
62+ win_host = args.win_host
63+ win_path = 'C:\\\Users\\\Administrator\\\juju-ci-tools\\\download_juju.py'
64+ osx_path = '$HOME/juju-ci-tools/download_juju.py'
65+ osx_host = args.osx_host
66+ rev = get_revisions(os.environ['HOME'])
67+ with NamedTemporaryFile() as temp_file:
68+ for path, host in [[win_path, win_host], [osx_path, osx_host]]:
69+ create_workspace_yaml(juju_home, path, temp_file, rev)
70+ run_command(['workspace-run', temp_file.name, host])
71+
72+
73+if __name__ == '__main__':
74+ main()
75
76=== added file 'test_run_download_juju.py'
77--- test_run_download_juju.py 1970-01-01 00:00:00 +0000
78+++ test_run_download_juju.py 2015-09-22 22:39:06 +0000
79@@ -0,0 +1,144 @@
80+from argparse import Namespace
81+import os
82+from tempfile import NamedTemporaryFile
83+from unittest import TestCase
84+
85+from mock import patch, call
86+
87+from run_download_juju import (
88+ create_workspace_yaml,
89+ get_revisions,
90+ parse_args,
91+ main
92+)
93+from jujupy import ensure_dir
94+from test_schedule_hetero_control import make_build_var_file
95+from test_utility import parse_error
96+from utility import temp_dir
97+
98+
99+class TestGetCandidateInfo(TestCase):
100+
101+ def test_get_candidate_info(self):
102+ with temp_dir() as dir_path:
103+ candidate_dir = os.path.join(dir_path, 'candidate')
104+ ensure_dir(candidate_dir)
105+ for ver, rev in [['1.24.3', '2870'], ['1.24.5', '2999']]:
106+ ver_dir = os.path.join(candidate_dir, ver)
107+ ensure_dir(ver_dir)
108+ make_build_var_file(ver_dir, version=ver, revision_build=rev)
109+ rev = get_revisions(dir_path)
110+ self.assertEqual(rev, ['2870', '2999'])
111+
112+ def test_create_workspace_yaml(self):
113+ with NamedTemporaryFile() as temp_file:
114+ create_workspace_yaml("/my/home", "/script/path", temp_file)
115+ with open(temp_file.name) as yaml_file:
116+ content = yaml_file.read()
117+ self.assertEqual(content, self.expected())
118+
119+ def test_create_workspace_yaml_with_rev(self):
120+ with NamedTemporaryFile() as temp_file:
121+ create_workspace_yaml(
122+ "/my/home", "/script/path", temp_file, ['2870', '2999'])
123+ with open(temp_file.name) as yaml_file:
124+ content = yaml_file.read()
125+ self.assertEqual(content, self.expected('-c 2870 2999'))
126+
127+ def test_parse_args_default(self):
128+ org = os.environ.get('JUJU_HOME')
129+ os.environ['JUJU_HOME'] = '/juju/home'
130+ args = parse_args([])
131+ expected_args = Namespace(
132+ osx_host='jenkins@osx-slave.vapour.ws',
133+ win_host='Administrator@win-slave.vapour.ws',
134+ juju_home='/juju/home')
135+ self.assertEqual(args, expected_args)
136+ set_env('JUJU_HOME', org)
137+
138+ def test_parse_args(self):
139+ args = parse_args(['-o', 'j@my-osx-host', '-w', 'j@my-win-host',
140+ '-j' '/juju/home'])
141+ expected_args = Namespace(
142+ osx_host='j@my-osx-host', win_host='j@my-win-host',
143+ juju_home='/juju/home')
144+ self.assertEqual(args, expected_args)
145+
146+ def test_parse_args_juju_home_unset(self):
147+ org = del_env('JUJU_HOME')
148+ with parse_error(self) as stderr:
149+ parse_args([])
150+ self.assertRegexpMatches(stderr.getvalue(), 'Invalid JUJU_HOME value')
151+ if org:
152+ set_env('JUJU_HOME', org)
153+
154+ def test_main(self):
155+ org = os.environ.get('JUJU_HOME')
156+ with temp_dir() as juju_home:
157+ set_env('JUJU_HOME', juju_home)
158+ temp_file = NamedTemporaryFile(delete=False)
159+ with patch('run_download_juju.NamedTemporaryFile', autospec=True,
160+ return_value=temp_file) as ntf_mock:
161+ with patch('subprocess.check_output') as sco_mock:
162+ main([])
163+ calls = [
164+ call(['workspace-run', temp_file.name,
165+ 'Administrator@win-slave.vapour.ws']),
166+ call(['workspace-run', temp_file.name,
167+ 'jenkins@osx-slave.vapour.ws'])]
168+ set_env('JUJU_HOME', org)
169+ self.assertEqual(sco_mock.call_args_list, calls)
170+ ntf_mock.assert_called_once_with()
171+
172+ def test_main_expected_arguments(self):
173+ org = os.environ.get('JUJU_HOME')
174+ with temp_dir() as juju_home:
175+ set_env('JUJU_HOME', juju_home)
176+ temp_file = NamedTemporaryFile(delete=False)
177+ with patch('run_download_juju.get_revisions',
178+ autospec=True) as gr_mock:
179+ with patch('run_download_juju.create_workspace_yaml',
180+ autospec=True) as cwy_mock:
181+ with patch('run_download_juju.NamedTemporaryFile',
182+ autospec=True,
183+ return_value=temp_file) as ntf_mock:
184+ with patch('subprocess.check_output') as sco_mock:
185+ main([])
186+ sco_calls = [
187+ call(['workspace-run', temp_file.name,
188+ 'Administrator@win-slave.vapour.ws']),
189+ call(['workspace-run', temp_file.name,
190+ 'jenkins@osx-slave.vapour.ws'])]
191+ cwy_calls = [
192+ call(juju_home,
193+ ('C:\\\Users\\\Administrator\\\juju-ci-tools\\\download_'
194+ 'juju.py'),
195+ temp_file, gr_mock.return_value),
196+ call(juju_home, '$HOME/juju-ci-tools/download_juju.py',
197+ temp_file, gr_mock.return_value)]
198+ set_env('JUJU_HOME', org)
199+ self.assertEqual(sco_mock.call_args_list, sco_calls)
200+ ntf_mock.assert_called_once_with()
201+ gr_mock.assert_called_once_with(os.environ['HOME'])
202+ self.assertEqual(cwy_mock.call_args_list, cwy_calls)
203+
204+ def expected(self, rev="''"):
205+ return ("command: [python, /script/path, cloud-city, -r, -v, {}]\n"
206+ "install:\n"
207+ " cloud-city: [/my/home/ec2rc]\n".format(rev))
208+
209+
210+def del_env(key):
211+ try:
212+ org = os.environ[key]
213+ del os.environ[key]
214+ except KeyError:
215+ org = None
216+ return org
217+
218+
219+def set_env(key, value):
220+ if value is None:
221+ del os.environ[key]
222+ else:
223+ os.environ[key] = value
224
225=== modified file 'tests/test_schedule_hetero_control.py'
226--- tests/test_schedule_hetero_control.py 2015-09-17 20:39:25 +0000
227+++ tests/test_schedule_hetero_control.py 2015-09-22 22:39:06 +0000
228@@ -122,8 +122,8 @@
229 return jobs
230
231
232-def make_build_var_file(dir_path, version):
233- build_vars = {"version": version, "revision_build": "2870"}
234+def make_build_var_file(dir_path, version, revision_build="2870"):
235+ build_vars = {"version": version, "revision_build": revision_build}
236 file_path = os.path.join(dir_path, 'buildvars.json')
237 with open(file_path, 'w') as json_file:
238 json.dump(build_vars, json_file)

Subscribers

People subscribed via source and target branches