| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Unit test for ExecWorker""" |
| 5 |
|
| 6 |
import os |
| 7 |
import unittest |
| 8 |
|
| 9 |
from .TLib import HOSTNAME, make_temp_file, make_temp_filename, make_temp_dir |
| 10 |
|
| 11 |
from ClusterShell.Event import EventHandler |
| 12 |
from ClusterShell.Worker.Exec import ExecWorker, WorkerError |
| 13 |
from ClusterShell.Task import task_self |
| 14 |
|
| 15 |
class ExecTest(unittest.TestCase): |
| 16 |
|
| 17 |
def execw(self, **kwargs): |
| 18 |
"""helper method to spawn and run ExecWorker""" |
| 19 |
worker = ExecWorker(**kwargs) |
| 20 |
task_self().schedule(worker) |
| 21 |
task_self().run() |
| 22 |
return worker |
| 23 |
|
| 24 |
def test_no_nodes(self): |
| 25 |
"""test ExecWorker with a simple command without nodes""" |
| 26 |
self.execw(nodes=None, handler=None, command="echo ok") |
| 27 |
self.assertEqual(task_self().max_retcode(), None) |
| 28 |
|
| 29 |
def test_shell_syntax(self): |
| 30 |
"""test ExecWorker with a command using shell syntax""" |
| 31 |
cmd = "printf 1; printf 2" |
| 32 |
self.execw(nodes='localhost', handler=None, command=cmd) |
| 33 |
self.assertEqual(task_self().max_retcode(), 0) |
| 34 |
self.assertEqual(task_self().node_buffer('localhost'), b'12') |
| 35 |
|
| 36 |
def test_one_node(self): |
| 37 |
"""test ExecWorker with a simple command on localhost""" |
| 38 |
self.execw(nodes='localhost', handler=None, command="echo ok") |
| 39 |
self.assertEqual(task_self().max_retcode(), 0) |
| 40 |
self.assertEqual(task_self().node_buffer('localhost'), b'ok') |
| 41 |
|
| 42 |
def test_one_node_error(self): |
| 43 |
"""test ExecWorker with an error command on localhost""" |
| 44 |
self.execw(nodes='localhost', handler=None, command="false") |
| 45 |
self.assertEqual(task_self().max_retcode(), 1) |
| 46 |
self.assertEqual(task_self().node_buffer('localhost'), b'') |
| 47 |
|
| 48 |
@unittest.skipIf(HOSTNAME == 'localhost', "does not work with hostname set to 'localhost'") |
| 49 |
def test_timeout(self): |
| 50 |
"""test ExecWorker with a timeout""" |
| 51 |
nodes = "localhost,%s" % HOSTNAME |
| 52 |
self.execw(nodes=nodes, handler=None, command="sleep 1", timeout=0.2) |
| 53 |
self.assertEqual(task_self().max_retcode(), None) |
| 54 |
self.assertEqual(task_self().num_timeout(), 2) |
| 55 |
|
| 56 |
def test_node_placeholder(self): |
| 57 |
"""test ExecWorker with several nodes and %h (host)""" |
| 58 |
nodes = "localhost,%s" % HOSTNAME |
| 59 |
self.execw(nodes=nodes, handler=None, command="echo %h") |
| 60 |
self.assertEqual(task_self().max_retcode(), 0) |
| 61 |
self.assertEqual(task_self().node_buffer('localhost'), b'localhost') |
| 62 |
self.assertEqual(task_self().node_buffer(HOSTNAME), HOSTNAME.encode('utf-8')) |
| 63 |
|
| 64 |
def test_bad_placeholder(self): |
| 65 |
"""test ExecWorker with unknown placeholder pattern""" |
| 66 |
self.assertRaises(WorkerError, self.execw, |
| 67 |
nodes="localhost", handler=None, command="echo %x") |
| 68 |
self.assertRaises(WorkerError, self.execw, |
| 69 |
nodes="localhost", handler=None, command="echo %") |
| 70 |
|
| 71 |
@unittest.skipIf(HOSTNAME == 'localhost', "does not work with hostname set to 'localhost'") |
| 72 |
def test_rank_placeholder(self): |
| 73 |
"""test ExecWorker with several nodes and %n (rank)""" |
| 74 |
nodes = "localhost,%s" % HOSTNAME |
| 75 |
self.execw(nodes=nodes, handler=None, command="echo %n") |
| 76 |
self.assertEqual(task_self().max_retcode(), 0) |
| 77 |
self.assertEqual(set(bytes(msg) for msg, _ in task_self().iter_buffers()), |
| 78 |
set([b'0', b'1'])) |
| 79 |
|
| 80 |
def test_copy(self): |
| 81 |
"""test copying with an ExecWorker and host placeholder""" |
| 82 |
src = make_temp_file(b"data") |
| 83 |
dstdir = make_temp_dir() |
| 84 |
dstpath = os.path.join(dstdir.name, os.path.basename(src.name)) |
| 85 |
try: |
| 86 |
pattern = dstpath + ".%h" |
| 87 |
self.execw(nodes='localhost', handler=None, source=src.name, |
| 88 |
dest=pattern) |
| 89 |
self.assertEqual(task_self().max_retcode(), 0) |
| 90 |
self.assertTrue(os.path.isfile(dstpath + '.localhost')) |
| 91 |
finally: |
| 92 |
os.unlink(dstpath + '.localhost') |
| 93 |
dstdir.cleanup() |
| 94 |
|
| 95 |
def test_copy_preserve(self): |
| 96 |
"""test copying with an ExecWorker (preserve=True)""" |
| 97 |
src = make_temp_file(b"data") |
| 98 |
past_time = 443757600 |
| 99 |
os.utime(src.name, (past_time, past_time)) |
| 100 |
dstpath = make_temp_filename() |
| 101 |
try: |
| 102 |
self.execw(nodes='localhost', handler=None, source=src.name, |
| 103 |
dest=dstpath, preserve=True) |
| 104 |
self.assertEqual(task_self().max_retcode(), 0) |
| 105 |
self.assertTrue(os.stat(dstpath).st_mtime, past_time) |
| 106 |
finally: |
| 107 |
os.unlink(dstpath) |
| 108 |
|
| 109 |
def test_copy_directory(self): |
| 110 |
"""test copying directory with an ExecWorker""" |
| 111 |
srcdir = make_temp_dir() |
| 112 |
dstdir = make_temp_dir() |
| 113 |
ref1 = make_temp_file(b"data1", dir=srcdir.name) |
| 114 |
pathdstsrcdir = os.path.join(dstdir.name, os.path.basename(srcdir.name)) |
| 115 |
pathdst1 = os.path.join(pathdstsrcdir, os.path.basename(ref1.name)) |
| 116 |
try: |
| 117 |
self.execw(nodes='localhost', handler=None, source=srcdir.name, |
| 118 |
dest=dstdir.name) |
| 119 |
self.assertEqual(task_self().max_retcode(), 0) |
| 120 |
self.assertTrue(os.path.isdir(pathdstsrcdir)) |
| 121 |
self.assertTrue(os.path.isfile(pathdst1)) |
| 122 |
with open(pathdst1) as dst1: |
| 123 |
self.assertEqual(dst1.readlines()[0], "data1") |
| 124 |
finally: |
| 125 |
os.unlink(pathdst1) |
| 126 |
os.rmdir(pathdstsrcdir) |
| 127 |
ref1.close() |
| 128 |
dstdir.cleanup() |
| 129 |
srcdir.cleanup() |
| 130 |
|
| 131 |
def test_copy_wrong_directory(self): |
| 132 |
"""test copying wrong directory with an ExecWorker""" |
| 133 |
srcdir = make_temp_dir() |
| 134 |
dst = make_temp_file(b"data") |
| 135 |
ref1 = make_temp_file(b"data1", dir=srcdir.name) |
| 136 |
try: |
| 137 |
self.execw(nodes='localhost', handler=None, source=srcdir.name, |
| 138 |
dest=dst.name, stderr=True) |
| 139 |
self.assertEqual(task_self().max_retcode(), 1) |
| 140 |
self.assertTrue(len(task_self().node_error("localhost")) > 0) |
| 141 |
self.assertTrue(os.path.isfile(ref1.name)) |
| 142 |
finally: |
| 143 |
ref1.close() |
| 144 |
srcdir.cleanup() |
| 145 |
|
| 146 |
def test_rcopy_wrong_directory(self): |
| 147 |
"""test ExecWorker reverse copying with wrong directory""" |
| 148 |
with make_temp_dir() as dstbasedirname: |
| 149 |
dstdir = os.path.join(dstbasedirname, "wrong") |
| 150 |
src = make_temp_file(b"data") |
| 151 |
self.assertRaises(ValueError, self.execw, nodes='localhost', |
| 152 |
handler=None, source=src.name, dest=dstdir, |
| 153 |
stderr=True, reverse=True) |
| 154 |
|
| 155 |
def test_abort_on_read(self): |
| 156 |
"""test ExecWorker.abort() on read""" |
| 157 |
|
| 158 |
class TestH(EventHandler): |
| 159 |
def ev_read(self, worker, node, sname, msg): |
| 160 |
worker.abort() |
| 161 |
worker.abort() |
| 162 |
|
| 163 |
self.execw(nodes='localhost', handler=TestH(), |
| 164 |
command="echo ok; tail -f /dev/null") |
| 165 |
self.assertEqual(task_self().max_retcode(), None) |
| 166 |
self.assertEqual(task_self().node_buffer('localhost'), b'ok') |
| 167 |
|
| 168 |
def test_abort_on_close(self): |
| 169 |
"""test ExecWorker.abort() on close""" |
| 170 |
|
| 171 |
class TestH(EventHandler): |
| 172 |
def ev_close(self, worker, timedout): |
| 173 |
worker.abort() |
| 174 |
worker.abort() |
| 175 |
|
| 176 |
self.execw(nodes='localhost', handler=TestH(), |
| 177 |
command="echo ok; sleep .1") |
| 178 |
self.assertEqual(task_self().max_retcode(), 0) |
| 179 |
self.assertEqual(task_self().node_buffer('localhost'), b'ok') |