|
16 | 16 | from openstackclient.network.v2 import security_group |
17 | 17 | from openstackclient.tests.compute.v2 import fakes as compute_fakes |
18 | 18 | from openstackclient.tests.network.v2 import fakes as network_fakes |
| 19 | +from openstackclient.tests import utils as tests_utils |
19 | 20 |
|
20 | 21 |
|
21 | 22 | class TestSecurityGroupNetwork(network_fakes.TestNetworkV2): |
@@ -230,3 +231,135 @@ def test_security_group_list_all_projects(self): |
230 | 231 | self.compute.security_groups.list.assert_called_with(**kwargs) |
231 | 232 | self.assertEqual(self.expected_columns_all_projects, columns) |
232 | 233 | self.assertEqual(self.expected_data_all_projects, tuple(data)) |
| 234 | + |
| 235 | + |
| 236 | +class TestSetSecurityGroupNetwork(TestSecurityGroupNetwork): |
| 237 | + |
| 238 | + # The security group to be set. |
| 239 | + _security_group = \ |
| 240 | + network_fakes.FakeSecurityGroup.create_one_security_group() |
| 241 | + |
| 242 | + def setUp(self): |
| 243 | + super(TestSetSecurityGroupNetwork, self).setUp() |
| 244 | + |
| 245 | + self.network.update_security_group = mock.Mock(return_value=None) |
| 246 | + |
| 247 | + self.network.find_security_group = mock.Mock( |
| 248 | + return_value=self._security_group) |
| 249 | + |
| 250 | + # Get the command object to test |
| 251 | + self.cmd = security_group.SetSecurityGroup(self.app, self.namespace) |
| 252 | + |
| 253 | + def test_set_no_options(self): |
| 254 | + self.assertRaises(tests_utils.ParserException, |
| 255 | + self.check_parser, self.cmd, [], []) |
| 256 | + |
| 257 | + def test_set_no_updates(self): |
| 258 | + arglist = [ |
| 259 | + self._security_group.name, |
| 260 | + ] |
| 261 | + verifylist = [ |
| 262 | + ('group', self._security_group.name), |
| 263 | + ] |
| 264 | + |
| 265 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 266 | + result = self.cmd.take_action(parsed_args) |
| 267 | + |
| 268 | + self.network.update_security_group.assert_called_once_with( |
| 269 | + self._security_group, |
| 270 | + **{} |
| 271 | + ) |
| 272 | + self.assertIsNone(result) |
| 273 | + |
| 274 | + def test_set_all_options(self): |
| 275 | + new_name = 'new-' + self._security_group.name |
| 276 | + new_description = 'new-' + self._security_group.description |
| 277 | + arglist = [ |
| 278 | + '--name', new_name, |
| 279 | + '--description', new_description, |
| 280 | + self._security_group.name, |
| 281 | + ] |
| 282 | + verifylist = [ |
| 283 | + ('description', new_description), |
| 284 | + ('group', self._security_group.name), |
| 285 | + ('name', new_name), |
| 286 | + ] |
| 287 | + |
| 288 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 289 | + result = self.cmd.take_action(parsed_args) |
| 290 | + |
| 291 | + attrs = { |
| 292 | + 'description': new_description, |
| 293 | + 'name': new_name, |
| 294 | + } |
| 295 | + self.network.update_security_group.assert_called_once_with( |
| 296 | + self._security_group, |
| 297 | + **attrs |
| 298 | + ) |
| 299 | + self.assertIsNone(result) |
| 300 | + |
| 301 | + |
| 302 | +class TestSetSecurityGroupCompute(TestSecurityGroupCompute): |
| 303 | + |
| 304 | + # The security group to be set. |
| 305 | + _security_group = \ |
| 306 | + compute_fakes.FakeSecurityGroup.create_one_security_group() |
| 307 | + |
| 308 | + def setUp(self): |
| 309 | + super(TestSetSecurityGroupCompute, self).setUp() |
| 310 | + |
| 311 | + self.app.client_manager.network_endpoint_enabled = False |
| 312 | + |
| 313 | + self.compute.security_groups.update = mock.Mock(return_value=None) |
| 314 | + |
| 315 | + self.compute.security_groups.get = mock.Mock( |
| 316 | + return_value=self._security_group) |
| 317 | + |
| 318 | + # Get the command object to test |
| 319 | + self.cmd = security_group.SetSecurityGroup(self.app, None) |
| 320 | + |
| 321 | + def test_set_no_options(self): |
| 322 | + self.assertRaises(tests_utils.ParserException, |
| 323 | + self.check_parser, self.cmd, [], []) |
| 324 | + |
| 325 | + def test_set_no_updates(self): |
| 326 | + arglist = [ |
| 327 | + self._security_group.name, |
| 328 | + ] |
| 329 | + verifylist = [ |
| 330 | + ('group', self._security_group.name), |
| 331 | + ] |
| 332 | + |
| 333 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 334 | + result = self.cmd.take_action(parsed_args) |
| 335 | + |
| 336 | + self.compute.security_groups.update.assert_called_once_with( |
| 337 | + self._security_group, |
| 338 | + self._security_group.name, |
| 339 | + self._security_group.description |
| 340 | + ) |
| 341 | + self.assertIsNone(result) |
| 342 | + |
| 343 | + def test_set_all_options(self): |
| 344 | + new_name = 'new-' + self._security_group.name |
| 345 | + new_description = 'new-' + self._security_group.description |
| 346 | + arglist = [ |
| 347 | + '--name', new_name, |
| 348 | + '--description', new_description, |
| 349 | + self._security_group.name, |
| 350 | + ] |
| 351 | + verifylist = [ |
| 352 | + ('description', new_description), |
| 353 | + ('group', self._security_group.name), |
| 354 | + ('name', new_name), |
| 355 | + ] |
| 356 | + |
| 357 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 358 | + result = self.cmd.take_action(parsed_args) |
| 359 | + |
| 360 | + self.compute.security_groups.update.assert_called_once_with( |
| 361 | + self._security_group, |
| 362 | + new_name, |
| 363 | + new_description |
| 364 | + ) |
| 365 | + self.assertIsNone(result) |
0 commit comments