Skip to content

Commit b311aed

Browse files
committed
Input validation before use and var rename
1 parent b3a3197 commit b311aed

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

ansible/roles/test/files/ptftests/arista.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -378,22 +378,31 @@ def verify_bgp_neigh_state(self, dut=None, state="Active"):
378378
def change_neigh_lag_state(self, lag, is_up=True):
379379
state = ['shut', 'no shut']
380380
self.do_cmd('configure')
381-
self.do_cmd('interface %s' % lag)
382-
self.do_cmd(state[is_up])
383-
self.do_cmd('exit')
384-
self.do_cmd('exit')
381+
is_match = re.match('(Port-Channel|Ethernet)\d+', lag)
382+
if is_match:
383+
output = self.do_cmd('interface %s' % lag)
384+
if 'Invalid' not in output:
385+
self.do_cmd(state[is_up])
386+
self.do_cmd('exit')
387+
self.do_cmd('exit')
385388

386389
def verify_neigh_lag_state(self, lag, state="connected", pre_check=True):
387390
lag_state = False
388391
msg_prefix = ['Postboot', 'Preboot']
389-
output = self.do_cmd('show interfaces %s | json' % lag)
390-
data = '\n'.join(output.split('\r\n')[1:-1])
391-
obj = json.loads(data)
392+
is_match = re.match('(Port-Channel|Ethernet)\d+', lag)
393+
if is_match:
394+
output = self.do_cmd('show interfaces %s | json' % lag)
395+
if 'Invalid' not in output:
396+
data = '\n'.join(output.split('\r\n')[1:-1])
397+
obj = json.loads(data)
398+
399+
if 'interfaces' in obj and lag in obj['interfaces']:
400+
lag_state = (obj['interfaces'][lag]['interfaceStatus'] == state)
401+
else:
402+
self.fails.add('%s: Verify LAG %s: Object missing in output' % (msg_prefix[pre_check], lag))
403+
return self.fails, lag_state
392404

393-
if 'interfaces' in obj and lag in obj['interfaces']:
394-
lag_state = (obj['interfaces'][lag]['interfaceStatus'] == state)
395-
else:
396-
self.fails.add('%s: Verify LAG %s: Object missing in output' % (msg_prefix[pre_check], lag))
405+
self.fails.add('%s: Invalid interface name' % msg_prefix[pre_check])
397406
return self.fails, lag_state
398407

399408
def check_gr_peer_status(self, output):

ansible/roles/test/files/ptftests/sad_path.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,19 @@ def change_bgp_dut_state(self, is_up=True):
176176
self.fails['dut'].add('Stderr: %s' % stderr)
177177

178178
def verify_bgp_dut_state(self, state='Idle'):
179-
state = state.split(',')
179+
states = state.split(',')
180180
bgp_state = {}
181181
bgp_state['v4'] = bgp_state['v6'] = False
182182
for key in self.neigh_bgp.keys():
183183
if key not in ['v4', 'v6']:
184184
continue
185-
self.log.append('Verifying if the DUT side BGP peer %s is %s' % (self.neigh_bgp[key], state))
185+
self.log.append('Verifying if the DUT side BGP peer %s is %s' % (self.neigh_bgp[key], states))
186186
stdout, stderr, return_code = self.cmd(['ssh', '-oStrictHostKeyChecking=no', self.dut_ssh, 'show ip bgp neighbor %s' % self.neigh_bgp[key]])
187187
if return_code == 0:
188188
for line in stdout.split('\n'):
189189
if 'BGP state' in line:
190190
curr_state = re.findall('BGP state = (\w+)', line)[0]
191-
bgp_state[key] = (curr_state in state)
191+
bgp_state[key] = (curr_state in states)
192192
break
193193
else:
194194
self.fails['dut'].add('Retreiving BGP info for peer %s from DUT side failed' % self.neigh_bgp[key])
@@ -220,17 +220,19 @@ def populate_lag_state(self):
220220

221221
def change_dut_lag_state(self, is_up=True):
222222
state = ['shutdown', 'startup']
223-
self.log.append('Changing state of %s from DUT side to %s' % (self.vm_dut_map[self.neigh_name]['dut_portchannel'], state[is_up]))
224-
stdout, stderr, return_code = self.cmd(['ssh', '-oStrictHostKeyChecking=no', self.dut_ssh, 'sudo config interface %s %s' % (state[is_up], self.vm_dut_map[self.neigh_name]['dut_portchannel'])])
223+
dut_portchannel = self.vm_dut_map[self.neigh_name]['dut_portchannel']
224+
if not re.match('(PortChannel|Ethernet)\d+', dut_portchannel): return
225+
self.log.append('Changing state of %s from DUT side to %s' % (dut_portchannel, state[is_up]))
226+
stdout, stderr, return_code = self.cmd(['ssh', '-oStrictHostKeyChecking=no', self.dut_ssh, 'sudo config interface %s %s' % (state[is_up], dut_portchannel)])
225227
if return_code != 0:
226-
self.fails['dut'].add('%s: State change not successful from DUT side for %s' % (self.msg_prefix[1 - is_up], self.vm_dut_map[self.neigh_name]['dut_portchannel']))
228+
self.fails['dut'].add('%s: State change not successful from DUT side for %s' % (self.msg_prefix[1 - is_up], dut_portchannel))
227229
self.fails['dut'].add('%s: Return code: %d' % (self.msg_prefix[1 - is_up], return_code))
228230
self.fails['dut'].add('%s: Stderr: %s' % (self.msg_prefix[1 - is_up], stderr))
229231
else:
230232
self.log.append('State change successful on DUT')
231233

232234
def verify_dut_lag_member_state(self, lag_memb_output, pre_check=True):
233-
fails = False
235+
success = True
234236
for member in self.vm_dut_map[self.neigh_name]['dut_ports']:
235237
if self.lag_members_down is not None and member in self.lag_members_down:
236238
search_str = '%s(D)' % member
@@ -240,9 +242,9 @@ def verify_dut_lag_member_state(self, lag_memb_output, pre_check=True):
240242
if lag_memb_output.find(search_str) != -1:
241243
self.log.append('Lag member %s state as expected' % member)
242244
else:
243-
fails = True
245+
success = False
244246
self.fails['dut'].add('%s: Lag member %s state not as expected' % (self.msg_prefix[pre_check], member))
245-
return fails
247+
return success
246248

247249
def verify_dut_lag_state(self, pre_check=True):
248250
pat = re.compile(".*%s\s+LACP\(A\)\(Dw\)\s+(.*)" % self.vm_dut_map[self.neigh_name]['dut_portchannel'])
@@ -251,7 +253,7 @@ def verify_dut_lag_state(self, pre_check=True):
251253
for line in stdout.split('\n'):
252254
if self.vm_dut_map[self.neigh_name]['dut_portchannel'] in line:
253255
is_match = pat.match(line)
254-
if is_match and not self.verify_dut_lag_member_state(is_match.group(1), pre_check=pre_check):
256+
if is_match and self.verify_dut_lag_member_state(is_match.group(1), pre_check=pre_check):
255257
self.log.append('Lag state is down as expected on the DUT')
256258
self.log.append('Pattern check: %s' % line)
257259
else:

0 commit comments

Comments
 (0)