Skip to content

Commit 5b949e4

Browse files
authored
Merge pull request #830 from TasmiyaNalatwad/paramiko_to_ssh
Replacing Paramiko with ssh connection to host
2 parents e8c4a28 + 91205e6 commit 5b949e4

File tree

1 file changed

+19
-85
lines changed

1 file changed

+19
-85
lines changed

testcases/OpTestHtxBootme.py

+19-85
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import time
3333
import sys
3434
import subprocess
35-
import paramiko
3635
import itertools
3736
from datetime import datetime
3837

@@ -68,13 +67,20 @@ def setUp(self):
6867
if 'ppc64' not in res[-1]:
6968
self.fail("Platform does not support HTX tests")
7069

70+
self.host_ip = self.conf.args.host_ip
71+
self.host_user = self.conf.args.host_user
7172
self.host_password = self.conf.args.host_password
7273
self.mdt_file = self.conf.args.mdt_file
7374
self.time_limit = int(self.conf.args.time_limit)
7475
self.boot_count = int(self.conf.args.boot_count)
7576
self.htx_rpm_link=self.conf.args.htx_rpm_link
77+
78+
self.ssh_host = OpTestSSH(self.host_ip, self.host_user, self.host_password)
79+
self.ssh_host.set_system(self.conf.system())
7680

77-
if not self.execute_remote_command('test -e {}'.format(path)):
81+
path = "/usr/lpp/htx/mdt/%s" % self.mdt_file
82+
res = self.ssh_host.run_command("if [ -f %s ];then echo 'true';else echo 'false';fi" % path)
83+
if 'false' in res:
7884
log.debug("MDT file %s not found due to config" % self.mdt_file)
7985

8086
self.host_distro_name = self.util.distro_name()
@@ -140,9 +146,8 @@ def setup_htx(self):
140146
for rpm in ins_htx:
141147
self.con.run_command_ignore_fail("rpm -e %s" % rpm, timeout=30)
142148
log.debug("Deleted old htx rpm package from host")
143-
if self.execute_remote_command('test -e {}'.format('/usr/lpp/htx')):
144-
if not self.execute_remote_command('rm -rf {}'.format('/usr/lpp/htx')):
145-
self.fail("Failed to delete the file at /usr/lpp/htx")
149+
self.ssh_host.run_command("if [ -d %s ];then rm -rf %s;fi" %
150+
('/usr/lpp/htx', '/usr/lpp/htx'), timeout=60)
146151
if self.current_test_case == "HtxBootme_NicDevices":
147152
peer_ins_htx = self.ssh.run_command_ignore_fail('rpm -qa | grep htx')
148153
if peer_ins_htx:
@@ -176,9 +181,8 @@ def htx_check(self):
176181
Checks if HTX is running, and if no errors.
177182
"""
178183
log.debug("HTX Error logs")
179-
file_size = self.check_remote_file_size_command('wc -c {}'.format("/tmp/htx/htxerr"))
180-
file_size = int(file_size.split()[0])
181-
if file_size != 0:
184+
file_size = self.ssh_host.run_command('wc -c {}'.format("/tmp/htx/htxerr"))
185+
if int(file_size[0].split()[0]) != 0:
182186
self.fail("check errorlogs for exact error and failure")
183187
cmd = 'htxcmdline -query -mdt %s' % self.mdt_file
184188
res = self.con.run_command(cmd)
@@ -212,8 +216,8 @@ def htx_bootme_test(self):
212216
time.sleep(10)
213217
log.debug("Mdt start is still in progress")
214218
self.con.run_command(cmd)
215-
htxerr_file = self.check_remote_file_size_command('wc -c {}'.format("/tmp/htx/htxerr"))
216-
if int(htxerr_file.split()[0]) != 0:
219+
htxerr_file = self.ssh_host.run_command('wc -c {}'.format("/tmp/htx/htxerr"))
220+
if int(htxerr_file[0].split()[0]) != 0:
217221
self.fail("check error logs for exact error and failure")
218222
log.info("Reboot cycle %s completed successfully" % (i+1))
219223
reboot_time = time.time() - start_time
@@ -246,27 +250,6 @@ def is_system_online(self):
246250
i_try -= 1
247251
return False
248252

249-
def check_remote_file_size_command(self, command):
250-
"""
251-
Creates a new SSH client connection, executes a command,
252-
and then closes the connection.
253-
254-
:param command: Command to execute on the remote machine.
255-
:return: Command output as a string.
256-
or Returns Execption error msg if there was an error.
257-
"""
258-
try:
259-
client = paramiko.SSHClient()
260-
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
261-
client.connect(self.cv_HOST.ip, 22, self.cv_HOST.user, self.host_password)
262-
stdin, stdout, stderr = client.exec_command(command)
263-
output = stdout.read().decode().strip()
264-
client.close()
265-
return output
266-
except Exception as e:
267-
print(f"An error occurred: {e}")
268-
return e
269-
270253
def wait_for_reboot_completion(self, ip_addr, timeout=500):
271254
"""
272255
Wait for the system to become available after reboot.
@@ -367,13 +350,12 @@ def setUp(self):
367350

