diff --git a/examples/SCPAgent/README.rst b/examples/SCPAgent/README.rst index 30d568c638..66e7f976bf 100644 --- a/examples/SCPAgent/README.rst +++ b/examples/SCPAgent/README.rst @@ -55,3 +55,15 @@ the scp transfer. local_path=local_path, direction="RECEIVING")).get(timeout=5) + +Testing +------- + +Within the agent directory there is a trigger_scp.py script. By default the trigger will run through 4 different +tests. The tests will exercise the sending and receiving for both the rpc and pubsub interfaces. The trigger will +require user interaction so run it with a shell that can receive input. + +.. code-block::shell + + (volttron) (base) osboxes@osboxes:~/repos/volttron$ python examples/SCPAgent/trigger_scp.py + diff --git a/examples/SCPAgent/trigger_scp.py b/examples/SCPAgent/trigger_scp.py index da6323e021..3782ed8b68 100644 --- a/examples/SCPAgent/trigger_scp.py +++ b/examples/SCPAgent/trigger_scp.py @@ -38,87 +38,91 @@ from pathlib import Path import os import shutil -import time +import argparse import gevent from volttron.platform.vip.agent.utils import build_agent -agent = build_agent(identity='trigger') - -local_root = "/home/osboxes/Desktop/local_files" -remote_root = "/home/osboxes/Desktop/remote_files" - - -def build_remote_filename(filename): - os.makedirs(remote_root, exist_ok=True) - return str(Path(remote_root).joinpath(filename)) - - -def build_local_filename(filename): - os.makedirs(local_root, exist_ok=True) - return str(Path(local_root).joinpath(filename)) - - -def create_remote_file(filename, content): - full_path = build_remote_filename(filename) - with open(full_path, 'w') as fp: - fp.write(content) - return full_path - - -def create_local_file(filename, content): - full_path = build_local_filename(filename) - with open(full_path, 'w') as fp: - fp.write(content) - return full_path - - -def remove_files(): - shutil.rmtree(remote_root, ignore_errors=True) - shutil.rmtree(local_root, ignore_errors=True) - - -remove_files() - -remote_path = create_remote_file("t1.txt", "this is f1") -local_path = build_local_filename("t1.after.transfer.txt") - -go = input(f"Test 1: rpc: trigger_download\n\tdownload remote_path: {remote_path}\n\tto local_path: {local_path} ") -result = agent.vip.rpc.call("scp.agent", "trigger_download", - remote_path=remote_path, - local_path=local_path).get() -print(f"The result was {result}\n") - -print(f"Creating test2 file") -remote_path = build_remote_filename("t2.remote.transfer.txt") -local_path = create_local_file("t2.txt", "This is test 2") -go = input(f"Test 2: rpc: trigger_upload\n\tupload local_path: {local_path}\n\tto remote_path: {remote_path} ") -result = agent.vip.rpc.call("scp.agent", "trigger_upload", - remote_path=remote_path, - local_path=local_path).get() -print(f"The result was {result}\n") - -print(f"Creating test3 file") -remote_path = build_remote_filename("t3.sent.pubsub.txt") -local_path = create_local_file("t3.txt", "This is test 3") - -go = input(f"Test 3: pubsub: SENDING\n\tlocal_path: {local_path}\n\tto remote_path: {remote_path} ") - -agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path, - local_path=local_path, - direction="SENDING")).get() -gevent.sleep(1) -print(f"The result is {Path(remote_path).exists()}\n") -print(f"Creating test4 file") -remote_path = create_remote_file("t4.receive.pubsub.txt", "This is test 4") -local_path = build_local_filename("t4.receive.txt") - -go = input(f"Test 4: pubsub: RECEIVING\n\tlocal_path: {local_path}\n\tfrom remote_path: {remote_path} ") -agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path, - local_path=local_path, - direction="RECEIVING")).get() -gevent.sleep(1) -print(f"The result is {Path(local_path).exists()}\n") -agent.core.stop() -print("Complete") +def run_tests(local_root="~/local_files", remote_root="~/remote_files"): + agent = build_agent(identity='trigger') + + local_root = Path(local_root).expanduser() + remote_root = Path(remote_root).expanduser() + + def build_remote_filename(filename): + os.makedirs(remote_root, exist_ok=True) + return str(Path(remote_root).joinpath(filename)) + + def build_local_filename(filename): + os.makedirs(local_root, exist_ok=True) + return str(Path(local_root).joinpath(filename)) + + def create_remote_file(filename, content): + full_path = build_remote_filename(filename) + with open(full_path, 'w') as fp: + fp.write(content) + return full_path + + def create_local_file(filename, content): + full_path = build_local_filename(filename) + with open(full_path, 'w') as fp: + fp.write(content) + return full_path + + def remove_files(): + shutil.rmtree(remote_root, ignore_errors=True) + shutil.rmtree(local_root, ignore_errors=True) + + remove_files() + + remote_path = create_remote_file("t1.txt", "this is f1") + local_path = build_local_filename("t1.after.transfer.txt") + + go = input(f"Test 1: rpc: trigger_download\n\tdownload remote_path: {remote_path}\n\tto local_path: {local_path} ") + result = agent.vip.rpc.call("scp.agent", "trigger_download", + remote_path=remote_path, + local_path=local_path).get() + print(f"The result was {result}\n") + + print(f"Creating test2 file") + remote_path = build_remote_filename("t2.remote.transfer.txt") + local_path = create_local_file("t2.txt", "This is test 2") + go = input(f"Test 2: rpc: trigger_upload\n\tupload local_path: {local_path}\n\tto remote_path: {remote_path} ") + result = agent.vip.rpc.call("scp.agent", "trigger_upload", + remote_path=remote_path, + local_path=local_path).get() + print(f"The result was {result}\n") + + print(f"Creating test3 file") + remote_path = build_remote_filename("t3.sent.pubsub.txt") + local_path = create_local_file("t3.txt", "This is test 3") + + go = input(f"Test 3: pubsub: SENDING\n\tlocal_path: {local_path}\n\tto remote_path: {remote_path} ") + + agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path, + local_path=local_path, + direction="SENDING")).get() + gevent.sleep(1) + print(f"The result is {Path(remote_path).exists()}\n") + print(f"Creating test4 file") + remote_path = create_remote_file("t4.receive.pubsub.txt", "This is test 4") + local_path = build_local_filename("t4.receive.txt") + + go = input(f"Test 4: pubsub: RECEIVING\n\tlocal_path: {local_path}\n\tfrom remote_path: {remote_path} ") + agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path, + local_path=local_path, + direction="RECEIVING")).get() + gevent.sleep(1) + print(f"The result is {Path(local_path).exists()}\n") + agent.core.stop() + print("Complete") + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("-l", "--local_root", help="Local path", default="~/local_root") + parser.add_argument("-r", "--remote_root", help="Remote path", default="~/remote_root") + + args = parser.parse_args() + run_tests(args.local_root, args.remote_root)