blob: cedd7ee1618448b5651e797e6b723b63d456aebb [file] [log] [blame]
Gilles Depatie2e683692019-02-22 16:06:52 -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 DHCP Test Case module
19"""
20
21import os
22import testCaseUtils
23import logging
24import subprocess
25
26
27class 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
43 self.__rgName = None
44 self.__fields = None
45 self.__deviceId = None
46 self.__portNumber = None
47
48 def h_set_log_dirs(self, root_dir, voltha_dir, log_dir):
49 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
50
51 def lookup_rg_pod_name(self):
52 self.__rgName = testCaseUtils.extract_pod_name('rg-').strip()
53
54 def discover_authorized_users(self):
55 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
56 'voltha_onos_users.log', 'aaa-users')
57
58 def retrieve_authorized_users_device_id_and_port_number(self):
59 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'AUTHORIZED', 'voltha_onos_users.log')
60 assert statusLines, 'No Users Authorized'
61 self.__fields = testCaseUtils.parse_fields(statusLines, ',')
62 deviceField = self.__fields[2].strip()
63 deviceStr, equal, deviceId = deviceField.partition('=')
64 self.__deviceId = deviceId
65 portField = self.__fields[4].strip()
66 portNumStr, equal, portNum = portField.partition('=')
67 self.__portNumber = portNum
68
69 def add_subscriber_access(self):
70 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
71 'voltha_add_subscriber_access.log', 'volt-add-subscriber-access %s %s'
72 % (self.__deviceId, self.__portNumber))
73
74 def should_now_have_two_dhcp_flows(self):
75 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
76 'voltha_onos_flows.log', 'flows')
77 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'IP_PROTO:17', 'voltha_onos_flows.log')
78 assert statusLines, 'No DHCP Detection flows'
79 lines = statusLines.splitlines()
80 for line in lines:
81 self.__fields = testCaseUtils.parse_fields(line, ',')
82 inPortStr = self.__fields[10].strip()
83 selector, delimiter, inPort = inPortStr.partition('=[')
84 assert (inPort == 'IN_PORT:2' or inPort == 'IN_PORT:128'), 'DHCP detection flows not associated with expected ports'
85
86 def add_dhcp_server_configuration_data_in_onos(self):
87 logging.info('Adding DHCP Configuration Data to Onos NetCfg')
88 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
89 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcp_json'
90 % testCaseUtils.get_dir(self, 'voltha'))
91 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
92 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcp_json'
93 % testCaseUtils.get_dir(self, 'voltha'))
94
95 def activate_dhcp_server_in_onos(self):
96 logging.info('Activating DHCP server on Onos')
97 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
98 'voltha_dhcp_server_activate.log', 'app activate dhcp')
99 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Activated', 'voltha_dhcp_server_activate.log')
100 assert statusLines, 'DHCP server failed to be Activated'
101
102 def deactivate_dhcp_server_in_onos(self):
103 logging.info('Deactivating DHCP server on Onos')
104 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
105 'voltha_dhcp_server_deactivate.log', 'app deactivate dhcp')
106 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Deactivated', 'voltha_dhcp_server_deactivate.log')
107 assert statusLines, 'DHCP server failed to be Deactivated'
108
109 def query_for_default_ip_on_rg(self):
110 logging.info('De-assigning default IP on RG')
111 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.CHECK_IP_FILENAME), 'w')
112 ifconfigCheck1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
113 'ifconfig'],
114 stdout=subprocess.PIPE,
115 stderr=subprocess.PIPE)
116 ifconfigCheck2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifconfigCheck1.stdout,
117 stdout=process_output,
118 stderr=process_output)
119
120 ifconfigCheck1.wait()
121 ifconfigCheck1.stdout.close()
122 ifconfigCheck2.wait()
123
124 process_output.close()
125
126 testCaseUtils.print_log_file(self, self.CHECK_IP_FILENAME)
127
128 def de_assign_default_ip_on_rg(self):
129
130 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.CHECK_IP_FILENAME)
131 if statusLines:
132 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.DE_ASSIGN_IP_FILENAME), 'w')
133 os.system('/usr/bin/kubectl exec -n voltha %s -- bash -c "ifconfig eth0 0.0.0.0"' % self.__rgName)
134 ifconfigDeassign1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
135 'ifconfig'],
136 stdout=subprocess.PIPE,
137 stderr=subprocess.PIPE)
138
139 ifconfigDeassign2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifconfigDeassign1.stdout,
140 stdout=process_output,
141 stderr=process_output)
142 ifconfigDeassign1.wait()
143 ifconfigDeassign1.stdout.close()
144 ifconfigDeassign2.wait()
145
146 process_output.close()
147
148 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.DE_ASSIGN_IP_FILENAME)
149 assert not statusLines, 'IP addr not de-assigned'
150
151 else:
152 logging.info('No default IP addr assigned to eth0')
153
154 def assign_dhcp_ip_addr_to_rg(self):
155 logging.info('Assigning IP addr on RG using DHCP')
156 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.ASSIGN_DHCP_IP_FILENAME), 'w')
157 dhcpAssignIp1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-it', '-n', 'voltha', self.__rgName, '--',
158 'dhclient', '-v', 'eth0'],
159 stdout=process_output,
160 stderr=process_output)
161
162 dhcpAssignIp1.wait()
163 process_output.close()
164
165 testCaseUtils.print_log_file(self, self.ASSIGN_DHCP_IP_FILENAME)
166
167 def should_have_dhcp_assigned_ip(self):
168 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.CHECK_ASSIGNED_IP_FILENAME), 'w')
169 ifConfigCheck1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'bash', '-c',
170 'ifconfig'],
171 stdout=subprocess.PIPE,
172 stderr=subprocess.PIPE)
173
174 ifConfigCheck2 = subprocess.Popen(['grep', '-e', 'eth0', '-A1'], stdin=ifConfigCheck1.stdout,
175 stdout=process_output,
176 stderr=process_output)
177 ifConfigCheck1.wait()
178 ifConfigCheck1.stdout.close()
179 ifConfigCheck2.wait()
180
181 process_output.close()
182
183 testCaseUtils.print_log_file(self, self.CHECK_ASSIGNED_IP_FILENAME)
184
185 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'inet', self.CHECK_ASSIGNED_IP_FILENAME)
186 assert statusLines, 'DHCP IP addr not assigned'
187
188
189def set_firewall_rules():
190 logging.info('Setting Firewall rules for DHCP test')
191 os.system('sudo iptables -P FORWARD ACCEPT')
192
193
194def run_test(root_dir, voltha_dir, log_dir):
195
196 set_firewall_rules()
197 dhcp = DHCP()
198 dhcp.h_set_log_dirs(root_dir, voltha_dir, log_dir)
199 dhcp.lookup_rg_pod_name()
200 dhcp.discover_authorized_users()
201 dhcp.retrieve_authorized_users_device_id_and_port_number()
202 dhcp.add_subscriber_access()
203 dhcp.should_now_have_two_dhcp_flows()
204 dhcp.deactivate_dhcp_server_in_onos()
205 dhcp.add_dhcp_server_configuration_data_in_onos()
206 dhcp.activate_dhcp_server_in_onos()
207 dhcp.query_for_default_ip_on_rg()
208 dhcp.de_assign_default_ip_on_rg()
209 dhcp.assign_dhcp_ip_addr_to_rg()
210 dhcp.should_have_dhcp_assigned_ip()