From 8ad2bbf8060f65ec9deeea158b4d0e727001749f Mon Sep 17 00:00:00 2001 From: Eli Qiao Date: Tue, 2 Dec 2014 11:42:08 +0800 Subject: [PATCH] Share admin_password unit test between V2 & V2.1 This patch shares admin_password unit test cases between v2 & v21 Partially implements blueprint v2-on-v3-api Change-Id: I69f55602d2f229c452706bb63c4471380f17d76e --- .../compute/contrib/test_admin_password.py | 79 +++++++++++++++---- .../openstack/compute/test_server_actions.py | 60 -------------- 2 files changed, 64 insertions(+), 75 deletions(-) diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_admin_password.py b/nova/tests/unit/api/openstack/compute/contrib/test_admin_password.py index c014dbdd05..5374196c0c 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_admin_password.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_admin_password.py @@ -18,6 +18,7 @@ import webob from nova.api.openstack.compute.plugins.v3 import admin_password \ as admin_password_v21 +from nova.api.openstack.compute import servers from nova.compute import api as compute_api from nova import exception from nova import test @@ -33,31 +34,37 @@ def fake_set_admin_password(self, context, instance, password=None): class AdminPasswordTestV21(test.NoDBTestCase): + validiation_error = exception.ValidationError def setUp(self): super(AdminPasswordTestV21, self).setUp() self.stubs.Set(compute_api.API, 'set_admin_password', fake_set_admin_password) self.stubs.Set(compute_api.API, 'get', fake_get) - self.controller = admin_password_v21.AdminPasswordController() self.fake_req = fakes.HTTPRequest.blank('') + def _get_action(self): + return admin_password_v21.AdminPasswordController().change_password + + def _check_status(self, expected_status, res, controller_method): + self.assertEqual(expected_status, controller_method.wsgi_code) + def test_change_password(self): body = {'changePassword': {'adminPass': 'test'}} - self.controller.change_password(self.fake_req, '1', body=body) - self.assertEqual(self.controller.change_password.wsgi_code, 202) + res = self._get_action()(self.fake_req, '1', body=body) + self._check_status(202, res, self._get_action()) def test_change_password_empty_string(self): body = {'changePassword': {'adminPass': ''}} - self.controller.change_password(self.fake_req, '1', body=body) - self.assertEqual(self.controller.change_password.wsgi_code, 202) + res = self._get_action()(self.fake_req, '1', body=body) + self._check_status(202, res, self._get_action()) @mock.patch('nova.compute.api.API.set_admin_password', side_effect=NotImplementedError()) def test_change_password_with_non_implement(self, mock_set_admin_password): body = {'changePassword': {'adminPass': 'test'}} self.assertRaises(webob.exc.HTTPNotImplemented, - self.controller.change_password, + self._get_action(), self.fake_req, '1', body=body) @mock.patch('nova.compute.api.API.get', @@ -65,13 +72,13 @@ class AdminPasswordTestV21(test.NoDBTestCase): def test_change_password_with_non_existed_instance(self, mock_get): body = {'changePassword': {'adminPass': 'test'}} self.assertRaises(webob.exc.HTTPNotFound, - self.controller.change_password, + self._get_action(), self.fake_req, '1', body=body) def test_change_password_with_non_string_password(self): body = {'changePassword': {'adminPass': 1234}} - self.assertRaises(exception.ValidationError, - self.controller.change_password, + self.assertRaises(self.validiation_error, + self._get_action(), self.fake_req, '1', body=body) @mock.patch('nova.compute.api.API.set_admin_password', @@ -80,17 +87,59 @@ class AdminPasswordTestV21(test.NoDBTestCase): def test_change_password_failed(self, mock_set_admin_password): body = {'changePassword': {'adminPass': 'test'}} self.assertRaises(webob.exc.HTTPConflict, - self.controller.change_password, + self._get_action(), self.fake_req, '1', body=body) def test_change_password_without_admin_password(self): body = {'changPassword': {}} - self.assertRaises(exception.ValidationError, - self.controller.change_password, + self.assertRaises(self.validiation_error, + self._get_action(), self.fake_req, '1', body=body) def test_change_password_none(self): - body = {'changePassword': None} - self.assertRaises(exception.ValidationError, - self.controller.change_password, + body = {'changePassword': {'adminPass': None}} + self.assertRaises(self.validiation_error, + self._get_action(), self.fake_req, '1', body=body) + + def test_change_password_adminpass_none(self): + body = {'changePassword': None} + self.assertRaises(self.validiation_error, + self._get_action(), + self.fake_req, '1', body=body) + + def test_change_password_bad_request(self): + body = {'changePassword': {'pass': '12345'}} + self.assertRaises(self.validiation_error, + self._get_action(), + self.fake_req, '1', body=body) + + def test_server_change_password_pass_disabled(self): + # run with enable_instance_password disabled to verify adminPass + # is missing from response. See lp bug 921814 + self.flags(enable_instance_password=False) + body = {'changePassword': {'adminPass': '1234pass'}} + res = self._get_action()(self.fake_req, '1', body=body) + self._check_status(202, res, self._get_action()) + + +class AdminPasswordTestV2(AdminPasswordTestV21): + validiation_error = webob.exc.HTTPBadRequest + + def _get_action(self): + class FakeExtManager(object): + def is_loaded(self, ext): + return False + return servers.Controller(ext_mgr=FakeExtManager()).\ + _action_change_password + + def _check_status(self, expected_status, res, controller_method): + self.assertEqual(expected_status, res.status_int) + + def test_change_password_failed(self): + # TODO(eliqiao): need to handle InstancePasswordSetFailed in v2 api + pass + + def test_change_password_adminpass_none(self): + # TODO(eliqiao): need to handle adminpass is None in v2 api + pass diff --git a/nova/tests/unit/api/openstack/compute/test_server_actions.py b/nova/tests/unit/api/openstack/compute/test_server_actions.py index 911e1602f0..686c9cd93b 100644 --- a/nova/tests/unit/api/openstack/compute/test_server_actions.py +++ b/nova/tests/unit/api/openstack/compute/test_server_actions.py @@ -1344,66 +1344,6 @@ class ServerActionsControllerTestV2(ServerActionsControllerTestV21): location = response.headers['Location'] self.assertEqual('https://glancehost/v2/fake/images/123', location) - # TODO(eliqiao): password cases should be move to test_admin_password - def test_server_change_password(self): - mock_method = MockSetAdminPassword() - self.stubs.Set(compute_api.API, 'set_admin_password', mock_method) - body = {'changePassword': {'adminPass': '1234pass'}} - - req = fakes.HTTPRequest.blank(self.url) - self.controller._action_change_password(req, FAKE_UUID, body) - - self.assertEqual(mock_method.instance_id, self.uuid) - self.assertEqual(mock_method.password, '1234pass') - - def test_server_change_password_pass_disabled(self): - # run with enable_instance_password disabled to verify adminPass - # is missing from response. See lp bug 921814 - self.flags(enable_instance_password=False) - - mock_method = MockSetAdminPassword() - self.stubs.Set(compute_api.API, 'set_admin_password', mock_method) - body = {'changePassword': {'adminPass': '1234pass'}} - - req = fakes.HTTPRequest.blank(self.url) - self.controller._action_change_password(req, FAKE_UUID, body) - - self.assertEqual(mock_method.instance_id, self.uuid) - # note,the mock still contains the password. - self.assertEqual(mock_method.password, '1234pass') - - def test_server_change_password_not_a_string(self): - body = {'changePassword': {'adminPass': 1234}} - req = fakes.HTTPRequest.blank(self.url) - self.assertRaises(webob.exc.HTTPBadRequest, - self.controller._action_change_password, - req, FAKE_UUID, body) - - def test_server_change_password_bad_request(self): - body = {'changePassword': {'pass': '12345'}} - req = fakes.HTTPRequest.blank(self.url) - self.assertRaises(webob.exc.HTTPBadRequest, - self.controller._action_change_password, - req, FAKE_UUID, body) - - def test_server_change_password_empty_string(self): - mock_method = MockSetAdminPassword() - self.stubs.Set(compute_api.API, 'set_admin_password', mock_method) - body = {'changePassword': {'adminPass': ''}} - - req = fakes.HTTPRequest.blank(self.url) - self.controller._action_change_password(req, FAKE_UUID, body) - - self.assertEqual(mock_method.instance_id, self.uuid) - self.assertEqual(mock_method.password, '') - - def test_server_change_password_none(self): - body = {'changePassword': {'adminPass': None}} - req = fakes.HTTPRequest.blank(self.url) - self.assertRaises(webob.exc.HTTPBadRequest, - self.controller._action_change_password, - req, FAKE_UUID, body) - def test_rebuild_preserve_ephemeral_is_ignored_when_ext_not_loaded(self): return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE,