-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_ssh.py
98 lines (84 loc) · 3.94 KB
/
test_ssh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import logging
import os
import time
import io
from automation_infra.plugins.ssh_direct import SshDirect
# from devops_automation_infra.installers import docker
from devops_automation_infra.plugins.ssh import SSH
from automation_infra.plugins.admin import Admin
from pytest_automation_infra.helpers import hardware_config
installer = 'devops_docker'
def _test_fileobj_upload(host):
string_obj = io.StringIO("sasha king")
host.SSH.put_content_from_fileobj(string_obj, "/tmp/test_obj/file")
content_to_verify = host.SSH.get_contents("/tmp/test_obj/file")
assert content_to_verify == b"sasha king"
logging.info("Uploading content from fileobj to existing dir")
string_obj = io.StringIO("sasha king")
host.SSH.put_content_from_fileobj(string_obj, "/tmp/test_obj/file2")
content_to_verify = host.SSH.get_contents("/tmp/test_obj/file2")
assert content_to_verify == b"sasha king"
logging.info("Testing very large content of 1MB")
large_content = "s" * 1024 * 1024
content = io.StringIO(large_content)
host.SSH.put_content_from_fileobj(content, "/tmp/test_obj/large_content")
content_to_verify = host.SSH.get_contents("/tmp/test_obj/large_content").decode()
assert content_to_verify == large_content
def _test_upload_download(host):
os.system("echo this is a test > /tmp/temp.txt")
host.SSH.upload('/tmp/temp.txt', '/tmp/test_upload.txt')
host.SSH.download('/tmp/test_download.txt', '/tmp/test_upload.txt')
with open('/tmp/test_download.txt', 'r') as f:
contents = f.read().strip()
assert contents == 'this is a test'
def _test_rsync_ssh(host, host_ssh):
logging.info("create some files")
os.system("mkdir -p /tmp/rsync/1 /tmp/rsync/2")
os.system('echo "file 1" > /tmp/rsync/1/file')
os.system('echo "file 2" > /tmp/rsync/2/file')
logging.info("rsync")
sync_dir = host.mktemp()
if host.Admin.exists(sync_dir):
host.Admin.rm(sync_dir)
host_ssh.rsync('/tmp/rsync', sync_dir)
files = host_ssh.execute(f'find {sync_dir}/rsync/ -type f').split()
assert set(files) == set([f'{sync_dir}/rsync/1/file', f'{sync_dir}/rsync/2/file'])
logging.info("add one more file")
os.system('echo "file 3" > /tmp/rsync/2/file3')
host_ssh.rsync('/tmp/rsync', sync_dir)
files = host_ssh.execute(f'find {sync_dir}/rsync/ -type f').split()
assert set(files) == set([f'{sync_dir}/rsync/1/file', f'{sync_dir}/rsync/2/file', f'{sync_dir}/rsync/2/file3'])
logging.info("Delete a file on sorce")
os.system('rm /tmp/rsync/2/file3')
host_ssh.rsync('/tmp/rsync', sync_dir)
files = host_ssh.execute(f'find {sync_dir}/rsync/ -type f').split()
assert set(files) == set([f'{sync_dir}/rsync/1/file', f'{sync_dir}/rsync/2/file'])
@hardware_config(hardware={"host": {}})
def test_ssh(base_config):
logging.info(f"PID of test_Ssh: {os.getpid()}")
logging.info(f"Running ssh test on host {base_config.hosts.host.ip}")
os.system("echo this is a test > /tmp/temp.txt")
base_config.hosts.host.SSH.put('/tmp/temp.txt', '/tmp')
base_config.hosts.host.SshDirect.upload('/tmp/temp.txt', '/tmp')
logging.info("put file!")
res = base_config.hosts.host.SSH.execute('ls /tmp')
assert 'temp.txt' in res.split()
base_config.hosts.host.SSH.execute('rm /tmp/temp.txt')
res = base_config.hosts.host.SSH.execute('ls /tmp')
logging.info("sleeping..")
time.sleep(1)
logging.info("woke up !")
assert 'temp.txt' not in res.split()
time.sleep(1)
logging.info("Uploading content from fileobj")
_test_fileobj_upload(base_config.hosts.host)
logging.info("Testing upload and download of files")
_test_upload_download(base_config.hosts.host)
logging.info("Test rsync on ssh direct")
host = base_config.hosts.host
try:
_test_rsync_ssh(host, host.SshDirect)
except NotImplementedError:
logging.warning("rsync not implemented on key_file auth")
logging.info("Test rsync on ssh")
_test_rsync_ssh(host, host.SSH)