Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 1 | # |
| 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 | """ |
| 18 | vOLT-HA DHCP Test Case module |
| 19 | """ |
| 20 | |
| 21 | import os |
| 22 | import testCaseUtils |
| 23 | import logging |
| 24 | import subprocess |
| 25 | |
| 26 | |
| 27 | class 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 Depatie | ed99efe | 2019-03-12 16:12:26 -0400 | [diff] [blame] | 43 | self.__rgName = testCaseUtils.discover_rg_pod_name() |
Gilles Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 44 | self.__onuCount = None |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 45 | 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 Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 52 | def h_configure(self, onu_count): |
| 53 | self.__onuCount = onu_count |
| 54 | |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 55 | 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 Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 59 | def extract_authorized_user_device_id_and_port_number(self): |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 60 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'AUTHORIZED', 'voltha_onos_users.log') |
Gilles Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 61 | assert statusLines, 'No ONU has been authenticated' |
| 62 | self.__deviceId, self.__portNumber = testCaseUtils.retrieve_authorized_users_device_id_and_port_number(statusLines) |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 63 | |
Gilles Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 64 | def add_onu_bound_dhcp_flows(self): |
| 65 | testCaseUtils.add_subscriber_access(self, self.__deviceId, self.__portNumber) |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 66 | |
| 67 | def should_now_have_two_dhcp_flows(self): |
| 68 | testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'), |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 69 | 'voltha_onos_flows.log', 'flows -s') |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 70 | 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 Depatie | c9a26cf | 2019-05-10 15:43:12 -0400 | [diff] [blame] | 73 | assert len(lines) >= 2, 'Expected at least 2 DHCP Detection Flows but result was %s' % len(lines) |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 74 | for line in lines: |
| 75 | self.__fields = testCaseUtils.parse_fields(line, ',') |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 76 | inPortStr = self.__fields[5].strip() |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 77 | 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 Depatie | ed99efe | 2019-03-12 16:12:26 -0400 | [diff] [blame] | 161 | 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 Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 184 | 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 Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 205 | 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 Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 219 | |
| 220 | def set_firewall_rules(): |
| 221 | logging.info('Setting Firewall rules for DHCP test') |
| 222 | os.system('sudo iptables -P FORWARD ACCEPT') |
| 223 | |
| 224 | |
Gilles Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 225 | def run_test(onu_count, root_dir, voltha_dir, log_dir, simtype): |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 226 | dhcp = DHCP() |
| 227 | dhcp.h_set_log_dirs(root_dir, voltha_dir, log_dir) |
Gilles Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame^] | 228 | 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() |