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,

        insecure=insecure)

    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,

                  self.client.list_routers()['routers'])

......

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')
    clients.neutronclient.Client.show_network(
        'aaaa-netid'
    ).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):
    self.mock_keystone()
    self.mock_create_gateway_attachment()
    self.mock_show_network()
    ....

    self.m.ReplayAll()

    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

Comments

oops comment by mistake

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