blob: d52234a8556c2ba5721574775c926d292f49ed97 [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 Depatie2e683692019-02-22 16:06:52 -050044 self.__fields = None
45 self.__deviceId = None
46 self.__portNumber = None
47
48 def h_set_log_dirs(self, root_dir, voltha_dir, log_dir):
49 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
50
Gilles Depatie2e683692019-02-22 16:06:52 -050051 def discover_authorized_users(self):
52 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
53 'voltha_onos_users.log', 'aaa-users')
54
55 def retrieve_authorized_users_device_id_and_port_number(self):
56 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'AUTHORIZED', 'voltha_onos_users.log')
57 assert statusLines, 'No Users Authorized'
58 self.__fields = testCaseUtils.parse_fields(statusLines, ',')
59 deviceField = self.__fields[2].strip()
60 deviceStr, equal, deviceId = deviceField.partition('=')
61 self.__deviceId = deviceId
62 portField = self.__fields[4].strip()
63 portNumStr, equal, portNum = portField.partition('=')
64 self.__portNumber = portNum
65
66 def add_subscriber_access(self):
67 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
68 'voltha_add_subscriber_access.log', 'volt-add-subscriber-access %s %s'
69 % (self.__deviceId, self.__portNumber))
70
71 def should_now_have_two_dhcp_flows(self):
72 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
73 'voltha_onos_flows.log', 'flows')
74 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'IP_PROTO:17', 'voltha_onos_flows.log')
75 assert statusLines, 'No DHCP Detection flows'
76 lines = statusLines.splitlines()
77 for line in lines:
78 self.__fields = testCaseUtils.parse_fields(line, ',')
79 inPortStr = self.__fields[10].strip()
80 selector, delimiter, inPort = inPortStr.partition('=[')
81 assert (inPort == 'IN_PORT:2' or inPort == 'IN_PORT:128'), 'DHCP detection flows not associated with expected ports'
82
83 def add_dhcp_server_configuration_data_in_onos(self):
84 logging.info('Adding DHCP Configuration Data to Onos NetCfg')
85 logging.debug('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 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
89 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcp_json'
90 % testCaseUtils.get_dir(self, 'voltha'))
91
92 def activate_dhcp_server_in_onos(self):
93 logging.info('Activating DHCP server on Onos')
94 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
95 'voltha_dhcp_server_activate.log', 'app activate dhcp')
96 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Activated', 'voltha_dhcp_server_activate.log')
97 assert statusLines, 'DHCP server failed to be Activated'
98
99 def deactivate_dhcp_server_in_onos(self):
100 logging.info('Deactivating DHCP server on Onos')
101 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
102 'voltha_dhcp_server_deactivate.log', 'app deactivate dhcp')
103 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Deactivated', 'voltha_dhcp_server_deactivate.log')
104 assert statusLines, 'DHCP server failed to be Deactivated'
105
106 def query_for_default_ip_on_rg(self):
107 logging.info('De-assigning default IP on RG')
108 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.CHECK_IP_FILENAME), 'w')
109 ifconfigCheck1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
110 'ifconfig'],
111 stdout=subprocess.PIPE,
112 stderr=subprocess.PIPE)
113 ifconfigCheck2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifconfigCheck1.stdout,
114 stdout=process_output,
115 stderr=process_output)
116
117 ifconfigCheck1.wait()
118 ifconfigCheck1.stdout.close()
119 ifconfigCheck2.wait()
120
121 process_output.close()
122
123 testCaseUtils.print_log_file(self, self.CHECK_IP_FILENAME)
124
125 def de_assign_default_ip_on_rg(self):
126
127 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.CHECK_IP_FILENAME)
128 if statusLines:
129 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.DE_ASSIGN_IP_FILENAME), 'w')
130 os.system('/usr/bin/kubectl exec -n voltha %s -- bash -c "ifconfig eth0 0.0.0.0"' % self.__rgName)
131 ifconfigDeassign1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
132 'ifconfig'],
133 stdout=subprocess.PIPE,
134 stderr=subprocess.PIPE)
135
136 ifconfigDeassign2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifconfigDeassign1.stdout,
137 stdout=process_output,
138 stderr=process_output)
139 ifconfigDeassign1.wait()
140 ifconfigDeassign1.stdout.close()
141 ifconfigDeassign2.wait()
142
143 process_output.close()
144
145 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.DE_ASSIGN_IP_FILENAME)
146 assert not statusLines, 'IP addr not de-assigned'
147
148 else:
149 logging.info('No default IP addr assigned to eth0')
150
151 def assign_dhcp_ip_addr_to_rg(self):
152 logging.info('Assigning IP addr on RG using DHCP')
153 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.ASSIGN_DHCP_IP_FILENAME), 'w')
154 dhcpAssignIp1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-it', '-n', 'voltha', self.__rgName, '--',
155 'dhclient', '-v', 'eth0'],
156 stdout=process_output,
157 stderr=process_output)
158
159 dhcpAssignIp1.wait()
160 process_output.close()
161
162 testCaseUtils.print_log_file(self, self.ASSIGN_DHCP_IP_FILENAME)
163
Gilles Depatieed99efe2019-03-12 16:12:26 -0400164 procPidDhclient1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
165 stdout=subprocess.PIPE,
166 stderr=subprocess.PIPE)
167 procPidDhclient2 = subprocess.Popen(['grep', '-e', 'dhclient'], stdin=procPidDhclient1.stdout,
168 stdout=subprocess.PIPE,
169 stderr=subprocess.PIPE)
170 procPidDhclient3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidDhclient2.stdout,
171 stdout=subprocess.PIPE,
172 stderr=subprocess.PIPE)
173
174 procPidDhclient1.stdout.close()
175 procPidDhclient2.stdout.close()
176
177 out, err = procPidDhclient3.communicate()
178 dhclientPid = out.strip()
179 if dhclientPid:
180 procKillDhclient = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'kill', dhclientPid],
181 stdout=subprocess.PIPE,
182 stderr=subprocess.PIPE)
183
184 out, err = procKillDhclient.communicate()
185 assert not err, 'Killing dhclient returned %s' % err
186
Gilles Depatie2e683692019-02-22 16:06:52 -0500187 def should_have_dhcp_assigned_ip(self):
188 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.CHECK_ASSIGNED_IP_FILENAME), 'w')
189 ifConfigCheck1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
190 'ifconfig'],
191 stdout=subprocess.PIPE,
192 stderr=subprocess.PIPE)
193
194 ifConfigCheck2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifConfigCheck1.stdout,
195 stdout=process_output,
196 stderr=process_output)
197 ifConfigCheck1.wait()
198 ifConfigCheck1.stdout.close()
199 ifConfigCheck2.wait()
200
201 process_output.close()
202
203 testCaseUtils.print_log_file(self, self.CHECK_ASSIGNED_IP_FILENAME)
204
205 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.CHECK_ASSIGNED_IP_FILENAME)
206 assert statusLines, 'DHCP IP addr not assigned'
207
208
209def set_firewall_rules():
210 logging.info('Setting Firewall rules for DHCP test')
211 os.system('sudo iptables -P FORWARD ACCEPT')
212
213
214def run_test(root_dir, voltha_dir, log_dir):
215
216 set_firewall_rules()
217 dhcp = DHCP()
218 dhcp.h_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatie2e683692019-02-22 16:06:52 -0500219 dhcp.discover_authorized_users()
220 dhcp.retrieve_authorized_users_device_id_and_port_number()
221 dhcp.add_subscriber_access()
222 dhcp.should_now_have_two_dhcp_flows()
223 dhcp.deactivate_dhcp_server_in_onos()
224 dhcp.add_dhcp_server_configuration_data_in_onos()
225 dhcp.activate_dhcp_server_in_onos()
226 dhcp.query_for_default_ip_on_rg()
227 dhcp.de_assign_default_ip_on_rg()
228 dhcp.assign_dhcp_ip_addr_to_rg()
229 dhcp.should_have_dhcp_assigned_ip()