|
| 1 | +# Copyright 2016 EasyStack Corporation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +from openstackclient.common import exceptions |
| 17 | +from openstackclient.compute.v2 import hypervisor |
| 18 | +from openstackclient.tests.compute.v2 import fakes as compute_fakes |
| 19 | + |
| 20 | + |
| 21 | +class TestHypervisor(compute_fakes.TestComputev2): |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + super(TestHypervisor, self).setUp() |
| 25 | + |
| 26 | + # Get a shortcut to the compute client hypervisors mock |
| 27 | + self.hypervisors_mock = self.app.client_manager.compute.hypervisors |
| 28 | + self.hypervisors_mock.reset_mock() |
| 29 | + |
| 30 | + |
| 31 | +class TestHypervisorList(TestHypervisor): |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + super(TestHypervisorList, self).setUp() |
| 35 | + |
| 36 | + # Fake hypervisors to be listed up |
| 37 | + self.hypervisors = compute_fakes.FakeHypervisor.create_hypervisors() |
| 38 | + self.hypervisors_mock.list.return_value = self.hypervisors |
| 39 | + |
| 40 | + self.columns = ( |
| 41 | + "ID", |
| 42 | + "Hypervisor Hostname" |
| 43 | + ) |
| 44 | + self.data = ( |
| 45 | + ( |
| 46 | + self.hypervisors[0].id, |
| 47 | + self.hypervisors[0].hypervisor_hostname, |
| 48 | + ), |
| 49 | + ( |
| 50 | + self.hypervisors[1].id, |
| 51 | + self.hypervisors[1].hypervisor_hostname, |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + # Get the command object to test |
| 56 | + self.cmd = hypervisor.ListHypervisor(self.app, None) |
| 57 | + |
| 58 | + def test_hypervisor_list_no_option(self): |
| 59 | + arglist = [] |
| 60 | + verifylist = [] |
| 61 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 62 | + |
| 63 | + # In base command class Lister in cliff, abstractmethod take_action() |
| 64 | + # returns a tuple containing the column names and an iterable |
| 65 | + # containing the data to be listed. |
| 66 | + columns, data = self.cmd.take_action(parsed_args) |
| 67 | + |
| 68 | + self.hypervisors_mock.list.assert_called_with() |
| 69 | + self.assertEqual(self.columns, columns) |
| 70 | + self.assertEqual(self.data, tuple(data)) |
| 71 | + |
| 72 | + def test_hypervisor_list_matching_option_found(self): |
| 73 | + arglist = [ |
| 74 | + '--matching', self.hypervisors[0].hypervisor_hostname, |
| 75 | + ] |
| 76 | + verifylist = [ |
| 77 | + ('matching', self.hypervisors[0].hypervisor_hostname), |
| 78 | + ] |
| 79 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 80 | + |
| 81 | + # Fake the return value of search() |
| 82 | + self.hypervisors_mock.search.return_value = [self.hypervisors[0]] |
| 83 | + self.data = ( |
| 84 | + ( |
| 85 | + self.hypervisors[0].id, |
| 86 | + self.hypervisors[0].hypervisor_hostname, |
| 87 | + ), |
| 88 | + ) |
| 89 | + |
| 90 | + # In base command class Lister in cliff, abstractmethod take_action() |
| 91 | + # returns a tuple containing the column names and an iterable |
| 92 | + # containing the data to be listed. |
| 93 | + columns, data = self.cmd.take_action(parsed_args) |
| 94 | + |
| 95 | + self.hypervisors_mock.search.assert_called_with( |
| 96 | + self.hypervisors[0].hypervisor_hostname |
| 97 | + ) |
| 98 | + self.assertEqual(self.columns, columns) |
| 99 | + self.assertEqual(self.data, tuple(data)) |
| 100 | + |
| 101 | + def test_hypervisor_list_matching_option_not_found(self): |
| 102 | + arglist = [ |
| 103 | + '--matching', 'xxx', |
| 104 | + ] |
| 105 | + verifylist = [ |
| 106 | + ('matching', 'xxx'), |
| 107 | + ] |
| 108 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 109 | + |
| 110 | + # Fake exception raised from search() |
| 111 | + self.hypervisors_mock.search.side_effect = exceptions.NotFound(None) |
| 112 | + |
| 113 | + self.assertRaises(exceptions.NotFound, |
| 114 | + self.cmd.take_action, |
| 115 | + parsed_args) |
0 commit comments