blob: aedbbf2ebe874b38b3470d9e26fc1f5426222592 [file] [log] [blame]
Gilles Depatiea85fe812019-01-23 15:55:53 -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 Authentication Test Case module
19"""
20
21import time
22import os
23import subprocess
Gilles Depatiea85fe812019-01-23 15:55:53 -050024import testCaseUtils
25import logging
Gilles Depatie0bf31352019-02-04 13:48:41 -050026
Gilles Depatiea85fe812019-01-23 15:55:53 -050027
28class 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 Depatie0bf31352019-02-04 13:48:41 -050036 self.dirs = dict()
37 self.dirs['log'] = None
38 self.dirs['root'] = None
39 self.dirs['voltha'] = None
Gilles Depatiea85fe812019-01-23 15:55:53 -050040
Gilles Depatieed99efe2019-03-12 16:12:26 -040041 self.__rgName = testCaseUtils.discover_rg_pod_name()
Gilles Depatiea85fe812019-01-23 15:55:53 -050042 self.__radiusName = None
43 self.__radiusIp = None
44
Gilles Depatie0bf31352019-02-04 13:48:41 -050045 def a_set_log_dirs(self, root_dir, voltha_dir, log_dir):
46 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
Gilles Depatiea85fe812019-01-23 15:55:53 -050047
Gilles Depatiea85fe812019-01-23 15:55:53 -050048 def discover_freeradius_pod_name(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050049 self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip()
50 logging.info('freeradius Name = %s' % self.__radiusName)
Gilles Depatiea85fe812019-01-23 15:55:53 -050051
52 def discover_freeradius_ip_addr(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050053 ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName)
Gilles Depatiea85fe812019-01-23 15:55:53 -050054 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 Depatie0bf31352019-02-04 13:48:41 -050059 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 Depatiea85fe812019-01-23 15:55:53 -050061
62 def alter_aaa_application_configuration_in_onos_using_aaa_json(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050063 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 Depatiea85fe812019-01-23 15:55:53 -050067 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050068 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
69 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatiea85fe812019-01-23 15:55:53 -050070
Gilles Depatie0bf31352019-02-04 13:48:41 -050071 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 Depatiea85fe812019-01-23 15:55:53 -050075 '/sbin/wpa_supplicant -Dwired -ieth0 -c /etc/wpa_supplicant/wpa_supplicant.conf'],
76 stdout=process_output,
77 stderr=process_output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050078
Gilles Depatiea85fe812019-01-23 15:55:53 -050079 time.sleep(15)
Gilles Depatie0bf31352019-02-04 13:48:41 -050080 logging.debug('return value from supplicant subprocess = %s' % proc1.returncode)
Gilles Depatiea85fe812019-01-23 15:55:53 -050081 procPidSupplicant1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
Gilles Depatie0bf31352019-02-04 13:48:41 -050082 stdout=subprocess.PIPE,
83 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -050084 procPidSupplicant2 = subprocess.Popen(['grep', '-e', '/sbin/wpa_supplicant'], stdin=procPidSupplicant1.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -050085 stdout=subprocess.PIPE,
86 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -050087 procPidSupplicant3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidSupplicant2.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -050088 stdout=subprocess.PIPE,
89 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -050090
91 procPidSupplicant1.stdout.close()
92 procPidSupplicant2.stdout.close()
93
94 out, err = procPidSupplicant3.communicate()
95 supplicantPid = out.strip()
Gilles Depatieed99efe2019-03-12 16:12:26 -040096
Gilles Depatiea85fe812019-01-23 15:55:53 -050097 procKillSupplicant1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'kill', supplicantPid],
Gilles Depatie0bf31352019-02-04 13:48:41 -050098 stdout=subprocess.PIPE,
99 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500100 out, err = procKillSupplicant1.communicate()
Gilles Depatie0bf31352019-02-04 13:48:41 -0500101 assert not err, 'Killing Supplicant returned %s' % err
Gilles Depatiea85fe812019-01-23 15:55:53 -0500102
103 procPidBash1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
Gilles Depatie0bf31352019-02-04 13:48:41 -0500104 stdout=subprocess.PIPE,
105 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500106 procPidBash2 = subprocess.Popen(['grep', '-e', '/bin/bash'], stdin=procPidBash1.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -0500107 stdout=subprocess.PIPE,
108 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500109 procPidBash3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidBash2.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -0500110 stdout=subprocess.PIPE,
111 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500112
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 Depatie0bf31352019-02-04 13:48:41 -0500120 stdout=subprocess.PIPE,
121 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500122 out, err = procKillBash1.communicate()
Gilles Depatie0bf31352019-02-04 13:48:41 -0500123 assert not err, 'Killing Bash returned %s' % err
Gilles Depatiea85fe812019-01-23 15:55:53 -0500124
125 process_output.close()
126
Gilles Depatie0bf31352019-02-04 13:48:41 -0500127 testCaseUtils.print_log_file(self, self.AUTHENTICATE_FILENAME)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500128
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 Depatie0bf31352019-02-04 13:48:41 -0500145
146def run_test(root_dir, voltha_dir, log_dir):
Gilles Depatiea85fe812019-01-23 15:55:53 -0500147 auth = Authentication()
Gilles Depatie0bf31352019-02-04 13:48:41 -0500148 auth.a_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500149 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 Depatie0bf31352019-02-04 13:48:41 -0500153 auth.execute_authentication_on_rg()
Gilles Depatiea85fe812019-01-23 15:55:53 -0500154 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()