Add to_dict() method to Resource class

Commit a07b8b0b introduces a regression when running 'heat
resource-show' command because heatclient.v1.resources.Resource class
didn't implement the to_dict() method.

This commit adds unit tests for resource show and list commands. It
also removes the use of MagicMock in these tests because it introduced
a recursion issue (see https://review.openstack.org/#/c/65269/).

Change-Id: I6c4c0e293d2955c67a0240136faa2721ef2e4ffa
Closes-Bug: 1266678
This commit is contained in:
Simon Pasquier
2014-01-07 13:48:27 +01:00
parent 2d3ea2e70a
commit d19174be97
3 changed files with 126 additions and 6 deletions

View File

@@ -14,10 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from heatclient.v1.resources import Resource
from heatclient.v1.resources import ResourceManager
from mock import MagicMock
from mox3 import mox
import testtools
@@ -44,8 +42,6 @@ class ResourceManagerTest(testtools.TestCase):
return {}, {key: ret}
manager = ResourceManager(FakeAPI())
Resource.__init__ = MagicMock()
Resource.__init__.return_value = None
self.m.StubOutWithMock(manager, '_resolve_stack_id')
manager._resolve_stack_id('teststack').AndReturn('teststack/abcd1234')
self.m.ReplayAll()
@@ -89,8 +85,6 @@ class ResourceManagerTest(testtools.TestCase):
return FakeResponse()
manager = ResourceManager(FakeClient())
Resource.__init__ = MagicMock()
Resource.__init__.return_value = None
self.m.StubOutWithMock(manager, '_resolve_stack_id')
manager._resolve_stack_id('teststack').AndReturn('teststack/abcd1234')
self.m.ReplayAll()

View File

@@ -853,6 +853,127 @@ class ShellTestEvents(ShellBase):
self.assertRegexpMatches(event_list_text, r)
class ShellTestResources(ShellBase):
def setUp(self):
super(ShellTestResources, self).setUp()
self._set_fake_env()
# Patch os.environ to avoid required auth info.
def _set_fake_env(self):
fake_env = {
'OS_USERNAME': 'username',
'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': 'http://no.where',
}
self.set_fake_env(fake_env)
def _script_keystone_client(self):
fakes.script_keystone_client()
def test_resource_list(self):
self._script_keystone_client()
resp_dict = {"resources": [
{"links": [{"href": "http://heat.example.com:8004/foo",
"rel": "self"},
{"href": "http://heat.example.com:8004/foo2",
"rel": "resource"}],
"logical_resource_id": "aResource",
"physical_resource_id":
"43b68bae-ed5d-4aed-a99f-0b3d39c2418a",
"resource_name": "aResource",
"resource_status": "CREATE_COMPLETE",
"resource_status_reason": "state changed",
"resource_type": "OS::Nova::Server",
"updated_time": "2014-01-06T16:14:26Z"}]}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
stack_id = 'teststack/1'
http.HTTPClient.json_request(
'GET', '/stacks/%s/resources' % (
stack_id)).AndReturn((resp, resp_dict))
self.m.ReplayAll()
resource_list_text = self.shell('resource-list {0}'.format(stack_id))
required = [
'resource_name',
'resource_type',
'resource_status',
'updated_time',
'aResource',
'OS::Nova::Server',
'CREATE_COMPLETE',
'2014-01-06T16:14:26Z'
]
for r in required:
self.assertRegexpMatches(resource_list_text, r)
def test_resource_show(self):
self._script_keystone_client()
resp_dict = {"resource":
{"description": "",
"links": [{"href": "http://heat.example.com:8004/foo",
"rel": "self"},
{"href": "http://heat.example.com:8004/foo2",
"rel": "resource"}],
"logical_resource_id": "aResource",
"physical_resource_id":
"43b68bae-ed5d-4aed-a99f-0b3d39c2418a",
"required_by": [],
"resource_name": "aResource",
"resource_status": "CREATE_COMPLETE",
"resource_status_reason": "state changed",
"resource_type": "OS::Nova::Server",
"updated_time": "2014-01-06T16:14:26Z"}}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
stack_id = 'teststack/1'
resource_name = 'aResource'
http.HTTPClient.json_request(
'GET', '/stacks/%s/resources/%s' %
(
urlutils.quote(stack_id, ''),
urlutils.quote(strutils.safe_encode(
resource_name), '')
)).AndReturn((resp, resp_dict))
self.m.ReplayAll()
resource_show_text = self.shell('resource-show {0} {1}'.format(
stack_id, resource_name))
required = [
'description',
'links',
'http://heat.example.com:8004/foo[0-9]',
'logical_resource_id',
'aResource',
'physical_resource_id',
'43b68bae-ed5d-4aed-a99f-0b3d39c2418a',
'required_by',
'resource_name',
'aResource',
'resource_status',
'CREATE_COMPLETE',
'resource_status_reason',
'state changed',
'resource_type',
'OS::Nova::Server',
'updated_time',
'2014-01-06T16:14:26Z',
]
for r in required:
self.assertRegexpMatches(resource_show_text, r)
class ShellTestToken(ShellTestUserPass):
# Rerun all ShellTestUserPass test with token auth

View File

@@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
from heatclient.openstack.common.apiclient import base
from heatclient.openstack.common.py3kcompat import urlutils
from heatclient.openstack.common import strutils
@@ -34,6 +36,9 @@ class Resource(base.Resource):
def data(self, **kwargs):
return self.manager.data(self, **kwargs)
def to_dict(self):
return copy.deepcopy(self._info)
class ResourceManager(stacks.StackChildManager):
resource_class = Resource