blob: 69d3d1a4efe7e7b603d186fb52cf66f18f747c77 [file] [log] [blame]
Gilles Depatieed99efe2019-03-12 16:12:26 -04001#
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 Unicast Test Case module
19"""
20
21import time
22import testCaseUtils
23import logging
24import subprocess
Gilles Depatieea423712019-04-12 16:39:12 -040025import json
Gilles Depatieed99efe2019-03-12 16:12:26 -040026
27
28class Unicast(object):
29 """
30 This class implements voltha Unicast test case
31 """
32
33 PING_TEST_FILENAME = 'voltha_ping_test.log'
34 TCPDUMP_FILENAME = 'voltha_tcpdump.log'
35
36 def __init__(self):
37 self.dirs = dict()
38 self.dirs['log'] = None
39 self.dirs['root'] = None
40 self.dirs['voltha'] = None
41
42 self.__rgName = testCaseUtils.discover_rg_pod_name()
43 self.__fields = None
44 self.__tcpdumpPid = None
Gilles Depatieea423712019-04-12 16:39:12 -040045 self.__onuType = None
46 self.__onuCount = None
47 self.__onuSerialNum = []
Gilles Depatieed99efe2019-03-12 16:12:26 -040048 self.__sadisCTag = None
49 self.__sadisSTag = None
Gilles Depatieea423712019-04-12 16:39:12 -040050 self.__datastore = None
Gilles Depatieed99efe2019-03-12 16:12:26 -040051
52 def u_set_log_dirs(self, root_dir, voltha_dir, log_dir):
53 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
54
Gilles Depatieea423712019-04-12 16:39:12 -040055 def u_configure(self, onu_type, onu_count):
56 self.__onuType = onu_type
57 self.__onuCount = onu_count
58
Gilles Depatieed99efe2019-03-12 16:12:26 -040059 def execute_ping_test(self):
60 logging.info('Ping 1.2.3.4 IP Test')
61 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.PING_TEST_FILENAME), 'w')
62 pingTest = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-it', '-n', 'voltha', self.__rgName, '--',
63 '/bin/ping', '-I', 'eth0', '1.2.3.4'],
64 stdout=process_output,
65 stderr=process_output)
66
67 self.execute_tcpdump()
68
69 self.kill_ping_test()
70
71 pingTest.wait()
72 process_output.close()
73
74 testCaseUtils.print_log_file(self, self.PING_TEST_FILENAME)
75 testCaseUtils.print_log_file(self, self.TCPDUMP_FILENAME)
76 print
77
78 def execute_tcpdump(self):
79 logging.info('Execute tcpdump')
80 process_output = open('%s/%s' % (testCaseUtils.get_dir(self, 'log'), self.TCPDUMP_FILENAME), 'w')
81 tcpdump = subprocess.Popen(['sudo', '/usr/sbin/tcpdump', '-nei', 'pon1'],
82 stdout=process_output,
83 stderr=process_output)
84 self.__tcpdumpPid = tcpdump.pid
85
86 time.sleep(20)
87
88 self.kill_tcpdump()
89 tcpdump.wait()
90 process_output.close()
91
92 def kill_ping_test(self):
93 procPidPing1 = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'ps', '-ef'],
94 stdout=subprocess.PIPE,
95 stderr=subprocess.PIPE)
96 procPidPing2 = subprocess.Popen(['grep', '-e', '/bin/ping'], stdin=procPidPing1.stdout,
97 stdout=subprocess.PIPE,
98 stderr=subprocess.PIPE)
99 procPidPing3 = subprocess.Popen(['awk', "{print $2}"], stdin=procPidPing2.stdout,
100 stdout=subprocess.PIPE,
101 stderr=subprocess.PIPE)
102
103 procPidPing1.stdout.close()
104 procPidPing2.stdout.close()
105
106 out, err = procPidPing3.communicate()
107 pingPid = out.strip()
108
109 procKillPing = subprocess.Popen(['/usr/bin/kubectl', 'exec', '-n', 'voltha', self.__rgName, '--', 'kill', pingPid],
110 stdout=subprocess.PIPE,
111 stderr=subprocess.PIPE)
112 out, err = procKillPing.communicate()
113 assert not err, 'Killing Ping returned %s' % err
114
115 def kill_tcpdump(self):
116 procKillTcpdump = subprocess.Popen(['sudo', 'pkill', '-P', str(self.__tcpdumpPid)],
117 stdout=subprocess.PIPE,
118 stderr=subprocess.PIPE)
119 out, err = procKillTcpdump.communicate()
120 assert not err, 'Killing Tcpdump returned %s' % err
121
122 def ping_test_should_have_failed(self):
123 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Destination Host Unreachable', self.PING_TEST_FILENAME)
124 assert statusLines, 'Ping Test Issue, no Destination Host Unreachable'
125 lineCount = 0
126 lines = statusLines.splitlines()
127 for line in lines:
128 logging.debug(line)
129 lineCount += 1
130 if lineCount > 1: # Must have 2 or more instances
131 return True
132
133 def stag_and_ctag_should_match_sadis_entry(self):
134 logging.info('Evaluating sTag and cTag in each packet')
135 statusLines = testCaseUtils.get_fields_from_grep_command(self, '802.1Q', self.TCPDUMP_FILENAME)
136 assert statusLines, 'tcpdump contains no 802.1Q tagged packets'
137 lines = statusLines.splitlines()
138 for line in lines:
139 header, tagId, after = line.partition('802.1Q')
140 self.__fields = testCaseUtils.parse_fields(after, ',')
141 stag = self.__fields[1].strip().split(':')[1].strip().split()[1].strip()
142 before, tagId, after = line.rpartition('802.1Q')
143 self.__fields = testCaseUtils.parse_fields(after, ',')
144 ctag = self.__fields[1].strip().split()[1].strip()
145 self.stag_and_ctag_should_match_sadis_file(ctag, stag)
146
147 def should_have_q_in_q_vlan_tagging(self):
148 statusLines = testCaseUtils.get_fields_from_grep_command(self, '"Request who-has"', self.TCPDUMP_FILENAME)
149 assert statusLines, 'tcpdump contains no ping packets'
150 lines = statusLines.splitlines()
151 for line in lines:
152 tagCount = line.count('802.1Q')
153 assert tagCount == 2, 'Found a non double tagged packet'
154
Gilles Depatieea423712019-04-12 16:39:12 -0400155 def retrieve_onu_serial_numbers(self):
156 logging.info('Onu Serial Number Discovery')
157 statusLines = testCaseUtils.get_fields_from_grep_command(self, self.__onuType, 'voltha_devices_after_enable.log')
158 assert statusLines, 'No Onu listed under devices'
159 lines = statusLines.splitlines()
160 assert len(lines) == self.__onuCount, 'Onu count mismatch found: %s, should be: %s' % (len(lines), self.__onuCount)
161 for line in lines:
162 self.__fields = testCaseUtils.parse_fields(line, '|')
163 onuSerialNum = self.__fields[5].strip()
164 self.__onuSerialNum.append(onuSerialNum)
165
166 def retrieve_stag_and_ctag_for_onu(self, onu_serial_num):
167 entries = self.__datastore['org.opencord.sadis']['sadis']['entries']
168 for entry in entries:
169 entry_id = entry['id']
170 if entry_id == onu_serial_num:
171 self.__sadisCTag = entry['cTag']
172 self.__sadisSTag = entry['sTag']
173
174 def read_sadis_entries_from_sadis_json(self):
175 with open('%s/tests/atests/build/sadis_json' % testCaseUtils.get_dir(self, 'voltha'), 'r') as sadis:
176 self.__datastore = json.load(sadis)
Gilles Depatieed99efe2019-03-12 16:12:26 -0400177
178 def stag_and_ctag_should_match_sadis_file(self, ctag, stag):
Gilles Depatieea423712019-04-12 16:39:12 -0400179 assert ctag == str(self.__sadisCTag) and stag == str(self.__sadisSTag), 'cTag and/or sTag do not match value in sadis file\n \
Gilles Depatieed99efe2019-03-12 16:12:26 -0400180 vlan cTag = %s, sadis cTag = %s : vlan sTag = %s, sadis sTag = %s' % (ctag, self.__sadisCTag, stag, self.__sadisSTag)
181
Gilles Depatieea423712019-04-12 16:39:12 -0400182 def manage_onu_testing(self):
183 for onuSerial in self.__onuSerialNum:
184 self.retrieve_stag_and_ctag_for_onu(onuSerial)
185 self.execute_ping_test()
186 self.ping_test_should_have_failed()
187 self.should_have_q_in_q_vlan_tagging()
188 self.stag_and_ctag_should_match_sadis_entry()
Gilles Depatieed99efe2019-03-12 16:12:26 -0400189
Gilles Depatieea423712019-04-12 16:39:12 -0400190
191def run_test(onu_type, onu_count, root_dir, voltha_dir, log_dir):
Gilles Depatieed99efe2019-03-12 16:12:26 -0400192
193 unicast = Unicast()
194 unicast.u_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatieea423712019-04-12 16:39:12 -0400195 unicast.u_configure(onu_type, onu_count)
196 unicast.read_sadis_entries_from_sadis_json()
197 unicast.retrieve_onu_serial_numbers()
198 unicast.manage_onu_testing()