From 2f60ab5a68f97426907f9186c583ed27a395fb63 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Wed, 14 Dec 2022 11:07:15 +0100 Subject: [PATCH] test_ssh: switching to unittest --- .github/workflows/ci.yml | 2 +- tests/ssh_test.py | 153 +++++++++++++++++++-------------------- 2 files changed, 77 insertions(+), 78 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9a254bd..ad318ff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,4 +65,4 @@ jobs: - name: Run SSH tests if: runner.os == 'Linux' working-directory: tests - run: python ssh_test.py --host localhost --prefix "${PWD}/remote" + run: python ssh_test.py localhost "${PWD}/remote" diff --git a/tests/ssh_test.py b/tests/ssh_test.py index b0b8437..330c2a7 100644 --- a/tests/ssh_test.py +++ b/tests/ssh_test.py @@ -1,113 +1,112 @@ -"""ssh_test - Run SSH test. - -Usage: - ssh_test [options] --host=N --prefix=N - -Options: - -r, --host=N Host name. - -p, --prefix=M Path on host. - --version Print version. - -g, --help Print this help. +""" +Run as:: -(c - MIT) T.W.J. de Geus | tom@geus.me | www.geus.me | github.com/tdegeus/shelephant + python ssh_test.py HOST PREFIX """ import os import subprocess +import sys +import unittest +from functools import partialmethod -import docopt +from tqdm import tqdm import shelephant +tqdm.__init__ = partialmethod(tqdm.__init__, disable=True) + def run(cmd): print(cmd) return subprocess.check_output(cmd, shell=True).decode("utf-8") -args = docopt.docopt(__doc__, version=shelephant.version) +class Test_ssh(unittest.TestCase): + def test_all(self): -# shelephant_send - local checksum + operations = [ + "bar.txt -> bar.txt", + "foo.txt == foo.txt", + ] -operations = [ - "bar.txt -> bar.txt", - "foo.txt == foo.txt", -] + output = run( + ( + "shelephant_hostinfo -o myssh_send/shelephant_hostinfo.yaml --force " + '--host "{:s}" --prefix "{:s}" -f -c' + ).format(self.HOST, os.path.join(self.PREFIX, "myssh_get")) + ) -output = run( - ( - "shelephant_hostinfo -o myssh_send/shelephant_hostinfo.yaml --force " - '--host "{:s}" --prefix "{:s}" -f -c' - ).format(args["--host"], os.path.join(args["--prefix"], "myssh_get")) -) + output = run( + "shelephant_send --detail --colors none --force " + "myssh_send/shelephant_dump.yaml myssh_send/shelephant_hostinfo.yaml" + ) -output = run( - "shelephant_send --detail --colors none --force " - "myssh_send/shelephant_dump.yaml myssh_send/shelephant_hostinfo.yaml" -) + output = list(filter(None, output.split("\n"))) -output = list(filter(None, output.split("\n"))) -assert output == operations + self.assertEqual(output, operations) + operations = [ + "bar.txt == bar.txt", + "foo.txt -> foo.txt", + ] -# shelephant_send - basic + output = run( + ( + "shelephant_hostinfo -o myssh_send/shelephant_hostinfo.yaml --force " + '--host "{:s}" --prefix "{:s}" -f' + ).format(self.HOST, os.path.join(self.PREFIX, "myssh_get")) + ) -operations = [ - "bar.txt == bar.txt", - "foo.txt -> foo.txt", -] + output = run( + "shelephant_send --detail --colors none --force " + "myssh_send/shelephant_dump.yaml myssh_send/shelephant_hostinfo.yaml" + ) -output = run( - ( - "shelephant_hostinfo -o myssh_send/shelephant_hostinfo.yaml --force " - '--host "{:s}" --prefix "{:s}" -f' - ).format(args["--host"], os.path.join(args["--prefix"], "myssh_get")) -) + output = list(filter(None, output.split("\n"))) + self.assertEqual(output, operations) -output = run( - "shelephant_send --detail --colors none --force " - "myssh_send/shelephant_dump.yaml myssh_send/shelephant_hostinfo.yaml" -) + operations = { + "<-": [], + "->": [], + "!=": [], + "==": ["bar.txt", "foo.txt"], + } -output = list(filter(None, output.split("\n"))) -assert output == operations + output = run( + "shelephant_diff -f --yaml shelephant_diff.yaml " + "myssh_send/shelephant_dump.yaml myssh_send/shelephant_hostinfo.yaml" + ) -# shelephant_diff + output = shelephant.yaml.read("shelephant_diff.yaml") -operations = { - "<-": [], - "->": [], - "!=": [], - "==": ["bar.txt", "foo.txt"], -} + for key in operations: + self.assertListEqual(output[key], operations[key]) -output = run( - "shelephant_diff -f --yaml shelephant_diff.yaml " - "myssh_send/shelephant_dump.yaml myssh_send/shelephant_hostinfo.yaml" -) + operations = [ + "bar.txt -> bar.txt", + "foo.txt == foo.txt", + ] -output = shelephant.yaml.read("shelephant_diff.yaml") + output = run( + ( + "shelephant_hostinfo -o myssh_get/shelephant_hostinfo.yaml --force " + '--host "{:s}" --prefix "{:s}" -f -c' + ).format(self.HOST, os.path.join(self.PREFIX, "myssh_send")) + ) -for key in operations: - assert output[key] == operations[key] + output = run( + "shelephant_get --detail --colors none --force myssh_get/shelephant_hostinfo.yaml" + ) -# shelephant_get - basic + output = list(filter(None, output.split("\n"))) + self.assertEqual(output, operations) -operations = [ - "bar.txt -> bar.txt", - "foo.txt == foo.txt", -] + os.remove("myssh_get/bar.txt") -output = run( - ( - "shelephant_hostinfo -o myssh_get/shelephant_hostinfo.yaml --force " - '--host "{:s}" --prefix "{:s}" -f -c' - ).format(args["--host"], os.path.join(args["--prefix"], "myssh_send")) -) -output = run("shelephant_get --detail --colors none --force myssh_get/shelephant_hostinfo.yaml") +if __name__ == "__main__": -output = list(filter(None, output.split("\n"))) -assert output == operations + Test_ssh.PREFIX = sys.argv.pop() + Test_ssh.HOST = sys.argv.pop() -os.remove("myssh_get/bar.txt") + unittest.main()