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
=== added file 'run_download_juju.py'
--- run_download_juju.py 1970-01-01 00:00:00 +0000
+++ run_download_juju.py 2015-09-22 22:39:06 +0000
@@ -0,0 +1,70 @@
1#!/usr/bin/python
2from argparse import ArgumentParser
3import os
4from tempfile import NamedTemporaryFile
5import yaml
6
7from utility import run_command
8
9from schedule_hetero_control import get_candidate_info
10from utility import find_candidates
11
12
13def get_revisions(root_dir):
14 revisions = []
15 for candidate in find_candidates(root_dir):
16 _, rev = get_candidate_info(candidate)
17 revisions.append(rev)
18 return revisions
19
20
21def create_workspace_yaml(juju_home, script_path, stream, rev=None):
22 rev = '-c {}'.format(" ".join(rev)) if rev else ''
23 yl = {
24 "install": {
25 "cloud-city": [os.path.join(juju_home, 'ec2rc')]},
26 "command": [
27 "python",
28 script_path,
29 "cloud-city", "-r", "-v", rev]
30 }
31 yaml.safe_dump(yl, stream)
32
33
34def parse_args(args=None):
35 parser = ArgumentParser(
36 "Run download_juju script on the OS X and Windows machines.")
37 parser.add_argument('-o', '--osx-host',
38 default='jenkins@osx-slave.vapour.ws',
39 help="OS X machine's username and hostname.")
40 parser.add_argument('-w', '--win-host',
41 default='Administrator@win-slave.vapour.ws',
42 help="Windows' username and hostname.")
43 parser.add_argument('-j', '--juju-home',
44 default=os.environ.get('JUJU_HOME'),
45 help="Juju home directory (cloud-city dir).")
46 parsed_args = parser.parse_args(args)
47 if parsed_args.juju_home is None:
48 parser.error(
49 'Invalid JUJU_HOME value: either set $JUJU_HOME env variable or '
50 'use --juju-home option to set the value.')
51
52 return parsed_args
53
54
55def main(argv=None):
56 args = parse_args(argv)
57 juju_home = args.juju_home
58 win_host = args.win_host
59 win_path = 'C:\\\Users\\\Administrator\\\juju-ci-tools\\\download_juju.py'
60 osx_path = '$HOME/juju-ci-tools/download_juju.py'
61 osx_host = args.osx_host
62 rev = get_revisions(os.environ['HOME'])
63 with NamedTemporaryFile() as temp_file:
64 for path, host in [[win_path, win_host], [osx_path, osx_host]]:
65 create_workspace_yaml(juju_home, path, temp_file, rev)
66 run_command(['workspace-run', temp_file.name, host])
67
68
69if __name__ == '__main__':
70 main()
071
=== added file 'test_run_download_juju.py'
--- test_run_download_juju.py 1970-01-01 00:00:00 +0000
+++ test_run_download_juju.py 2015-09-22 22:39:06 +0000
@@ -0,0 +1,144 @@
1from argparse import Namespace
2import os
3from tempfile import NamedTemporaryFile
4from unittest import TestCase
5
6from mock import patch, call
7
8from run_download_juju import (
9 create_workspace_yaml,
10 get_revisions,
11 parse_args,
12 main
13)
14from jujupy import ensure_dir
15from test_schedule_hetero_control import make_build_var_file
16from test_utility import parse_error
17from utility import temp_dir
18
19
20class TestGetCandidateInfo(TestCase):
21
22 def test_get_candidate_info(self):
23 with temp_dir() as dir_path:
24 candidate_dir = os.path.join(dir_path, 'candidate')
25 ensure_dir(candidate_dir)
26 for ver, rev in [['1.24.3', '2870'], ['1.24.5', '2999']]:
27 ver_dir = os.path.join(candidate_dir, ver)
28 ensure_dir(ver_dir)
29 make_build_var_file(ver_dir, version=ver, revision_build=rev)
30 rev = get_revisions(dir_path)
31 self.assertEqual(rev, ['2870', '2999'])
32
33 def test_create_workspace_yaml(self):
34 with NamedTemporaryFile() as temp_file:
35 create_workspace_yaml("/my/home", "/script/path", temp_file)
36 with open(temp_file.name) as yaml_file:
37 content = yaml_file.read()
38 self.assertEqual(content, self.expected())
39
40 def test_create_workspace_yaml_with_rev(self):
41 with NamedTemporaryFile() as temp_file:
42 create_workspace_yaml(
43 "/my/home", "/script/path", temp_file, ['2870', '2999'])
44 with open(temp_file.name) as yaml_file:
45 content = yaml_file.read()
46 self.assertEqual(content, self.expected('-c 2870 2999'))
47
48 def test_parse_args_default(self):
49 org = os.environ.get('JUJU_HOME')
50 os.environ['JUJU_HOME'] = '/juju/home'
51 args = parse_args([])
52 expected_args = Namespace(
53 osx_host='jenkins@osx-slave.vapour.ws',
54 win_host='Administrator@win-slave.vapour.ws',
55 juju_home='/juju/home')
56 self.assertEqual(args, expected_args)
57 set_env('JUJU_HOME', org)
58
59 def test_parse_args(self):
60 args = parse_args(['-o', 'j@my-osx-host', '-w', 'j@my-win-host',
61 '-j' '/juju/home'])
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)
66
67 def test_parse_args_juju_home_unset(self):
68 org = del_env('JUJU_HOME')
69 with parse_error(self) as stderr:
70 parse_args([])
71 self.assertRegexpMatches(stderr.getvalue(), 'Invalid JUJU_HOME value')
72 if org:
73 set_env('JUJU_HOME', org)
74
75 def test_main(self):
76 org = os.environ.get('JUJU_HOME')
77 with temp_dir() as juju_home:
78 set_env('JUJU_HOME', juju_home)
79 temp_file = NamedTemporaryFile(delete=False)
80 with patch('run_download_juju.NamedTemporaryFile', autospec=True,
81 return_value=temp_file) as ntf_mock:
82 with patch('subprocess.check_output') as sco_mock:
83 main([])
84 calls = [
85 call(['workspace-run', temp_file.name,
86 'Administrator@win-slave.vapour.ws']),
87 call(['workspace-run', temp_file.name,
88 'jenkins@osx-slave.vapour.ws'])]
89 set_env('JUJU_HOME', org)
90 self.assertEqual(sco_mock.call_args_list, calls)
91 ntf_mock.assert_called_once_with()
92
93 def test_main_expected_arguments(self):
94 org = os.environ.get('JUJU_HOME')
95 with temp_dir() as juju_home:
96 set_env('JUJU_HOME', juju_home)
97 temp_file = NamedTemporaryFile(delete=False)
98 with patch('run_download_juju.get_revisions',
99 autospec=True) as gr_mock:
100 with patch('run_download_juju.create_workspace_yaml',
101 autospec=True) as cwy_mock:
102 with patch('run_download_juju.NamedTemporaryFile',
103 autospec=True,
104 return_value=temp_file) as ntf_mock:
105 with patch('subprocess.check_output') as sco_mock:
106 main([])
107 sco_calls = [
108 call(['workspace-run', temp_file.name,
109 'Administrator@win-slave.vapour.ws']),
110 call(['workspace-run', temp_file.name,
111 'jenkins@osx-slave.vapour.ws'])]
112 cwy_calls = [
113 call(juju_home,
114 ('C:\\\Users\\\Administrator\\\juju-ci-tools\\\download_'
115 'juju.py'),
116 temp_file, gr_mock.return_value),
117 call(juju_home, '$HOME/juju-ci-tools/download_juju.py',
118 temp_file, gr_mock.return_value)]
119 set_env('JUJU_HOME', org)
120 self.assertEqual(sco_mock.call_args_list, sco_calls)
121 ntf_mock.assert_called_once_with()
122 gr_mock.assert_called_once_with(os.environ['HOME'])
123 self.assertEqual(cwy_mock.call_args_list, cwy_calls)
124
125 def expected(self, rev="''"):
126 return ("command: [python, /script/path, cloud-city, -r, -v, {}]\n"
127 "install:\n"
128 " cloud-city: [/my/home/ec2rc]\n".format(rev))
129
130
131def del_env(key):
132 try:
133 org = os.environ[key]
134 del os.environ[key]
135 except KeyError:
136 org = None
137 return org
138
139
140def set_env(key, value):
141 if value is None:
142 del os.environ[key]
143 else:
144 os.environ[key] = value
0145
=== modified file 'tests/test_schedule_hetero_control.py'
--- tests/test_schedule_hetero_control.py 2015-09-17 20:39:25 +0000
+++ tests/test_schedule_hetero_control.py 2015-09-22 22:39:06 +0000
@@ -122,8 +122,8 @@
122 return jobs122 return jobs
123123
124124
125def make_build_var_file(dir_path, version):125def make_build_var_file(dir_path, version, revision_build="2870"):
126 build_vars = {"version": version, "revision_build": "2870"}126 build_vars = {"version": version, "revision_build": revision_build}
127 file_path = os.path.join(dir_path, 'buildvars.json')127 file_path = os.path.join(dir_path, 'buildvars.json')
128 with open(file_path, 'w') as json_file:128 with open(file_path, 'w') as json_file:
129 json.dump(build_vars, json_file)129 json.dump(build_vars, json_file)

Subscribers

People subscribed via source and target branches