From 118a83732ae1d6d0748f1713902b370fb7eaedc7 Mon Sep 17 00:00:00 2001
From: Daira Hopwood <david-sarah@jacaranda.org>
Date: Sat, 18 May 2013 02:31:19 +0100
Subject: [PATCH] Move failure handling for create_container to make it more
 testable. refs #1971

Signed-off-by: Daira Hopwood <david-sarah@jacaranda.org>
---
 src/allmydata/scripts/admin.py | 47 +++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 21 deletions(-)

diff --git a/src/allmydata/scripts/admin.py b/src/allmydata/scripts/admin.py
index 7973a72c..5d28138e 100644
--- a/src/allmydata/scripts/admin.py
+++ b/src/allmydata/scripts/admin.py
@@ -64,35 +64,40 @@ for more details.
 def create_container(options):
     from twisted.internet import reactor, defer
 
-    err = options.stderr
-
     d = defer.maybeDeferred(do_create_container, options)
-    def _failed(f):
-        print >>err, "Container creation failed."
-        print >>err, "%s: %s" % (f.value.__class__.__name__, f.value)
-        print >>err
-        return f
-    d.addErrback(_failed)
     d.addCallbacks(lambda ign: os._exit(0), lambda ign: os._exit(1))
     reactor.run()
 
 def do_create_container(options):
+    from twisted.internet import defer
     from allmydata.node import ConfigOnly
     from allmydata.client import Client
 
-    out = options.stderr
-    config = ConfigOnly(options['basedir'])
-    (backend, _) = Client.configure_backend(config)
-
-    d = backend.create_container()
-    def _done(res):
-        if res is False:
-            print >>out, ("It is not necessary to create a container for this backend type (%s)."
-                          % (backend.__class__.__name__,))
-        else:
-            print >>out, "The container was successfully created."
-        print >>out
-    d.addCallback(_done)
+    out = options.stdout
+    err = options.stderr
+
+    d = defer.succeed(None)
+    def _do_create(ign):
+        config = ConfigOnly(options['basedir'])
+        (backend, _) = Client.configure_backend(config)
+
+        d2 = backend.create_container()
+        def _done(res):
+            if res is False:
+                print >>out, ("It is not necessary to create a container for this backend type (%s)."
+                              % (backend.__class__.__name__,))
+            else:
+                print >>out, "The container was successfully created."
+            print >>out
+        d2.addCallback(_done)
+        return d2
+    d.addCallback(_do_create)
+    def _failed(f):
+        print >>err, "Container creation failed."
+        print >>err, "%s: %s" % (f.value.__class__.__name__, f.value)
+        print >>err
+        return f
+    d.addErrback(_failed)
     return d
 
 
-- 
2.45.2