Merge lp:~viswesn/juju-ci-tools/juju-ci-sync-metadata-source into lp:juju-ci-tools

Proposed by viswesuwara nathan
Status: Work in progress
Proposed branch: lp:~viswesn/juju-ci-tools/juju-ci-sync-metadata-source
Merge into: lp:juju-ci-tools
Prerequisite: lp:~viswesn/juju-ci-tools/juju-ci-pvt-tools
Diff against target: 252 lines (+175/-10) (has conflicts)
5 files modified
assess_bootstrap.py (+5/-4)
assess_sync_agent_metadata.py (+130/-0)
jujupy.py (+9/-4)
tests/test_assess_bootstrap.py (+23/-2)
tests/test_jujupy.py (+8/-0)
Text conflict in tests/test_jujupy.py
To merge this branch: bzr merge lp:~viswesn/juju-ci-tools/juju-ci-sync-metadata-source
Reviewer Review Type Date Requested Status
Christopher Lee Pending
Review via email: mp+314308@code.launchpad.net

Commit message

jujupy sync_tool now support stream argument.

Description of the change

Update juju-ci-sync-metadata-source code to make use of stream as part of sync_tool.

To post a comment you must log in.
1832. By viswesuwara nathan

Review comments addressed

Unmerged revisions

1832. By viswesuwara nathan

Review comments addressed

1831. By viswesuwara nathan

sync_tool added

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'assess_bootstrap.py'
2--- assess_bootstrap.py 2016-10-30 15:02:38 +0000
3+++ assess_bootstrap.py 2017-01-10 16:27:10 +0000
4@@ -43,19 +43,20 @@
5 log.info('Environment successfully bootstrapped.')
6
7
8-def prepare_metadata(client, local_dir):
9+def prepare_metadata(client, local_dir, agent_stream=None, source=None):
10 """Fill the given directory with metadata using sync_tools."""
11- client.sync_tools(local_dir)
12+ client.sync_tools(local_dir, agent_stream, source)
13
14
15 @contextmanager
16-def prepare_temp_metadata(client, source_dir=None):
17+def prepare_temp_metadata(client, source_dir=None, agent_stream=None,
18+ source=None):
19 """Fill a temporary directory with metadata using sync_tools."""
20 if source_dir is not None:
21 yield source_dir
22 else:
23 with temp_dir() as md_dir:
24- prepare_metadata(client, md_dir)
25+ prepare_metadata(client, md_dir, agent_stream, source)
26 yield md_dir
27
28
29
30=== added file 'assess_sync_agent_metadata.py'
31--- assess_sync_agent_metadata.py 1970-01-01 00:00:00 +0000
32+++ assess_sync_agent_metadata.py 2017-01-10 16:27:10 +0000
33@@ -0,0 +1,130 @@
34+#!/usr/bin/env python
35+"""
36+ Do juju bootstrap with agent-metadata-url option by doing sync-tools first.
37+ On doing bootstrap of controller then deploy dummy charm.
38+ Validation will be done to verify right agent-metadata-url is used during
39+ bootstrap and then deployed dummy charm made use of the controller agent.
40+
41+ Usage: python assess_sync_agent_metadata.py
42+"""
43+
44+from __future__ import print_function
45+
46+import argparse
47+import logging
48+import sys
49+import json
50+
51+from assess_agent_metadata import (
52+ verify_deployed_tool,
53+ get_controller_url_and_sha256,
54+)
55+
56+from assess_bootstrap import (
57+ prepare_temp_metadata,
58+ )
59+
60+from deploy_stack import (
61+ BootstrapManager,
62+ )
63+
64+from remote import (
65+ remote_from_unit,
66+ )
67+
68+from utility import (
69+ add_basic_testing_arguments,
70+ configure_logging,
71+ JujuAssertionError,
72+ )
73+from jujucharm import (
74+ local_charm_path,
75+)
76+
77+__metaclass__ = type
78+
79+
80+log = logging.getLogger("assess_sync_agent_metadata")
81+
82+
83+def verify_deployed_charm(charm_app, client):
84+ """
85+ Verify the deployed charm, to make sure it used the same
86+ juju tool of the controller by verifying the sha256 sum
87+ :param charm_app: The app name that need to be verified
88+ :param client: Juju client
89+ """
90+ remote = remote_from_unit(client, "{0}/0".format(charm_app))
91+ output = remote.cat(
92+ "/var/lib/juju/tools/machine-0/downloaded-tools.txt")
93+
94+ deserialized_output = json.loads(output)
95+ _, controller_sha256 = get_controller_url_and_sha256(client)
96+
97+ if deserialized_output['sha256'] != controller_sha256:
98+ raise JujuAssertionError(
99+ 'agent-metadata-url mismatch. Expected: {} Got: {}'.format(
100+ controller_sha256, deserialized_output))
101+
102+ log.info("Charm verification done successfully")
103+
104+
105+def deploy_charm_and_verify(client):
106+ """
107+ Deploy dummy charm from local repository and
108+ verify it uses the specified agent-metadata-url option
109+ :param client: Juju client
110+ """
111+ charm_app = "dummy-sink"
112+ charm_source = local_charm_path(
113+ charm=charm_app, juju_ver=client.version)
114+ client.deploy(charm_source)
115+ client.wait_for_started()
116+ verify_deployed_charm(charm_app, client)
117+
118+
119+def assess_sync_bootstrap(args, agent_stream):
120+ """
121+ Do sync-tool and then perform juju bootstrap with
122+ metadata_source.
123+ :param args: Parsed command line arguments
124+ :param agent_stream: choice of release or develop
125+ """
126+ bs_manager = BootstrapManager.from_args(args)
127+ client = bs_manager.client
128+ source = client.env.get_option('tools-metadata-url')
129+
130+ with prepare_temp_metadata(
131+ client, args.agent_dir, agent_stream, source) as agent_dir:
132+ client.env.update_config({'agent-stream:': agent_stream})
133+ log.info('Metadata written to: {}'.format(agent_dir))
134+
135+ with bs_manager.booted_context(args.upload_tools,
136+ metadata_source=agent_dir):
137+ log.info('Metadata bootstrap successful.')
138+ verify_deployed_tool(agent_dir, client, agent_stream)
139+ deploy_charm_and_verify(client)
140+
141+
142+def parse_args(argv):
143+ """Parse all arguments."""
144+ parser = argparse.ArgumentParser(
145+ description="Test metadata-source on sync-tool")
146+ parser.add_argument('--agent-dir',
147+ action='store', default=None,
148+ help='tool dir to be used during bootstrap.')
149+ add_basic_testing_arguments(parser)
150+
151+ return parser.parse_args(argv)
152+
153+
154+def main(argv=None):
155+ args = parse_args(argv)
156+ configure_logging(args.verbose)
157+
158+ assess_sync_bootstrap(args, agent_stream="devel")
159+ return 0
160+
161+
162+if __name__ == '__main__':
163+ sys.exit(main())
164
165=== modified file 'jujupy.py'
166--- jujupy.py 2017-01-10 16:27:10 +0000
167+++ jujupy.py 2017-01-10 16:27:10 +0000
168@@ -2846,13 +2846,18 @@
169 """Enable a command-set."""
170 return self.juju('enable-command', args)
171
172- def sync_tools(self, local_dir=None):
173+ def sync_tools(self, local_dir=None, stream=None, source=None):
174 """Copy tools into a local directory or model."""
175+ args = ()
176+ if stream is not None:
177+ args += ('--stream', stream)
178+ if source is not None:
179+ args += ('--source', source)
180 if local_dir is None:
181- return self.juju('sync-tools', ())
182+ return self.juju('sync-tools', args)
183 else:
184- return self.juju('sync-tools', ('--local-dir', local_dir),
185- include_e=False)
186+ args += ('--local-dir', local_dir)
187+ return self.juju('sync-tools', args, include_e=False)
188
189 def switch(self, model=None, controller=None):
190 """Switch between models."""
191
192=== modified file 'tests/test_assess_bootstrap.py'
193--- tests/test_assess_bootstrap.py 2016-11-25 15:12:11 +0000
194+++ tests/test_assess_bootstrap.py 2017-01-10 16:27:10 +0000
195@@ -129,7 +129,28 @@
196 with patch.object(client, 'sync_tools') as sync_mock:
197 with temp_dir() as metadata_dir:
198 prepare_metadata(client, metadata_dir)
199- sync_mock.assert_called_once_with(metadata_dir)
200+ sync_mock.assert_called_once_with(metadata_dir, None, None)
201+
202+ def test_prepare_metadata_with_stream(self):
203+ client = fake_juju_client()
204+ with patch.object(client, 'sync_tools') as sync_mock:
205+ with temp_dir() as metadata_dir:
206+ prepare_metadata(client, metadata_dir, "testing")
207+ sync_mock.assert_called_once_with(metadata_dir, "testing", None)
208+
209+ def test_prepare_metadata_with_source(self):
210+ client = fake_juju_client()
211+ with patch.object(client, 'sync_tools') as sync_mock:
212+ with temp_dir() as metadata_dir:
213+ prepare_metadata(client, metadata_dir, None, "foo")
214+ sync_mock.assert_called_once_with(metadata_dir, None, "foo")
215+
216+ def test_prepare_metadata_with_all_args(self):
217+ client = fake_juju_client()
218+ with patch.object(client, 'sync_tools') as sync_mock:
219+ with temp_dir() as metadata_dir:
220+ prepare_metadata(client, metadata_dir, "bar", "foo")
221+ sync_mock.assert_called_once_with(metadata_dir, "bar", "foo")
222
223 def test_prepare_temp_metadata(self):
224 client = fake_juju_client()
225@@ -137,7 +158,7 @@
226 autospec=True) as prepare_mock:
227 with prepare_temp_metadata(client) as metadata_dir:
228 pass
229- prepare_mock.assert_called_once_with(client, metadata_dir)
230+ prepare_mock.assert_called_once_with(client, metadata_dir, None, None)
231
232 def test_prepare_temp_metadata_source(self):
233 client = fake_juju_client()
234
235=== modified file 'tests/test_jujupy.py'
236--- tests/test_jujupy.py 2017-01-10 16:27:10 +0000
237+++ tests/test_jujupy.py 2017-01-10 16:27:10 +0000
238@@ -3700,6 +3700,14 @@
239 self.assertRaises(ValueError, client.switch)
240
241 =======
242+ def test_sync_tools_stream(self):
243+ client = EnvJujuClient(JujuData('foo'), None, None)
244+ with patch.object(client, 'juju', autospec=True) as mock:
245+ client.sync_tools('/agents', "testing")
246+ mock.assert_called_once_with(
247+ 'sync-tools', ('--stream', 'testing', '--local-dir', '/agents'),
248+ include_e=False)
249+
250 def test_generate_tool(self):
251 client = EnvJujuClient(JujuData('foo'), None, None)
252 with patch.object(client, 'juju', autospec=True) as mock:

Subscribers

People subscribed via source and target branches