|
13 | 13 | # * See the License for the specific language governing permissions and |
14 | 14 | # * limitations under the License. |
15 | 15 |
|
| 16 | +import collections |
16 | 17 | from os import path |
17 | 18 | import tempfile |
18 | 19 |
|
@@ -1009,6 +1010,9 @@ def test_handle_boot_image_no_image(self, *_): |
1009 | 1010 |
|
1010 | 1011 | @mock.patch('openstack_plugin_common.OpenStackClient._validate_auth_params') |
1011 | 1012 | class TestServerSGAttachments(unittest.TestCase): |
| 1013 | + SecurityGroup = collections.namedtuple( |
| 1014 | + 'SecurityGroup', ['id', 'name'], verbose=True) |
| 1015 | + |
1012 | 1016 | def setUp(self): |
1013 | 1017 | ctx = MockCloudifyContext( |
1014 | 1018 | target=MockContext({ |
@@ -1036,14 +1040,32 @@ def setUp(self): |
1036 | 1040 | findctx.start() |
1037 | 1041 | self.addCleanup(findctx.stop) |
1038 | 1042 |
|
1039 | | - def test_detach_already_detached(self, *kwargs): |
1040 | | - fake_client = mock.MagicMock() |
1041 | | - fake_client().servers.get = mock.MagicMock() |
1042 | | - fake_client().servers.get().remove_security_group = mock.MagicMock( |
1043 | | - side_effect=nova_exceptions.NotFound('test')) |
1044 | | - with mock.patch('openstack_plugin_common.NovaClientWithSugar', |
1045 | | - fake_client): |
1046 | | - nova_plugin.server.disconnect_security_group() |
| 1043 | + @mock.patch('openstack_plugin_common.NovaClientWithSugar') |
| 1044 | + def test_detach_already_detached(self, client, *kwargs): |
| 1045 | + server = client.return_value.servers.get.return_value |
| 1046 | + server.remove_security_group.side_effect = \ |
| 1047 | + nova_exceptions.NotFound('test') |
| 1048 | + nova_plugin.server.disconnect_security_group() |
| 1049 | + |
| 1050 | + @mock.patch('openstack_plugin_common.NovaClientWithSugar') |
| 1051 | + def test_connect_not_connected(self, client, *kwargs): |
| 1052 | + security_groups = [self.SecurityGroup('test-sg-2', 'test-sg-2-name')] |
| 1053 | + server = client.return_value.servers.get.return_value |
| 1054 | + server.list_security_group.return_value = security_groups |
| 1055 | + server.add_security_group.side_effect = ( |
| 1056 | + lambda _: security_groups.append( |
| 1057 | + self.SecurityGroup('test-sg', 'test-sg-name'))) |
| 1058 | + nova_plugin.server.connect_security_group() |
| 1059 | + server.add_security_group.assert_called_once_with('test-sg-name') |
| 1060 | + |
| 1061 | + @mock.patch('openstack_plugin_common.NovaClientWithSugar') |
| 1062 | + def test_connect_already_connected(self, client, *kwargs): |
| 1063 | + security_groups = [self.SecurityGroup('test-sg', 'test-sg-name'), |
| 1064 | + self.SecurityGroup('test-sg-2', 'test-sg-2-name')] |
| 1065 | + server = client.return_value.servers.get.return_value |
| 1066 | + server.list_security_group.return_value = security_groups |
| 1067 | + nova_plugin.server.connect_security_group() |
| 1068 | + server.add_security_group.assert_not_called() |
1047 | 1069 |
|
1048 | 1070 |
|
1049 | 1071 | class TestServerRelationships(unittest.TestCase): |
|
0 commit comments