Debian Codegraph @

neutron_fwaas/tests/functional/privileged/test_netlink_lib.py

raw symbols SCIP
1 # Copyright (c) 2017 Fujitsu Limited
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from neutron.agent.linux import utils as linux_utils
17 from neutron.tests.common import net_helpers
18 from neutron.tests.functional import base as functional_base
19 from oslo_log import log as logging
20
21 import neutron_fwaas.privileged.netlink_lib as nl_lib
22
23 LOG = logging.getLogger(__name__)
24
25
26 def check_nf_conntrack_ipv6_is_loaded():
27 try:
28 output = linux_utils.execute(['lsmod'])
29 except RuntimeError:
30 msg = "Failed execute command lsmod!"
31 raise RuntimeError(msg)
32 if 'nf_conntrack' in output:
33 return True
34 return False
35
36
37 def _create_entries(namespace, conntrack_cmds):
38 for cmd in conntrack_cmds:
39 exec_cmd = ['ip', 'netns', 'exec', namespace] + cmd
40 try:
41 linux_utils.execute(exec_cmd,
42 run_as_root=True,
43 check_exit_code=True,
44 extra_ok_codes=[1],
45 privsep_exec=True)
46 except RuntimeError:
47 raise Exception('Error while creating entry')
48
49
50 class NetlinkLibTestCase(functional_base.BaseSudoTestCase):
51 """Functional test for netlink_lib: List, delete, flush conntrack entries.
52
53 For each function, first we add a specific namespace, then create real
54 conntrack entries. netlink_lib function will do list, delete and flush
55 these entries. This class will test this netlink_lib function work
56 as expected.
57 """
58
59 CONNTRACK_CMDS = (
60 ['conntrack', '-I', '-p', 'tcp',
61 '-s', '1.1.1.1', '-d', '2.2.2.2',
62 '--sport', '1', '--dport', '2',
63 '--state', 'ESTABLISHED', '--timeout', '1234'],
64 ['conntrack', '-I', '-p', 'udp',
65 '-s', '1.1.1.1', '-d', '2.2.2.2',
66 '--sport', '1', '--dport', '2',
67 '--timeout', '1234'],
68 ['conntrack', '-I', '-p', 'icmp',
69 '-s', '1.1.1.1', '-d', '2.2.2.2',
70 '--icmp-type', '8', '--icmp-code', '0', '--icmp-id', '3333',
71 '--timeout', '1234'],
72 ['conntrack', '-I', '-p', 'icmp',
73 '-s', '1.1.1.1', '-d', '2.2.2.2',
74 '--icmp-type', '8', '--icmp-code', '0', '--icmp-id', '3333',
75 '--timeout', '1234'],
76 )
77
78 def test_list_entries(self):
79 namespace = self.useFixture(net_helpers.NamespaceFixture()).name
80 _create_entries(namespace, self.CONNTRACK_CMDS)
81 expected = (
82 (4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333),
83 (4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2'),
84 (4, 'udp', 1, 2, '1.1.1.1', '2.2.2.2')
85 )
86 entries_list = nl_lib.list_entries(namespace=namespace)
87 self.assertEqual(expected, entries_list)
88
89 def _delete_entry(self, delete_entries, remain_entries):
90 namespace = self.useFixture(net_helpers.NamespaceFixture()).name
91 _create_entries(namespace, self.CONNTRACK_CMDS)
92 nl_lib.delete_entries(namespace=namespace, entries=delete_entries)
93 entries_list = nl_lib.list_entries(namespace)
94 self.assertEqual(remain_entries, entries_list)
95
96 def test_delete_icmp_entry(self):
97 icmp_entry = [(4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333)]
98 remain_entries = (
99 (4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2'),
100 (4, 'udp', 1, 2, '1.1.1.1', '2.2.2.2'),
101 )
102 self._delete_entry(icmp_entry, remain_entries)
103
104 def test_delete_tcp_entry(self):
105 tcp_entry = [(4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2')]
106 remain_entries = (
107 (4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333),
108 (4, 'udp', 1, 2, '1.1.1.1', '2.2.2.2')
109 )
110 self._delete_entry(tcp_entry, remain_entries)
111
112 def test_delete_udp_entry(self):
113 udp_entry = [(4, 'udp', 1, 2, '1.1.1.1', '2.2.2.2')]
114 remain_entries = (
115 (4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333),
116 (4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2')
117 )
118 self._delete_entry(udp_entry, remain_entries)
119
120 def test_delete_multiple_entries(self):
121 delete_entries = (
122 (4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333),
123 (4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2'),
124 (4, 'udp', 1, 2, '1.1.1.1', '2.2.2.2')
125 )
126 remain_entries = ()
127 self._delete_entry(delete_entries, remain_entries)
128
129 def test_flush_entries(self):
130 namespace = self.useFixture(net_helpers.NamespaceFixture()).name
131 _create_entries(namespace, self.CONNTRACK_CMDS)
132 nl_lib.flush_entries(namespace)
133 entries_list = nl_lib.list_entries(namespace)
134 self.assertEqual((), entries_list)
135
136
137 class NetlinkLibTestCaseIPv6(functional_base.BaseSudoTestCase):
138
139 CONNTRACK_CMDS = (
140 ['conntrack', '-I', '-p', 'icmp',
141 '-s', '1.1.1.1', '-d', '2.2.2.2',
142 '--icmp-type', '8', '--icmp-code', '0', '--icmp-id', '3333',
143 '--timeout', '1234'],
144 ['conntrack', '-I', '-p', 'icmpv6',
145 '-s', '10::10', '-d', '20::20',
146 '--icmpv6-type', '128', '--icmpv6-code', '0', '--icmpv6-id', '3456',
147 '--timeout', '1234'],
148 )
149
150 def setUp(self):
151 super().setUp()
152 if not check_nf_conntrack_ipv6_is_loaded():
153 self.skipTest(
154 "nf_conntrack_ipv6 module wasn't loaded. Please load"
155 "this module into your system if you want to use "
156 "netlink conntrack with ipv6"
157 )
158
159 def test_list_entries(self):
160 namespace = self.useFixture(net_helpers.NamespaceFixture()).name
161 _create_entries(namespace, self.CONNTRACK_CMDS)
162 expected = (
163 (4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333),
164 (6, 'icmpv6', 128, 0, '10::10', '20::20', 3456),
165 )
166 entries_list = nl_lib.list_entries(namespace=namespace)
167 self.assertEqual(expected, entries_list)
168
169 def _delete_entry(self, delete_entries, remain_entries):
170 namespace = self.useFixture(net_helpers.NamespaceFixture()).name
171 _create_entries(namespace, self.CONNTRACK_CMDS)
172 nl_lib.delete_entries(namespace=namespace, entries=delete_entries)
173 entries_list = nl_lib.list_entries(namespace)
174 self.assertEqual(remain_entries, entries_list)
175
176 def test_delete_icmpv6_entry(self):
177 icmp_entry = [(6, 'icmpv6', 128, 0, '10::10', '20::20', 3456)]
178 remain_entries = (
179 (4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 3333),
180 )
181 self._delete_entry(icmp_entry, remain_entries)
182
183 def test_flush_entries(self):
184 namespace = self.useFixture(net_helpers.NamespaceFixture()).name
185 _create_entries(namespace, self.CONNTRACK_CMDS)
186 nl_lib.flush_entries(namespace)
187 entries_list = nl_lib.list_entries(namespace)
188 self.assertEqual((), entries_list)