Created
October 21, 2012 02:28
-
-
Save cgibson/3925499 to your computer and use it in GitHub Desktop.
Run fabric commands in unittests using a function wrapper
This file contains 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
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() |
This file contains 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
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 this works by simply bubbling return_code
s 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
Added a version that didn't have a dependency on vagrant. Easier to test