|
| 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 copy |
| 15 | +import mock |
| 16 | + |
| 17 | +from openstackclient.compute.v2 import security_group |
| 18 | +from openstackclient.tests.compute.v2 import fakes as compute_fakes |
| 19 | +from openstackclient.tests import fakes |
| 20 | +from openstackclient.tests.identity.v2_0 import fakes as identity_fakes |
| 21 | + |
| 22 | + |
| 23 | +security_group_id = '11' |
| 24 | +security_group_name = 'wide-open' |
| 25 | +security_group_description = 'nothing but net' |
| 26 | + |
| 27 | +SECURITY_GROUP = { |
| 28 | + 'id': security_group_id, |
| 29 | + 'name': security_group_name, |
| 30 | + 'description': security_group_description, |
| 31 | + 'tenant_id': identity_fakes.project_id, |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +class FakeSecurityGroupResource(fakes.FakeResource): |
| 36 | + |
| 37 | + def get_keys(self): |
| 38 | + return {'property': 'value'} |
| 39 | + |
| 40 | + |
| 41 | +class TestSecurityGroup(compute_fakes.TestComputev2): |
| 42 | + |
| 43 | + def setUp(self): |
| 44 | + super(TestSecurityGroup, self).setUp() |
| 45 | + |
| 46 | + self.secgroups_mock = mock.Mock() |
| 47 | + self.secgroups_mock.resource_class = fakes.FakeResource(None, {}) |
| 48 | + self.app.client_manager.compute.security_groups = self.secgroups_mock |
| 49 | + self.secgroups_mock.reset_mock() |
| 50 | + |
| 51 | + self.projects_mock = mock.Mock() |
| 52 | + self.projects_mock.resource_class = fakes.FakeResource(None, {}) |
| 53 | + self.app.client_manager.identity.projects = self.projects_mock |
| 54 | + self.projects_mock.reset_mock() |
| 55 | + |
| 56 | + |
| 57 | +class TestSecurityGroupCreate(TestSecurityGroup): |
| 58 | + |
| 59 | + def setUp(self): |
| 60 | + super(TestSecurityGroupCreate, self).setUp() |
| 61 | + |
| 62 | + self.secgroups_mock.create.return_value = FakeSecurityGroupResource( |
| 63 | + None, |
| 64 | + copy.deepcopy(SECURITY_GROUP), |
| 65 | + loaded=True, |
| 66 | + ) |
| 67 | + |
| 68 | + # Get the command object to test |
| 69 | + self.cmd = security_group.CreateSecurityGroup(self.app, None) |
| 70 | + |
| 71 | + def test_security_group_create_no_options(self): |
| 72 | + arglist = [ |
| 73 | + security_group_name, |
| 74 | + ] |
| 75 | + verifylist = [ |
| 76 | + ('name', security_group_name), |
| 77 | + ] |
| 78 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 79 | + |
| 80 | + # DisplayCommandBase.take_action() returns two tuples |
| 81 | + columns, data = self.cmd.take_action(parsed_args) |
| 82 | + |
| 83 | + # SecurityGroupManager.create(name, description) |
| 84 | + self.secgroups_mock.create.assert_called_with( |
| 85 | + security_group_name, |
| 86 | + security_group_name, |
| 87 | + ) |
| 88 | + |
| 89 | + collist = ( |
| 90 | + 'description', |
| 91 | + 'id', |
| 92 | + 'name', |
| 93 | + 'tenant_id', |
| 94 | + ) |
| 95 | + self.assertEqual(collist, columns) |
| 96 | + datalist = ( |
| 97 | + security_group_description, |
| 98 | + security_group_id, |
| 99 | + security_group_name, |
| 100 | + identity_fakes.project_id, |
| 101 | + ) |
| 102 | + self.assertEqual(datalist, data) |
| 103 | + |
| 104 | + def test_security_group_create_description(self): |
| 105 | + arglist = [ |
| 106 | + security_group_name, |
| 107 | + '--description', security_group_description, |
| 108 | + ] |
| 109 | + verifylist = [ |
| 110 | + ('name', security_group_name), |
| 111 | + ('description', security_group_description), |
| 112 | + ] |
| 113 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 114 | + |
| 115 | + # DisplayCommandBase.take_action() returns two tuples |
| 116 | + columns, data = self.cmd.take_action(parsed_args) |
| 117 | + |
| 118 | + # SecurityGroupManager.create(name, description) |
| 119 | + self.secgroups_mock.create.assert_called_with( |
| 120 | + security_group_name, |
| 121 | + security_group_description, |
| 122 | + ) |
| 123 | + |
| 124 | + collist = ( |
| 125 | + 'description', |
| 126 | + 'id', |
| 127 | + 'name', |
| 128 | + 'tenant_id', |
| 129 | + ) |
| 130 | + self.assertEqual(collist, columns) |
| 131 | + datalist = ( |
| 132 | + security_group_description, |
| 133 | + security_group_id, |
| 134 | + security_group_name, |
| 135 | + identity_fakes.project_id, |
| 136 | + ) |
| 137 | + self.assertEqual(datalist, data) |
| 138 | + |
| 139 | + |
| 140 | +class TestSecurityGroupList(TestSecurityGroup): |
| 141 | + |
| 142 | + def setUp(self): |
| 143 | + super(TestSecurityGroupList, self).setUp() |
| 144 | + |
| 145 | + self.secgroups_mock.list.return_value = [ |
| 146 | + FakeSecurityGroupResource( |
| 147 | + None, |
| 148 | + copy.deepcopy(SECURITY_GROUP), |
| 149 | + loaded=True, |
| 150 | + ), |
| 151 | + ] |
| 152 | + |
| 153 | + # Get the command object to test |
| 154 | + self.cmd = security_group.ListSecurityGroup(self.app, None) |
| 155 | + |
| 156 | + def test_security_group_list_no_options(self): |
| 157 | + self.projects_mock.list.return_value = [ |
| 158 | + fakes.FakeResource( |
| 159 | + None, |
| 160 | + copy.deepcopy(identity_fakes.PROJECT), |
| 161 | + loaded=True, |
| 162 | + ), |
| 163 | + ] |
| 164 | + |
| 165 | + arglist = [] |
| 166 | + verifylist = [ |
| 167 | + ('all_projects', False), |
| 168 | + ] |
| 169 | + |
| 170 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 171 | + |
| 172 | + # DisplayCommandBase.take_action() returns two tuples |
| 173 | + columns, data = self.cmd.take_action(parsed_args) |
| 174 | + |
| 175 | + # Set expected values |
| 176 | + kwargs = { |
| 177 | + 'search_opts': { |
| 178 | + 'all_tenants': False, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + self.secgroups_mock.list.assert_called_with( |
| 183 | + **kwargs |
| 184 | + ) |
| 185 | + |
| 186 | + collist = ( |
| 187 | + 'ID', |
| 188 | + 'Name', |
| 189 | + 'Description', |
| 190 | + ) |
| 191 | + self.assertEqual(collist, columns) |
| 192 | + datalist = (( |
| 193 | + security_group_id, |
| 194 | + security_group_name, |
| 195 | + security_group_description, |
| 196 | + ), ) |
| 197 | + self.assertEqual(datalist, tuple(data)) |
0 commit comments