0b2987fef3
The find_resource method had two hacks in in to support cinder and keystone and I have removed those in favor of a monkey patch for cinder. The find_resource method used to attempt to UUID parse the id, but it would do a manager.get anyway. I changed it to skip the UUID parsing. This will make things run minorly faster and it supports LDAP for keystone. The find_resource used to attempt to use display_name=name_or_id when finding. This was a hack for cinder support, but it breaks keystone because keystone totally messes up with the bogus filter and keystone refuses to fix it. Change-Id: I66e45a6341f704900f1d5321a0e70eac3d051665 Closes-Bug: #1306699
133 lines
5.3 KiB
Python
133 lines
5.3 KiB
Python
# Copyright 2012-2013 OpenStack, LLC.
|
|
#
|
|
# 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 mock
|
|
|
|
from openstackclient.common import exceptions
|
|
from openstackclient.common import utils
|
|
from openstackclient.tests import utils as test_utils
|
|
|
|
PASSWORD = "Pa$$w0rd"
|
|
WASSPORD = "Wa$$p0rd"
|
|
DROWSSAP = "dr0w$$aP"
|
|
|
|
|
|
class TestUtils(test_utils.TestCase):
|
|
|
|
def test_get_password_good(self):
|
|
with mock.patch("getpass.getpass", return_value=PASSWORD):
|
|
mock_stdin = mock.Mock()
|
|
mock_stdin.isatty = mock.Mock()
|
|
mock_stdin.isatty.return_value = True
|
|
self.assertEqual(utils.get_password(mock_stdin), PASSWORD)
|
|
|
|
def test_get_password_bad_once(self):
|
|
answers = [PASSWORD, WASSPORD, DROWSSAP, DROWSSAP]
|
|
with mock.patch("getpass.getpass", side_effect=answers):
|
|
mock_stdin = mock.Mock()
|
|
mock_stdin.isatty = mock.Mock()
|
|
mock_stdin.isatty.return_value = True
|
|
self.assertEqual(utils.get_password(mock_stdin), DROWSSAP)
|
|
|
|
def test_get_password_no_tty(self):
|
|
mock_stdin = mock.Mock()
|
|
mock_stdin.isatty = mock.Mock()
|
|
mock_stdin.isatty.return_value = False
|
|
self.assertRaises(exceptions.CommandError,
|
|
utils.get_password,
|
|
mock_stdin)
|
|
|
|
def test_get_password_cntrl_d(self):
|
|
with mock.patch("getpass.getpass", side_effect=EOFError()):
|
|
mock_stdin = mock.Mock()
|
|
mock_stdin.isatty = mock.Mock()
|
|
mock_stdin.isatty.return_value = True
|
|
self.assertRaises(exceptions.CommandError,
|
|
utils.get_password,
|
|
mock_stdin)
|
|
|
|
|
|
class NoUniqueMatch(Exception):
|
|
pass
|
|
|
|
|
|
class TestFindResource(test_utils.TestCase):
|
|
def setUp(self):
|
|
super(TestFindResource, self).setUp()
|
|
self.name = 'legos'
|
|
self.expected = mock.Mock()
|
|
self.manager = mock.Mock()
|
|
self.manager.resource_class = mock.Mock()
|
|
self.manager.resource_class.__name__ = 'lego'
|
|
|
|
def test_find_resource_get_int(self):
|
|
self.manager.get = mock.Mock(return_value=self.expected)
|
|
result = utils.find_resource(self.manager, 1)
|
|
self.assertEqual(self.expected, result)
|
|
self.manager.get.assert_called_with(1)
|
|
|
|
def test_find_resource_get_int_string(self):
|
|
self.manager.get = mock.Mock(return_value=self.expected)
|
|
result = utils.find_resource(self.manager, "2")
|
|
self.assertEqual(self.expected, result)
|
|
self.manager.get.assert_called_with(2)
|
|
|
|
def test_find_resource_get_uuid(self):
|
|
uuid = '9a0dc2a0-ad0d-11e3-a5e2-0800200c9a66'
|
|
self.manager.get = mock.Mock(return_value=self.expected)
|
|
result = utils.find_resource(self.manager, uuid)
|
|
self.assertEqual(self.expected, result)
|
|
self.manager.get.assert_called_with(uuid)
|
|
|
|
def test_find_resource_get_whatever(self):
|
|
self.manager.get = mock.Mock(return_value=self.expected)
|
|
result = utils.find_resource(self.manager, 'whatever')
|
|
self.assertEqual(self.expected, result)
|
|
self.manager.get.assert_called_with('whatever')
|
|
|
|
def test_find_resource_find(self):
|
|
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
|
|
self.manager.find = mock.Mock(return_value=self.expected)
|
|
result = utils.find_resource(self.manager, self.name)
|
|
self.assertEqual(self.expected, result)
|
|
self.manager.get.assert_called_with(self.name)
|
|
self.manager.find.assert_called_with(name=self.name)
|
|
|
|
def test_find_resource_find_not_found(self):
|
|
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
|
|
self.manager.find = mock.Mock(
|
|
side_effect=exceptions.NotFound(404, "2")
|
|
)
|
|
result = self.assertRaises(exceptions.CommandError,
|
|
utils.find_resource,
|
|
self.manager,
|
|
self.name)
|
|
self.assertEqual("No lego with a name or ID of 'legos' exists.",
|
|
str(result))
|
|
self.manager.get.assert_called_with(self.name)
|
|
self.manager.find.assert_called_with(name=self.name)
|
|
|
|
def test_find_resource_find_no_unique(self):
|
|
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
|
|
self.manager.find = mock.Mock(side_effect=NoUniqueMatch())
|
|
result = self.assertRaises(exceptions.CommandError,
|
|
utils.find_resource,
|
|
self.manager,
|
|
self.name)
|
|
self.assertEqual("More than one lego exists with the name 'legos'.",
|
|
str(result))
|
|
self.manager.get.assert_called_with(self.name)
|
|
self.manager.find.assert_called_with(name=self.name)
|