more unit tests and fixes
This commit is contained in:
@ -4,6 +4,7 @@ import unittest
|
|||||||
from zfs_autobackup import *
|
from zfs_autobackup import *
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
print("THIS TEST REQUIRES SSH TO LOCALHOST")
|
print("THIS TEST REQUIRES SSH TO LOCALHOST")
|
||||||
|
|
||||||
@ -15,8 +16,8 @@ class TestExecuteNode(unittest.TestCase):
|
|||||||
|
|
||||||
def basics(self, node ):
|
def basics(self, node ):
|
||||||
|
|
||||||
#single line with spaces
|
#single line
|
||||||
self.assertEqual(node.run(["echo","test test"]), ["test test"])
|
self.assertEqual(node.run(["echo","test"]), ["test"])
|
||||||
|
|
||||||
#error exit code
|
#error exit code
|
||||||
with self.assertRaises(subprocess.CalledProcessError):
|
with self.assertRaises(subprocess.CalledProcessError):
|
||||||
@ -24,18 +25,26 @@ class TestExecuteNode(unittest.TestCase):
|
|||||||
|
|
||||||
#multiline without tabsplit
|
#multiline without tabsplit
|
||||||
self.assertEqual(node.run(["echo","l1c1\tl1c2\nl2c1\tl2c2"], tab_split=False), ["l1c1\tl1c2", "l2c1\tl2c2"])
|
self.assertEqual(node.run(["echo","l1c1\tl1c2\nl2c1\tl2c2"], tab_split=False), ["l1c1\tl1c2", "l2c1\tl2c2"])
|
||||||
# self.assertEqual(node.run(["echo","l1\nl2"]), ["l1", "l2"])
|
|
||||||
|
|
||||||
#multiline tabsplit
|
#multiline tabsplit
|
||||||
self.assertEqual(node.run(["echo","l1c1\tl1c2\nl2c1\tl2c2"], tab_split=True), [['l1c1', 'l1c2'], ['l2c1', 'l2c2']])
|
self.assertEqual(node.run(["echo","l1c1\tl1c2\nl2c1\tl2c2"], tab_split=True), [['l1c1', 'l1c2'], ['l2c1', 'l2c2']])
|
||||||
|
|
||||||
|
#escaping test (shouldnt be a problem locally, single quotes can be a problem remote via ssh)
|
||||||
#escaping (shouldnt be a problem locally, single quotes can be a problem remote via ssh)
|
s="><`'\"@&$()$bla\\/.*!#test _+-={}[]|"
|
||||||
s="><`'\"@&$()$bla\\//.*!#test"
|
|
||||||
self.assertEqual(node.run(["echo",s]), [s])
|
self.assertEqual(node.run(["echo",s]), [s])
|
||||||
|
|
||||||
#return std err
|
#return std err as well, trigger stderr by listing something non existing
|
||||||
# self.assertEqual(node.run(["echo","test test"], return_stderr=True), ["test test"])
|
(stdout, stderr)=node.run(["ls", "nonexistingfile"], return_stderr=True, valid_exitcodes=[2])
|
||||||
|
self.assertEqual(stdout,[])
|
||||||
|
self.assertRegex(stderr[0],"nonexistingfile")
|
||||||
|
|
||||||
|
#slow command, make sure things dont exit too early
|
||||||
|
start_time=time.time()
|
||||||
|
node.run(["sleep","1"])
|
||||||
|
self.assertGreaterEqual(time.time()-start_time,1)
|
||||||
|
|
||||||
|
#input a string and check it via cat
|
||||||
|
self.assertEqual(node.run(["cat"], input="test"), ["test"])
|
||||||
|
|
||||||
|
|
||||||
def test_basicslocal(self):
|
def test_basicslocal(self):
|
||||||
@ -47,17 +56,6 @@ class TestExecuteNode(unittest.TestCase):
|
|||||||
self.basics(node)
|
self.basics(node)
|
||||||
|
|
||||||
|
|
||||||
# def test_remoteecho(self):
|
|
||||||
# node=ExecuteNode(ssh_to="localhost", debug_output=True)
|
|
||||||
# self.assertEqual(node.run(["echo","test"]), ["test"])
|
|
||||||
|
|
||||||
# def test_exitlocal(self):
|
|
||||||
# node=ExecuteNode(debug_output=True)
|
|
||||||
|
|
||||||
# def test_exitremote(self):
|
|
||||||
# node=ExecuteNode(ssh_to="localhost", debug_output=True)
|
|
||||||
# with self.assertRaises(subprocess.CalledProcessError):
|
|
||||||
# self.remote1.run(["false"])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -378,7 +378,10 @@ class ExecuteNode:
|
|||||||
|
|
||||||
#Note: make streaming?
|
#Note: make streaming?
|
||||||
if isinstance(input,str) or type(input)=='unicode':
|
if isinstance(input,str) or type(input)=='unicode':
|
||||||
p.stdin.write(input)
|
p.stdin.write(input.encode('utf-8'))
|
||||||
|
|
||||||
|
if p.stdin:
|
||||||
|
p.stdin.close()
|
||||||
|
|
||||||
#return pipe
|
#return pipe
|
||||||
if pipe:
|
if pipe:
|
||||||
|
|||||||
Reference in New Issue
Block a user