From 8825f0d8f36f46eaa4aae121ba49c0f8fe6e1026 Mon Sep 17 00:00:00 2001
From: "ting.wang" <ting.wang@easystack.cn>
Date: Mon, 22 Feb 2016 23:36:29 +0800
Subject: [PATCH] Add unit tests for 'hypervisor stats' command

'hypervisor stats show' command isn't covered by unit tests,
so add unit tests to test it.

Change-Id: Ic355230cbdd596e848191b599803dca7f27c2ffb
---
 openstackclient/tests/compute/v2/fakes.py     | 61 ++++++++++++++
 .../tests/compute/v2/test_hypervisor_stats.py | 79 +++++++++++++++++++
 2 files changed, 140 insertions(+)
 create mode 100644 openstackclient/tests/compute/v2/test_hypervisor_stats.py

diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py
index 96d85b0c06..52279f2a96 100644
--- a/openstackclient/tests/compute/v2/fakes.py
+++ b/openstackclient/tests/compute/v2/fakes.py
@@ -121,6 +121,9 @@ class FakeComputev2Client(object):
         self.hypervisors = mock.Mock()
         self.hypervisors.resource_class = fakes.FakeResource(None, {})
 
+        self.hypervisors_stats = mock.Mock()
+        self.hypervisors_stats.resource_class = fakes.FakeResource(None, {})
+
         self.security_groups = mock.Mock()
         self.security_groups.resource_class = fakes.FakeResource(None, {})
 
@@ -235,6 +238,64 @@ class FakeHypervisor(object):
         return hypervisors
 
 
+class FakehypervisorStats(object):
+    """Fake one or more hypervisor stats."""
+
+    @staticmethod
+    def create_one_hypervisor_stats(attrs={}, methods={}):
+        """Create a fake hypervisor stats.
+
+        :param Dictionary attrs:
+            A dictionary with all attributes
+        :return:
+            A FakeResource object, with id, hypervisor_hostname, and so on
+        """
+        # Set default attributes.
+        stats_info = {
+            'count': 2,
+            'current_workload': 0,
+            'disk_available_least': 50,
+            'free_disk_gb': 100,
+            'free_ram_mb': 23000,
+            'local_gb': 100,
+            'local_gb_used': 0,
+            'memory_mb': 23800,
+            'memory_mb_used': 1400,
+            'running_vms': 3,
+            'vcpus': 8,
+            'vcpus_used': 3,
+        }
+        stats_info.update(attrs)
+
+        # Set default method.
+        hypervisor_stats_method = {'to_dict': stats_info}
+        hypervisor_stats_method.update(methods)
+
+        hypervisor_stats = fakes.FakeResource(
+            info=copy.deepcopy(stats_info),
+            methods=copy.deepcopy(hypervisor_stats_method),
+            loaded=True)
+        return hypervisor_stats
+
+    @staticmethod
+    def create_hypervisors_stats(attrs={}, count=2):
+        """Create multiple fake hypervisors stats.
+
+        :param Dictionary attrs:
+            A dictionary with all attributes
+        :param int count:
+            The number of hypervisors to fake
+        :return:
+            A list of FakeResource objects faking the hypervisors
+        """
+        hypervisors = []
+        for i in range(0, count):
+            hypervisors.append(
+                FakehypervisorStats.create_one_hypervisor_stats(attrs))
+
+        return hypervisors
+
+
 class FakeSecurityGroupRule(object):
     """Fake one or more security group rules."""
 
diff --git a/openstackclient/tests/compute/v2/test_hypervisor_stats.py b/openstackclient/tests/compute/v2/test_hypervisor_stats.py
new file mode 100644
index 0000000000..39e303a84e
--- /dev/null
+++ b/openstackclient/tests/compute/v2/test_hypervisor_stats.py
@@ -0,0 +1,79 @@
+#   Copyright 2016 EasyStack Corporation
+#
+#   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.
+#
+
+from openstackclient.compute.v2 import hypervisor_stats
+from openstackclient.tests.compute.v2 import fakes as compute_fakes
+
+
+class TestHypervisorStats(compute_fakes.TestComputev2):
+
+    def setUp(self):
+        super(TestHypervisorStats, self).setUp()
+
+        # Get a shortcut to the compute client hypervisors mock
+        self.hypervisors_mock = self.app.client_manager.compute.hypervisors
+        self.hypervisors_mock.reset_mock()
+
+
+class TestHypervisorStatsShow(TestHypervisorStats):
+
+    def setUp(self):
+        super(TestHypervisorStatsShow, self).setUp()
+
+        self.hypervisor_stats = \
+            compute_fakes.FakehypervisorStats.create_one_hypervisor_stats()
+
+        self.hypervisors_mock.statistics.return_value =\
+            self.hypervisor_stats
+
+        self.cmd = hypervisor_stats.ShowHypervisorStats(self.app, None)
+
+        self.columns = (
+            'count',
+            'current_workload',
+            'disk_available_least',
+            'free_disk_gb',
+            'free_ram_mb',
+            'local_gb',
+            'local_gb_used',
+            'memory_mb',
+            'memory_mb_used',
+            'running_vms',
+            'vcpus',
+            'vcpus_used',
+        )
+
+        self.data = (
+            2,
+            0,
+            50,
+            100,
+            23000,
+            100,
+            0,
+            23800,
+            1400,
+            3,
+            8,
+            3,
+        )
+
+    def test_hypervisor_show_stats(self):
+        arglist = []
+        verifylist = []
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+        columns, data = self.cmd.take_action(parsed_args)
+        self.assertEqual(self.columns, columns)
+        self.assertEqual(self.data, data)