Browse Source

Merge "Add test_security_group_on_vrouter"

Jenkins 1 year ago
parent
commit
2b3aa395b5

+ 53
- 13
plugin_test/vapor/vapor/fixtures/contrail.py View File

@@ -28,12 +28,17 @@ def client_contrail_analytics(session):
28 28
 
29 29
 
30 30
 @pytest.fixture
31
-def client_contrail_vrouter_agent(contrail_vrouter_agent_endpoint):
32
-    LOGGER.debug('VRouter endpoint: {0}'.format(
33
-        contrail_vrouter_agent_endpoint))
34
-    return clients.ContrailVRouterAgentClient(
35
-        agent_ip=contrail_vrouter_agent_endpoint['ip'],
36
-        agent_port=contrail_vrouter_agent_endpoint['port'])
31
+def client_contrail_vrouter_agents(contrail_vrouter_agent_endpoints):
32
+    endpoints = contrail_vrouter_agent_endpoints
33
+    port = endpoints['port']
34
+    _clients = {}
35
+    for node in endpoints['nodes']:
36
+        LOGGER.debug('VRouter `{node}` endpoint: {ip}:{port}'.format(
37
+            node=node['fqdn'], ip=node['ip'], port=port))
38
+        _clients[node['fqdn']] = clients.ContrailVRouterAgentClient(
39
+            agent_ip=node['ip'], agent_port=endpoints['port'])
40
+
41
+    return _clients
37 42
 
38 43
 
39 44
 def get_nodes_fixture(cmd, scope='function'):
@@ -86,18 +91,53 @@ def contrail_api_endpoint(os_faults_steps):
86 91
 
87 92
 
88 93
 @pytest.fixture(scope='module')
89
-def contrail_vrouter_agent_endpoint(contrail_services_http_introspect_ports):
90
-    """Return contrail agent endpoint."""
94
+def contrail_vrouter_agent_endpoints(contrail_services_http_introspect_ports):
95
+    """Return contrail agent endpoints info.
96
+
97
+    Return format:
98
+        {
99
+            'port': 8100,
100
+            'nodes': [
101
+                {
102
+                    'ip': '1.22.3.4',
103
+                    'fqdn': 'cmp001.mcp.local'
104
+                },
105
+                {
106
+                    'ip': '1.22.3.5',
107
+                    'fqdn': 'cmp002.mcp.local'
108
+                }
109
+            ]
110
+        }
111
+    """
91 112
     service_name = 'contrail-vrouter-agent'
92
-    ip = contrail_services_http_introspect_ports[service_name]['nodes'][0][
93
-        'ip']
94
-    port = contrail_services_http_introspect_ports[service_name]['port']
95
-    return {'ip': ip, 'port': port}
113
+    return contrail_services_http_introspect_ports[service_name]
96 114
 
97 115
 
98 116
 @pytest.fixture(scope='module')
99 117
 def contrail_services_http_introspect_ports(os_faults_steps, contrail_nodes):
100
-    """Return contrail services ips and ports."""
118
+    """Return contrail services ips and ports.
119
+
120
+    Return format:
121
+        {
122
+            'contrail-vrouter-agent':{
123
+                'port': 8100,
124
+                'nodes': [
125
+                    {
126
+                        'ip': '1.22.3.4',
127
+                        'fqdn': 'cmp001.mcp.local'
128
+                    },
129
+                    {
130
+                        'ip': '1.22.3.5',
131
+                        'fqdn': 'cmp002.mcp.local'
132
+                    }
133
+                ]
134
+            },
135
+            'contrail-opserver': {...},
136
+            ....
137
+
138
+        }
139
+
140
+    """
101 141
 