368351
if not self.all and self.block_devices is None:
369352
self.fail("Needs the block devices to run the HTX")
370-
if self.all:
353+
if self.all == "True":
371354
self.block_device = ""
372355
else:
373356
self.block_device = []
374357
for dev in self.block_devices.split():
375-
dev_path = self.get_absolute_disk_path(dev)
376-
dev_base = self.execute_remote_command('basename $(realpath {})'.format(dev_path))
358+
dev_base = self.ssh_host.run_command('basename $(realpath {})'.format(dev))[0]
377359
if 'dm' in dev_base:
378360
dev_base = self.get_mpath_from_dm(dev_base)
379361
self.block_device.append(dev_base)
@@ -383,7 +365,8 @@ def start_htx_run(self):
383365
super(HtxBootme_BlockDevice, self).start_htx_run()
384366

385367
path = "/usr/lpp/htx/mdt/%s" % self.mdt_file
386-
if self.execute_remote_command('test -e {}'.format(path)):
368+
res = self.ssh_host.run_command("if [ -d %s ];then echo 'true';else echo 'false';fi" % path)
369+
if 'true' in res:
387370
self.fail(f"MDT file {self.mdt_file} not found")
388371

389372
log.debug("selecting the mdt file ")
@@ -469,53 +452,4 @@ def get_mpath_from_dm(self, dm_id):
469452
raise MPException(f"Multipathd Command Failed : {ex} ")
470453
for mpath in mpaths:
471454
if dm_id in mpath:
472-
return mpath.split()[1]
473-
474-
def get_all_disk_paths(self):
475-
"""
476-
Returns all available disk names and alias on this system
477-
478-
This will get all the sysfs disks name entries by its device
479-
node name, by-uuid, by-id and by-path, irrespective of any
480-
platform and device type
481-
482-
:returns: a list of all disk path names
483-
:rtype: list of str
484-
"""
485-
disk_list = abs_path = []
486-
for path in [
487-
"/dev",
488-
"/dev/mapper",
489-
"/dev/disk/by-id",
490-
"/dev/disk/by-path",
491-
"/dev/disk/by-uuid",
492-
"/dev/disk/by-partuuid",
493-
"/dev/disk/by-partlabel",
494-
]:
495-
if self.execute_remote_command('test -e {}'.format(path)):
496-
directory = self.execute_remote_command('ls -l {}'.format(path))
497-
for device in directory:
498-
abs_path.append(path + '/' + device)
499-
disk_list.extend(abs_path)
500-
return disk_list
501-
502-
def get_absolute_disk_path(self, device):
503-
"""
504-
Returns absolute device path of given disk
505-
506-
This will get actual disks path of given device, it can take
507-
node name, by-uuid, by-id and by-path, irrespective of any
508-
platform and device type
509-
510-
:param device: disk name or disk alias names sda or scsi-xxx
511-
:type device: str
512-
513-
:returns: the device absolute path name
514-
:rtype: bool
515-
"""
516-
if not self.execute_remote_command('test -e {}'.format(device)):
517-
for dev_path in self.get_all_disk_paths():
518-
dev_base = self.execute_remote_command('basename $(realpath {})'.format(dev_path))
519-
if device == dev_base:
520-
return dev_path
521-
return device
455+
return mpath.split()[-1]

0 commit comments

Comments
 (0)