Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -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 Authentication Test Case module |
| 19 | """ |
| 20 | |
| 21 | import time |
| 22 | import os |
| 23 | import subprocess |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 24 | import testCaseUtils |
| 25 | import logging |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 26 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 27 | |
| 28 | class Authentication(object): |
| 29 | |
| 30 | """ |
| 31 | This class implements voltha authentication test case |
| 32 | """ |
| 33 | AUTHENTICATE_FILENAME = 'voltha_authenticate.log' |
| 34 | |
| 35 | def __init__(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 36 | self.dirs = dict() |
| 37 | self.dirs['log'] = None |
| 38 | self.dirs['root'] = None |
| 39 | self.dirs['voltha'] = None |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 40 | |
| 41 | self.__rgName = None |
| 42 | self.__radiusName = None |
| 43 | self.__radiusIp = None |
| 44 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 45 | def a_set_log_dirs(self, root_dir, voltha_dir, log_dir): |
| 46 | testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 47 | |
| 48 | def discover_rg_pod_name(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 49 | self.__rgName = testCaseUtils.extract_pod_name('rg-').strip() |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 50 | |
| 51 | def discover_freeradius_pod_name(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 52 | self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip() |
| 53 | logging.info('freeradius Name = %s' % self.__radiusName) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 54 | |
| 55 | def discover_freeradius_ip_addr(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 56 | ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 57 | assert ipAddr, 'No IP address listed for freeradius' |
| 58 | self.__radiusIp = ipAddr.strip() |
| 59 | logging.info('freeradius IP = %s' % self.__radiusIp) |
| 60 | |
| 61 | def set_current_freeradius_ip_in_aaa_json(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 62 | status = testCaseUtils.modify_radius_ip_in_json_using_sed(self, self.__radiusIp) |
| 63 | assert (status == 0), 'Setting Radius Ip in Json File did not return Success' |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 64 | |
| 65 | def alter_aaa_application_configuration_in_onos_using_aaa_json(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 66 | logging.info('Altering the Onos NetCfg AAA apps with Freeradius IP address') |
| 67 | logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
| 68 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json' |
| 69 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 70 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 71 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json' |
| 72 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 73 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 74 | def execute_authentication_on_rg(self): |
| 75 | logging.info('Running Radius Authentication from RG') |
| 76 | process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.AUTHENTICATE_FILENAME), 'w') |
| 77 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c', |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 78 | '/sbin/wpa_supplicant -Dwired -ieth0 -c /etc/wpa_supplicant/wpa_supplicant.conf'], |
| 79 | stdout=process_output, |
| 80 | stderr=process_output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 81 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 82 | time.sleep(15) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 83 | logging.debug('return value from supplicant subprocess = %s' % proc1.returncode) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 84 | procPidSupplicant1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'], |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 85 | stdout=subprocess.PIPE, |
| 86 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 87 | procPidSupplicant2 = subprocess.Popen(['grep', '-e', '/sbin/wpa_supplicant'], stdin=procPidSupplicant1.stdout, |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 88 | stdout=subprocess.PIPE, |
| 89 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 90 | procPidSupplicant3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidSupplicant2.stdout, |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 91 | stdout=subprocess.PIPE, |
| 92 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 93 | |
| 94 | procPidSupplicant1.stdout.close() |
| 95 | procPidSupplicant2.stdout.close() |
| 96 | |
| 97 | out, err = procPidSupplicant3.communicate() |
| 98 | supplicantPid = out.strip() |
| 99 | |
| 100 | procKillSupplicant1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'kill', supplicantPid], |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 101 | stdout=subprocess.PIPE, |
| 102 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 103 | out, err = procKillSupplicant1.communicate() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 104 | assert not err, 'Killing Supplicant returned %s' % err |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 105 | |
| 106 | procPidBash1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'], |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 107 | stdout=subprocess.PIPE, |
| 108 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 109 | procPidBash2 = subprocess.Popen(['grep', '-e', '/bin/bash'], stdin=procPidBash1.stdout, |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 110 | stdout=subprocess.PIPE, |
| 111 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 112 | procPidBash3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidBash2.stdout, |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 113 | stdout=subprocess.PIPE, |
| 114 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 115 | |
| 116 | procPidBash1.stdout.close() |
| 117 | procPidBash2.stdout.close() |
| 118 | |
| 119 | out, err = procPidBash3.communicate() |
| 120 | bashPid = out.strip() |
| 121 | |
| 122 | procKillBash1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'kill', '-9', bashPid], |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 123 | stdout=subprocess.PIPE, |
| 124 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 125 | out, err = procKillBash1.communicate() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 126 | assert not err, 'Killing Bash returned %s' % err |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 127 | |
| 128 | process_output.close() |
| 129 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 130 | testCaseUtils.print_log_file(self, self.AUTHENTICATE_FILENAME) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 131 | |
| 132 | def verify_authentication_should_have_started(self): |
| 133 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-EAP-STARTED', self.AUTHENTICATE_FILENAME) |
| 134 | assert statusLines, 'Authentication was not started' |
| 135 | |
| 136 | def verify_authentication_should_have_completed(self): |
| 137 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-EAP-SUCCESS', self.AUTHENTICATE_FILENAME) |
| 138 | assert statusLines, 'Authentication was not completed successfully' |
| 139 | |
| 140 | def verify_authentication_should_have_disconnected(self): |
| 141 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-DISCONNECTED', self.AUTHENTICATE_FILENAME) |
| 142 | assert statusLines, 'Authentication was not disconnected' |
| 143 | |
| 144 | def verify_authentication_should_have_terminated(self): |
| 145 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-TERMINATING', self.AUTHENTICATE_FILENAME) |
| 146 | assert statusLines, 'Authentication was not terminated' |
| 147 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 148 | |
| 149 | def run_test(root_dir, voltha_dir, log_dir): |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 150 | auth = Authentication() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 151 | auth.a_set_log_dirs(root_dir, voltha_dir, log_dir) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 152 | auth.discover_rg_pod_name() |
| 153 | auth.discover_freeradius_pod_name() |
| 154 | auth.discover_freeradius_ip_addr() |
| 155 | auth.set_current_freeradius_ip_in_aaa_json() |
| 156 | auth.alter_aaa_application_configuration_in_onos_using_aaa_json() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 157 | auth.execute_authentication_on_rg() |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 158 | auth.verify_authentication_should_have_started() |
| 159 | auth.verify_authentication_should_have_completed() |
| 160 | auth.verify_authentication_should_have_disconnected() |
| 161 | auth.verify_authentication_should_have_terminated() |