diff --git a/openstackclient/tests/unit/compute/v2/fakes.py b/openstackclient/tests/unit/compute/v2/fakes.py index 985ce5e2ed..4fe735b6b4 100644 --- a/openstackclient/tests/unit/compute/v2/fakes.py +++ b/openstackclient/tests/unit/compute/v2/fakes.py @@ -168,6 +168,9 @@ class FakeComputev2Client(object): self.quota_classes = mock.Mock() self.quota_classes.resource_class = fakes.FakeResource(None, {}) + self.usage = mock.Mock() + self.usage.resource_class = fakes.FakeResource(None, {}) + self.volumes = mock.Mock() self.volumes.resource_class = fakes.FakeResource(None, {}) @@ -1248,3 +1251,65 @@ class FakeServerGroup(object): info=copy.deepcopy(server_group_info), loaded=True) return server_group + + +class FakeUsage(object): + """Fake one or more usage.""" + + @staticmethod + def create_one_usage(attrs=None): + """Create a fake usage. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object, with tenant_id and other attributes + """ + if attrs is None: + attrs = {} + + # Set default attributes. + usage_info = { + 'tenant_id': 'usage-tenant-id-' + uuid.uuid4().hex, + 'total_memory_mb_usage': 512.0, + 'total_vcpus_usage': 1.0, + 'total_local_gb_usage': 1.0, + 'server_usages': [ + { + 'ended_at': None, + 'flavor': 'usage-flavor-' + uuid.uuid4().hex, + 'hours': 1.0, + 'local_gb': 1, + 'memory_mb': 512, + 'name': 'usage-name-' + uuid.uuid4().hex, + 'state': 'active', + 'uptime': 3600, + 'vcpus': 1 + } + ] + } + + # Overwrite default attributes. + usage_info.update(attrs) + + usage = fakes.FakeResource(info=copy.deepcopy(usage_info), + loaded=True) + + return usage + + @staticmethod + def create_usages(attrs=None, count=2): + """Create multiple fake services. + + :param Dictionary attrs: + A dictionary with all attributes + :param int count: + The number of services to fake + :return: + A list of FakeResource objects faking the services + """ + usages = [] + for i in range(0, count): + usages.append(FakeUsage.create_one_usage(attrs)) + + return usages diff --git a/openstackclient/tests/unit/compute/v2/test_usage.py b/openstackclient/tests/unit/compute/v2/test_usage.py new file mode 100644 index 0000000000..a383e9036d --- /dev/null +++ b/openstackclient/tests/unit/compute/v2/test_usage.py @@ -0,0 +1,179 @@ +# 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. +# + +import datetime +import mock + +from openstackclient.compute.v2 import usage +from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes + + +class TestUsage(compute_fakes.TestComputev2): + + def setUp(self): + super(TestUsage, self).setUp() + + self.usage_mock = self.app.client_manager.compute.usage + self.usage_mock.reset_mock() + + self.projects_mock = self.app.client_manager.identity.projects + self.projects_mock.reset_mock() + + +class TestUsageList(TestUsage): + + project = identity_fakes.FakeProject.create_one_project() + # Return value of self.usage_mock.list(). + usages = compute_fakes.FakeUsage.create_usages( + attrs={'tenant_id': project.name}, count=1) + + columns = ( + "Project", + "Servers", + "RAM MB-Hours", + "CPU Hours", + "Disk GB-Hours" + ) + + data = [( + usages[0].tenant_id, + len(usages[0].server_usages), + float("%.2f" % usages[0].total_memory_mb_usage), + float("%.2f" % usages[0].total_vcpus_usage), + float("%.2f" % usages[0].total_local_gb_usage), + )] + + def setUp(self): + super(TestUsageList, self).setUp() + + self.usage_mock.list.return_value = self.usages + + self.projects_mock.list.return_value = [self.project] + # Get the command object to test + self.cmd = usage.ListUsage(self.app, None) + + def test_usage_list_no_options(self): + + arglist = [] + verifylist = [ + ('start', None), + ('end', None), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.projects_mock.list.assert_called_with() + + self.assertEqual(self.columns, columns) + self.assertEqual(tuple(self.data), tuple(data)) + + def test_usage_list_with_options(self): + arglist = [ + '--start', '2016-11-11', + '--end', '2016-12-20', + ] + verifylist = [ + ('start', '2016-11-11'), + ('end', '2016-12-20'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.projects_mock.list.assert_called_with() + self.usage_mock.list.assert_called_with( + datetime.datetime(2016, 11, 11, 0, 0), + datetime.datetime(2016, 12, 20, 0, 0), + detailed=True) + + self.assertEqual(self.columns, columns) + self.assertEqual(tuple(self.data), tuple(data)) + + +class TestUsageShow(TestUsage): + + project = identity_fakes.FakeProject.create_one_project() + # Return value of self.usage_mock.list(). + usage = compute_fakes.FakeUsage.create_one_usage( + attrs={'tenant_id': project.name}) + + columns = ( + 'CPU Hours', + 'Disk GB-Hours', + 'RAM MB-Hours', + 'Servers', + ) + + data = ( + float("%.2f" % usage.total_vcpus_usage), + float("%.2f" % usage.total_local_gb_usage), + float("%.2f" % usage.total_memory_mb_usage), + len(usage.server_usages), + ) + + def setUp(self): + super(TestUsageShow, self).setUp() + + self.usage_mock.get.return_value = self.usage + + self.projects_mock.get.return_value = self.project + # Get the command object to test + self.cmd = usage.ShowUsage(self.app, None) + + def test_usage_show_no_options(self): + + self.app.client_manager.auth_ref = mock.Mock() + self.app.client_manager.auth_ref.project_id = self.project.id + + arglist = [] + verifylist = [ + ('project', None), + ('start', None), + ('end', None), + ] + + 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) + + def test_usage_show_with_options(self): + + arglist = [ + '--project', self.project.id, + '--start', '2016-11-11', + '--end', '2016-12-20', + ] + verifylist = [ + ('project', self.project.id), + ('start', '2016-11-11'), + ('end', '2016-12-20'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.usage_mock.get.assert_called_with( + self.project.id, + datetime.datetime(2016, 11, 11, 0, 0), + datetime.datetime(2016, 12, 20, 0, 0)) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data)