102 142
     default_ports = {
103 143
         'contrail-config-nodemgr': 8100,

+ 41
- 55
plugin_test/vapor/vapor/helpers/clients/contrail_agent.py View File

@@ -1,15 +1,10 @@
1
-import six
2 1
 import copy
2
+
3
+from six.moves.urllib import request
3 4
 import xmltodict
4
-from collections import OrderedDict
5 5
 
6 6
 from vapor.settings import logger
7 7
 
8
-if six.PY2:
9
-    from urllib2 import Request, urlopen
10
-else:
11
-    from urllib.request import Request, urlopen
12
-
13 8
 
14 9
 __all__ = ['ContrailVRouterAgentClient']
15 10
 
@@ -20,21 +15,16 @@ class ClientContrailVRouterAgentBase(object):
20 15
         self.port = agent_port
21 16
 
22 17
     def get_snh_dict_data(self, data):
23
-        key = data.keys()
24
-        if key[0].find(r'__') == 0:
25
-            data = data[key[0]]
18
+        key = next(iter(data.keys()))
19
+        if key.startswith(r'__'):
20
+            data = data[key]
26 21
         data = self.del_unused_key(data)
27
-        return_dict = {}
28
-        key = data.keys()
29
-        for i in key:
30
-            return_dict[i] = self.get_data(data[i])
31
-        return return_dict
22
+        return {k: self.get_data(v) for k, v in data.items()}
32 23
 
33 24
     def get_resource(self, path):
34 25
         url = 'http://%s:%s/%s' % (self.ip, self.port, path)
35
-        req = Request(url)
36 26
         try:
37
-            response = urlopen(req)
27
+            response = request.urlopen(url)
38 28
             xmldata = response.read()
39 29
         except Exception as e:
40 30
             logger.error('get_xml exception: {} url: {}'.format(e, url))
@@ -46,38 +36,35 @@ class ClientContrailVRouterAgentBase(object):
46 36
     def del_unused_key(data):
47 37
         key_list = ['@type', '@identifier', '@size', 'more',
48 38
                     'Pagination', 'OvsdbPageResp', 'next_batch']
49
-        return OrderedDict({k: v for (k, v) in data.items()
50
-                            if k not in key_list})
39
+        return {k: v for (k, v) in data.items() if k not in key_list}
51 40
 
52 41
     def get_data(self, data):
53 42
         if isinstance(data, list):
54 43
             data_list = []
55
-            for i in data:
56
-                data_dict = self.get_data(i)
44
+            for item in data:
45
+                data_dict = self.get_data(item)
57 46
                 data_list.append(data_dict)
58 47
             return_data = data_list
59 48
         else:
60 49
             if '@type' in data:
61 50
                 if data['@type'] == 'sandesh':
62
-                    sandesh_dict = {}
63 51
                     data = self.del_unused_key(data)
64
-                    key = data.keys()
65
-                    for i in key:
66
-                        sandesh_dict[i] = self.get_data(data[i])
67
-                    return_data = sandesh_dict
52
+                    return_data = {k: self.get_data(v)
53
+                                   for k, v in data.items()}
68 54
                 elif data['@type'] == 'list':
69 55
                     data = self.del_unused_key(data)
70
-                    key = data.keys()
71
-                    data = data[key[0]]
56
+                    key = next(iter(data.keys()))
57
+                    data = data[key]
72 58
                     return_data = self.get_data(data)
73 59
                 elif data['@type'] == 'struct':
60
+                    is_list = '@size' in data
74 61
                     data = self.del_unused_key(data)
75 62
                     if len(data) == 0:
76 63
                         return ''
77
-                    keys = data.keys()
78
-                    for i in keys:
79
-                        sdata = self.get_data(data[i])
80
-                    return_data = sdata
64
+                    value = next(iter(data.values()))
65
+                    if is_list and not (isinstance(value, list)):
66
+                        value = [value]
67
+                    return_data = self.get_data(value)
81 68
                 elif data['@type'] in ['i64', 'i32', 'i16', 'u64', 'u32',
82 69
                                        'u16', 'double', 'string', 'bool']:
83 70
                     if '#text' in data:
@@ -87,11 +74,8 @@ class ClientContrailVRouterAgentBase(object):
87 74
             elif 'element' in data:
88 75
                 return_data = data['#text']
89 76
             else:
90
-                data_dict = {}
91 77
                 data = self.del_unused_key(data)
92
-                for i in data:
93
-                    data_dict[i] = self.get_data(data[i])
94
-                return_data = data_dict
78
+                return_data = {k: self.get_data(v) for k, v in data.items()}
95 79
         return return_data
96 80
 
97 81
     def find_ifmap_list(self, data):
@@ -118,34 +102,32 @@ class ClientContrailVRouterAgentBase(object):
118 102
 
119 103
     def get_snhdict(self, path):
120 104
         data = self.get_resource(path)
121
-        all_path = ''
105
+        # Check all data link
106
+        all_path = None
122 107
         try:
123
-            top_key = data.keys()
124
-            url = data[top_key[0]]['Pagination']['req']['PageReqData']['all']['#text']  # noqa
108
+            top_key = next(iter(data.keys()))
109
+            url = data[top_key]['Pagination']['req']['PageReqData']['all']['#text']  # noqa
125 110
             all_path = 'Snh_PageReq?x=%s' % url
126 111
         except KeyError:
127 112
             pass
128 113
         try:
129
-            top_key = data.keys()
130
-            url = data[top_key[0]]['OvsdbPageResp']['req']['OvsdbPageRespData']['all']['#text']  # noqa
114
+            top_key = next(iter(data.keys()))
115
+            url = data[top_key]['OvsdbPageResp']['req']['OvsdbPageRespData']['all']['#text']  # noqa
131 116
             all_path = 'Snh_OvsdbPageReq?x=%s' % url
132 117
         except KeyError:
133 118
             pass
134
-        if all_path != '':
119
+        if all_path:
135 120
             data = self.get_resource(all_path)
136
-        keys = data.keys()
137
-        if 'next_batch' in data[keys[0]]:
138
-            while True:
139
-                if 'next_batch' in data[keys[0]]:
140
-                    old_data = data.copy()
141
-                    path1 = data[keys[0]]['next_batch']['@link']
142
-                    path2 = data[keys[0]]['next_batch']['#text']
143
-                    path = 'Snh_%s?x=%s' % (path1, path2)
144
-                    data = self.get_resource(path)
145
-                    old_list = self.find_ifmap_list(old_data)
146
-                    self.merge_ifmap_list(data, old_list)
147
-                else:
148
-                    break
121
+        # Check pagination
122
+        key = next(iter(data.keys()))
123
+        while 'next_batch' in data[key]:
124
+            old_data = data.copy()
125
+            path1 = data[key]['next_batch']['@link']
126
+            path2 = data[key]['next_batch']['#text']
127
+            path = 'Snh_%s?x=%s' % (path1, path2)
128
+            data = self.get_resource(path)
129
+            old_list = self.find_ifmap_list(old_data)
130
+            data = self.merge_ifmap_list(data, old_list)
149 131
         return data
150 132
 
151 133
     def get_path_to_dict(self, path):
@@ -164,3 +146,7 @@ class ContrailVRouterAgentClient(ClientContrailVRouterAgentBase):
164 146
     def get_itf_by_name(self, interface_name):
165 147
         data = self.get_path_to_dict('Snh_ItfReq?x={}'.format(interface_name))
166 148
         return data
149
+
150
+    def get_sg_list(self):
151
+        data = self.get_path_to_dict('Snh_SgListReq')
152
+        return data

+ 9
- 2
plugin_test/vapor/vapor/tests/common/test_base.py View File

@@ -647,7 +647,8 @@ def test_update_vm_ip(server, subnet, port_steps, server_steps):
647 647
                          [dict(ips=('10.0.0.10', '10.0.0.20'))],
648 648
                          indirect=True)
