|
11 | 11 | # under the License. |
12 | 12 | # |
13 | 13 |
|
| 14 | +import copy |
14 | 15 | import mock |
15 | 16 |
|
16 | 17 | from openstackclient.network.v2 import security_group_rule |
17 | 18 | from openstackclient.tests.compute.v2 import fakes as compute_fakes |
| 19 | +from openstackclient.tests import fakes |
18 | 20 | from openstackclient.tests.network.v2 import fakes as network_fakes |
| 21 | +from openstackclient.tests import utils as tests_utils |
19 | 22 |
|
20 | 23 |
|
21 | 24 | class TestSecurityGroupRuleNetwork(network_fakes.TestNetworkV2): |
@@ -98,3 +101,112 @@ def test_security_group_rule_delete(self): |
98 | 101 | self.compute.security_group_rules.delete.assert_called_with( |
99 | 102 | self._security_group_rule.id) |
100 | 103 | self.assertIsNone(result) |
| 104 | + |
| 105 | + |
| 106 | +class TestShowSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork): |
| 107 | + |
| 108 | + # The security group rule to be shown. |
| 109 | + _security_group_rule = \ |
| 110 | + network_fakes.FakeSecurityGroupRule.create_one_security_group_rule() |
| 111 | + |
| 112 | + columns = ( |
| 113 | + 'direction', |
| 114 | + 'ethertype', |
| 115 | + 'id', |
| 116 | + 'port_range_max', |
| 117 | + 'port_range_min', |
| 118 | + 'project_id', |
| 119 | + 'protocol', |
| 120 | + 'remote_group_id', |
| 121 | + 'remote_ip_prefix', |
| 122 | + 'security_group_id', |
| 123 | + ) |
| 124 | + |
| 125 | + data = ( |
| 126 | + _security_group_rule.direction, |
| 127 | + _security_group_rule.ethertype, |
| 128 | + _security_group_rule.id, |
| 129 | + _security_group_rule.port_range_max, |
| 130 | + _security_group_rule.port_range_min, |
| 131 | + _security_group_rule.project_id, |
| 132 | + _security_group_rule.protocol, |
| 133 | + _security_group_rule.remote_group_id, |
| 134 | + _security_group_rule.remote_ip_prefix, |
| 135 | + _security_group_rule.security_group_id, |
| 136 | + ) |
| 137 | + |
| 138 | + def setUp(self): |
| 139 | + super(TestShowSecurityGroupRuleNetwork, self).setUp() |
| 140 | + |
| 141 | + self.network.find_security_group_rule = mock.Mock( |
| 142 | + return_value=self._security_group_rule) |
| 143 | + |
| 144 | + # Get the command object to test |
| 145 | + self.cmd = security_group_rule.ShowSecurityGroupRule( |
| 146 | + self.app, self.namespace) |
| 147 | + |
| 148 | + def test_show_no_options(self): |
| 149 | + self.assertRaises(tests_utils.ParserException, |
| 150 | + self.check_parser, self.cmd, [], []) |
| 151 | + |
| 152 | + def test_show_all_options(self): |
| 153 | + arglist = [ |
| 154 | + self._security_group_rule.id, |
| 155 | + ] |
| 156 | + verifylist = [ |
| 157 | + ('rule', self._security_group_rule.id), |
| 158 | + ] |
| 159 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 160 | + |
| 161 | + columns, data = self.cmd.take_action(parsed_args) |
| 162 | + |
| 163 | + self.network.find_security_group_rule.assert_called_with( |
| 164 | + self._security_group_rule.id, ignore_missing=False) |
| 165 | + self.assertEqual(tuple(self.columns), columns) |
| 166 | + self.assertEqual(self.data, data) |
| 167 | + |
| 168 | + |
| 169 | +class TestShowSecurityGroupRuleCompute(TestSecurityGroupRuleCompute): |
| 170 | + |
| 171 | + # The security group rule to be shown. |
| 172 | + _security_group_rule = \ |
| 173 | + compute_fakes.FakeSecurityGroupRule.create_one_security_group_rule() |
| 174 | + |
| 175 | + columns, data = \ |
| 176 | + security_group_rule._format_security_group_rule_show( |
| 177 | + _security_group_rule._info) |
| 178 | + |
| 179 | + def setUp(self): |
| 180 | + super(TestShowSecurityGroupRuleCompute, self).setUp() |
| 181 | + |
| 182 | + self.app.client_manager.network_endpoint_enabled = False |
| 183 | + |
| 184 | + # Build a security group fake customized for this test. |
| 185 | + security_group_rules = [self._security_group_rule._info] |
| 186 | + security_group = fakes.FakeResource( |
| 187 | + info=copy.deepcopy({'rules': security_group_rules}), |
| 188 | + loaded=True) |
| 189 | + security_group.rules = security_group_rules |
| 190 | + self.compute.security_groups.list.return_value = [security_group] |
| 191 | + |
| 192 | + # Get the command object to test |
| 193 | + self.cmd = security_group_rule.ShowSecurityGroupRule(self.app, None) |
| 194 | + |
| 195 | + def test_show_no_options(self): |
| 196 | + self.assertRaises(tests_utils.ParserException, |
| 197 | + self.check_parser, self.cmd, [], []) |
| 198 | + |
| 199 | + def test_show_all_options(self): |
| 200 | + arglist = [ |
| 201 | + self._security_group_rule.id, |
| 202 | + ] |
| 203 | + verifylist = [ |
| 204 | + ('rule', self._security_group_rule.id), |
| 205 | + ] |
| 206 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 207 | + |
| 208 | + columns, data = self.cmd.take_action(parsed_args) |
| 209 | + |
| 210 | + self.compute.security_groups.list.assert_called_with() |
| 211 | + self.assertEqual(self.columns, columns) |
| 212 | + self.assertEqual(self.data, data) |
0 commit comments