run() now uses CmdPipe for better pipe handling and cleaner code

This commit is contained in:
Edwin Eefting
2021-04-12 18:16:42 +02:00
parent ed7cd41ad7
commit 4b97f789df
7 changed files with 125 additions and 230 deletions

View File

@ -10,11 +10,12 @@ class TestCmdPipe(unittest2.TestCase):
err=[]
out=[]
p.add(["ls", "-d", "/", "/", "/nonexistent"], stderr_handler=lambda line: err.append(line))
exits=p.execute(stdout_handler=lambda line: out.append(line))
executed=p.execute(stdout_handler=lambda line: out.append(line))
self.assertEqual(err, ["ls: cannot access '/nonexistent': No such file or directory"])
self.assertEqual(out, ["/","/"])
self.assertEqual(exits, [2])
self.assertTrue(executed)
self.assertEqual(p.items[0]['process'].returncode,2)
def test_input(self):
"""test stdinput"""
@ -22,11 +23,12 @@ class TestCmdPipe(unittest2.TestCase):
err=[]
out=[]
p.add(["echo", "test"], stderr_handler=lambda line: err.append(line))
exits=p.execute(stdout_handler=lambda line: out.append(line))
executed=p.execute(stdout_handler=lambda line: out.append(line))
self.assertEqual(err, [])
self.assertEqual(out, ["test"])
self.assertEqual(exits, [0])
self.assertTrue(executed)
self.assertEqual(p.items[0]['process'].returncode,0)
def test_pipe(self):
"""test piped"""
@ -38,13 +40,16 @@ class TestCmdPipe(unittest2.TestCase):
p.add(["echo", "test"], stderr_handler=lambda line: err1.append(line))
p.add(["tr", "e", "E"], stderr_handler=lambda line: err2.append(line))
p.add(["tr", "t", "T"], stderr_handler=lambda line: err3.append(line))
exits=p.execute(stdout_handler=lambda line: out.append(line))
executed=p.execute(stdout_handler=lambda line: out.append(line))
self.assertEqual(err1, [])
self.assertEqual(err2, [])
self.assertEqual(err3, [])
self.assertEqual(out, ["TEsT"])
self.assertEqual(exits, [0,0,0])
self.assertTrue(executed)
self.assertEqual(p.items[0]['process'].returncode,0)
self.assertEqual(p.items[1]['process'].returncode,0)
self.assertEqual(p.items[2]['process'].returncode,0)
#test str representation as well
self.assertEqual(str(p), "(echo test) | (tr e E) | (tr t T)")
@ -59,13 +64,16 @@ class TestCmdPipe(unittest2.TestCase):
p.add(["ls", "/nonexistent1"], stderr_handler=lambda line: err1.append(line))
p.add(["ls", "/nonexistent2"], stderr_handler=lambda line: err2.append(line))
p.add(["ls", "/nonexistent3"], stderr_handler=lambda line: err3.append(line))
exits=p.execute(stdout_handler=lambda line: out.append(line))
executed=p.execute(stdout_handler=lambda line: out.append(line))
self.assertEqual(err1, ["ls: cannot access '/nonexistent1': No such file or directory"])
self.assertEqual(err2, ["ls: cannot access '/nonexistent2': No such file or directory"])
self.assertEqual(err3, ["ls: cannot access '/nonexistent3': No such file or directory"])
self.assertEqual(out, [])
self.assertEqual(exits, [2,2,2])
self.assertTrue(executed)
self.assertEqual(p.items[0]['process'].returncode,2)
self.assertEqual(p.items[1]['process'].returncode,2)
self.assertEqual(p.items[2]['process'].returncode,2)
def test_readonly_execute(self):
"""everything readonly, just should execute"""
@ -76,12 +84,14 @@ class TestCmdPipe(unittest2.TestCase):
out=[]
p.add(["echo", "test1"], stderr_handler=lambda line: err1.append(line), readonly=True)
p.add(["echo", "test2"], stderr_handler=lambda line: err2.append(line), readonly=True)
exits=p.execute(stdout_handler=lambda line: out.append(line))
executed=p.execute(stdout_handler=lambda line: out.append(line))
self.assertEqual(err1, [])
self.assertEqual(err2, [])
self.assertEqual(out, ["test2"])
self.assertEqual(exits, [0,0])
self.assertTrue(executed)
self.assertEqual(p.items[0]['process'].returncode,0)
self.assertEqual(p.items[1]['process'].returncode,0)
def test_readonly_skip(self):
"""one command not readonly, skip"""
@ -92,10 +102,10 @@ class TestCmdPipe(unittest2.TestCase):
out=[]
p.add(["echo", "test1"], stderr_handler=lambda line: err1.append(line), readonly=False)
p.add(["echo", "test2"], stderr_handler=lambda line: err2.append(line), readonly=True)
exits=p.execute(stdout_handler=lambda line: out.append(line))
executed=p.execute(stdout_handler=lambda line: out.append(line))
self.assertEqual(err1, [])
self.assertEqual(err2, [])
self.assertEqual(out, [])
self.assertEqual(exits, None)
self.assertFalse(executed)

