blob: 5cde7b510457b3b5e27974ba5c18134f066287ab [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
41 self.__rgName = None
42 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
48 def discover_rg_pod_name(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050049 self.__rgName = testCaseUtils.extract_pod_name('rg-').strip()
Gilles Depatiea85fe812019-01-23 15:55:53 -050050
51 def discover_freeradius_pod_name(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050052 self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip()
53 logging.info('freeradius Name = %s' % self.__radiusName)
Gilles Depatiea85fe812019-01-23 15:55:53 -050054
55 def discover_freeradius_ip_addr(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050056 ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName)
Gilles Depatiea85fe812019-01-23 15:55:53 -050057 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 Depatie0bf31352019-02-04 13:48:41 -050062 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 Depatiea85fe812019-01-23 15:55:53 -050064
65 def alter_aaa_application_configuration_in_onos_using_aaa_json(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050066 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 Depatiea85fe812019-01-23 15:55:53 -050070 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050071 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
72 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatiea85fe812019-01-23 15:55:53 -050073
Gilles Depatie0bf31352019-02-04 13:48:41 -050074 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 Depatiea85fe812019-01-23 15:55:53 -050078 '/sbin/wpa_supplicant -Dwired -ieth0 -c /etc/wpa_supplicant/wpa_supplicant.conf'],
79 stdout=process_output,
80 stderr=process_output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050081
Gilles Depatiea85fe812019-01-23 15:55:53 -050082 time.sleep(15)
Gilles Depatie0bf31352019-02-04 13:48:41 -050083 logging.debug('return value from supplicant subprocess = %s' % proc1.returncode)
Gilles Depatiea85fe812019-01-23 15:55:53 -050084 procPidSupplicant1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
Gilles Depatie0bf31352019-02-04 13:48:41 -050085 stdout=subprocess.PIPE,
86 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -050087 procPidSupplicant2 = subprocess.Popen(['grep', '-e', '/sbin/wpa_supplicant'], stdin=procPidSupplicant1.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -050088 stdout=subprocess.PIPE,
89 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -050090 procPidSupplicant3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidSupplicant2.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -050091 stdout=subprocess.PIPE,
92 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -050093
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 Depatie0bf31352019-02-04 13:48:41 -0500101 stdout=subprocess.PIPE,
102 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500103 out, err = procKillSupplicant1.communicate()
Gilles Depatie0bf31352019-02-04 13:48:41 -0500104 assert not err, 'Killing Supplicant returned %s' % err
Gilles Depatiea85fe812019-01-23 15:55:53 -0500105
106 procPidBash1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
Gilles Depatie0bf31352019-02-04 13:48:41 -0500107 stdout=subprocess.PIPE,
108 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500109 procPidBash2 = subprocess.Popen(['grep', '-e', '/bin/bash'], stdin=procPidBash1.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -0500110 stdout=subprocess.PIPE,
111 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500112 procPidBash3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidBash2.stdout,
Gilles Depatie0bf31352019-02-04 13:48:41 -0500113 stdout=subprocess.PIPE,
114 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500115
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 Depatie0bf31352019-02-04 13:48:41 -0500123 stdout=subprocess.PIPE,
124 stderr=subprocess.PIPE)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500125 out, err = procKillBash1.communicate()
Gilles Depatie0bf31352019-02-04 13:48:41 -0500126 assert not err, 'Killing Bash returned %s' % err
Gilles Depatiea85fe812019-01-23 15:55:53 -0500127
128 process_output.close()
129
Gilles Depatie0bf31352019-02-04 13:48:41 -0500130 testCaseUtils.print_log_file(self, self.AUTHENTICATE_FILENAME)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500131
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 Depatie0bf31352019-02-04 13:48:41 -0500148
149def run_test(root_dir, voltha_dir, log_dir):
Gilles Depatiea85fe812019-01-23 15:55:53 -0500150 auth = Authentication()
Gilles Depatie0bf31352019-02-04 13:48:41 -0500151 auth.a_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatiea85fe812019-01-23 15:55:53 -0500152 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 Depatie0bf31352019-02-04 13:48:41 -0500157 auth.execute_authentication_on_rg()
Gilles Depatiea85fe812019-01-23 15:55:53 -0500158 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()