subprocess-util/test.py

183 lines
5.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2023-01-10 20:18:28 +01:00
import os
import shlex
2023-01-10 22:13:56 +01:00
import shutil
import socket
2023-01-10 17:24:35 +01:00
from pathlib import Path
2023-01-10 20:18:28 +01:00
from exec_capture import execute_capture
from exec_print_capture import execute_print_capture
from exec_print_receive import execute_print_receive_chunks
2023-01-10 20:18:28 +01:00
from exec_print_transfer import execute_print_transfer_chunks
2023-01-10 22:13:56 +01:00
from transfer_inform import transfer_inform
2023-01-10 15:42:33 +01:00
def test():
2023-01-10 20:18:28 +01:00
# test1()
# test2()
# test3()
# test4()
# test5()
test67()
def test1():
2023-01-10 22:13:56 +01:00
_init(1)
returncode, out, err = execute_capture(['ls', '-la'])
print(f'stdout:\n{out}\nstderr:\n{err}')
print()
returncode, out, err = execute_capture(['ls', '/foo/bar'])
print(f'stdout:\n{out}\nstderr:\n{err}')
2023-01-10 17:24:35 +01:00
def test2():
2023-01-10 22:13:56 +01:00
_init(2)
2023-01-10 15:42:33 +01:00
returncode, out, err = execute_print_capture(['ls', '-la'])
print()
returncode, out, err = execute_print_capture(['ls', '/foo/bar'])
def test3():
2023-01-10 22:13:56 +01:00
_init(3)
2023-01-10 20:18:28 +01:00
def _chunk_transfer(chunk_file: Path, eof: bool):
print(f'Transferring chunk {chunk_file} to ... (This is the default method, it has no effect)')
if eof:
print(f'The last chunk has been transferred.')
returncode = execute_print_transfer_chunks(['ls', '-la'], Path('test/3-1'), _chunk_transfer, ())
2023-01-10 20:18:28 +01:00
print("TEST 3-2")
returncode = execute_print_transfer_chunks(['ls', '/foo/bar'], Path('test/3-2'), _chunk_transfer, ())
2023-01-10 20:18:28 +01:00
print("TEST 3-3")
2023-01-10 22:13:56 +01:00
returncode = execute_print_transfer_chunks(['cat', 'transfer_inform.py'], Path('test/3-3'),
_chunk_transfer, (),
chunk_size=1024)
def test4():
2023-01-10 22:13:56 +01:00
_init(4)
2023-01-10 20:18:28 +01:00
2023-01-10 22:13:56 +01:00
transfer_inform(
2023-01-10 20:18:28 +01:00
['ls', 'test/4-rsync-error'], # rsync src to dst
['ls', 'test/4-ssh-error'], # ssh target-pc 'echo "OK" | nc -U "/path/to/unix-socket"'
Path('test/4-UNIX-socket')
)
def test5():
2023-01-10 22:13:56 +01:00
_init(5)
2023-01-10 20:18:28 +01:00
2023-01-10 22:13:56 +01:00
chunk_file_tmpl = Path('test/5')
source_file = Path('transfer_inform.py') # A python script file with some content to copy ;)
2023-01-10 20:18:28 +01:00
remote_target_file = Path(f'test/5-copy-of-{source_file}')
concat_script = Path('test/5-concat')
concat_script.write_text(f'#!/usr/bin/bash\n'
2023-01-10 22:13:56 +01:00
f'echo "rsync $1 ... command output"\n'
2023-01-10 20:18:28 +01:00
f'cat "$1" >> "$2"')
os.chmod(concat_script, 0o0755)
# TODO:
# When running this test:
2023-01-10 22:13:56 +01:00
# The intentionally generated error, can be fixed by touching (creating) the missing file.
2023-01-10 20:18:28 +01:00
execute_print_transfer_chunks(
command=['cat', str(source_file)],
2023-01-10 22:13:56 +01:00
chunk_file_tmpl=chunk_file_tmpl,
chunk_transfer_fun=_test5_chunk_transfer_fun,
chunk_transfer_args=(concat_script, remote_target_file),
2023-01-10 20:18:28 +01:00
chunk_size=512,
)
2023-01-10 17:24:35 +01:00
2023-01-10 15:42:33 +01:00
def _test5_chunk_transfer_fun(chunk_file: Path,
chunk_number: int,
eof: bool,
concat_script: Path,
remote_target_file: Path):
2023-01-10 22:13:56 +01:00
rsync_cmd = [str(concat_script), str(chunk_file), str(remote_target_file)]
inform_cmd = ['ls',
chunk_file.parent.joinpath(f'5.remote.EOF={eof}'),
]
transfer_inform(
rsync_cmd=rsync_cmd,
inform_cmd=inform_cmd,
user_input_file=chunk_file.parent.joinpath(f'5.SOCKET'),
)
def test67():
hostname = socket.gethostname()
if hostname == 'yodaTux':
test6() # LOCAL
elif hostname == 'danctnix':
test7() # REMOTE
else:
print(f'Unknown hostname {hostname}')
def test6():
_init(6)
source_file = Path('transfer_inform.py') # A python script file with some content to copy ;)
chunk_file_tmpl = Path('test/6-transfer_inform.py')
ssh_target = 'pine-pwdless'
target_file_tmpl = Path(f'/home/alarm/subprocess_util/test/7-transfer_inform.py')
execute_print_transfer_chunks(
command=['cat', str(source_file)],
chunk_file_tmpl=chunk_file_tmpl,
chunk_transfer_fun=_test6_chunk_transfer_fun,
chunk_transfer_args=(chunk_file_tmpl, ssh_target, target_file_tmpl),
chunk_size=512,
)
def _test6_chunk_transfer_fun(source_chunk: Path,
chunk_number: int,
eof: bool,
chunk_file_tmpl: Path,
ssh_target: str,
target_file_tmpl: Path,
):
target_chunk = target_file_tmpl.parent.joinpath(f'{target_file_tmpl.name}.{chunk_number}')
rsync_cmd = ['rsync', str(source_chunk), f'{ssh_target}:{str(target_chunk)}']
message = 'EOF' if eof else 'OK'
target_socket = target_file_tmpl.parent.joinpath(f'{target_file_tmpl.name}.SOCKET')
inform_cmd = ['ssh', ssh_target, f'echo {message} | nc -U {shlex.quote(str(target_socket))}']
transfer_inform(
rsync_cmd=rsync_cmd,
inform_cmd=inform_cmd,
user_input_file=source_chunk.parent.joinpath(f'{source_chunk.name}.SOCKET'),
)
def test7():
_init(7)
target_file_tmpl = Path(f'/home/alarm/subprocess_util/test/7-transfer_inform.py')
target_socket = target_file_tmpl.parent.joinpath(f'{target_file_tmpl.name}.SOCKET')
execute_print_receive_chunks(
['tee', str(target_file_tmpl)],
target_socket,
target_file_tmpl,
)
2023-01-10 22:13:56 +01:00
def _init(test_number: int):
print(f"TEST {test_number}")
test_dir = Path('test')
if test_dir.exists():
shutil.rmtree('test')
Path('test').mkdir(exist_ok=False)
2023-01-10 15:42:33 +01:00
if __name__ == '__main__':
test()