python-openstackclient 6.3.0__py3-none-any.whl → 6.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. openstackclient/common/availability_zone.py +4 -4
  2. openstackclient/common/pagination.py +82 -0
  3. openstackclient/compute/v2/flavor.py +2 -16
  4. openstackclient/compute/v2/hypervisor.py +2 -21
  5. openstackclient/compute/v2/keypair.py +2 -9
  6. openstackclient/compute/v2/server.py +220 -131
  7. openstackclient/compute/v2/server_event.py +30 -19
  8. openstackclient/compute/v2/server_group.py +2 -23
  9. openstackclient/compute/v2/server_migration.py +2 -22
  10. openstackclient/compute/v2/usage.py +4 -6
  11. openstackclient/identity/v3/mapping.py +25 -3
  12. openstackclient/identity/v3/policy.py +3 -1
  13. openstackclient/image/v2/cache.py +218 -0
  14. openstackclient/image/v2/image.py +40 -17
  15. openstackclient/image/v2/metadef_namespaces.py +25 -21
  16. openstackclient/image/v2/metadef_objects.py +189 -0
  17. openstackclient/image/v2/metadef_properties.py +284 -0
  18. openstackclient/network/utils.py +100 -0
  19. openstackclient/network/v2/default_security_group_rule.py +418 -0
  20. openstackclient/network/v2/local_ip_association.py +1 -1
  21. openstackclient/network/v2/ndp_proxy.py +7 -3
  22. openstackclient/network/v2/network.py +2 -2
  23. openstackclient/network/v2/port.py +65 -19
  24. openstackclient/network/v2/security_group_rule.py +18 -111
  25. openstackclient/network/v2/subnet.py +1 -0
  26. openstackclient/object/v1/container.py +2 -12
  27. openstackclient/object/v1/object.py +2 -11
  28. openstackclient/tests/functional/base.py +13 -6
  29. openstackclient/tests/functional/identity/v3/test_role.py +11 -3
  30. openstackclient/tests/functional/network/v2/common.py +7 -1
  31. openstackclient/tests/functional/network/v2/test_address_group.py +2 -4
  32. openstackclient/tests/functional/network/v2/test_address_scope.py +0 -6
  33. openstackclient/tests/functional/network/v2/test_default_security_group_rule.py +67 -0
  34. openstackclient/tests/functional/network/v2/test_floating_ip.py +3 -6
  35. openstackclient/tests/functional/network/v2/test_ip_availability.py +3 -8
  36. openstackclient/tests/functional/network/v2/test_l3_conntrack_helper.py +3 -4
  37. openstackclient/tests/functional/network/v2/test_local_ip.py +2 -4
  38. openstackclient/tests/functional/network/v2/test_network.py +18 -17
  39. openstackclient/tests/functional/network/v2/test_network_agent.py +24 -21
  40. openstackclient/tests/functional/network/v2/test_network_flavor.py +0 -6
  41. openstackclient/tests/functional/network/v2/test_network_flavor_profile.py +0 -6
  42. openstackclient/tests/functional/network/v2/test_network_meter.py +6 -6
  43. openstackclient/tests/functional/network/v2/test_network_meter_rule.py +7 -8
  44. openstackclient/tests/functional/network/v2/test_network_ndp_proxy.py +1 -3
  45. openstackclient/tests/functional/network/v2/test_network_qos_policy.py +4 -4
  46. openstackclient/tests/functional/network/v2/test_network_qos_rule.py +16 -20
  47. openstackclient/tests/functional/network/v2/test_network_qos_rule_type.py +4 -4
  48. openstackclient/tests/functional/network/v2/test_network_rbac.py +1 -4
  49. openstackclient/tests/functional/network/v2/test_network_segment.py +7 -12
  50. openstackclient/tests/functional/network/v2/test_network_segment_range.py +3 -4
  51. openstackclient/tests/functional/network/v2/test_network_service_provider.py +2 -4
  52. openstackclient/tests/functional/network/v2/test_network_trunk.py +3 -3
  53. openstackclient/tests/functional/network/v2/test_port.py +2 -8
  54. openstackclient/tests/functional/network/v2/test_router.py +0 -6
  55. openstackclient/tests/functional/network/v2/test_security_group.py +1 -4
  56. openstackclient/tests/functional/network/v2/test_security_group_rule.py +1 -4
  57. openstackclient/tests/functional/network/v2/test_subnet.py +4 -22
  58. openstackclient/tests/functional/network/v2/test_subnet_pool.py +0 -6
  59. openstackclient/tests/unit/common/test_availability_zone.py +28 -30
  60. openstackclient/tests/unit/common/test_extension.py +1 -4
  61. openstackclient/tests/unit/common/test_limits.py +2 -4
  62. openstackclient/tests/unit/common/test_project_cleanup.py +3 -10
  63. openstackclient/tests/unit/common/test_quota.py +18 -24
  64. openstackclient/tests/unit/compute/v2/fakes.py +24 -11
  65. openstackclient/tests/unit/compute/v2/test_agent.py +1 -1
  66. openstackclient/tests/unit/compute/v2/test_aggregate.py +62 -72
  67. openstackclient/tests/unit/compute/v2/test_console.py +18 -30
  68. openstackclient/tests/unit/compute/v2/test_flavor.py +85 -89
  69. openstackclient/tests/unit/compute/v2/test_host.py +12 -19
  70. openstackclient/tests/unit/compute/v2/test_hypervisor.py +23 -25
  71. openstackclient/tests/unit/compute/v2/test_hypervisor_stats.py +2 -6
  72. openstackclient/tests/unit/compute/v2/test_keypair.py +25 -39
  73. openstackclient/tests/unit/compute/v2/test_server.py +316 -365
  74. openstackclient/tests/unit/compute/v2/test_server_backup.py +5 -17
  75. openstackclient/tests/unit/compute/v2/test_server_event.py +23 -25
  76. openstackclient/tests/unit/compute/v2/test_server_group.py +41 -33
  77. openstackclient/tests/unit/compute/v2/test_server_image.py +6 -18
  78. openstackclient/tests/unit/compute/v2/test_server_migration.py +45 -45
  79. openstackclient/tests/unit/compute/v2/test_server_volume.py +15 -31
  80. openstackclient/tests/unit/compute/v2/test_service.py +51 -56
  81. openstackclient/tests/unit/compute/v2/test_usage.py +10 -13
  82. openstackclient/tests/unit/fakes.py +4 -0
  83. openstackclient/tests/unit/identity/v3/test_mappings.py +9 -4
  84. openstackclient/tests/unit/identity/v3/test_trust.py +0 -2
  85. openstackclient/tests/unit/image/v1/fakes.py +2 -1
  86. openstackclient/tests/unit/image/v1/test_image.py +1 -1
  87. openstackclient/tests/unit/image/v2/fakes.py +82 -0
  88. openstackclient/tests/unit/image/v2/test_cache.py +214 -0
  89. openstackclient/tests/unit/image/v2/test_image.py +62 -4
  90. openstackclient/tests/unit/image/v2/test_metadef_namespaces.py +5 -19
  91. openstackclient/tests/unit/image/v2/test_metadef_objects.py +162 -0
  92. openstackclient/tests/unit/image/v2/test_metadef_properties.py +227 -0
  93. openstackclient/tests/unit/integ/cli/test_shell.py +0 -2
  94. openstackclient/tests/unit/network/test_common.py +3 -3
  95. openstackclient/tests/unit/network/v2/fakes.py +1 -0
  96. openstackclient/tests/unit/network/v2/test_default_security_group_rule.py +1133 -0
  97. openstackclient/tests/unit/network/v2/test_floating_ip_compute.py +5 -13
  98. openstackclient/tests/unit/network/v2/test_floating_ip_pool_compute.py +1 -9
  99. openstackclient/tests/unit/network/v2/test_network.py +33 -0
  100. openstackclient/tests/unit/network/v2/test_network_compute.py +5 -11
  101. openstackclient/tests/unit/network/v2/test_network_trunk.py +6 -8
  102. openstackclient/tests/unit/network/v2/test_port.py +83 -38
  103. openstackclient/tests/unit/network/v2/test_security_group_compute.py +7 -15
  104. openstackclient/tests/unit/network/v2/test_security_group_rule_compute.py +19 -27
  105. openstackclient/tests/unit/network/v2/test_security_group_rule_network.py +3 -6
  106. openstackclient/tests/unit/network/v2/test_subnet.py +92 -0
  107. openstackclient/tests/unit/network/v2/test_subnet_pool.py +11 -13
  108. openstackclient/tests/unit/test_shell.py +1 -7
  109. openstackclient/tests/unit/utils.py +10 -4
  110. openstackclient/tests/unit/volume/v1/fakes.py +7 -1
  111. openstackclient/tests/unit/volume/v1/test_qos_specs.py +2 -2
  112. openstackclient/tests/unit/volume/v1/test_service.py +1 -1
  113. openstackclient/tests/unit/volume/v1/test_transfer_request.py +2 -2
  114. openstackclient/tests/unit/volume/v1/test_type.py +2 -4
  115. openstackclient/tests/unit/volume/v1/test_volume.py +5 -7
  116. openstackclient/tests/unit/volume/v1/test_volume_backup.py +4 -4
  117. openstackclient/tests/unit/volume/v2/fakes.py +32 -12
  118. openstackclient/tests/unit/volume/v2/test_backup_record.py +1 -1
  119. openstackclient/tests/unit/volume/v2/test_consistency_group.py +4 -6
  120. openstackclient/tests/unit/volume/v2/test_consistency_group_snapshot.py +2 -4
  121. openstackclient/tests/unit/volume/v2/test_qos_specs.py +2 -2
  122. openstackclient/tests/unit/volume/v2/test_service.py +1 -1
  123. openstackclient/tests/unit/volume/v2/test_volume.py +78 -16
  124. openstackclient/tests/unit/volume/v2/test_volume_backend.py +10 -22
  125. openstackclient/tests/unit/volume/v2/test_volume_backup.py +76 -89
  126. openstackclient/tests/unit/volume/v2/test_volume_host.py +1 -1
  127. openstackclient/tests/unit/volume/v2/test_volume_snapshot.py +5 -7
  128. openstackclient/tests/unit/volume/v2/test_volume_transfer_request.py +4 -8
  129. openstackclient/tests/unit/volume/v2/test_volume_type.py +164 -24
  130. openstackclient/tests/unit/volume/v3/fakes.py +91 -15
  131. openstackclient/tests/unit/volume/v3/test_block_storage_cleanup.py +3 -7
  132. openstackclient/tests/unit/volume/v3/test_block_storage_cluster.py +11 -31
  133. openstackclient/tests/unit/volume/v3/test_block_storage_log_level.py +6 -16
  134. openstackclient/tests/unit/volume/v3/test_block_storage_manage.py +219 -157
  135. openstackclient/tests/unit/volume/v3/test_block_storage_resource_filter.py +32 -23
  136. openstackclient/tests/unit/volume/v3/test_volume.py +50 -48
  137. openstackclient/tests/unit/volume/v3/test_volume_attachment.py +17 -47
  138. openstackclient/tests/unit/volume/v3/test_volume_group.py +23 -65
  139. openstackclient/tests/unit/volume/v3/test_volume_group_snapshot.py +88 -77
  140. openstackclient/tests/unit/volume/v3/test_volume_group_type.py +14 -42
  141. openstackclient/tests/unit/volume/v3/test_volume_message.py +10 -28
  142. openstackclient/volume/v1/volume.py +2 -14
  143. openstackclient/volume/v2/volume.py +30 -15
  144. openstackclient/volume/v2/volume_backend.py +10 -18
  145. openstackclient/volume/v2/volume_backup.py +18 -15
  146. openstackclient/volume/v2/volume_snapshot.py +2 -12
  147. openstackclient/volume/v2/volume_type.py +211 -14
  148. openstackclient/volume/v3/block_storage_manage.py +72 -11
  149. openstackclient/volume/v3/block_storage_resource_filter.py +33 -11
  150. openstackclient/volume/v3/volume_attachment.py +2 -14
  151. openstackclient/volume/v3/volume_group_snapshot.py +27 -27
  152. openstackclient/volume/v3/volume_message.py +2 -13
  153. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/AUTHORS +11 -0
  154. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/METADATA +6 -5
  155. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/RECORD +160 -151
  156. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/entry_points.txt +23 -5
  157. python_openstackclient-6.5.0.dist-info/pbr.json +1 -0
  158. openstackclient/tests/unit/common/test_parseractions.py +0 -233
  159. python_openstackclient-6.3.0.dist-info/pbr.json +0 -1
  160. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/LICENSE +0 -0
  161. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/WHEEL +0 -0
  162. {python_openstackclient-6.3.0.dist-info → python_openstackclient-6.5.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1133 @@
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
+ from unittest import mock
15
+ from unittest.mock import call
16
+ import uuid
17
+
18
+ from openstack.network.v2 import _proxy
19
+ from openstack.network.v2 import (
20
+ default_security_group_rule as _default_security_group_rule,
21
+ )
22
+ from openstack.test import fakes as sdk_fakes
23
+ from osc_lib import exceptions
24
+
25
+ from openstackclient.network import utils as network_utils
26
+ from openstackclient.network.v2 import default_security_group_rule
27
+ from openstackclient.tests.unit.network.v2 import fakes as network_fakes
28
+ from openstackclient.tests.unit import utils as tests_utils
29
+
30
+
31
+ class TestDefaultSecurityGroupRule(network_fakes.TestNetworkV2):
32
+ def setUp(self):
33
+ super(TestDefaultSecurityGroupRule, self).setUp()
34
+
35
+ self.app.client_manager.sdk_connection = mock.Mock()
36
+ self.app.client_manager.sdk_connection.network = mock.Mock(
37
+ spec=_proxy.Proxy,
38
+ )
39
+ self.sdk_client = self.app.client_manager.sdk_connection.network
40
+
41
+
42
+ class TestCreateDefaultSecurityGroupRule(TestDefaultSecurityGroupRule):
43
+ expected_columns = (
44
+ 'description',
45
+ 'direction',
46
+ 'ether_type',
47
+ 'id',
48
+ 'port_range_max',
49
+ 'port_range_min',
50
+ 'protocol',
51
+ 'remote_address_group_id',
52
+ 'remote_group_id',
53
+ 'remote_ip_prefix',
54
+ 'used_in_default_sg',
55
+ 'used_in_non_default_sg',
56
+ )
57
+
58
+ expected_data = None
59
+
60
+ def _setup_default_security_group_rule(self, attrs=None):
61
+ default_security_group_rule_attrs = {
62
+ 'description': 'default-security-group-rule-description-'
63
+ + uuid.uuid4().hex,
64
+ 'direction': 'ingress',
65
+ 'ether_type': 'IPv4',
66
+ 'id': 'default-security-group-rule-id-' + uuid.uuid4().hex,
67
+ 'port_range_max': None,
68
+ 'port_range_min': None,
69
+ 'protocol': None,
70
+ 'remote_group_id': None,
71
+ 'remote_address_group_id': None,
72
+ 'remote_ip_prefix': '0.0.0.0/0',
73
+ 'location': 'MUNCHMUNCHMUNCH',
74
+ 'used_in_default_sg': False,
75
+ 'used_in_non_default_sg': True,
76
+ }
77
+ attrs = attrs or {}
78
+ # Overwrite default attributes.
79
+ default_security_group_rule_attrs.update(attrs)
80
+ self._default_sg_rule = sdk_fakes.generate_fake_resource(
81
+ _default_security_group_rule.DefaultSecurityGroupRule,
82
+ **default_security_group_rule_attrs
83
+ )
84
+
85
+ self.sdk_client.create_default_security_group_rule.return_value = (
86
+ self._default_sg_rule
87
+ )
88
+ self.expected_data = (
89
+ self._default_sg_rule.description,
90
+ self._default_sg_rule.direction,
91
+ self._default_sg_rule.ether_type,
92
+ self._default_sg_rule.id,
93
+ self._default_sg_rule.port_range_max,
94
+ self._default_sg_rule.port_range_min,
95
+ self._default_sg_rule.protocol,
96
+ self._default_sg_rule.remote_address_group_id,
97
+ self._default_sg_rule.remote_group_id,
98
+ self._default_sg_rule.remote_ip_prefix,
99
+ self._default_sg_rule.used_in_default_sg,
100
+ self._default_sg_rule.used_in_non_default_sg,
101
+ )
102
+
103
+ def setUp(self):
104
+ super(TestCreateDefaultSecurityGroupRule, self).setUp()
105
+
106
+ # Get the command object to test
107
+ self.cmd = default_security_group_rule.CreateDefaultSecurityGroupRule(
108
+ self.app, self.namespace
109
+ )
110
+
111
+ def test_create_all_remote_options(self):
112
+ arglist = [
113
+ '--remote-ip',
114
+ '10.10.0.0/24',
115
+ '--remote-group',
116
+ 'test-remote-group-id',
117
+ '--remote-address-group',
118
+ 'test-remote-address-group-id',
119
+ ]
120
+ self.assertRaises(
121
+ tests_utils.ParserException,
122
+ self.check_parser,
123
+ self.cmd,
124
+ arglist,
125
+ [],
126
+ )
127
+
128
+ def test_create_bad_ethertype(self):
129
+ arglist = [
130
+ '--ethertype',
131
+ 'foo',
132
+ ]
133
+ self.assertRaises(
134
+ tests_utils.ParserException,
135
+ self.check_parser,
136
+ self.cmd,
137
+ arglist,
138
+ [],
139
+ )
140
+
141
+ def test_lowercase_ethertype(self):
142
+ arglist = [
143
+ '--ethertype',
144
+ 'ipv4',
145
+ ]
146
+ parsed_args = self.check_parser(self.cmd, arglist, [])
147
+ self.assertEqual('IPv4', parsed_args.ethertype)
148
+
149
+ def test_lowercase_v6_ethertype(self):
150
+ arglist = [
151
+ '--ethertype',
152
+ 'ipv6',
153
+ ]
154
+ parsed_args = self.check_parser(self.cmd, arglist, [])
155
+ self.assertEqual('IPv6', parsed_args.ethertype)
156
+
157
+ def test_proper_case_ethertype(self):
158
+ arglist = [
159
+ '--ethertype',
160
+ 'IPv6',
161
+ ]
162
+ parsed_args = self.check_parser(self.cmd, arglist, [])
163
+ self.assertEqual('IPv6', parsed_args.ethertype)
164
+
165
+ def test_create_all_port_range_options(self):
166
+ arglist = [
167
+ '--dst-port',
168
+ '80:80',
169
+ '--icmp-type',
170
+ '3',
171
+ '--icmp-code',
172
+ '1',
173
+ ]
174
+ verifylist = [
175
+ ('dst_port', (80, 80)),
176
+ ('icmp_type', 3),
177
+ ('icmp_code', 1),
178
+ ]
179
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
180
+ self.assertRaises(
181
+ exceptions.CommandError, self.cmd.take_action, parsed_args
182
+ )
183
+
184
+ def test_create_default_rule(self):
185
+ self._setup_default_security_group_rule(
186
+ {
187
+ 'protocol': 'tcp',
188
+ 'port_range_max': 443,
189
+ 'port_range_min': 443,
190
+ }
191
+ )
192
+ arglist = [
193
+ '--protocol',
194
+ 'tcp',
195
+ '--dst-port',
196
+ str(self._default_sg_rule.port_range_min),
197
+ ]
198
+ verifylist = [
199
+ (
200
+ 'dst_port',
201
+ (
202
+ self._default_sg_rule.port_range_min,
203
+ self._default_sg_rule.port_range_max,
204
+ ),
205
+ ),
206
+ ]
207
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
208
+
209
+ columns, data = self.cmd.take_action(parsed_args)
210
+
211
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
212
+ **{
213
+ 'direction': self._default_sg_rule.direction,
214
+ 'ethertype': self._default_sg_rule.ether_type,
215
+ 'port_range_max': self._default_sg_rule.port_range_max,
216
+ 'port_range_min': self._default_sg_rule.port_range_min,
217
+ 'protocol': self._default_sg_rule.protocol,
218
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
219
+ 'used_in_default_sg': False,
220
+ 'used_in_non_default_sg': True,
221
+ }
222
+ )
223
+ self.assertEqual(self.expected_columns, columns)
224
+ self.assertEqual(self.expected_data, data)
225
+
226
+ def test_create_protocol_any(self):
227
+ self._setup_default_security_group_rule(
228
+ {
229
+ 'protocol': None,
230
+ 'remote_ip_prefix': '10.0.2.0/24',
231
+ }
232
+ )
233
+ arglist = [
234
+ '--protocol',
235
+ 'any',
236
+ '--remote-ip',
237
+ self._default_sg_rule.remote_ip_prefix,
238
+ ]
239
+ verifylist = [
240
+ ('protocol', 'any'),
241
+ ('remote_ip', self._default_sg_rule.remote_ip_prefix),
242
+ ]
243
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
244
+
245
+ columns, data = self.cmd.take_action(parsed_args)
246
+
247
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
248
+ **{
249
+ 'direction': self._default_sg_rule.direction,
250
+ 'ethertype': self._default_sg_rule.ether_type,
251
+ 'protocol': self._default_sg_rule.protocol,
252
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
253
+ 'used_in_default_sg': False,
254
+ 'used_in_non_default_sg': True,
255
+ }
256
+ )
257
+ self.assertEqual(self.expected_columns, columns)
258
+ self.assertEqual(self.expected_data, data)
259
+
260
+ def test_create_remote_address_group(self):
261
+ self._setup_default_security_group_rule(
262
+ {
263
+ 'protocol': 'icmp',
264
+ 'remote_address_group_id': 'remote-address-group-id',
265
+ }
266
+ )
267
+ arglist = [
268
+ '--protocol',
269
+ 'icmp',
270
+ '--remote-address-group',
271
+ self._default_sg_rule.remote_address_group_id,
272
+ ]
273
+ verifylist = [
274
+ (
275
+ 'remote_address_group',
276
+ self._default_sg_rule.remote_address_group_id,
277
+ ),
278
+ ]
279
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
280
+
281
+ columns, data = self.cmd.take_action(parsed_args)
282
+
283
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
284
+ **{
285
+ 'direction': self._default_sg_rule.direction,
286
+ 'ethertype': self._default_sg_rule.ether_type,
287
+ 'protocol': self._default_sg_rule.protocol,
288
+ 'remote_address_group_id': self._default_sg_rule.remote_address_group_id,
289
+ 'used_in_default_sg': False,
290
+ 'used_in_non_default_sg': True,
291
+ }
292
+ )
293
+ self.assertEqual(self.expected_columns, columns)
294
+ self.assertEqual(self.expected_data, data)
295
+
296
+ def test_create_remote_group(self):
297
+ self._setup_default_security_group_rule(
298
+ {
299
+ 'protocol': 'tcp',
300
+ 'port_range_max': 22,
301
+ 'port_range_min': 22,
302
+ }
303
+ )
304
+ arglist = [
305
+ '--protocol',
306
+ 'tcp',
307
+ '--dst-port',
308
+ str(self._default_sg_rule.port_range_min),
309
+ '--ingress',
310
+ '--remote-group',
311
+ 'remote-group-id',
312
+ ]
313
+ verifylist = [
314
+ (
315
+ 'dst_port',
316
+ (
317
+ self._default_sg_rule.port_range_min,
318
+ self._default_sg_rule.port_range_max,
319
+ ),
320
+ ),
321
+ ('ingress', True),
322
+ ('remote_group', 'remote-group-id'),
323
+ ]
324
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
325
+
326
+ columns, data = self.cmd.take_action(parsed_args)
327
+
328
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
329
+ **{
330
+ 'direction': self._default_sg_rule.direction,
331
+ 'ethertype': self._default_sg_rule.ether_type,
332
+ 'port_range_max': self._default_sg_rule.port_range_max,
333
+ 'port_range_min': self._default_sg_rule.port_range_min,
334
+ 'protocol': self._default_sg_rule.protocol,
335
+ 'remote_group_id': 'remote-group-id',
336
+ 'used_in_default_sg': False,
337
+ 'used_in_non_default_sg': True,
338
+ }
339
+ )
340
+ self.assertEqual(self.expected_columns, columns)
341
+ self.assertEqual(self.expected_data, data)
342
+
343
+ def test_create_source_group(self):
344
+ self._setup_default_security_group_rule(
345
+ {
346
+ 'remote_group_id': 'remote-group-id',
347
+ }
348
+ )
349
+ arglist = [
350
+ '--ingress',
351
+ '--remote-group',
352
+ 'remote-group-id',
353
+ ]
354
+ verifylist = [
355
+ ('ingress', True),
356
+ ('remote_group', 'remote-group-id'),
357
+ ]
358
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
359
+
360
+ columns, data = self.cmd.take_action(parsed_args)
361
+
362
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
363
+ **{
364
+ 'direction': self._default_sg_rule.direction,
365
+ 'ethertype': self._default_sg_rule.ether_type,
366
+ 'protocol': self._default_sg_rule.protocol,
367
+ 'remote_group_id': 'remote-group-id',
368
+ 'used_in_default_sg': False,
369
+ 'used_in_non_default_sg': True,
370
+ }
371
+ )
372
+ self.assertEqual(self.expected_columns, columns)
373
+ self.assertEqual(self.expected_data, data)
374
+
375
+ def test_create_source_ip(self):
376
+ self._setup_default_security_group_rule(
377
+ {
378
+ 'protocol': 'icmp',
379
+ 'remote_ip_prefix': '10.0.2.0/24',
380
+ }
381
+ )
382
+ arglist = [
383
+ '--protocol',
384
+ self._default_sg_rule.protocol,
385
+ '--remote-ip',
386
+ self._default_sg_rule.remote_ip_prefix,
387
+ ]
388
+ verifylist = [
389
+ ('protocol', self._default_sg_rule.protocol),
390
+ ('remote_ip', self._default_sg_rule.remote_ip_prefix),
391
+ ]
392
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
393
+
394
+ columns, data = self.cmd.take_action(parsed_args)
395
+
396
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
397
+ **{
398
+ 'direction': self._default_sg_rule.direction,
399
+ 'ethertype': self._default_sg_rule.ether_type,
400
+ 'protocol': self._default_sg_rule.protocol,
401
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
402
+ 'used_in_default_sg': False,
403
+ 'used_in_non_default_sg': True,
404
+ }
405
+ )
406
+ self.assertEqual(self.expected_columns, columns)
407
+ self.assertEqual(self.expected_data, data)
408
+
409
+ def test_create_remote_ip(self):
410
+ self._setup_default_security_group_rule(
411
+ {
412
+ 'protocol': 'icmp',
413
+ 'remote_ip_prefix': '10.0.2.0/24',
414
+ }
415
+ )
416
+ arglist = [
417
+ '--protocol',
418
+ self._default_sg_rule.protocol,
419
+ '--remote-ip',
420
+ self._default_sg_rule.remote_ip_prefix,
421
+ ]
422
+ verifylist = [
423
+ ('protocol', self._default_sg_rule.protocol),
424
+ ('remote_ip', self._default_sg_rule.remote_ip_prefix),
425
+ ]
426
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
427
+
428
+ columns, data = self.cmd.take_action(parsed_args)
429
+
430
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
431
+ **{
432
+ 'direction': self._default_sg_rule.direction,
433
+ 'ethertype': self._default_sg_rule.ether_type,
434
+ 'protocol': self._default_sg_rule.protocol,
435
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
436
+ 'used_in_default_sg': False,
437
+ 'used_in_non_default_sg': True,
438
+ }
439
+ )
440
+ self.assertEqual(self.expected_columns, columns)
441
+ self.assertEqual(self.expected_data, data)
442
+
443
+ def test_create_tcp_with_icmp_type(self):
444
+ arglist = [
445
+ '--protocol',
446
+ 'tcp',
447
+ '--icmp-type',
448
+ '15',
449
+ ]
450
+ verifylist = [
451
+ ('protocol', 'tcp'),
452
+ ('icmp_type', 15),
453
+ ]
454
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
455
+ self.assertRaises(
456
+ exceptions.CommandError, self.cmd.take_action, parsed_args
457
+ )
458
+
459
+ def test_create_icmp_code(self):
460
+ arglist = [
461
+ '--protocol',
462
+ '1',
463
+ '--icmp-code',
464
+ '1',
465
+ ]
466
+ verifylist = [
467
+ ('protocol', '1'),
468
+ ('icmp_code', 1),
469
+ ]
470
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
471
+ self.assertRaises(
472
+ exceptions.CommandError, self.cmd.take_action, parsed_args
473
+ )
474
+
475
+ def test_create_icmp_code_zero(self):
476
+ self._setup_default_security_group_rule(
477
+ {
478
+ 'port_range_min': 15,
479
+ 'port_range_max': 0,
480
+ 'protocol': 'icmp',
481
+ 'remote_ip_prefix': '0.0.0.0/0',
482
+ }
483
+ )
484
+ arglist = [
485
+ '--protocol',
486
+ self._default_sg_rule.protocol,
487
+ '--icmp-type',
488
+ str(self._default_sg_rule.port_range_min),
489
+ '--icmp-code',
490
+ str(self._default_sg_rule.port_range_max),
491
+ ]
492
+ verifylist = [
493
+ ('protocol', self._default_sg_rule.protocol),
494
+ ('icmp_code', self._default_sg_rule.port_range_max),
495
+ ('icmp_type', self._default_sg_rule.port_range_min),
496
+ ]
497
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
498
+ columns, data = self.cmd.take_action(parsed_args)
499
+ self.assertEqual(self.expected_columns, columns)
500
+ self.assertEqual(self.expected_data, data)
501
+
502
+ def test_create_icmp_code_greater_than_zero(self):
503
+ self._setup_default_security_group_rule(
504
+ {
505
+ 'port_range_min': 15,
506
+ 'port_range_max': 18,
507
+ 'protocol': 'icmp',
508
+ 'remote_ip_prefix': '0.0.0.0/0',
509
+ }
510
+ )
511
+ arglist = [
512
+ '--protocol',
513
+ self._default_sg_rule.protocol,
514
+ '--icmp-type',
515
+ str(self._default_sg_rule.port_range_min),
516
+ '--icmp-code',
517
+ str(self._default_sg_rule.port_range_max),
518
+ ]
519
+ verifylist = [
520
+ ('protocol', self._default_sg_rule.protocol),
521
+ ('icmp_type', self._default_sg_rule.port_range_min),
522
+ ('icmp_code', self._default_sg_rule.port_range_max),
523
+ ]
524
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
525
+ columns, data = self.cmd.take_action(parsed_args)
526
+ self.assertEqual(self.expected_columns, columns)
527
+ self.assertEqual(self.expected_data, data)
528
+
529
+ def test_create_icmp_code_negative_value(self):
530
+ self._setup_default_security_group_rule(
531
+ {
532
+ 'port_range_min': 15,
533
+ 'port_range_max': None,
534
+ 'protocol': 'icmp',
535
+ 'remote_ip_prefix': '0.0.0.0/0',
536
+ }
537
+ )
538
+ arglist = [
539
+ '--protocol',
540
+ self._default_sg_rule.protocol,
541
+ '--icmp-type',
542
+ str(self._default_sg_rule.port_range_min),
543
+ '--icmp-code',
544
+ '-2',
545
+ ]
546
+ verifylist = [
547
+ ('protocol', self._default_sg_rule.protocol),
548
+ ('icmp_type', self._default_sg_rule.port_range_min),
549
+ ('icmp_code', -2),
550
+ ]
551
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
552
+ columns, data = self.cmd.take_action(parsed_args)
553
+ self.assertEqual(self.expected_columns, columns)
554
+ self.assertEqual(self.expected_data, data)
555
+
556
+ def test_create_icmp_type(self):
557
+ self._setup_default_security_group_rule(
558
+ {
559
+ 'port_range_min': 15,
560
+ 'protocol': 'icmp',
561
+ 'remote_ip_prefix': '0.0.0.0/0',
562
+ }
563
+ )
564
+ arglist = [
565
+ '--icmp-type',
566
+ str(self._default_sg_rule.port_range_min),
567
+ '--protocol',
568
+ self._default_sg_rule.protocol,
569
+ ]
570
+ verifylist = [
571
+ ('dst_port', None),
572
+ ('icmp_type', self._default_sg_rule.port_range_min),
573
+ ('icmp_code', None),
574
+ ('protocol', self._default_sg_rule.protocol),
575
+ ]
576
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
577
+
578
+ columns, data = self.cmd.take_action(parsed_args)
579
+
580
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
581
+ **{
582
+ 'direction': self._default_sg_rule.direction,
583
+ 'ethertype': self._default_sg_rule.ether_type,
584
+ 'port_range_min': self._default_sg_rule.port_range_min,
585
+ 'protocol': self._default_sg_rule.protocol,
586
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
587
+ 'used_in_default_sg': False,
588
+ 'used_in_non_default_sg': True,
589
+ }
590
+ )
591
+ self.assertEqual(self.expected_columns, columns)
592
+ self.assertEqual(self.expected_data, data)
593
+
594
+ def test_create_icmp_type_zero(self):
595
+ self._setup_default_security_group_rule(
596
+ {
597
+ 'port_range_min': 0,
598
+ 'protocol': 'icmp',
599
+ 'remote_ip_prefix': '0.0.0.0/0',
600
+ }
601
+ )
602
+ arglist = [
603
+ '--icmp-type',
604
+ str(self._default_sg_rule.port_range_min),
605
+ '--protocol',
606
+ self._default_sg_rule.protocol,
607
+ ]
608
+ verifylist = [
609
+ ('dst_port', None),
610
+ ('icmp_type', self._default_sg_rule.port_range_min),
611
+ ('icmp_code', None),
612
+ ('protocol', self._default_sg_rule.protocol),
613
+ ]
614
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
615
+
616
+ columns, data = self.cmd.take_action(parsed_args)
617
+
618
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
619
+ **{
620
+ 'direction': self._default_sg_rule.direction,
621
+ 'ethertype': self._default_sg_rule.ether_type,
622
+ 'port_range_min': self._default_sg_rule.port_range_min,
623
+ 'protocol': self._default_sg_rule.protocol,
624
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
625
+ 'used_in_default_sg': False,
626
+ 'used_in_non_default_sg': True,
627
+ }
628
+ )
629
+ self.assertEqual(self.expected_columns, columns)
630
+ self.assertEqual(self.expected_data, data)
631
+
632
+ def test_create_icmp_type_greater_than_zero(self):
633
+ self._setup_default_security_group_rule(
634
+ {
635
+ 'port_range_min': 13, # timestamp
636
+ 'protocol': 'icmp',
637
+ 'remote_ip_prefix': '0.0.0.0/0',
638
+ }
639
+ )
640
+ arglist = [
641
+ '--icmp-type',
642
+ str(self._default_sg_rule.port_range_min),
643
+ '--protocol',
644
+ self._default_sg_rule.protocol,
645
+ ]
646
+ verifylist = [
647
+ ('dst_port', None),
648
+ ('icmp_type', self._default_sg_rule.port_range_min),
649
+ ('icmp_code', None),
650
+ ('protocol', self._default_sg_rule.protocol),
651
+ ]
652
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
653
+
654
+ columns, data = self.cmd.take_action(parsed_args)
655
+
656
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
657
+ **{
658
+ 'direction': self._default_sg_rule.direction,
659
+ 'ethertype': self._default_sg_rule.ether_type,
660
+ 'port_range_min': self._default_sg_rule.port_range_min,
661
+ 'protocol': self._default_sg_rule.protocol,
662
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
663
+ 'used_in_default_sg': False,
664
+ 'used_in_non_default_sg': True,
665
+ }
666
+ )
667
+ self.assertEqual(self.expected_columns, columns)
668
+ self.assertEqual(self.expected_data, data)
669
+
670
+ def test_create_icmp_type_negative_value(self):
671
+ self._setup_default_security_group_rule(
672
+ {
673
+ 'port_range_min': None, # timestamp
674
+ 'protocol': 'icmp',
675
+ 'remote_ip_prefix': '0.0.0.0/0',
676
+ }
677
+ )
678
+ arglist = [
679
+ '--icmp-type',
680
+ '-13',
681
+ '--protocol',
682
+ self._default_sg_rule.protocol,
683
+ ]
684
+ verifylist = [
685
+ ('dst_port', None),
686
+ ('icmp_type', -13),
687
+ ('icmp_code', None),
688
+ ('protocol', self._default_sg_rule.protocol),
689
+ ]
690
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
691
+
692
+ columns, data = self.cmd.take_action(parsed_args)
693
+
694
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
695
+ **{
696
+ 'direction': self._default_sg_rule.direction,
697
+ 'ethertype': self._default_sg_rule.ether_type,
698
+ 'protocol': self._default_sg_rule.protocol,
699
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
700
+ 'used_in_default_sg': False,
701
+ 'used_in_non_default_sg': True,
702
+ }
703
+ )
704
+ self.assertEqual(self.expected_columns, columns)
705
+ self.assertEqual(self.expected_data, data)
706
+
707
+ def test_create_ipv6_icmp_type_code(self):
708
+ self._setup_default_security_group_rule(
709
+ {
710
+ 'ether_type': 'IPv6',
711
+ 'port_range_min': 139,
712
+ 'port_range_max': 2,
713
+ 'protocol': 'ipv6-icmp',
714
+ 'remote_ip_prefix': '::/0',
715
+ }
716
+ )
717
+ arglist = [
718
+ '--icmp-type',
719
+ str(self._default_sg_rule.port_range_min),
720
+ '--icmp-code',
721
+ str(self._default_sg_rule.port_range_max),
722
+ '--protocol',
723
+ self._default_sg_rule.protocol,
724
+ ]
725
+ verifylist = [
726
+ ('dst_port', None),
727
+ ('icmp_type', self._default_sg_rule.port_range_min),
728
+ ('icmp_code', self._default_sg_rule.port_range_max),
729
+ ('protocol', self._default_sg_rule.protocol),
730
+ ]
731
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
732
+
733
+ columns, data = self.cmd.take_action(parsed_args)
734
+
735
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
736
+ **{
737
+ 'direction': self._default_sg_rule.direction,
738
+ 'ethertype': self._default_sg_rule.ether_type,
739
+ 'port_range_min': self._default_sg_rule.port_range_min,
740
+ 'port_range_max': self._default_sg_rule.port_range_max,
741
+ 'protocol': self._default_sg_rule.protocol,
742
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
743
+ 'used_in_default_sg': False,
744
+ 'used_in_non_default_sg': True,
745
+ }
746
+ )
747
+ self.assertEqual(self.expected_columns, columns)
748
+ self.assertEqual(self.expected_data, data)
749
+
750
+ def test_create_icmpv6_type(self):
751
+ self._setup_default_security_group_rule(
752
+ {
753
+ 'ether_type': 'IPv6',
754
+ 'port_range_min': 139,
755
+ 'protocol': 'icmpv6',
756
+ 'remote_ip_prefix': '::/0',
757
+ }
758
+ )
759
+ arglist = [
760
+ '--icmp-type',
761
+ str(self._default_sg_rule.port_range_min),
762
+ '--protocol',
763
+ self._default_sg_rule.protocol,
764
+ ]
765
+ verifylist = [
766
+ ('dst_port', None),
767
+ ('icmp_type', self._default_sg_rule.port_range_min),
768
+ ('icmp_code', None),
769
+ ('protocol', self._default_sg_rule.protocol),
770
+ ]
771
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
772
+
773
+ columns, data = self.cmd.take_action(parsed_args)
774
+
775
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
776
+ **{
777
+ 'direction': self._default_sg_rule.direction,
778
+ 'ethertype': self._default_sg_rule.ether_type,
779
+ 'port_range_min': self._default_sg_rule.port_range_min,
780
+ 'protocol': self._default_sg_rule.protocol,
781
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
782
+ 'used_in_default_sg': False,
783
+ 'used_in_non_default_sg': True,
784
+ }
785
+ )
786
+ self.assertEqual(self.expected_columns, columns)
787
+ self.assertEqual(self.expected_data, data)
788
+
789
+ def test_create_with_description(self):
790
+ self._setup_default_security_group_rule(
791
+ {
792
+ 'description': 'Setting SGR',
793
+ }
794
+ )
795
+ arglist = [
796
+ '--description',
797
+ self._default_sg_rule.description,
798
+ ]
799
+ verifylist = [
800
+ ('description', self._default_sg_rule.description),
801
+ ]
802
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
803
+
804
+ columns, data = self.cmd.take_action(parsed_args)
805
+
806
+ self.sdk_client.create_default_security_group_rule.assert_called_once_with(
807
+ **{
808
+ 'description': self._default_sg_rule.description,
809
+ 'direction': self._default_sg_rule.direction,
810
+ 'ethertype': self._default_sg_rule.ether_type,
811
+ 'protocol': self._default_sg_rule.protocol,
812
+ 'remote_ip_prefix': self._default_sg_rule.remote_ip_prefix,
813
+ 'used_in_default_sg': False,
814
+ 'used_in_non_default_sg': True,
815
+ }
816
+ )
817
+ self.assertEqual(self.expected_columns, columns)
818
+ self.assertEqual(self.expected_data, data)
819
+
820
+
821
+ class TestDeleteDefaultSecurityGroupRule(TestDefaultSecurityGroupRule):
822
+ # The default security group rules to be deleted.
823
+ default_security_group_rule_attrs = {
824
+ 'direction': 'ingress',
825
+ 'ether_type': 'IPv4',
826
+ 'port_range_max': None,
827
+ 'port_range_min': None,
828
+ 'protocol': None,
829
+ 'remote_group_id': None,
830
+ 'remote_address_group_id': None,
831
+ 'remote_ip_prefix': '0.0.0.0/0',
832
+ 'location': 'MUNCHMUNCHMUNCH',
833
+ 'used_in_default_sg': False,
834
+ 'used_in_non_default_sg': True,
835
+ }
836
+ _default_sg_rules = list(
837
+ sdk_fakes.generate_fake_resources(
838
+ _default_security_group_rule.DefaultSecurityGroupRule,
839
+ count=2,
840
+ attrs=default_security_group_rule_attrs,
841
+ )
842
+ )
843
+
844
+ def setUp(self):
845
+ super(TestDeleteDefaultSecurityGroupRule, self).setUp()
846
+
847
+ self.sdk_client.delete_default_security_group_rule.return_value = None
848
+
849
+ # Get the command object to test
850
+ self.cmd = default_security_group_rule.DeleteDefaultSecurityGroupRule(
851
+ self.app, self.namespace
852
+ )
853
+
854
+ def test_default_security_group_rule_delete(self):
855
+ arglist = [
856
+ self._default_sg_rules[0].id,
857
+ ]
858
+ verifylist = [
859
+ ('rule', [self._default_sg_rules[0].id]),
860
+ ]
861
+ self.sdk_client.find_default_security_group_rule.return_value = (
862
+ self._default_sg_rules[0]
863
+ )
864
+
865
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
866
+
867
+ result = self.cmd.take_action(parsed_args)
868
+
869
+ self.sdk_client.delete_default_security_group_rule.assert_called_once_with(
870
+ self._default_sg_rules[0]
871
+ )
872
+ self.assertIsNone(result)
873
+
874
+ def test_multi_default_security_group_rules_delete(self):
875
+ arglist = []
876
+ verifylist = []
877
+
878
+ for s in self._default_sg_rules:
879
+ arglist.append(s.id)
880
+ verifylist = [
881
+ ('rule', arglist),
882
+ ]
883
+ self.sdk_client.find_default_security_group_rule.side_effect = (
884
+ self._default_sg_rules
885
+ )
886
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
887
+
888
+ result = self.cmd.take_action(parsed_args)
889
+
890
+ calls = []
891
+ for s in self._default_sg_rules:
892
+ calls.append(call(s))
893
+ self.sdk_client.delete_default_security_group_rule.assert_has_calls(
894
+ calls
895
+ )
896
+ self.assertIsNone(result)
897
+
898
+ def test_multi_default_security_group_rules_delete_with_exception(self):
899
+ arglist = [
900
+ self._default_sg_rules[0].id,
901
+ 'unexist_rule',
902
+ ]
903
+ verifylist = [
904
+ ('rule', [self._default_sg_rules[0].id, 'unexist_rule']),
905
+ ]
906
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
907
+
908
+ find_mock_result = [
909
+ self._default_sg_rules[0],
910
+ exceptions.CommandError,
911
+ ]
912
+ self.sdk_client.find_default_security_group_rule = mock.Mock(
913
+ side_effect=find_mock_result
914
+ )
915
+
916
+ try:
917
+ self.cmd.take_action(parsed_args)
918
+ self.fail('CommandError should be raised.')
919
+ except exceptions.CommandError as e:
920
+ self.assertEqual('1 of 2 default rules failed to delete.', str(e))
921
+
922
+ self.sdk_client.find_default_security_group_rule.assert_any_call(
923
+ self._default_sg_rules[0].id, ignore_missing=False
924
+ )
925
+ self.sdk_client.find_default_security_group_rule.assert_any_call(
926
+ 'unexist_rule', ignore_missing=False
927
+ )
928
+ self.sdk_client.delete_default_security_group_rule.assert_called_once_with(
929
+ self._default_sg_rules[0]
930
+ )
931
+
932
+
933
+ class TestListDefaultSecurityGroupRule(TestDefaultSecurityGroupRule):
934
+ # The security group rule to be listed.
935
+ _default_sg_rule_tcp = sdk_fakes.generate_fake_resource(
936
+ _default_security_group_rule.DefaultSecurityGroupRule,
937
+ **{'protocol': 'tcp', 'port_range_max': 80, 'port_range_min': 80}
938
+ )
939
+ _default_sg_rule_icmp = sdk_fakes.generate_fake_resource(
940
+ _default_security_group_rule.DefaultSecurityGroupRule,
941
+ **{'protocol': 'icmp', 'remote_ip_prefix': '10.0.2.0/24'}
942
+ )
943
+ _default_sg_rules = [
944
+ _default_sg_rule_tcp,
945
+ _default_sg_rule_icmp,
946
+ ]
947
+
948
+ expected_columns = (
949
+ 'ID',
950
+ 'IP Protocol',
951
+ 'Ethertype',
952
+ 'IP Range',
953
+ 'Port Range',
954
+ 'Direction',
955
+ 'Remote Security Group',
956
+ 'Remote Address Group',
957
+ 'Used in default Security Group',
958
+ 'Used in custom Security Group',
959
+ )
960
+
961
+ expected_data = []
962
+ expected_data_no_group = []
963
+ for _default_sg_rule in _default_sg_rules:
964
+ expected_data.append(
965
+ (
966
+ _default_sg_rule.id,
967
+ _default_sg_rule.protocol,
968
+ _default_sg_rule.ether_type,
969
+ _default_sg_rule.remote_ip_prefix,
970
+ network_utils.format_network_port_range(_default_sg_rule),
971
+ _default_sg_rule.direction,
972
+ _default_sg_rule.remote_group_id,
973
+ _default_sg_rule.remote_address_group_id,
974
+ _default_sg_rule.used_in_default_sg,
975
+ _default_sg_rule.used_in_non_default_sg,
976
+ )
977
+ )
978
+
979
+ def setUp(self):
980
+ super(TestListDefaultSecurityGroupRule, self).setUp()
981
+
982
+ self.sdk_client.default_security_group_rules.return_value = (
983
+ self._default_sg_rules
984
+ )
985
+
986
+ # Get the command object to test
987
+ self.cmd = default_security_group_rule.ListDefaultSecurityGroupRule(
988
+ self.app, self.namespace
989
+ )
990
+
991
+ def test_list_default(self):
992
+ self._default_sg_rule_tcp.port_range_min = 80
993
+ parsed_args = self.check_parser(self.cmd, [], [])
994
+
995
+ columns, data = self.cmd.take_action(parsed_args)
996
+
997
+ self.sdk_client.default_security_group_rules.assert_called_once_with(
998
+ **{}
999
+ )
1000
+ self.assertEqual(self.expected_columns, columns)
1001
+ self.assertEqual(self.expected_data, list(data))
1002
+
1003
+ def test_list_with_protocol(self):
1004
+ self._default_sg_rule_tcp.port_range_min = 80
1005
+ arglist = [
1006
+ '--protocol',
1007
+ 'tcp',
1008
+ ]
1009
+ verifylist = [
1010
+ ('protocol', 'tcp'),
1011
+ ]
1012
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1013
+
1014
+ columns, data = self.cmd.take_action(parsed_args)
1015
+
1016
+ self.sdk_client.default_security_group_rules.assert_called_once_with(
1017
+ **{
1018
+ 'protocol': 'tcp',
1019
+ }
1020
+ )
1021
+ self.assertEqual(self.expected_columns, columns)
1022
+ self.assertEqual(self.expected_data, list(data))
1023
+
1024
+ def test_list_with_ingress(self):
1025
+ self._default_sg_rule_tcp.port_range_min = 80
1026
+ arglist = [
1027
+ '--ingress',
1028
+ ]
1029
+ verifylist = [
1030
+ ('ingress', True),
1031
+ ]
1032
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1033
+
1034
+ columns, data = self.cmd.take_action(parsed_args)
1035
+
1036
+ self.sdk_client.default_security_group_rules.assert_called_once_with(
1037
+ **{
1038
+ 'direction': 'ingress',
1039
+ }
1040
+ )
1041
+ self.assertEqual(self.expected_columns, columns)
1042
+ self.assertEqual(self.expected_data, list(data))
1043
+
1044
+ def test_list_with_wrong_egress(self):
1045
+ self._default_sg_rule_tcp.port_range_min = 80
1046
+ arglist = [
1047
+ '--egress',
1048
+ ]
1049
+ verifylist = [
1050
+ ('egress', True),
1051
+ ]
1052
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1053
+
1054
+ columns, data = self.cmd.take_action(parsed_args)
1055
+
1056
+ self.sdk_client.default_security_group_rules.assert_called_once_with(
1057
+ **{
1058
+ 'direction': 'egress',
1059
+ }
1060
+ )
1061
+ self.assertEqual(self.expected_columns, columns)
1062
+ self.assertEqual(self.expected_data, list(data))
1063
+
1064
+
1065
+ class TestShowDefaultSecurityGroupRule(TestDefaultSecurityGroupRule):
1066
+ # The default security group rule to be shown.
1067
+ _default_sg_rule = sdk_fakes.generate_fake_resource(
1068
+ _default_security_group_rule.DefaultSecurityGroupRule
1069
+ )
1070
+
1071
+ columns = (
1072
+ 'description',
1073
+ 'direction',
1074
+ 'ether_type',
1075
+ 'id',
1076
+ 'port_range_max',
1077
+ 'port_range_min',
1078
+ 'protocol',
1079
+ 'remote_address_group_id',
1080
+ 'remote_group_id',
1081
+ 'remote_ip_prefix',
1082
+ 'used_in_default_sg',
1083
+ 'used_in_non_default_sg',
1084
+ )
1085
+
1086
+ data = (
1087
+ _default_sg_rule.description,
1088
+ _default_sg_rule.direction,
1089
+ _default_sg_rule.ether_type,
1090
+ _default_sg_rule.id,
1091
+ _default_sg_rule.port_range_max,
1092
+ _default_sg_rule.port_range_min,
1093
+ _default_sg_rule.protocol,
1094
+ _default_sg_rule.remote_address_group_id,
1095
+ _default_sg_rule.remote_group_id,
1096
+ _default_sg_rule.remote_ip_prefix,
1097
+ _default_sg_rule.used_in_default_sg,
1098
+ _default_sg_rule.used_in_non_default_sg,
1099
+ )
1100
+
1101
+ def setUp(self):
1102
+ super(TestShowDefaultSecurityGroupRule, self).setUp()
1103
+
1104
+ self.sdk_client.find_default_security_group_rule.return_value = (
1105
+ self._default_sg_rule
1106
+ )
1107
+
1108
+ # Get the command object to test
1109
+ self.cmd = default_security_group_rule.ShowDefaultSecurityGroupRule(
1110
+ self.app, self.namespace
1111
+ )
1112
+
1113
+ def test_show_no_options(self):
1114
+ self.assertRaises(
1115
+ tests_utils.ParserException, self.check_parser, self.cmd, [], []
1116
+ )
1117
+
1118
+ def test_show_all_options(self):
1119
+ arglist = [
1120
+ self._default_sg_rule.id,
1121
+ ]
1122
+ verifylist = [
1123
+ ('rule', self._default_sg_rule.id),
1124
+ ]
1125
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1126
+
1127
+ columns, data = self.cmd.take_action(parsed_args)
1128
+
1129
+ self.sdk_client.find_default_security_group_rule.assert_called_once_with(
1130
+ self._default_sg_rule.id, ignore_missing=False
1131
+ )
1132
+ self.assertEqual(self.columns, columns)
1133
+ self.assertEqual(self.data, data)