blob: 9647ba62f5c9c5e266fd3cae4e61b9ea719b1362 [file] [log] [blame]
Gilles Depatie2e683692019-02-22 16:06:52 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18vOLT-HA DHCP Test Case module
19"""
20
21import os
22import testCaseUtils
23import logging
24import subprocess
25
26
27class DHCP(object):
28 """
29 This class implements voltha DHCP test case
30 """
31
32 CHECK_IP_FILENAME = 'voltha_check_ip.log'
33 DE_ASSIGN_IP_FILENAME = 'voltha_de-assign_ip.log'
34 ASSIGN_DHCP_IP_FILENAME = 'voltha_assign_dhcp_ip.log'
35 CHECK_ASSIGNED_IP_FILENAME = 'voltha_check_assigned_dhcp_ip.log'
36
37 def __init__(self):
38 self.dirs = dict()
39 self.dirs['log'] = None
40 self.dirs['root'] = None
41 self.dirs['voltha'] = None
42
Gilles Depatieed99efe2019-03-12 16:12:26 -040043 self.__rgName = testCaseUtils.discover_rg_pod_name()
Gilles Depatie88c281a2019-07-30 16:17:03 -040044 self.__onuCount = None
Gilles Depatie2e683692019-02-22 16:06:52 -050045 self.__fields = None
46 self.__deviceId = None
47 self.__portNumber = None
48
49 def h_set_log_dirs(self, root_dir, voltha_dir, log_dir):
50 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
51
Gilles Depatie88c281a2019-07-30 16:17:03 -040052 def h_configure(self, onu_count):
53 self.__onuCount = onu_count
54
Gilles Depatie2e683692019-02-22 16:06:52 -050055 def discover_authorized_users(self):
56 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
57 'voltha_onos_users.log', 'aaa-users')
58
Gilles Depatie88c281a2019-07-30 16:17:03 -040059 def extract_authorized_user_device_id_and_port_number(self):
Gilles Depatie2e683692019-02-22 16:06:52 -050060 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'AUTHORIZED', 'voltha_onos_users.log')
Gilles Depatie88c281a2019-07-30 16:17:03 -040061 assert statusLines, 'No ONU has been authenticated'
62 self.__deviceId, self.__portNumber = testCaseUtils.retrieve_authorized_users_device_id_and_port_number(statusLines)
Gilles Depatie2e683692019-02-22 16:06:52 -050063
Gilles Depatie88c281a2019-07-30 16:17:03 -040064 def add_onu_bound_dhcp_flows(self):
65 testCaseUtils.add_subscriber_access(self, self.__deviceId, self.__portNumber)
Gilles Depatie2e683692019-02-22 16:06:52 -050066
67 def should_now_have_two_dhcp_flows(self):
68 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
Gilles Depatieea423712019-04-12 16:39:12 -040069 'voltha_onos_flows.log', 'flows -s')
Gilles Depatie2e683692019-02-22 16:06:52 -050070 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'IP_PROTO:17', 'voltha_onos_flows.log')
71 assert statusLines, 'No DHCP Detection flows'
72 lines = statusLines.splitlines()
Gilles Depatiec9a26cf2019-05-10 15:43:12 -040073 assert len(lines) >= 2, 'Expected at least 2 DHCP Detection Flows but result was %s' % len(lines)
Gilles Depatie2e683692019-02-22 16:06:52 -050074 for line in lines:
75 self.__fields = testCaseUtils.parse_fields(line, ',')
Gilles Depatieea423712019-04-12 16:39:12 -040076 inPortStr = self.__fields[5].strip()
Gilles Depatie2e683692019-02-22 16:06:52 -050077 selector, delimiter, inPort = inPortStr.partition('=[')
78 assert (inPort == 'IN_PORT:2' or inPort == 'IN_PORT:128'), 'DHCP detection flows not associated with expected ports'
79
80 def add_dhcp_server_configuration_data_in_onos(self):
81 logging.info('Adding DHCP Configuration Data to Onos NetCfg')
82 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
83 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcp_json'
84 % testCaseUtils.get_dir(self, 'voltha'))
85 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
86 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcp_json'
87 % testCaseUtils.get_dir(self, 'voltha'))
88
89 def activate_dhcp_server_in_onos(self):
90 logging.info('Activating DHCP server on Onos')
91 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
92 'voltha_dhcp_server_activate.log', 'app activate dhcp')
93 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Activated', 'voltha_dhcp_server_activate.log')
94 assert statusLines, 'DHCP server failed to be Activated'
95
96 def deactivate_dhcp_server_in_onos(self):
97 logging.info('Deactivating DHCP server on Onos')
98 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
99 'voltha_dhcp_server_deactivate.log', 'app deactivate dhcp')
100 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Deactivated', 'voltha_dhcp_server_deactivate.log')
101 assert statusLines, 'DHCP server failed to be Deactivated'
102
103 def query_for_default_ip_on_rg(self):
104 logging.info('De-assigning default IP on RG')
105 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.CHECK_IP_FILENAME), 'w')
106 ifconfigCheck1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
107 'ifconfig'],
108 stdout=subprocess.PIPE,
109 stderr=subprocess.PIPE)
110 ifconfigCheck2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifconfigCheck1.stdout,
111 stdout=process_output,
112 stderr=process_output)
113
114 ifconfigCheck1.wait()
115 ifconfigCheck1.stdout.close()
116 ifconfigCheck2.wait()
117
118 process_output.close()
119
120 testCaseUtils.print_log_file(self, self.CHECK_IP_FILENAME)
121
122 def de_assign_default_ip_on_rg(self):
123
124 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.CHECK_IP_FILENAME)
125 if statusLines:
126 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.DE_ASSIGN_IP_FILENAME), 'w')
127 os.system('/usr/bin/kubectl exec -n voltha %s -- bash -c "ifconfig eth0 0.0.0.0"' % self.__rgName)
128 ifconfigDeassign1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
129 'ifconfig'],
130 stdout=subprocess.PIPE,
131 stderr=subprocess.PIPE)
132
133 ifconfigDeassign2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifconfigDeassign1.stdout,
134 stdout=process_output,
135 stderr=process_output)
136 ifconfigDeassign1.wait()
137 ifconfigDeassign1.stdout.close()
138 ifconfigDeassign2.wait()
139
140 process_output.close()
141
142 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.DE_ASSIGN_IP_FILENAME)
143 assert not statusLines, 'IP addr not de-assigned'
144
145 else:
146 logging.info('No default IP addr assigned to eth0')
147
148 def assign_dhcp_ip_addr_to_rg(self):
149 logging.info('Assigning IP addr on RG using DHCP')
150 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.ASSIGN_DHCP_IP_FILENAME), 'w')
151 dhcpAssignIp1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-it', '-n', 'voltha', self.__rgName, '--',
152 'dhclient', '-v', 'eth0'],
153 stdout=process_output,
154 stderr=process_output)
155
156 dhcpAssignIp1.wait()
157 process_output.close()
158
159 testCaseUtils.print_log_file(self, self.ASSIGN_DHCP_IP_FILENAME)
160
Gilles Depatieed99efe2019-03-12 16:12:26 -0400161 procPidDhclient1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
162 stdout=subprocess.PIPE,
163 stderr=subprocess.PIPE)
164 procPidDhclient2 = subprocess.Popen(['grep', '-e', 'dhclient'], stdin=procPidDhclient1.stdout,
165 stdout=subprocess.PIPE,
166 stderr=subprocess.PIPE)
167 procPidDhclient3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidDhclient2.stdout,
168 stdout=subprocess.PIPE,
169 stderr=subprocess.PIPE)
170
171 procPidDhclient1.stdout.close()
172 procPidDhclient2.stdout.close()
173
174 out, err = procPidDhclient3.communicate()
175 dhclientPid = out.strip()
176 if dhclientPid:
177 procKillDhclient = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'kill', dhclientPid],
178 stdout=subprocess.PIPE,
179 stderr=subprocess.PIPE)
180
181 out, err = procKillDhclient.communicate()
182 assert not err, 'Killing dhclient returned %s' % err
183
Gilles Depatie2e683692019-02-22 16:06:52 -0500184 def should_have_dhcp_assigned_ip(self):
185 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.CHECK_ASSIGNED_IP_FILENAME), 'w')
186 ifConfigCheck1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
187 'ifconfig'],
188 stdout=subprocess.PIPE,
189 stderr=subprocess.PIPE)
190
191 ifConfigCheck2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifConfigCheck1.stdout,
192 stdout=process_output,
193 stderr=process_output)
194 ifConfigCheck1.wait()
195 ifConfigCheck1.stdout.close()
196 ifConfigCheck2.wait()
197
198 process_output.close()
199
200 testCaseUtils.print_log_file(self, self.CHECK_ASSIGNED_IP_FILENAME)
201
202 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.CHECK_ASSIGNED_IP_FILENAME)
203 assert statusLines, 'DHCP IP addr not assigned'
204
Gilles Depatie88c281a2019-07-30 16:17:03 -0400205 def should_have_ips_assigned_to_all_onus(self):
206 logging.info('Verifying IP Address assignment on all ONUs')
207 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
208 '_voltha_onos_dhcpl2relay_allocations.log', 'dhcpl2relay-allocations')
209 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'DHCPACK', '_voltha_onos_dhcpl2relay_allocations.log')
210 assert statusLines, 'No DHCP addresses allocated'
211 lines = statusLines.splitlines()
212 assert len(lines) == self.__onuCount, 'Allocated IPs does not match ONU count but result was %s' % len(lines)
213 for line in lines:
214 self.__fields = testCaseUtils.parse_fields(line, ',')
215 allocIp = self.__fields[5].strip()
216 allocated, delimiter, ipAddr = allocIp.partition('=')
217 assert ipAddr != '0.0.0.0', 'Invalid IP Address Allocated'
218
Gilles Depatie2e683692019-02-22 16:06:52 -0500219
220def set_firewall_rules():
221 logging.info('Setting Firewall rules for DHCP test')
222 os.system('sudo iptables -P FORWARD ACCEPT')
223
224
Gilles Depatie88c281a2019-07-30 16:17:03 -0400225def run_test(onu_count, root_dir, voltha_dir, log_dir, simtype):
Gilles Depatie2e683692019-02-22 16:06:52 -0500226 dhcp = DHCP()
227 dhcp.h_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatie88c281a2019-07-30 16:17:03 -0400228 dhcp.h_configure(onu_count)
229 if simtype == 'ponsim':
230 set_firewall_rules()
231 dhcp.discover_authorized_users()
232 dhcp.extract_authorized_user_device_id_and_port_number()
233 dhcp.add_onu_bound_dhcp_flows()
234 dhcp.should_now_have_two_dhcp_flows()
235 dhcp.deactivate_dhcp_server_in_onos()
236 dhcp.add_dhcp_server_configuration_data_in_onos()
237 dhcp.activate_dhcp_server_in_onos()
238 dhcp.query_for_default_ip_on_rg()
239 dhcp.de_assign_default_ip_on_rg()
240 dhcp.assign_dhcp_ip_addr_to_rg()
241 dhcp.should_have_dhcp_assigned_ip()
242 elif simtype == 'bbsim':
243 dhcp.should_have_ips_assigned_to_all_onus()