| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
|
| 14 |
import testtools |
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from aodhclient import exceptions |
| 18 |
from aodhclient.v2 import quota_cli |
| 19 |
|
| 20 |
|
| 21 |
class QuotaShowTest(testtools.TestCase): |
| 22 |
def setUp(self): |
| 23 |
super().setUp() |
| 24 |
self.app = mock.Mock() |
| 25 |
self.quota_mgr_mock = self.app.client_manager.alarming.quota |
| 26 |
self.parser = mock.Mock() |
| 27 |
self.quota_show = ( |
| 28 |
quota_cli.QuotaShow(self.app, self.parser)) |
| 29 |
|
| 30 |
def test_quota_show(self): |
| 31 |
self.quota_mgr_mock.list.return_value = { |
| 32 |
"project_id": "fake_project", |
| 33 |
"quotas": [ |
| 34 |
{ |
| 35 |
"limit": 20, |
| 36 |
"resource": "alarms" |
| 37 |
} |
| 38 |
] |
| 39 |
} |
| 40 |
parser = self.quota_show.get_parser('') |
| 41 |
args = parser.parse_args(['--project', 'fake_project']) |
| 42 |
# Something like [('alarms',), (20,)] |
| 43 |
ret = list(self.quota_show.take_action(args)) |
| 44 |
|
| 45 |
self.quota_mgr_mock.list.assert_called_once_with( |
| 46 |
project='fake_project') |
| 47 |
self.assertIn('alarms', ret[0]) |
| 48 |
self.assertIn(20, ret[1]) |
| 49 |
|
| 50 |
|
| 51 |
class QuotaSetTest(testtools.TestCase): |
| 52 |
def setUp(self): |
| 53 |
super().setUp() |
| 54 |
self.app = mock.Mock() |
| 55 |
self.quota_mgr_mock = self.app.client_manager.alarming.quota |
| 56 |
self.parser = mock.Mock() |
| 57 |
self.quota_set = ( |
| 58 |
quota_cli.QuotaSet(self.app, self.parser)) |
| 59 |
|
| 60 |
def test_quota_set(self): |
| 61 |
self.quota_mgr_mock.create.return_value = { |
| 62 |
"project_id": "fake_project", |
| 63 |
"quotas": [ |
| 64 |
{ |
| 65 |
"limit": 20, |
| 66 |
"resource": "alarms" |
| 67 |
} |
| 68 |
] |
| 69 |
} |
| 70 |
|
| 71 |
parser = self.quota_set.get_parser('') |
| 72 |
args = parser.parse_args(['fake_project', '--alarm', '20']) |
| 73 |
ret = list(self.quota_set.take_action(args)) |
| 74 |
|
| 75 |
self.quota_mgr_mock.create.assert_called_once_with( |
| 76 |
'fake_project', [{'resource': 'alarms', 'limit': 20}]) |
| 77 |
self.assertIn('alarms', ret[0]) |
| 78 |
self.assertIn(20, ret[1]) |
| 79 |
|
| 80 |
def test_quota_set_invalid_quota(self): |
| 81 |
parser = self.quota_set.get_parser('') |
| 82 |
args = parser.parse_args(['fake_project', '--alarm', '-2']) |
| 83 |
|
| 84 |
self.assertRaises(exceptions.CommandError, |
| 85 |
self.quota_set.take_action, |
| 86 |
args) |