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 | |
Gilles Depatie | ed99efe | 2019-03-12 16:12:26 -0400 | [diff] [blame] | 41 | self.__rgName = testCaseUtils.discover_rg_pod_name() |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 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 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 48 | def discover_freeradius_pod_name(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 49 | self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip() |
| 50 | logging.info('freeradius Name = %s' % self.__radiusName) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 51 | |
| 52 | def discover_freeradius_ip_addr(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 53 | ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 54 | assert ipAddr, 'No IP address listed for freeradius' |
| 55 | self.__radiusIp = ipAddr.strip() |
| 56 | logging.info('freeradius IP = %s' % self.__radiusIp) |
| 57 | |
| 58 | def set_current_freeradius_ip_in_aaa_json(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 59 | status = testCaseUtils.modify_radius_ip_in_json_using_sed(self, self.__radiusIp) |
| 60 | 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] | 61 | |
| 62 | def alter_aaa_application_configuration_in_onos_using_aaa_json(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 63 | logging.info('Altering the Onos NetCfg AAA apps with Freeradius IP address') |
| 64 | logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
| 65 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json' |
| 66 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 67 | 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] | 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 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 71 | def execute_authentication_on_rg(self): |
| 72 | logging.info('Running Radius Authentication from RG') |
| 73 | process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.AUTHENTICATE_FILENAME), 'w') |
| 74 | 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] | 75 | '/sbin/wpa_supplicant -Dwired -ieth0 -c /etc/wpa_supplicant/wpa_supplicant.conf'], |
| 76 | stdout=process_output, |
| 77 | stderr=process_output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 78 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 79 | time.sleep(15) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 80 | logging.debug('return value from supplicant subprocess = %s' % proc1.returncode) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 81 | 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] | 82 | stdout=subprocess.PIPE, |
| 83 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 84 | procPidSupplicant2 = subprocess.Popen(['grep', '-e', '/sbin/wpa_supplicant'], stdin=procPidSupplicant1.stdout, |
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 | procPidSupplicant3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidSupplicant2.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 | |
| 91 | procPidSupplicant1.stdout.close() |
| 92 | procPidSupplicant2.stdout.close() |
| 93 | |
| 94 | out, err = procPidSupplicant3.communicate() |
| 95 | supplicantPid = out.strip() |
Gilles Depatie | ed99efe | 2019-03-12 16:12:26 -0400 | [diff] [blame] | 96 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 97 | 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] | 98 | stdout=subprocess.PIPE, |
| 99 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 100 | out, err = procKillSupplicant1.communicate() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 101 | assert not err, 'Killing Supplicant returned %s' % err |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 102 | |
| 103 | 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] | 104 | stdout=subprocess.PIPE, |
| 105 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 106 | procPidBash2 = subprocess.Popen(['grep', '-e', '/bin/bash'], stdin=procPidBash1.stdout, |
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 | procPidBash3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidBash2.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 | |
| 113 | procPidBash1.stdout.close() |
| 114 | procPidBash2.stdout.close() |
| 115 | |
| 116 | out, err = procPidBash3.communicate() |
| 117 | bashPid = out.strip() |
| 118 | |
| 119 | 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] | 120 | stdout=subprocess.PIPE, |
| 121 | stderr=subprocess.PIPE) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 122 | out, err = procKillBash1.communicate() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 123 | assert not err, 'Killing Bash returned %s' % err |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 124 | |
| 125 | process_output.close() |
| 126 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 127 | testCaseUtils.print_log_file(self, self.AUTHENTICATE_FILENAME) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 128 | |
| 129 | def verify_authentication_should_have_started(self): |
| 130 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-EAP-STARTED', self.AUTHENTICATE_FILENAME) |
| 131 | assert statusLines, 'Authentication was not started' |
| 132 | |
| 133 | def verify_authentication_should_have_completed(self): |
| 134 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-EAP-SUCCESS', self.AUTHENTICATE_FILENAME) |
| 135 | assert statusLines, 'Authentication was not completed successfully' |
| 136 | |
| 137 | def verify_authentication_should_have_disconnected(self): |
| 138 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-DISCONNECTED', self.AUTHENTICATE_FILENAME) |
| 139 | assert statusLines, 'Authentication was not disconnected' |
| 140 | |
| 141 | def verify_authentication_should_have_terminated(self): |
| 142 | statusLines = testCaseUtils.get_fields_from_grep_command(self, 'CTRL-EVENT-TERMINATING', self.AUTHENTICATE_FILENAME) |
| 143 | assert statusLines, 'Authentication was not terminated' |
| 144 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 145 | |
| 146 | def run_test(root_dir, voltha_dir, log_dir): |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 147 | auth = Authentication() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 148 | auth.a_set_log_dirs(root_dir, voltha_dir, log_dir) |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 149 | auth.discover_freeradius_pod_name() |
| 150 | auth.discover_freeradius_ip_addr() |
| 151 | auth.set_current_freeradius_ip_in_aaa_json() |
| 152 | auth.alter_aaa_application_configuration_in_onos_using_aaa_json() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 153 | auth.execute_authentication_on_rg() |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 154 | auth.verify_authentication_should_have_started() |
| 155 | auth.verify_authentication_should_have_completed() |
| 156 | auth.verify_authentication_should_have_disconnected() |
| 157 | auth.verify_authentication_should_have_terminated() |