From 16e82611d69702969a39111e4fdaa87440b8519b Mon Sep 17 00:00:00 2001
From: Jeff Peeler <jpeeler@redhat.com>
Date: Mon, 27 Aug 2012 02:18:53 -0400
Subject: [PATCH] Functional test improvements

-Move everything out of init so unit tests are not affected.
-Make JEOS creation part of utils class, now tests can choose
which JEOS to use.
-Only attempt to create JEOS if a suitable one is not found in glance.

Change-Id: I162e7447ae1fed17855bdca8eba16ac7f952db01
Signed-off-by: Jeff Peeler <jpeeler@redhat.com>
---
 heat/tests/functional/__init__.py             | 58 -------------------
 .../test_WordPress_Single_Instance.py         |  1 +
 heat/tests/functional/util.py                 | 51 ++++++++++++++++
 3 files changed, 52 insertions(+), 58 deletions(-)

diff --git a/heat/tests/functional/__init__.py b/heat/tests/functional/__init__.py
index 3b92860144..13cd5bb111 100644
--- a/heat/tests/functional/__init__.py
+++ b/heat/tests/functional/__init__.py
@@ -11,61 +11,3 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-
-
-import os
-import subprocess
-import time  # for sleep
-import util as func_utils
-from glance import client as glance_client
-
-
-def setUp(self):
-    if os.geteuid() != 0:
-        print 'test must be run as root'
-        assert False
-
-    if os.environ['OS_AUTH_STRATEGY'] != 'keystone':
-        print 'keystone authentication required'
-        assert False
-
-    prepare_jeos()
-
-
-def prepare_jeos():
-    # verify JEOS and cloud init
-    args = ('F17', 'x86_64', 'cfntools')
-    imagename = '-'.join(str(i) for i in args)
-    creds = dict(username=os.environ['OS_USERNAME'],
-            password=os.environ['OS_PASSWORD'],
-            tenant=os.environ['OS_TENANT_NAME'],
-            auth_url=os.environ['OS_AUTH_URL'],
-            strategy=os.environ['OS_AUTH_STRATEGY'])
-
-    # -d: debug, -G: register with glance
-    subprocess.call(['heat-jeos', '-d', '-G', 'create', imagename])
-
-    gclient = glance_client.Client(host="0.0.0.0", port=9292,
-        use_ssl=False, auth_tok=None, creds=creds)
-
-    # Nose seems to change the behavior of the subprocess call to be
-    # asynchronous. So poll glance until image is registered.
-    imagelistname = None
-    tries = 0
-    while imagelistname != imagename:
-        tries += 1
-        assert tries < 50
-        time.sleep(15)
-        print "Checking glance for image registration"
-        imageslist = gclient.get_images()
-        for x in imageslist:
-            imagelistname = x['name']
-            if imagelistname == imagename:
-                print "Found image registration for %s" % imagename
-                break
-
-    # technically not necessary, but glance registers image before
-    # completely through with its operations
-    time.sleep(10)
-
-# TODO: could do teardown and delete jeos
diff --git a/heat/tests/functional/test_WordPress_Single_Instance.py b/heat/tests/functional/test_WordPress_Single_Instance.py
index 44a7d86088..c14d99a474 100644
--- a/heat/tests/functional/test_WordPress_Single_Instance.py
+++ b/heat/tests/functional/test_WordPress_Single_Instance.py
@@ -25,6 +25,7 @@ def test_template():
 
     func_utils = util.FuncUtils()
 
+    func_utils.prepare_jeos('F17', 'x86_64', 'cfntools')
     func_utils.create_stack(template, 'F17')
     func_utils.check_cfntools()
     func_utils.wait_for_provisioning()
diff --git a/heat/tests/functional/util.py b/heat/tests/functional/util.py
index 20367ec990..81c9dabaa8 100644
--- a/heat/tests/functional/util.py
+++ b/heat/tests/functional/util.py
@@ -30,6 +30,7 @@ from nose.plugins.attrib import attr
 from nose import with_setup
 from nose.exc import SkipTest
 
+from glance import client as glance_client
 from novaclient.v1_1 import client
 from heat import utils
 from heat.engine import parser
@@ -44,6 +45,10 @@ class FuncUtils:
     except KeyError:
         raise SkipTest('OS_AUTH_STRATEGY not set, skipping functional test')
 
+    if os.environ['OS_AUTH_STRATEGY'] != 'keystone':
+        print 'keystone authentication required'
+        assert False
+
     creds = dict(username=os.environ['OS_USERNAME'],
             password=os.environ['OS_PASSWORD'],
             tenant=os.environ['OS_TENANT_NAME'],
@@ -70,6 +75,52 @@ class FuncUtils:
             return self.sftp
         return None
 
+    def prepare_jeos(self, p_os, arch, type):
+        imagename = p_os + '-' + arch + '-' + type
+        creds = dict(username=os.environ['OS_USERNAME'],
+                password=os.environ['OS_PASSWORD'],
+                tenant=os.environ['OS_TENANT_NAME'],
+                auth_url=os.environ['OS_AUTH_URL'],
+                strategy=os.environ['OS_AUTH_STRATEGY'])
+
+        gclient = glance_client.Client(host="0.0.0.0", port=9292,
+            use_ssl=False, auth_tok=None, creds=creds)
+
+        # skip creating jeos if image already available
+        if not self.poll_glance(gclient, imagename, False):
+            if os.geteuid() != 0:
+                print 'test must be run as root to create jeos'
+                assert False
+
+            # -d: debug, -G: register with glance
+            subprocess.call(['heat-jeos', '-d', '-G', 'create', imagename])
+
+            # Nose seems to change the behavior of the subprocess call to be
+            # asynchronous. So poll glance until image is registered.
+            self.poll_glance(gclient, imagename, True)
+
+    def poll_glance(self, gclient, imagename, block):
+        imagelistname = None
+        tries = 0
+        while imagelistname != imagename:
+            tries += 1
+            assert tries < 50
+            if block:
+                time.sleep(15)
+            print "Checking glance for image registration"
+            imageslist = gclient.get_images()
+            for x in imageslist:
+                imagelistname = x['name']
+                if imagelistname == imagename:
+                    print "Found image registration for %s" % imagename
+                    # technically not necessary, but glance registers image
+                    # before completely through with its operations
+                    time.sleep(10)
+                    return True
+            if not block:
+                break
+        return False
+
     def create_stack(self, template_file, distribution):
         nt = client.Client(os.environ['OS_USERNAME'],
             os.environ['OS_PASSWORD'], os.environ['OS_TENANT_NAME'],