|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +import mock |
| 15 | + |
| 16 | +from openstackclient.common import exceptions |
| 17 | +from openstackclient.network import common |
| 18 | +from openstackclient.tests import utils |
| 19 | + |
| 20 | +RESOURCE = 'resource' |
| 21 | +RESOURCES = 'resources' |
| 22 | +NAME = 'matrix' |
| 23 | +ID = 'Fishburne' |
| 24 | + |
| 25 | + |
| 26 | +class TestFind(utils.TestCase): |
| 27 | + def setUp(self): |
| 28 | + super(TestFind, self).setUp() |
| 29 | + self.mock_client = mock.Mock() |
| 30 | + self.list_resources = mock.Mock() |
| 31 | + self.mock_client.list_resources = self.list_resources |
| 32 | + self.matrix = {'id': ID} |
| 33 | + |
| 34 | + def test_name(self): |
| 35 | + self.list_resources.return_value = {RESOURCES: [self.matrix]} |
| 36 | + |
| 37 | + result = common.find(self.mock_client, RESOURCE, RESOURCES, NAME) |
| 38 | + |
| 39 | + self.assertEqual(ID, result) |
| 40 | + self.list_resources.assert_called_with(fields='id', name=NAME) |
| 41 | + |
| 42 | + def test_id(self): |
| 43 | + self.list_resources.side_effect = [{RESOURCES: []}, |
| 44 | + {RESOURCES: [self.matrix]}] |
| 45 | + |
| 46 | + result = common.find(self.mock_client, RESOURCE, RESOURCES, NAME) |
| 47 | + |
| 48 | + self.assertEqual(ID, result) |
| 49 | + self.list_resources.assert_called_with(fields='id', id=NAME) |
| 50 | + |
| 51 | + def test_nameo(self): |
| 52 | + self.list_resources.return_value = {RESOURCES: [self.matrix]} |
| 53 | + |
| 54 | + result = common.find(self.mock_client, RESOURCE, RESOURCES, NAME, |
| 55 | + name_attr='nameo') |
| 56 | + |
| 57 | + self.assertEqual(ID, result) |
| 58 | + self.list_resources.assert_called_with(fields='id', nameo=NAME) |
| 59 | + |
| 60 | + def test_dups(self): |
| 61 | + dup = {'id': 'Larry'} |
| 62 | + self.list_resources.return_value = {RESOURCES: [self.matrix, dup]} |
| 63 | + |
| 64 | + self.assertRaises(exceptions.CommandError, common.find, |
| 65 | + self.mock_client, RESOURCE, RESOURCES, NAME) |
| 66 | + |
| 67 | + def test_nada(self): |
| 68 | + self.list_resources.side_effect = [{RESOURCES: []}, |
| 69 | + {RESOURCES: []}] |
| 70 | + |
| 71 | + self.assertRaises(exceptions.CommandError, common.find, |
| 72 | + self.mock_client, RESOURCE, RESOURCES, NAME) |
0 commit comments