Add test for compute API List Security Groups By Server

Change-Id: I36dfdee780ef1796b360537b5649b0957392ef08
This commit is contained in:
zwhe 2017-02-09 16:59:41 +08:00 committed by Zhongwei He
parent 0c6dc8e70c
commit 3beb6cf3c4
4 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,6 @@
---
features:
- |
Add the list security groups by server API to the servers_client
library. This feature enables the possibility to list security
groups for a server instance.

View File

@ -144,3 +144,31 @@ class SecurityGroupsTestJSON(base.BaseSecurityGroupsTest):
['security_group'])
self.assertEqual(s_new_name, fetched_group['name'])
self.assertEqual(s_new_des, fetched_group['description'])
@test.idempotent_id('79517d60-535a-438f-af3d-e6feab1cbea7')
@test.services('network')
def test_list_security_groups_by_server(self):
# Create a couple security groups that we will use
# for the server resource this test creates
sg = self.create_security_group()
sg2 = self.create_security_group()
assigned_security_groups_ids = [sg['id'], sg2['id']]
# Create server and add the security group created
# above to the server we just created
server_id = self.create_test_server(wait_until='ACTIVE')['id']
# add security groups to server
self.servers_client.add_security_group(server_id, name=sg['name'])
self.servers_client.add_security_group(server_id, name=sg2['name'])
# list security groups for a server
fetched_groups = (
self.servers_client.list_security_groups_by_server(
server_id)['security_groups'])
fetched_security_groups_ids = [i['id'] for i in fetched_groups]
# verifying the security groups ids in list
missing_security_groups =\
[p for p in assigned_security_groups_ids
if p not in fetched_security_groups_ids]
self.assertEmpty(missing_security_groups,
"Failed to find security_groups %s in fetched list" %
', '.join(missing_security_groups))

View File

@ -19,6 +19,8 @@ import copy
from oslo_serialization import jsonutils as json
from six.moves.urllib import parse as urllib
from tempest.lib.api_schema.response.compute.v2_1 import \
security_groups as security_groups_schema
from tempest.lib.api_schema.response.compute.v2_1 import servers as schema
from tempest.lib.api_schema.response.compute.v2_16 import servers as schemav216
from tempest.lib.api_schema.response.compute.v2_19 import servers as schemav219
@ -715,3 +717,16 @@ class ServersClient(base_compute_client.BaseComputeClient):
http://developer.openstack.org/api-ref-compute-v2.1.html#removeFixedIp
"""
return self.action(server_id, 'removeFixedIp', **kwargs)
def list_security_groups_by_server(self, server_id):
"""Lists security groups for a server.
For a full list of available parameters, please refer to the official
API reference:
http://developer.openstack.org/api-ref-compute-v2.1.html#listSecurityGroupsByServer
"""
resp, body = self.get("servers/%s/os-security-groups" % server_id)
body = json.loads(body)
self.validate_response(security_groups_schema.list_security_groups,
resp, body)
return rest_client.ResponseBody(resp, body)

View File

@ -172,6 +172,14 @@ class TestServersClient(base.BaseServiceTest):
"traceback": "fake-trace-back"
}
FAKE_SECURITY_GROUPS = [{
"description": "default",
"id": "3fb26eb3-581b-4420-9963-b0879a026506",
"name": "default",
"rules": [],
"tenant_id": "openstack"
}]
FAKE_INSTANCE_WITH_EVENTS = copy.deepcopy(FAKE_INSTANCE_ACTIONS)
FAKE_INSTANCE_WITH_EVENTS['events'] = [FAKE_INSTANCE_ACTION_EVENTS]
@ -1009,3 +1017,17 @@ class TestServersClient(base.BaseServiceTest):
server_id=self.server_id,
type='fake-console-type'
)
def test_list_security_groups_by_server_with_str_body(self):
self._test_list_security_groups_by_server()
def test_list_security_groups_by_server_with_bytes_body(self):
self._test_list_security_groups_by_server(True)
def _test_list_security_groups_by_server(self, bytes_body=False):
self.check_service_client_function(
self.client.list_security_groups_by_server,
'tempest.lib.common.rest_client.RestClient.get',
{'security_groups': self.FAKE_SECURITY_GROUPS},
server_id=self.server_id,
)