Created
February 4, 2011 19:29
-
-
Save novel/811604 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import libcloud.providers | |
import libcloud.types | |
from secret import USERNAME, PASSWORD | |
class Example(object): | |
def __init__(self): | |
driver = libcloud.providers.get_driver( | |
libcloud.types.Provider.RACKSPACE | |
) | |
self.connection = driver(USERNAME, PASSWORD) | |
def node_names(self): | |
return [node.name for node in self.connection.list_nodes()] | |
if __name__ == "__main__": | |
example = Example() | |
print example.node_names() |
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
#!/usr/bin/env python | |
import unittest | |
import mox | |
import libcloud.providers | |
from libcloud.drivers.rackspace import RackspaceNodeDriver | |
from example import Example | |
class TestExample(unittest.TestCase): | |
def setUp(self): | |
self.mox = mox.Mox() | |
def tearDown(self): | |
self.mox.UnsetStubs() | |
def test_that_node_names_calls_list_nodes(self): | |
mocked_driver = self.mox.CreateMock(RackspaceNodeDriver) | |
mocked_driver.list_nodes().AndReturn([ | |
type("Node", (), {"name": "node1"}), | |
type("Node", (), {"name": "node2"}) | |
]) | |
self.mox.StubOutWithMock(libcloud, "providers", | |
use_mock_anything=True) | |
libcloud.providers.get_driver(mox.IgnoreArg()).AndReturn( | |
lambda *args, **kwargs: mocked_driver) | |
self.mox.ReplayAll() | |
example = Example() | |
node_names = example.node_names() | |
self.assertEqual(set(node_names), set(["node1", "node2"])) | |
self.mox.VerifyAll() | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment