← Back to team overview

yellow team mailing list archive

[Merge] lp:~frankban/charms/oneiric/buildbot-master/tests-fixes-again into lp:~yellow/charms/oneiric/buildbot-master/trunk

 

Francesco Banconi has proposed merging lp:~frankban/charms/oneiric/buildbot-master/tests-fixes-again into lp:~yellow/charms/oneiric/buildbot-master/trunk.

Requested reviews:
  Launchpad Yellow Squad (yellow)

For more details, see:
https://code.launchpad.net/~frankban/charms/oneiric/buildbot-master/tests-fixes-again/+merge/92255

- Fixed multiple definition of unit_info function in helpers.
- Added wait_for_relation.
- Some import fixes.
-- 
https://code.launchpad.net/~frankban/charms/oneiric/buildbot-master/tests-fixes-again/+merge/92255
Your team Launchpad Yellow Squad is requested to review the proposed merge of lp:~frankban/charms/oneiric/buildbot-master/tests-fixes-again into lp:~yellow/charms/oneiric/buildbot-master/trunk.
=== modified file 'hooks/helpers.py'
--- hooks/helpers.py	2012-02-08 21:41:35 +0000
+++ hooks/helpers.py	2012-02-09 11:49:19 +0000
@@ -26,7 +26,6 @@
 import json
 import os
 import re
-import StringIO
 import subprocess
 import sys
 import time
@@ -103,23 +102,6 @@
     return cmd(*args)
 
 
-def unit_info(service_name, item_name, data=None):
-    if data is None:
-        output = subprocess.check_output(['juju', 'status'], shell=False)
-        data = yaml.safe_load(output)
-    service = data['services'].get(service_name)
-    if service is None:
-        # XXX 2012-02-08 gmb:
-        #     This allows us to cope with the race condition that we
-        #     have between deploying a service and having it come up in
-        #     `juju status`. We could probably do with cleaning it up so
-        #     that it fails a bit more noisily after a while.
-        return ''
-    units = service['units']
-    item = units.items()[0][1][item_name]
-    return item
-
-
 def encode_file(filename):
     """base64 encode the contents of a file and return it."""
     with open(filename) as f:
@@ -220,8 +202,15 @@
 def unit_info(service_name, item_name, data=None):
     if data is None:
         data = yaml.safe_load(run('juju', 'status'))
-    services = data['services'][service_name]
-    units = services['units']
+    service = data['services'].get(service_name)
+    if service is None:
+        # XXX 2012-02-08 gmb:
+        #     This allows us to cope with the race condition that we
+        #     have between deploying a service and having it come up in
+        #     `juju status`. We could probably do with cleaning it up so
+        #     that it fails a bit more noisily after a while.
+        return ''
+    units = service['units']
     item = units.items()[0][1][item_name]
     return item
 
@@ -238,6 +227,18 @@
     if state != 'started':
         raise RuntimeError('unit did not start, state: ' + state)
 
+
+def wait_for_relation(service_name, relation_name, timeout=120):
+    start_time = time.time()
+    while True:
+        relations = unit_info(service_name, 'relations')
+        if relations[relation_name]['state'] == 'up':
+            break
+        if time.time() - start_time >= timeout:
+            raise RuntimeError('timeout waiting for relation to be up')
+        time.sleep(0.1)
+
+
 class Serializer:
     """Handle JSON (de)serialization."""
 


Follow ups