|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +import datetime |
| 15 | +import mock |
| 16 | + |
| 17 | +from openstackclient.compute.v2 import usage |
| 18 | +from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes |
| 19 | +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes |
| 20 | + |
| 21 | + |
| 22 | +class TestUsage(compute_fakes.TestComputev2): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + super(TestUsage, self).setUp() |
| 26 | + |
| 27 | + self.usage_mock = self.app.client_manager.compute.usage |
| 28 | + self.usage_mock.reset_mock() |
| 29 | + |
| 30 | + self.projects_mock = self.app.client_manager.identity.projects |
| 31 | + self.projects_mock.reset_mock() |
| 32 | + |
| 33 | + |
| 34 | +class TestUsageList(TestUsage): |
| 35 | + |
| 36 | + project = identity_fakes.FakeProject.create_one_project() |
| 37 | + # Return value of self.usage_mock.list(). |
| 38 | + usages = compute_fakes.FakeUsage.create_usages( |
| 39 | + attrs={'tenant_id': project.name}, count=1) |
| 40 | + |
| 41 | + columns = ( |
| 42 | + "Project", |
| 43 | + "Servers", |
| 44 | + "RAM MB-Hours", |
| 45 | + "CPU Hours", |
| 46 | + "Disk GB-Hours" |
| 47 | + ) |
| 48 | + |
| 49 | + data = [( |
| 50 | + usages[0].tenant_id, |
| 51 | + len(usages[0].server_usages), |
| 52 | + float("%.2f" % usages[0].total_memory_mb_usage), |
| 53 | + float("%.2f" % usages[0].total_vcpus_usage), |
| 54 | + float("%.2f" % usages[0].total_local_gb_usage), |
| 55 | + )] |
| 56 | + |
| 57 | + def setUp(self): |
| 58 | + super(TestUsageList, self).setUp() |
| 59 | + |
| 60 | + self.usage_mock.list.return_value = self.usages |
| 61 | + |
| 62 | + self.projects_mock.list.return_value = [self.project] |
| 63 | + # Get the command object to test |
| 64 | + self.cmd = usage.ListUsage(self.app, None) |
| 65 | + |
| 66 | + def test_usage_list_no_options(self): |
| 67 | + |
| 68 | + arglist = [] |
| 69 | + verifylist = [ |
| 70 | + ('start', None), |
| 71 | + ('end', None), |
| 72 | + ] |
| 73 | + |
| 74 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 75 | + |
| 76 | + columns, data = self.cmd.take_action(parsed_args) |
| 77 | + |
| 78 | + self.projects_mock.list.assert_called_with() |
| 79 | + |
| 80 | + self.assertEqual(self.columns, columns) |
| 81 | + self.assertEqual(tuple(self.data), tuple(data)) |
| 82 | + |
| 83 | + def test_usage_list_with_options(self): |
| 84 | + arglist = [ |
| 85 | + '--start', '2016-11-11', |
| 86 | + '--end', '2016-12-20', |
| 87 | + ] |
| 88 | + verifylist = [ |
| 89 | + ('start', '2016-11-11'), |
| 90 | + ('end', '2016-12-20'), |
| 91 | + ] |
| 92 | + |
| 93 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 94 | + |
| 95 | + columns, data = self.cmd.take_action(parsed_args) |
| 96 | + |
| 97 | + self.projects_mock.list.assert_called_with() |
| 98 | + self.usage_mock.list.assert_called_with( |
| 99 | + datetime.datetime(2016, 11, 11, 0, 0), |
| 100 | + datetime.datetime(2016, 12, 20, 0, 0), |
| 101 | + detailed=True) |
| 102 | + |
| 103 | + self.assertEqual(self.columns, columns) |
| 104 | + self.assertEqual(tuple(self.data), tuple(data)) |
| 105 | + |
| 106 | + |
| 107 | +class TestUsageShow(TestUsage): |
| 108 | + |
| 109 | + project = identity_fakes.FakeProject.create_one_project() |
| 110 | + # Return value of self.usage_mock.list(). |
| 111 | + usage = compute_fakes.FakeUsage.create_one_usage( |
| 112 | + attrs={'tenant_id': project.name}) |
| 113 | + |
| 114 | + columns = ( |
| 115 | + 'CPU Hours', |
| 116 | + 'Disk GB-Hours', |
| 117 | + 'RAM MB-Hours', |
| 118 | + 'Servers', |
| 119 | + ) |
| 120 | + |
| 121 | + data = ( |
| 122 | + float("%.2f" % usage.total_vcpus_usage), |
| 123 | + float("%.2f" % usage.total_local_gb_usage), |
| 124 | + float("%.2f" % usage.total_memory_mb_usage), |
| 125 | + len(usage.server_usages), |
| 126 | + ) |
| 127 | + |
| 128 | + def setUp(self): |
| 129 | + super(TestUsageShow, self).setUp() |
| 130 | + |
| 131 | + self.usage_mock.get.return_value = self.usage |
| 132 | + |
| 133 | + self.projects_mock.get.return_value = self.project |
| 134 | + # Get the command object to test |
| 135 | + self.cmd = usage.ShowUsage(self.app, None) |
| 136 | + |
| 137 | + def test_usage_show_no_options(self): |
| 138 | + |
| 139 | + self.app.client_manager.auth_ref = mock.Mock() |
| 140 | + self.app.client_manager.auth_ref.project_id = self.project.id |
| 141 | + |
| 142 | + arglist = [] |
| 143 | + verifylist = [ |
| 144 | + ('project', None), |
| 145 | + ('start', None), |
| 146 | + ('end', None), |
| 147 | + ] |
| 148 | + |
| 149 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 150 | + |
| 151 | + columns, data = self.cmd.take_action(parsed_args) |
| 152 | + |
| 153 | + self.assertEqual(self.columns, columns) |
| 154 | + self.assertEqual(self.data, data) |
| 155 | + |
| 156 | + def test_usage_show_with_options(self): |
| 157 | + |
| 158 | + arglist = [ |
| 159 | + '--project', self.project.id, |
| 160 | + '--start', '2016-11-11', |
| 161 | + '--end', '2016-12-20', |
| 162 | + ] |
| 163 | + verifylist = [ |
| 164 | + ('project', self.project.id), |
| 165 | + ('start', '2016-11-11'), |
| 166 | + ('end', '2016-12-20'), |
| 167 | + ] |
| 168 | + |
| 169 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 170 | + |
| 171 | + columns, data = self.cmd.take_action(parsed_args) |
| 172 | + |
| 173 | + self.usage_mock.get.assert_called_with( |
| 174 | + self.project.id, |
| 175 | + datetime.datetime(2016, 11, 11, 0, 0), |
| 176 | + datetime.datetime(2016, 12, 20, 0, 0)) |
| 177 | + |
| 178 | + self.assertEqual(self.columns, columns) |
| 179 | + self.assertEqual(self.data, data) |
0 commit comments