summaryrefslogtreecommitdiff
path: root/openstackclient/tests
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2016-02-23 00:59:08 +0000
committerGerrit Code Review <review@openstack.org>2016-02-23 00:59:08 +0000
commit37f96c2c47e97eb630ef1a7086fc33712616fcc0 (patch)
treeb75adc88d711241d71b0ab971f1ab7a8e02e224e /openstackclient/tests
parent7c9bb377c1d9be92af676a06959f7ddc86403491 (diff)
parent8825f0d8f36f46eaa4aae121ba49c0f8fe6e1026 (diff)
downloadpython-openstackclient-37f96c2c47e97eb630ef1a7086fc33712616fcc0.tar.gz
Merge "Add unit tests for 'hypervisor stats' command"
Diffstat (limited to 'openstackclient/tests')
-rw-r--r--openstackclient/tests/compute/v2/fakes.py61
-rw-r--r--openstackclient/tests/compute/v2/test_hypervisor_stats.py79
2 files changed, 140 insertions, 0 deletions
diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py
index c0db9b24..d2341ccc 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 00000000..39e303a8
--- /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)