Skip to content

Commit

Permalink
test_ssh: switching to unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Dec 14, 2022
1 parent 6e3b12b commit 2f60ab5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
153 changes: 76 additions & 77 deletions tests/ssh_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 2f60ab5

Please sign in to comment.