From fc708c4991c6191ca684dca194420a93fdc4a35e Mon Sep 17 00:00:00 2001 From: Tang Chen Date: Sun, 7 Feb 2016 02:38:09 +0800 Subject: [PATCH] Add unit tests for "hypervisor show" command Change-Id: Ib75e5eb5b197e9d58fb87a595a43b8774b7b1987 --- openstackclient/tests/compute/v2/fakes.py | 26 +++++ .../tests/compute/v2/test_hypervisor.py | 105 ++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index 1a876a229b..ae6d9f5d16 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -88,6 +88,8 @@ SERVICE = { class FakeComputev2Client(object): def __init__(self, **kwargs): + self.aggregates = mock.Mock() + self.aggregates.resource_class = fakes.FakeResource(None, {}) self.availability_zones = mock.Mock() self.availability_zones.resource_class = fakes.FakeResource(None, {}) self.images = mock.Mock() @@ -158,6 +160,30 @@ class FakeHypervisor(object): hypervisor_info = { 'id': 'hypervisor-id-' + uuid.uuid4().hex, 'hypervisor_hostname': 'hypervisor-hostname-' + uuid.uuid4().hex, + 'status': 'enabled', + 'host_ip': '192.168.0.10', + 'cpu_info': { + 'aaa': 'aaa', + }, + 'free_disk_gb': 50, + 'hypervisor_version': 2004001, + 'disk_available_least': 50, + 'local_gb': 50, + 'free_ram_mb': 1024, + 'service': { + 'host': 'aaa', + 'disabled_reason': None, + 'id': 1, + }, + 'vcpus_used': 0, + 'hypervisor_type': 'QEMU', + 'local_gb_used': 0, + 'vcpus': 4, + 'memory_mb_used': 512, + 'memory_mb': 1024, + 'current_workload': 0, + 'state': 'up', + 'running_vms': 0, } # Overwrite default attributes. diff --git a/openstackclient/tests/compute/v2/test_hypervisor.py b/openstackclient/tests/compute/v2/test_hypervisor.py index 1f52ee0943..a11f59d2a8 100644 --- a/openstackclient/tests/compute/v2/test_hypervisor.py +++ b/openstackclient/tests/compute/v2/test_hypervisor.py @@ -13,9 +13,12 @@ # under the License. # +import copy + from openstackclient.common import exceptions from openstackclient.compute.v2 import hypervisor from openstackclient.tests.compute.v2 import fakes as compute_fakes +from openstackclient.tests import fakes class TestHypervisor(compute_fakes.TestComputev2): @@ -27,6 +30,10 @@ class TestHypervisor(compute_fakes.TestComputev2): self.hypervisors_mock = self.app.client_manager.compute.hypervisors self.hypervisors_mock.reset_mock() + # Get a shortcut to the compute client aggregates mock + self.aggregates_mock = self.app.client_manager.compute.aggregates + self.aggregates_mock.reset_mock() + class TestHypervisorList(TestHypervisor): @@ -113,3 +120,101 @@ class TestHypervisorList(TestHypervisor): self.assertRaises(exceptions.NotFound, self.cmd.take_action, parsed_args) + + +class TestHypervisorShow(TestHypervisor): + + def setUp(self): + super(TestHypervisorShow, self).setUp() + + # Fake hypervisors to be listed up + self.hypervisor = compute_fakes.FakeHypervisor.create_one_hypervisor() + + # Return value of utils.find_resource() + self.hypervisors_mock.get.return_value = self.hypervisor + + # Return value of compute_client.aggregates.list() + self.aggregates_mock.list.return_value = [] + + # Return value of compute_client.hypervisors.uptime() + uptime_info = { + 'status': self.hypervisor.status, + 'state': self.hypervisor.state, + 'id': self.hypervisor.id, + 'hypervisor_hostname': self.hypervisor.hypervisor_hostname, + 'uptime': ' 01:28:24 up 3 days, 11:15, 1 user, ' + ' load average: 0.94, 0.62, 0.50\n', + } + self.hypervisors_mock.uptime.return_value = fakes.FakeResource( + info=copy.deepcopy(uptime_info), + loaded=True + ) + + self.columns = ( + 'aggregates', + 'cpu_info', + 'current_workload', + 'disk_available_least', + 'free_disk_gb', + 'free_ram_mb', + 'host_ip', + 'hypervisor_hostname', + 'hypervisor_type', + 'hypervisor_version', + 'id', + 'local_gb', + 'local_gb_used', + 'memory_mb', + 'memory_mb_used', + 'running_vms', + 'service_host', + 'service_id', + 'state', + 'status', + 'vcpus', + 'vcpus_used', + ) + self.data = ( + [], + {'aaa': 'aaa'}, + 0, + 50, + 50, + 1024, + '192.168.0.10', + self.hypervisor.hypervisor_hostname, + 'QEMU', + 2004001, + self.hypervisor.id, + 50, + 0, + 1024, + 512, + 0, + 'aaa', + 1, + 'up', + 'enabled', + 4, + 0, + ) + + # Get the command object to test + self.cmd = hypervisor.ShowHypervisor(self.app, None) + + def test_hypervisor_show(self): + arglist = [ + self.hypervisor.hypervisor_hostname, + ] + verifylist = [ + ('hypervisor', self.hypervisor.hypervisor_hostname), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # In base command class ShowOne in cliff, abstractmethod take_action() + # returns a two-part tuple with a tuple of column names and a tuple of + # data to be shown. + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data)