forked from cea-hpc/clustershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeWorkerTest.py
448 lines (374 loc) · 17 KB
/
TreeWorkerTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""
Unit test for ClusterShell.Worker.TreeWorker
This unit test requires working ssh connections to the following
local addresses: $HOSTNAME, localhost, 127.0.0.[2-4]
You can use the following options in ~/.ssh/config:
Host your_hostname localhost 127.0.0.*
StrictHostKeyChecking no
LogLevel ERROR
"""
import os
from os.path import basename, join
import unittest
import warnings
from ClusterShell.NodeSet import NodeSet
from ClusterShell.Task import task_self, task_terminate, task_wait
from ClusterShell.Task import Task, task_cleanup
from ClusterShell.Topology import TopologyGraph
from ClusterShell.Worker.Tree import TreeWorker, WorkerTree
from TLib import HOSTNAME, make_temp_dir, make_temp_file, make_temp_filename
NODE_HEAD = HOSTNAME
NODE_GATEWAY = 'localhost'
NODE_DISTANT = '127.0.0.2'
NODE_DIRECT = '127.0.0.3'
NODE_FOREIGN = '127.0.0.4'
class TEventHandlerBase(object):
"""Base Test class for EventHandler"""
def __init__(self):
self.ev_start_cnt = 0
self.ev_pickup_cnt = 0
self.ev_read_cnt = 0
self.ev_written_cnt = 0
self.ev_written_sz = 0
self.ev_hup_cnt = 0
self.ev_close_cnt = 0
self.ev_timedout_cnt = 0
self.last_read = None
class TEventHandlerLegacy(TEventHandlerBase):
"""Test Legacy Event Handler (< 1.8)"""
def ev_start(self, worker):
self.ev_start_cnt += 1
def ev_pickup(self, worker):
self.ev_pickup_cnt += 1
def ev_read(self, worker):
self.ev_read_cnt += 1
self.last_read = worker.current_msg
def ev_written(self, worker, node, sname, size):
self.ev_written_cnt += 1
self.ev_written_sz += size
def ev_hup(self, worker):
self.ev_hup_cnt += 1
def ev_timeout(self, worker):
self.ev_timedout_cnt += 1
def ev_close(self, worker):
self.ev_close_cnt += 1
class TEventHandler(TEventHandlerBase):
"""Test Event Handler (1.8+)"""
def ev_start(self, worker):
self.ev_start_cnt += 1
def ev_pickup(self, worker, node):
self.ev_pickup_cnt += 1
def ev_read(self, worker, node, sname, msg):
self.ev_read_cnt += 1
self.last_read = msg
def ev_written(self, worker, node, sname, size):
self.ev_written_cnt += 1
self.ev_written_sz += size
def ev_hup(self, worker, node, rc):
self.ev_hup_cnt += 1
def ev_close(self, worker, timedout):
self.ev_close_cnt += 1
if timedout:
self.ev_timedout_cnt += 1
@unittest.skipIf(HOSTNAME == 'localhost', "does not work with hostname set to 'localhost'")
class TreeWorkerTest(unittest.TestCase):
"""
TreeWorkerTest: test TreeWorker
NODE_HEAD -> NODE_GATEWAY -> NODE_DISTANT
-> NODE_DIRECT [defined in topology]
-> NODE_FOREIGN [not defined in topology]
Connections are really established to the target and command results
are tested.
"""
def setUp(self):
"""setup test environment topology"""
task_terminate() # ideally shouldn't be needed...
self.task = task_self()
# set task topology
graph = TopologyGraph()
graph.add_route(NodeSet(HOSTNAME), NodeSet(NODE_GATEWAY))
graph.add_route(NodeSet(NODE_GATEWAY), NodeSet(NODE_DISTANT))
graph.add_route(NodeSet(HOSTNAME), NodeSet(NODE_DIRECT))
# NODE_FOREIGN is not included
self.task.topology = graph.to_tree(HOSTNAME)
def tearDown(self):
"""clean up test environment"""
task_terminate()
self.task = None
def test_tree_run_event_legacy(self):
"""test simple tree run with legacy EventHandler"""
teh = TEventHandlerLegacy()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.task.run('echo Lorem Ipsum', nodes=NODE_DISTANT, handler=teh)
self.assertEqual(len(w), 4)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
self.assertEqual(teh.last_read, b'Lorem Ipsum')
def test_tree_run_event_legacy_timeout(self):
"""test simple tree run with legacy EventHandler with timeout"""
teh = TEventHandlerLegacy()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.task.run('sleep 10', nodes=NODE_DISTANT, handler=teh, timeout=0.5)
self.assertEqual(len(w), 2)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 0) # nothing to read
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 0) # no hup event if timed out
self.assertEqual(teh.ev_timedout_cnt, 1) # command timed out
self.assertEqual(teh.ev_close_cnt, 1)
def test_tree_run_event(self):
"""test simple tree run with EventHandler (1.8+)"""
teh = TEventHandler()
self.task.run('echo Lorem Ipsum', nodes=NODE_DISTANT, handler=teh)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
self.assertEqual(teh.last_read, b'Lorem Ipsum')
def test_tree_run_event_timeout(self):
"""test simple tree run with EventHandler (1.8+) with timeout"""
teh = TEventHandler()
self.task.run('sleep 10', nodes=NODE_DISTANT, handler=teh, timeout=0.5)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 0) # nothing to read
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 0) # no hup event if timed out
self.assertEqual(teh.ev_timedout_cnt, 1) # command timed out
self.assertEqual(teh.ev_close_cnt, 1)
def test_tree_run_noremote(self):
"""test tree run with remote=False"""
teh = TEventHandler()
self.task.run('echo %h', nodes=NODE_DISTANT, handler=teh, remote=False)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
self.assertEqual(teh.last_read, NODE_DISTANT.encode('ascii'))
def test_tree_run_noremote_alt_localworker(self):
"""test tree run with remote=False and a non-exec localworker"""
teh = TEventHandler()
self.task.set_info('tree_default:local_workername', 'ssh')
self.task.run('echo %h', nodes=NODE_DISTANT, handler=teh, remote=False)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
# The exec worker will expand %h to the host, but ssh will just echo '%h'
self.assertEqual(teh.last_read, '%h'.encode('ascii'))
del self.task._info['tree_default:local_workername']
def test_tree_run_direct(self):
"""test tree run with direct target, in topology"""
teh = TEventHandler()
self.task.run('echo Lorem Ipsum', nodes=NODE_DIRECT, handler=teh)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
self.assertEqual(teh.last_read, b'Lorem Ipsum')
def test_tree_run_foreign(self):
"""test tree run with direct target, not in topology"""
teh = TEventHandler()
self.task.run('echo Lorem Ipsum', nodes=NODE_FOREIGN, handler=teh)
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 0)
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
self.assertEqual(teh.last_read, b'Lorem Ipsum')
def _tree_run_write(self, target, separate_thread=False):
if separate_thread:
task = Task()
else:
task = self.task
teh = TEventHandler()
worker = task.shell('cat', nodes=target, handler=teh)
worker.write(b'Lorem Ipsum')
worker.set_write_eof()
task.run()
if separate_thread:
task_wait()
task_cleanup()
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 1)
self.assertEqual(teh.ev_written_cnt, 1)
self.assertEqual(teh.ev_written_sz, len('Lorem Ipsum'))
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
self.assertEqual(teh.last_read, b'Lorem Ipsum')
def test_tree_run_write_distant(self):
"""test tree run with write(), distant target"""
self._tree_run_write(NODE_DISTANT)
def test_tree_run_write_direct(self):
"""test tree run with write(), direct target, in topology"""
self._tree_run_write(NODE_DIRECT)
def test_tree_run_write_foreign(self):
"""test tree run with write(), direct target, not in topology"""
self._tree_run_write(NODE_FOREIGN)
def test_tree_run_write_gateway(self):
"""test tree run with write(), gateway is target, not in topology"""
self._tree_run_write(NODE_GATEWAY)
def test_tree_run_write_distant_mt(self):
"""test tree run with write(), distant target, separate thread"""
self._tree_run_write(NODE_DISTANT, separate_thread=True)
def test_tree_run_write_direct_mt(self):
"""test tree run with write(), direct target, in topology, separate thread"""
self._tree_run_write(NODE_DIRECT, separate_thread=True)
def test_tree_run_write_foreign_mt(self):
"""test tree run with write(), direct target, not in topology, separate thread"""
self._tree_run_write(NODE_FOREIGN, separate_thread=True)
def test_tree_run_write_gateway_mt(self):
"""test tree run with write(), gateway is target, not in topology, separate thread"""
self._tree_run_write(NODE_GATEWAY, separate_thread=True)
def _tree_copy_file(self, target):
teh = TEventHandler()
srcf = make_temp_file(b'Lorem Ipsum', 'test_tree_copy_file_src')
dest = make_temp_filename('test_tree_copy_file_dest')
try:
worker = self.task.copy(srcf.name, dest, nodes=target, handler=teh)
self.task.run()
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 0)
#self.assertEqual(teh.ev_written_cnt, 0) # FIXME
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
with open(dest, 'r') as destf:
self.assertEqual(destf.read(), 'Lorem Ipsum')
finally:
os.remove(dest)
def test_tree_copy_file_distant(self):
"""test tree copy: file, distant target"""
self._tree_copy_file(NODE_DISTANT)
def test_tree_copy_file_direct(self):
"""test tree copy: file, direct target, in topology"""
self._tree_copy_file(NODE_DIRECT)
def test_tree_copy_file_foreign(self):
"""test tree copy: file, direct target, not in topology"""
self._tree_copy_file(NODE_FOREIGN)
def test_tree_copy_file_gateway(self):
"""test tree copy: file, gateway is target"""
self._tree_copy_file(NODE_GATEWAY)
def _tree_copy_dir(self, target):
teh = TEventHandler()
srcdir = make_temp_dir()
destdir = make_temp_dir()
file1 = make_temp_file(b'Lorem Ipsum Unum', suffix=".txt",
dir=srcdir.name)
file2 = make_temp_file(b'Lorem Ipsum Duo', suffix=".txt",
dir=srcdir.name)
try:
# add '/' to dest so that distant does like the others
worker = self.task.copy(srcdir.name, destdir.name + '/',
nodes=target, handler=teh)
self.task.run()
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 0)
#self.assertEqual(teh.ev_written_cnt, 0) # FIXME
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
# copy successful?
copy_dest = join(destdir.name, srcdir.name)
with open(join(copy_dest, basename(file1.name)), 'rb') as rfile1:
self.assertEqual(rfile1.read(), b'Lorem Ipsum Unum')
with open(join(copy_dest, basename(file2.name)), 'rb') as rfile2:
self.assertEqual(rfile2.read(), b'Lorem Ipsum Duo')
finally:
file1.close()
file2.close()
srcdir.cleanup()
destdir.cleanup()
def test_tree_copy_dir_distant(self):
"""test tree copy: directory, distant target"""
self._tree_copy_dir(NODE_DISTANT)
def test_tree_copy_dir_direct(self):
"""test tree copy: directory, direct target, in topology"""
self._tree_copy_dir(NODE_DIRECT)
def test_tree_copy_dir_foreign(self):
"""test tree copy: directory, direct target, not in topology"""
self._tree_copy_dir(NODE_FOREIGN)
def test_tree_copy_dir_gateway(self):
"""test tree copy: directory, gateway is target"""
self._tree_copy_dir(NODE_GATEWAY)
def _tree_rcopy_dir(self, target, dirsuffix=None):
teh = TEventHandler()
srcdir = make_temp_dir()
destdir = make_temp_dir()
file1 = make_temp_file(b'Lorem Ipsum Unum', suffix=".txt",
dir=srcdir.name)
file2 = make_temp_file(b'Lorem Ipsum Duo', suffix=".txt",
dir=srcdir.name)
try:
worker = self.task.rcopy(srcdir.name, destdir.name, nodes=target,
handler=teh)
self.task.run()
self.assertEqual(teh.ev_start_cnt, 1)
self.assertEqual(teh.ev_pickup_cnt, 1)
self.assertEqual(teh.ev_read_cnt, 0)
#self.assertEqual(teh.ev_written_cnt, 0) # FIXME
self.assertEqual(teh.ev_hup_cnt, 1)
self.assertEqual(teh.ev_timedout_cnt, 0)
self.assertEqual(teh.ev_close_cnt, 1)
# rcopy successful?
if not dirsuffix:
dirsuffix = target
rcopy_dest = join(destdir.name,
basename(srcdir.name) + '.' + dirsuffix)
with open(join(rcopy_dest, basename(file1.name)), 'rb') as rfile1:
self.assertEqual(rfile1.read(), b'Lorem Ipsum Unum')
with open(join(rcopy_dest, basename(file2.name)), 'rb') as rfile2:
self.assertEqual(rfile2.read(), b'Lorem Ipsum Duo')
finally:
file1.close()
file2.close()
srcdir.cleanup()
destdir.cleanup()
def test_tree_rcopy_dir_distant(self):
"""test tree rcopy: directory, distant target"""
# In distant tree mode, the returned result will include the
# hostname of the distant host, not target name
self._tree_rcopy_dir(NODE_DISTANT, dirsuffix=HOSTNAME)
def test_tree_rcopy_dir_direct(self):
"""test tree rcopy: directory, direct target, in topology"""
self._tree_rcopy_dir(NODE_DIRECT)
def test_tree_rcopy_dir_foreign(self):
"""test tree rcopy: directory, direct target, not in topology"""
self._tree_rcopy_dir(NODE_FOREIGN)
def test_tree_rcopy_dir_gateway(self):
"""test tree rcopy: directory, gateway is target"""
self._tree_rcopy_dir(NODE_GATEWAY)
def test_tree_worker_missing_arguments(self):
"""test TreeWorker with missing arguments"""
teh = TEventHandler()
# no command nor source
self.assertRaises(ValueError, TreeWorker, NODE_DISTANT, teh, 10)
def test_tree_worker_name_compat(self):
"""test TreeWorker former name (WorkerTree)"""
self.assertEqual(TreeWorker, WorkerTree)