649 649
 def test_diff_proj_same_vn_vm_add_delete(different_tenants_resources,
650
-                                         client_contrail_vrouter_agent):
650
+                                         client_contrail_vrouter_agents,
651
+                                         os_faults_steps):
651 652
     """Test to validate that a VN and VM with the same name and same subnet
652 653
     can be created in two different projects.
653 654
 
@@ -661,7 +662,13 @@ def test_diff_proj_same_vn_vm_add_delete(different_tenants_resources,
661 662
     """
662 663
     resources = different_tenants_resources
663 664
 
664
-    itfs = client_contrail_vrouter_agent.get_itfs()['ItfResp'][
665
+    compute_host = getattr(resources[0].server,
666
+                           stepler_config.SERVER_ATTR_HOST)
667
+    compute_fqdn = os_faults_steps.get_fqdn_by_host_name(compute_host)
668
+
669
+    vrouter_agent_client = client_contrail_vrouter_agents[compute_fqdn]
670
+
671
+    itfs = vrouter_agent_client.get_itfs()['ItfResp'][
665 672
         'itf_list']
666 673
 
667 674
     s1_net_label = next(vrif['label'] for vrif in itfs

+ 35
- 1
plugin_test/vapor/vapor/tests/common/test_security_group.py View File

@@ -13,7 +13,8 @@
13 13
 import time
14 14
 
15 15
 import attrdict
16
-from hamcrest import assert_that, equal_to  # noqa: H301
16
+from hamcrest import (assert_that, equal_to, only_contains,
17
+                      has_entries)  # noqa: H301
17 18
 from pycontrail import types
18 19
 import pytest
19 20
 from stepler import config as stepler_config
@@ -448,3 +449,36 @@ def test_add_remove_security_group_with_active_flow(
448 449
                                             ifaces[0], tcp_filter)
449 450
         connectivity.check_packets_on_iface(os_faults_steps, computes[1],
450 451
                                             ifaces[1], udp_filter)
452
+
453
+
454
+def test_security_group_on_vrouter(
455
+        server, security_group, client_contrail_vrouter_agents,
456
+        os_faults_steps, neutron_create_security_group):
457
+    """Check that server's compute vRouter "know" about security groups.
458
+
459
+    Steps:
460
+        #. Create server with security group
461
+        #. Check that security group uuid is present on server's vRouter agent
462
+            /Snh_SgListReq reply
463
+        #. Create new security group
464
+        #. Add created security group to server
465
+        #. Check that new security group uuid is present on server's vRouter
466
+            agent /Snh_SgListReq reply
467
+    """
468
+    compute_host = getattr(server, stepler_config.SERVER_ATTR_HOST)
469
+    compute_fqdn = os_faults_steps.get_fqdn_by_host_name(compute_host)
470
+    vrouter_agent = client_contrail_vrouter_agents[compute_fqdn]
471
+
472
+    sg_list = vrouter_agent.get_sg_list()['SgListResp']['sg_list']
473
+    assert_that(
474
+        sg_list, only_contains(has_entries(sg_uuid=security_group['id'])))
475
+
476
+    new_security_group = neutron_create_security_group(
477
+        next(utils.generate_ids()))
478
+    server.add_security_group(new_security_group['id'])
479
+
480
+    sg_list = vrouter_agent.get_sg_list()['SgListResp']['sg_list']
481
+    assert_that(sg_list,
482
+                only_contains(
483
+                    has_entries(sg_uuid=security_group['id']),
484
+                    has_entries(sg_uuid=new_security_group['id'])))

Loading…
Cancel
Save