Created
October 1, 2012 11:26
-
-
Save jupp0r/3811071 to your computer and use it in GitHub Desktop.
Fabric expectiations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# author: Jupp Mueller <[email protected]> | |
from fabric.tasks import Taskfrom fabric.state import env | |
import fabric.api | |
from fabric.colors import * | |
import shortuuid | |
from StringIO import StringIO | |
import os | |
import reclass ExpectTest(Task): | |
def __init__(self, alias=None, aliases=None, default=False, | |
*args, **kwargs): | |
super(ExpectTest, self).__init__(alias, aliases, default, args, kwargs) self.name = self.__class__.__name__ | |
def expecting(self, *e): return ExpectationContext(self,e)class ExpectationContext(object): | |
def __init__(self, test, expectations): | |
self.test = test self.expectations = expectations | |
def __enter__(self): | |
env.expectations = self.expectations | |
env.test = self.test def __exit__(self, type, value, tb): | |
env.expectations = [] | |
def run(cmd): | |
#cmd wrapper | |
return check_expectations(fabric.api.run(cmd), env.expectations, env.test) | |
def sudo(cmd): | |
#sudo wrapper | |
return check_expectations(fabric.api.sudo(cmd), env.expectations, env.test) | |
def check_expectations(output, expectations, test): | |
matched = False | |
failed_expectations = [] | |
matched_expectations = [] | |
for expectation_list in expectations: | |
matched_group = True | |
for expectation in expectation_list: | |
regex_match = re.search(expectation, output) | |
if(regex_match): | |
matched_expectations.append(expectation) | |
else: | |
failed_expectations.append(expectation) | |
matched_group = False | |
matched = matched or matched_group | |
if(matched): | |
print green("Expectation in " + test.name + " passed\n") | |
else: | |
print red("Expectation in " + test.name + | |
" failed the following expectations:") | |
for failed_expectation in failed_expectations: | |
print red(failed_expectation) | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment