Creating a model unit test to test the data() functionality.

* Created factories for instanciating models behind the scenes
* Stubbed out the remote calls to novaclient
This commit is contained in:
Michael Basnight 2012-03-08 21:36:13 -06:00
parent 9a9001da5b
commit 772d40b017
3 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,16 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

View File

@ -0,0 +1,34 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import factory
from reddwarf.common import context
from reddwarf.common import utils
from reddwarf.database import models
class Instance(factory.Factory):
FACTORY_FOR = models.Instance
context = context.ReddwarfContext()
uuid = utils.generate_uuid()
def factory_create(model_to_create, **kwargs):
return model_to_create(**kwargs)
factory.Factory.set_creation_function(factory_create)

View File

@ -0,0 +1,68 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mox
import novaclient
from reddwarf import tests
from reddwarf.common import exception
from reddwarf.common import utils
from reddwarf.database import models
from reddwarf.db import db_query
from reddwarf.tests import unit
from reddwarf.tests.factories import models as factory_models
class TestInstance(tests.BaseTest):
FAKE_SERVER = None
def setUp(self):
super(TestInstance, self).setUp()
def mock_out_client(self):
"""Stubs out a fake server returned from novaclient.
This is akin to calling Client.servers.get(uuid)
and getting the server object back."""
self.FAKE_SERVER = self.mock.CreateMock(object)
self.FAKE_SERVER.name = 'my_name'
self.FAKE_SERVER.status = 'ACTIVE'
self.FAKE_SERVER.updated = utils.utcnow()
self.FAKE_SERVER.id = utils.generate_uuid()
self.FAKE_SERVER.flavor = ('http://localhost/1234/flavors/',
'52415800-8b69-11e0-9b19-734f1195ff37')
client = self.mock.CreateMock(novaclient.v1_1.Client)
servers = self.mock.CreateMock(novaclient.v1_1.servers.ServerManager)
servers.get(mox.IgnoreArg()).AndReturn(self.FAKE_SERVER)
client.servers = servers
self.mock.StubOutWithMock(models.RemoteModelBase, 'get_client')
models.RemoteModelBase.get_client(mox.IgnoreArg()).AndReturn(client)
self.mock.ReplayAll()
def test_create_instance_data(self):
"""This ensures the data() call in a new
Instance object returns the proper mapped data
to a dict from attr's"""
self.mock_out_client()
# Creates the instance via __init__
instance = factory_models.Instance().data()
self.assertEqual(instance['name'], self.FAKE_SERVER.name)
self.assertEqual(instance['status'], self.FAKE_SERVER.status)
self.assertEqual(instance['updated'], self.FAKE_SERVER.updated)
self.assertEqual(instance['id'], self.FAKE_SERVER.id)
self.assertEqual(instance['flavor'], self.FAKE_SERVER.flavor)