Skip to content

Commit 2c1da34

Browse files
authored
Use paramiko for ssh connection in test_wr_arp (#2037)
* Use paramiko for ssh connection in test_wr_arp The test_wr_arp script put ssh key of ptfhost on dut to have password less ssh connection. Then the ptfhost can execute command remote on dut through this ssh connection. The problem is that the ssh key saved in ~/.ssh/authorized_keys on dut host may not persist after warm reboot. It would also be a problem when secure boot feature is introduced. This PR updated the test_wr_arp script to use paramiko for ssh connection and remote command execution between ptfhost and dut. Changes: * Update the ptf test script to use paramiko for SSH connection with dut * Disabled loganalyzer because test_wr_arp need to do warm-reboot on dut Signed-off-by: Xin Wang <[email protected]>
1 parent 322c707 commit 2c1da34

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import paramiko
22
import logging
3+
import socket
34
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException
45

56
logger = logging.getLogger(__name__)
@@ -57,6 +58,13 @@ def execCommand(self, cmd, timeout=DEFAULT_CMD_EXECUTION_TIMEOUT_SEC):
5758
logger.error('SSH Authentiaction failure with message: %s' % authenticationException)
5859
except BadHostKeyException as badHostKeyException:
5960
logger.error('SSH Authentiaction failure with message: %s' % badHostKeyException)
61+
except socket.timeout as e:
62+
# The ssh session will timeout in case of a successful reboot
63+
logger.error('Caught exception socket.timeout: {}, {}, {}'.format(repr(e), str(e), type(e)))
64+
retValue = 255
65+
except Exception as e:
66+
logger.error('Exception caught: {}, {}, type: {}'.format(repr(e), str(e), type(e)))
67+
logger.error(sys.exc_info())
6068
finally:
6169
client.close()
6270

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import datetime
1313
import traceback
1414
import sys
15+
import socket
1516
import threading
1617
from collections import defaultdict
1718
from pprint import pprint
@@ -22,6 +23,7 @@
2223
from ptf import config
2324
import ptf.dataplane as dataplane
2425
import ptf.testutils as testutils
26+
from device_connection import DeviceConnection
2527

2628

2729
class ArpTest(BaseTest):
@@ -60,19 +62,14 @@ def cmd(self, cmds):
6062

6163
return stdout, stderr, return_code
6264

63-
def ssh(self, cmds):
64-
ssh_cmds = ["ssh", "-oStrictHostKeyChecking=no", "-oServerAliveInterval=2", "admin@" + self.dut_ssh]
65-
ssh_cmds.extend(cmds)
66-
stdout, stderr, return_code = self.cmd(ssh_cmds)
67-
if stdout != []:
68-
self.log("stdout from dut: '%s'" % str(stdout))
69-
if stderr != []:
70-
self.log("stderr from dut '%s'" % str(stderr))
71-
self.log("return code from dut: '%s'" % str(return_code))
65+
def dut_exec_cmd(self, cmd):
66+
self.log("Executing cmd='{}'".format(cmd))
67+
stdout, stderr, return_code = self.dut_connection.execCommand(cmd, timeout=30)
68+
self.log("return_code={}, stdout={}, stderr={}".format(return_code, stdout, stderr))
7269

7370
if return_code == 0:
7471
return True, str(stdout)
75-
elif return_code == 255 and 'Timeout, server' in stderr and 'not responding' in stderr:
72+
elif return_code == 255:
7673
return True, str(stdout)
7774
else:
7875
return False, "return code: %d. stdout = '%s' stderr = '%s'" % (return_code, str(stdout), str(stderr))
@@ -82,14 +79,14 @@ def dut_thr(self, q_from, q_to):
8279
cmd = q_from.get()
8380
if cmd == 'WR':
8481
self.log("Rebooting remote side")
85-
res, res_text = self.ssh(["sudo", "warm-reboot", "-c", self.ferret_ip])
82+
res, res_text = self.dut_exec_cmd("sudo warm-reboot -c {}".format(self.ferret_ip))
8683
if res:
8784
q_to.put('ok: %s' % res_text)
8885
else:
8986
q_to.put('error: %s' % res_text)
9087
elif cmd == 'uptime':
9188
self.log("Check uptime remote side")
92-
res, res_text = self.ssh(["uptime", "-s"])
89+
res, res_text = self.dut_exec_cmd("uptime -s")
9390
if res:
9491
q_to.put('ok: %s' % res_text)
9592
else:
@@ -193,6 +190,9 @@ def setUp(self):
193190
config = self.get_param('config_file')
194191
self.ferret_ip = self.get_param('ferret_ip')
195192
self.dut_ssh = self.get_param('dut_ssh')
193+
self.dut_username = self.get_param('dut_username')
194+
self.dut_password = self.get_param('dut_password')
195+
self.dut_connection = DeviceConnection(self.dut_ssh, username=self.dut_username, password=self.dut_password)
196196
self.how_long = int(self.get_param('how_long', required=False, default=300))
197197

198198
if not os.path.isfile(config):

tests/arp/test_wr_arp.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from tests.common.fixtures.ptfhost_utils import copy_ptftests_directory # lgtm[py/unused-import]
77
from tests.common.fixtures.ptfhost_utils import change_mac_addresses # lgtm[py/unused-import]
88
from tests.common.fixtures.ptfhost_utils import remove_ip_addresses # lgtm[py/unused-import]
9-
from tests.common.platform.ssh_utils import prepare_testbed_ssh_keys as prepareTestbedSshKeys
109
from tests.ptf_runner import ptf_runner
1110

1211
logger = logging.getLogger(__name__)
1312

1413
pytestmark = [
15-
pytest.mark.topology('t0')
14+
pytest.mark.topology('t0'),
15+
pytest.mark.disable_loganalyzer
1616
]
1717

1818
# Globals
@@ -159,22 +159,7 @@ def setupRouteToPtfhost(self, duthost, ptfhost):
159159
assert result["rc"] == 0 or "No such process" in result["stderr"], \
160160
"Failed to delete route with error '{0}'".format(result["stderr"])
161161

162-
@pytest.fixture(scope='class', autouse=True)
163-
def prepareSshKeys(self, duthost, ptfhost, creds):
164-
'''
165-
Prepares testbed ssh keys by generating ssh key on ptf host and adding this key to known_hosts on duthost
166-
This class-scope fixture runs once before test start
167-
168-
Args:
169-
duthost (AnsibleHost): Device Under Test (DUT)
170-
ptfhost (AnsibleHost): Packet Test Framework (PTF)
171-
172-
Returns:
173-
None
174-
'''
175-
prepareTestbedSshKeys(duthost, ptfhost, creds['sonicadmin_user'])
176-
177-
def testWrArp(self, request, duthost, ptfhost):
162+
def testWrArp(self, request, duthost, ptfhost, creds):
178163
'''
179164
Control Plane Assistant test for Warm-Reboot.
180165
@@ -206,6 +191,8 @@ def testWrArp(self, request, duthost, ptfhost):
206191
params={
207192
'ferret_ip' : ptfIp,
208193
'dut_ssh' : dutIp,
194+
'dut_username': creds['sonicadmin_user'],
195+
'dut_password': creds['sonicadmin_password'],
209196
'config_file' : VXLAN_CONFIG_FILE,
210197
'how_long' : testDuration,
211198
},

0 commit comments

Comments
 (0)