From: Steven Hardy Date: Tue, 4 Sep 2012 16:18:37 +0000 (+0100) Subject: heat tests : Add verify_wordpress helper function X-Git-Tag: 2014.1~1484 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=94493237040f957ad2bd4186886d46309c2de88d;p=openstack-build%2Fheat-build.git heat tests : Add verify_wordpress helper function Functional tests : Add a new VerifyStack class and retry decorator, which allows easy implementation of tests for stack successful creation. Initial simple verify_wordpress function scrapes a wordpress homepage and looks for a known string in the html result. Change-Id: I32bd4bea04228c4f393894c65d040d55c65ddbca Signed-off-by: Steven Hardy --- diff --git a/heat/tests/functional/verify.py b/heat/tests/functional/verify.py new file mode 100644 index 00000000..f44b2288 --- /dev/null +++ b/heat/tests/functional/verify.py @@ -0,0 +1,77 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 urllib2 +import re +import time + +# Default retry timeout (seconds) and sleep-time +VERIFY_TIMEOUT = 300 +SLEEP_INTERVAL = 10 + + +class VerifyRetry(object): + def __init__(self, timeout=VERIFY_TIMEOUT, interval=SLEEP_INTERVAL): + """ + Decorator to wrap verify operations to retry until timeout + """ + self.timeout = timeout + self.interval = interval + + def __call__(self, f): + def fn(*args, **kwargs): + elapsed = 0 + result = False + while elapsed < self.timeout and not result: + result = f(*args, **kwargs) + if not result: + print "Failed verify, sleeping for %ss (%s/%s)" %\ + (self.interval, elapsed, self.timeout) + time.sleep(self.interval) + elapsed += self.interval + + return result + + return fn + + +class VerifyStack: + ''' + Class containing helper-functions to prove a stack resource or service + has been created correctly, e.g by accessing the service and checking + the result is as expected + ''' + + @VerifyRetry() + def verify_wordpress(self, url, timeout=VERIFY_TIMEOUT): + ''' + Verify the url provided has a functional wordpress installation + for now we simply scrape the page and do a regex for an expected + string + ''' + WORDPRESS_REGEX = "

Welcome to the famous five minute WordPress" + + print "Reading html from %s" % url + try: + content = urllib2.urlopen(url).read() + except: + return False + + matches = re.findall(WORDPRESS_REGEX, content) + if len(matches): + print "VERIFY WORDPRESS : looks OK!" + return True + else: + return False