Openstack unit tests and mock

asked 2014-09-14 01:50:13 -0500

dubi gravatar image

I see examples of using mock but I am not sure I get how results of mock fabricated data can be fed into an exiting unit tested openstack code through its input channels that inquires nova data by novaclient or likewise by cinderclient

example of existing real code:

from neutronclient.v2_0 import client as neutronclient

class NeutronManager(object):

def __init__(self, username, password, project, auth_url, insecure):

    self.client = neutronclient.Client(

        username=username, password=password,

        tenant_name=project, auth_url=auth_url,


    keystone_mgr = KeystoneManager(username, password, project,

                                   auth_url, insecure)

    self.project_id = keystone_mgr.get_project_id()

def router_list(self):

    return filter(self._owned_resource,



neutron_manager = managers.NeutronManager self.neutron = self.neutron_manager(*args) self.subnets = self.neutron.subnet_list() self.networks = self.neutron.network_list()

and test code like :

@skipIf(clients.neutronclient is None, 'neutronclient unavailable')
def setUp(self):
    super(AllocTest, self).setUp()
    self.fc = fakes.FakeClient()
    self.m.StubOutWithMock(clients.neutronclient.Client, 'show_network')

  def mock_show_network(self):
    vpc_name = utils.PhysName('test_stack', 'the_vpc')
    ).AndReturn({"network": {
        "status": "BUILD",
        "subnets": [],
        "name": vpc_name,
        "admin_state_up": False,
        "shared": False,
        "tenant_id": "c1210485b2424d48804aad5d39c61b8f",
        "id": "aaaa-netid"

and then the test code usage usage :

   def test_association_allocationid(self):


    t = template_format.parse(eip_template_ipassoc2)
    stack = utils.parse_stack(t)

    rsrc = self.create_eip(t, stack, 'the_eip')
    association = self.create_association(t, stack, 'IPAssoc')

edit retag flag offensive close merge delete


oops comment by mistake

dubi gravatar imagedubi ( 2014-09-16 01:22:32 -0500 )edit