From 8d5daf8b30fcd4edd63eebeae89ad9258d0e54bd Mon Sep 17 00:00:00 2001 From: park hei Date: Fri, 14 Nov 2014 15:04:43 +0800 Subject: [PATCH] Share server_password unit test between V2 & V2.1 This patch share unit tests of server_password between V2 & V2.1 Partially implements blueprint v2-on-v3-api Change-Id: I74d67ada017aa43c5221890dd86c204797d870ef --- .../compute/contrib/test_server_password.py | 25 ++++-- .../plugins/v3/test_server_password.py | 80 ------------------- 2 files changed, 17 insertions(+), 88 deletions(-) delete mode 100644 nova/tests/unit/api/openstack/compute/plugins/v3/test_server_password.py diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py b/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py index d29b0480f396..e908bc3eedcd 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py @@ -29,11 +29,11 @@ CONF = cfg.CONF CONF.import_opt('osapi_compute_ext_list', 'nova.api.openstack.compute.contrib') -class ServerPasswordTest(test.TestCase): +class ServerPasswordTestV21(test.TestCase): content_type = 'application/json' def setUp(self): - super(ServerPasswordTest, self).setUp() + super(ServerPasswordTestV21, self).setUp() fakes.stub_out_nw_api(self.stubs) self.stubs.Set( compute.api.API, 'get', @@ -43,6 +43,7 @@ class ServerPasswordTest(test.TestCase): system_metadata={}, expected_attrs=['system_metadata'])) self.password = 'fakepass' + self.fakes_wsgi_app = fakes.wsgi_app_v21 def fake_extract_password(instance): return self.password @@ -53,17 +54,14 @@ class ServerPasswordTest(test.TestCase): self.stubs.Set(password, 'extract_password', fake_extract_password) self.stubs.Set(password, 'convert_password', fake_convert_password) - self.flags( - osapi_compute_extension=[ - 'nova.api.openstack.compute.contrib.select_extensions'], - osapi_compute_ext_list=['Server_password']) def _make_request(self, url, method='GET'): req = webob.Request.blank(url) req.headers['Accept'] = self.content_type req.method = method res = req.get_response( - fakes.wsgi_app(init_only=('servers', 'os-server-password'))) + self.fakes_wsgi_app(init_only=('servers', + 'os-server-password'))) return res def _get_pass(self, body): @@ -86,7 +84,18 @@ class ServerPasswordTest(test.TestCase): self.assertEqual(self._get_pass(res.body), '') -class ServerPasswordXmlTest(ServerPasswordTest): +class ServerPasswordTestV2(ServerPasswordTestV21): + + def setUp(self): + super(ServerPasswordTestV2, self).setUp() + self.flags( + osapi_compute_extension=[ + 'nova.api.openstack.compute.contrib.select_extensions'], + osapi_compute_ext_list=['Server_password']) + self.fakes_wsgi_app = fakes.wsgi_app + + +class ServerPasswordXmlTestV2(ServerPasswordTestV2): content_type = 'application/xml' def _get_pass(self, body): diff --git a/nova/tests/unit/api/openstack/compute/plugins/v3/test_server_password.py b/nova/tests/unit/api/openstack/compute/plugins/v3/test_server_password.py deleted file mode 100644 index 20a8c1e0a120..000000000000 --- a/nova/tests/unit/api/openstack/compute/plugins/v3/test_server_password.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright 2012 Nebula, Inc. -# 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. - -from oslo.config import cfg -from oslo.serialization import jsonutils -import webob - -from nova.api.metadata import password -from nova import compute -from nova import test -from nova.tests.unit.api.openstack import fakes -from nova.tests.unit import fake_instance - - -CONF = cfg.CONF - - -class ServerPasswordTest(test.TestCase): - content_type = 'application/json' - - def setUp(self): - super(ServerPasswordTest, self).setUp() - fakes.stub_out_nw_api(self.stubs) - self.stubs.Set( - compute.api.API, 'get', - lambda self, ctxt, *a, **kw: - fake_instance.fake_instance_obj( - ctxt, - system_metadata={}, - expected_attrs=['system_metadata'])) - self.password = 'fakepass' - - def fake_extract_password(instance): - return self.password - - def fake_convert_password(context, password): - self.password = password - return {} - - self.stubs.Set(password, 'extract_password', fake_extract_password) - self.stubs.Set(password, 'convert_password', fake_convert_password) - - def _make_request(self, url, method='GET'): - req = webob.Request.blank(url) - req.headers['Accept'] = self.content_type - req.method = method - res = req.get_response( - fakes.wsgi_app_v21(init_only=('servers', 'os-server-password'))) - return res - - def _get_pass(self, body): - return jsonutils.loads(body).get('password') - - def test_get_password(self): - url = '/v2/fake/servers/fake/os-server-password' - res = self._make_request(url) - - self.assertEqual(res.status_int, 200) - self.assertEqual(self._get_pass(res.body), 'fakepass') - - def test_reset_password(self): - url = '/v2/fake/servers/fake/os-server-password' - res = self._make_request(url, 'DELETE') - self.assertEqual(res.status_int, 204) - - res = self._make_request(url) - self.assertEqual(res.status_int, 200) - self.assertEqual(self._get_pass(res.body), '')