View File

@ -64,7 +64,7 @@ class TestExecuteNode(unittest2.TestCase):
def test_readonly(self):
node=ExecuteNode(debug_output=True, readonly=True)
self.assertEqual(node.run(["echo","test"], readonly=False), None)
self.assertEqual(node.run(["echo","test"], readonly=False), [])
self.assertEqual(node.run(["echo","test"], readonly=True), ["test"])
@ -73,36 +73,36 @@ class TestExecuteNode(unittest2.TestCase):
def pipe(self, nodea, nodeb):
with self.subTest("pipe data"):
output=nodea.get_pipe(["dd", "if=/dev/zero", "count=1000"])
output=nodea.run(["dd", "if=/dev/zero", "count=1000"],pipe=True)
self.assertEqual(nodeb.run(["md5sum"], inp=output), ["816df6f64deba63b029ca19d880ee10a -"])
with self.subTest("exit code both ends of pipe ok"):
output=nodea.get_pipe(["true"])
output=nodea.run(["true"], pipe=True)
nodeb.run(["true"], inp=output)
with self.subTest("error on pipe input side"):
with self.assertRaises(subprocess.CalledProcessError):
output=nodea.get_pipe(["false"])
output=nodea.run(["false"], pipe=True)
nodeb.run(["true"], inp=output)
with self.subTest("error on pipe output side "):
with self.assertRaises(subprocess.CalledProcessError):
output=nodea.get_pipe(["true"])
output=nodea.run(["true"], pipe=True)
nodeb.run(["false"], inp=output)
with self.subTest("error on both sides of pipe"):
with self.assertRaises(subprocess.CalledProcessError):
output=nodea.get_pipe(["false"])
output=nodea.run(["false"], pipe=True)
nodeb.run(["false"], inp=output)
with self.subTest("check stderr on pipe output side"):
output=nodea.get_pipe(["true"])
output=nodea.run(["true"], pipe=True)
(stdout, stderr)=nodeb.run(["ls", "nonexistingfile"], inp=output, return_stderr=True, valid_exitcodes=[0,2])
self.assertEqual(stdout,[])
self.assertRegex(stderr[0], "nonexistingfile" )
with self.subTest("check stderr on pipe input side (should be only printed)"):
output=nodea.get_pipe(["ls", "nonexistingfile"])
output=nodea.run(["ls", "nonexistingfile"], pipe=True)
(stdout, stderr)=nodeb.run(["true"], inp=output, return_stderr=True, valid_exitcodes=[0,2])
self.assertEqual(stdout,[])
self.assertEqual(stderr,[])

View File

@ -38,7 +38,7 @@ class TestZfsScaling(unittest2.TestCase):
#this triggers if you make a change with an impact of more than O(snapshot_count/2)
expected_runs=240
expected_runs=343
print("ACTUAL RUNS: {}".format(run_counter))
self.assertLess(abs(run_counter-expected_runs), snapshot_count/2)
@ -69,6 +69,7 @@ class TestZfsScaling(unittest2.TestCase):
global run_counter
#first run
run_counter=0
with patch.object(ExecuteNode,'run', run_count) as p:
@ -77,11 +78,12 @@ class TestZfsScaling(unittest2.TestCase):
#this triggers if you make a change with an impact of more than O(snapshot_count/2)
expected_runs=640
expected_runs=743
print("ACTUAL RUNS: {}".format(run_counter))
self.assertLess(abs(run_counter-expected_runs), dataset_count/2)
#second run, should have higher number of expected_runs
run_counter=0
with patch.object(ExecuteNode,'run', run_count) as p:
@ -90,6 +92,6 @@ class TestZfsScaling(unittest2.TestCase):
#this triggers if you make a change with a performance impact of more than O(snapshot_count/2)
expected_runs=844
expected_runs=947
print("ACTUAL RUNS: {}".format(run_counter))
self.assertLess(abs(run_counter-expected_runs), dataset_count/2)