Skip to content

Instantly share code, notes, and snippets.

@cgibson
Created October 21, 2012 02:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgibson/3925499 to your computer and use it in GitHub Desktop.
Save cgibson/3925499 to your computer and use it in GitHub Desktop.
Run fabric commands in unittests using a function wrapper
import unittest
from fabric.api import env, execute, run, task
from functools import wraps
#
# Fabric command wrapper. Essentially, this surrounds any specified command
# call with the execute() function. Adding this function decoration to unit-
# tests allows us to avoid using execute() functions within our tests.
#
def execute_wrap(f):
@wraps(f)
def wrapper(*args, **kwds):
return execute( f, *args, **kwds )
return wrapper
# Regular fabric task, runnable via fabfile or our unittest
#
@task
def testTask():
run("echo 'running task'")
#
# Example test that should illustrate how our execute_wrap decorator works
#
class TestFabricWrapper(unittest.TestCase):
def setUp(self):
# This example connects to localhost, but you can change this to
# whatever host you would like
#
env.hosts = ["localhost"]
# Sample test that will run a fabric command to illustrate that the wrapper
# works
#
@execute_wrap
def test_executeFabricCommands(self):
run("echo 'Hello, Fabric!'")
testTask()
if __name__ == '__main__':
unittest.main()
import unittest
import vagrant
from fabric.api import env, execute, run, task
from functools import wraps
#
# Fabric command wrapper. Essentially, this surrounds any specified command
# call with the execute() function. Adding this function decoration to unit-
# tests allows us to avoid using execute() functions within our tests.
#
def execute_wrap(f):
@wraps(f)
def wrapper(*args, **kwds):
return execute( f, *args, **kwds )
return wrapper
# Regular fabric task, runnable via fabfile or our unittest
#
@task
def testTask():
run("echo 'running task'")
#
# Example test that should illustrate how our execute_wrap decorator works
#
class TestFabricWrapper(unittest.TestCase):
def setUp(self):
# Use the Vagrantfile in this directory to bring up a vagrant VM
# and grab the host/port information so that we can establish an
# ssh connection
#
self.v = vagrant.Vagrant()
self.v.up()
self.host = self.v.user_hostname_port()
# Using the host information, let fabric know where to connect
# when it begins running test commands
#
env.hosts = [self.host]
env.key_filename = self.v.keyfile()
env.disable_known_hosts = True
# Sample test that will run a fabric command to illustrate that the wrapper
# works
#
@execute_wrap
def test_executeFabricCommands(self):
run("echo 'Hello, Fabric!'")
testTask()
if __name__ == '__main__':
unittest.main()
@cgibson
Copy link
Author

cgibson commented Oct 21, 2012

Added a version that didn't have a dependency on vagrant. Easier to test

@gregorynicholas
Copy link

@cgibson this works by simply bubbling return_codes from tasks to the unittest module ?

do you have an example set of commands you used for the vagrant dependency setup ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment