Merge lp:~frankban/charms/oneiric/buildbot-slave/tests_again into lp:~yellow/charms/oneiric/buildbot-slave/trunk

Proposed by Francesco Banconi
Status: Merged
Approved by: Graham Binns
Approved revision: 11
Merged at revision: 10
Proposed branch: lp:~frankban/charms/oneiric/buildbot-slave/tests_again
Merge into: lp:~yellow/charms/oneiric/buildbot-slave/trunk
Diff against target: 185 lines (+56/-63)
2 files modified
hooks/install (+2/-2)
tests/buildbot-slave.test (+54/-61)
To merge this branch: bzr merge lp:~frankban/charms/oneiric/buildbot-slave/tests_again
Reviewer Review Type Date Requested Status
Graham Binns (community) code Approve
Review via email: mp+92256@code.launchpad.net

Description of the change

- buildslave charm tests refactoring
- using run function to chmod the script in install hook
- changed symlinks (helpers.py, tests.cfg)

To post a comment you must log in.
Revision history for this message
Graham Binns (gmb) wrote :

82 + wait_for_unit(service_name, timeout=600)

We should probably add an XXX here to remind ourselves to try and either bring this down or do something smarter with it depending on the state of the Juju environment.

review: Approve (code)
12. By Francesco Banconi

Added timeout XXX.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/install'
2--- hooks/install 2012-02-09 10:05:06 +0000
3+++ hooks/install 2012-02-09 12:09:17 +0000
4@@ -10,6 +10,7 @@
5 log,
6 log_entry,
7 log_exit,
8+ run,
9 )
10 import os
11 import shlex
12@@ -70,8 +71,7 @@
13 log('Retrieving script: {}.'.format(url))
14 script = retrieve(url, path)
15 log('Changing script mode.')
16- chmod = command('chmod')
17- chmod('+x', script)
18+ run('chmod', '+x', script)
19 log('Executing script: {}.'.format(script))
20 return subprocess.call([script] + shlex.split(str(args)))
21
22
23=== modified file 'tests/buildbot-slave.test'
24--- tests/buildbot-slave.test 2012-02-08 18:00:09 +0000
25+++ tests/buildbot-slave.test 2012-02-09 12:09:17 +0000
26@@ -12,6 +12,8 @@
27 command,
28 encode_file,
29 unit_info,
30+ wait_for_unit,
31+ wait_for_relation,
32 )
33 from openport import (
34 PORT,
35@@ -19,12 +21,6 @@
36 )
37
38
39-# To run tests, having juju bootstrapped:
40-# JUJU_REPOSITORY=. oneiric/buildbot-slave/tests/buildbot-slave.test
41-# XXX 2012-02-08 gmb:
42-# This needs reducing; it's set high for debugging purposes only.
43-MAX_ATTEMPTS = 600
44-
45 juju = command('juju')
46
47
48@@ -35,10 +31,15 @@
49 self.repository = os.getenv('JUJU_REPOSITORY')
50 self.environment = os.getenv('JUJU_ENVIRONMENT')
51 self.tests_dir = os.path.dirname(os.path.abspath(__file__))
52-
53- def deploy(self, config=None, sleep_time=0.1, charm_name=None):
54- deploy_name = charm_name = charm_name or self.charm_name
55+ self.started_services = set()
56+
57+ def tearDown(self):
58+ for service_name in self.started_services:
59+ juju('destroy-service', service_name)
60+
61+ def deploy(self, service_name, config=None):
62 args = ['deploy']
63+ deploy_name = service_name
64 if self.repository is not None:
65 args.append('--repository=' + self.repository)
66 deploy_name = 'local:' + deploy_name
67@@ -48,24 +49,29 @@
68 args.append('--config=' + config)
69 args.append(deploy_name)
70 juju(*args)
71- status_attempts = 0
72- while status_attempts < MAX_ATTEMPTS:
73- status = unit_info(charm_name, 'state')
74- if 'error' in status:
75- return False
76- if status == 'started':
77- return True
78- time.sleep(sleep_time)
79- status_attempts += 1
80- raise Exception("Charm took too long to deploy.")
81+ self.started_services.add(service_name)
82+ # XXX 2012-02-09 frankban
83+ # We use a timeout of 600 seconds to avoid runtime errors
84+ # occurring if the instance is not created
85+ # (e.g. just after `juju bootstrap`).
86+ # We should try to wait for unit in a smarter way
87+ # (maybe checking for "pending"?).
88+ wait_for_unit(service_name, timeout=600)
89+
90+ def add_relation(self, relation, service1, service2):
91+ juju('add-relation',
92+ '{}:{}'.format(service1, relation),
93+ '{}:{}'.format(service2, relation))
94+ wait_for_relation(service1, relation)
95+
96+ def get_url(self, service_name, port, path=''):
97+ address = unit_info(service_name, 'public-address')
98+ return 'http://{}:{}{}'.format(address, port, path)
99
100 def test_deploy(self):
101 # Ensure the charm starts correctly when deployed.
102- try:
103- started = self.deploy()
104- self.assertTrue(started)
105- finally:
106- juju('destroy-service', self.charm_name)
107+ self.deploy(self.charm_name)
108+ self.assertEqual('started', unit_info(self.charm_name, 'state'))
109
110 def do_not_test_script(self):
111 # DISABLED BECAUSE IT FAILS ALL THE TIME.
112@@ -87,44 +93,31 @@
113
114 def test_master_slave_relationship(self):
115 master_charm_name = 'buildbot-master'
116+ self.deploy(master_charm_name)
117+ self.deploy(self.charm_name)
118+ juju('set', master_charm_name, 'extra-packages=git')
119+ config_path = os.path.join(os.path.dirname(__file__), 'test.cfg')
120+ config = encode_file(config_path)
121+ juju('set', 'buildbot-master', 'config-file='+config)
122+ juju('expose', master_charm_name)
123+ juju('set', self.charm_name, 'builders=runtests')
124+ # XXX 2012-02-08 gmb
125+ # This avoids us trying to check whether the slave
126+ # is connected until the relation is initialized,
127+ # but... (see next XXX)
128+ self.add_relation('buildbot', self.charm_name, master_charm_name)
129+ # XXX 2012-02-08 gmb:
130+ # This still sometimes gets called _before_ the
131+ # buildslave connects to the buildbot master. Hence
132+ # this rather ugly sleep here.
133+ time.sleep(5)
134+ url = self.get_url(master_charm_name, 8010, '/buildslaves')
135 try:
136- self.deploy(charm_name=master_charm_name)
137- self.deploy()
138- juju('set', master_charm_name, 'extra-packages=git')
139- encoded_config = encode_file(
140- os.path.join(self.tests_dir, '..', 'examples', 'master.cfg'))
141- juju(
142- 'set', master_charm_name,
143- 'config-file={}'.format(encoded_config))
144- juju('expose', master_charm_name)
145- juju('set', self.charm_name, 'builders=runtests')
146- juju('add-relation', self.charm_name, master_charm_name)
147- address = unit_info(master_charm_name, 'public-address')
148- url = 'http://{}:{}/buildslaves'.format(address, 8010)
149- # Wait for buildbot to restart, since there's a
150- # potential race condition with an asynchronous service.
151- while True:
152- # XXX 2012-02-08 gmb
153- # This avoids us trying to check whether the slave
154- # is connected until the relation is initialized,
155- # but... (see next XXX)
156- relation_info = unit_info(self.charm_name, 'relations')
157- if relation_info['buildbot']['state'] == 'up':
158- break
159- time.sleep(0.1)
160- try:
161- # XXX 2012-02-08 gmb:
162- # This still sometimes gets called _before_ the
163- # buildslave connects to the buildbot master. Hence
164- # this rather ugly sleep here.
165- time.sleep(5)
166- stream = urllib2.urlopen(url)
167- except urllib2.HTTPError:
168- self.fail('Unable to connect to {}.'.format(url))
169- self.assertIn('Idle', stream.read())
170- finally:
171- juju('destroy-service', self.charm_name)
172- juju('destroy-service', master_charm_name)
173+ stream = urllib2.urlopen(url)
174+ except urllib2.HTTPError:
175+ self.fail('Unable to connect to {}.'.format(url))
176+ self.assertIn('Idle', stream.read())
177+
178
179 if __name__ == '__main__':
180 unittest.main()
181
182=== modified symlink 'tests/helpers.py'
183=== target changed u'../../buildbot-master/hooks/helpers.py' => u'../hooks/helpers.py'
184=== added symlink 'tests/test.cfg'
185=== target is u'../../buildbot-master/tests/test.cfg'

Subscribers

People subscribed via source and target branches