File: TaskDistantMixin.py

package info (click to toggle)
clustershell 1.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: python: 20,978; makefile: 149
file content (741 lines) | stat: -rw-r--r-- 29,752 bytes parent folder | download
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# ClusterShell (distant) test suite
# Written by S. Thiell

"""Unit test for ClusterShell Task (distant)"""

import pwd
import unittest
import warnings

from TLib import HOSTNAME, make_temp_filename, make_temp_dir
from ClusterShell.Event import EventHandler
from ClusterShell.Task import *
from ClusterShell.Worker.Ssh import WorkerSsh
from ClusterShell.Worker.EngineClient import *
from ClusterShell.Worker.Worker import FANOUT_UNLIMITED, WorkerBadArgumentError

import socket

# TEventHandlerChecker 'received event' flags
EV_START = 0x01
EV_PICKUP = 0x02
EV_READ = 0x04
EV_WRITTEN = 0x08
EV_HUP = 0x10
EV_TIMEOUT = 0x20
EV_CLOSE = 0x40


class TaskDistantMixin(object):

    def setUp(self):
        self._task = task_self()

    def testLocalhostCommand(self):
        # init worker
        worker = self._task.shell("/bin/hostname", nodes=HOSTNAME)
        # run task
        self._task.resume()

    def testLocalhostCommand2(self):
        # init worker
        worker = self._task.shell("/bin/hostname", nodes=HOSTNAME)

        worker = self._task.shell("/bin/uname -r", nodes=HOSTNAME)
        # run task
        self._task.resume()

    def testTaskShellWorkerGetCommand(self):
        worker1 = self._task.shell("/bin/hostname", nodes=HOSTNAME)
        worker2 = self._task.shell("/bin/uname -r", nodes=HOSTNAME)
        self._task.resume()
        self.assertTrue(hasattr(worker1, 'command'))
        self.assertTrue(hasattr(worker2, 'command'))
        self.assertEqual(worker1.command, "/bin/hostname")
        self.assertEqual(worker2.command, "/bin/uname -r")

    def testTaskShellRunDistant(self):
        wrk = task_self().run("false", nodes=HOSTNAME)
        self.assertEqual(wrk.node_retcode(HOSTNAME), 1)

    def testLocalhostCopy(self):
        dests = []
        try:
            for i in range(5):
                dest = make_temp_filename(suffix='LocalhostCopy')
                dests.append(dest)
                worker = self._task.copy("/etc/hosts", dest, nodes=HOSTNAME)
            self._task.resume()
        finally:
            for dest in dests:
                os.unlink(dest)

    def testCopyNodeFailure(self):
        # == stderr merged ==
        self._task.set_default("stderr", False)
        dest = make_temp_filename(suffix='LocalhostCopyF')
        worker = self._task.copy("/etc/hosts", dest,
                                 nodes='unlikely-node,%s' % HOSTNAME)
        self._task.resume()
        self.assertEqual(worker.node_error_buffer("unlikely-node"), None)
        self.assertTrue(len(worker.node_buffer("unlikely-node")) > 2)
        os.unlink(dest)

        # == stderr separated ==
        self._task.set_default("stderr", True)
        try:
            dest = make_temp_filename(suffix='LocalhostCopyF2')
            worker = self._task.copy("/etc/hosts", dest,
                                     nodes='unlikely-node,%s' % HOSTNAME)
            # run task
            self._task.resume()
            self.assertTrue(worker.node_buffer("unlikely-node") is None)
            self.assertTrue(len(worker.node_error_buffer("unlikely-node")) > 2)
            os.unlink(dest)
        finally:
            self._task.set_default("stderr", False)

    def testLocalhostCopyDir(self):
        dtmp_src = make_temp_dir('src')
        dtmp_dst = make_temp_dir('testLocalhostCopyDir')
        try:
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_b"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a", "lev2"))
            worker = self._task.copy(dtmp_src.name, dtmp_dst.name,
                                     nodes=HOSTNAME)
            self._task.resume()
            self.assertTrue(os.path.exists(os.path.join(dtmp_dst.name,
                            os.path.basename(dtmp_src.name), "lev1_a", "lev2")))
        finally:
            dtmp_dst.cleanup()
            dtmp_src.cleanup()

    def testLocalhostExplicitSshCopy(self):
        dest = make_temp_filename('testLocalhostExplicitSshCopy')
        srcsz = os.path.getsize("/etc/hosts")
        try:
            worker = WorkerSsh(HOSTNAME, source="/etc/hosts", dest=dest,
                               handler=None, timeout=10)
            self._task.schedule(worker)
            self._task.resume()
            self.assertEqual(srcsz, os.path.getsize(dest))
        finally:
            os.remove(dest)

    def testLocalhostExplicitSshCopyWithOptions(self):
        dest = make_temp_dir('testLocalhostExplicitSshCopyWithOptions')
        self._task.set_info("scp_path", "/usr/bin/scp -l 10")
        self._task.set_info("scp_options", "-oLogLevel=QUIET")
        try:
            worker = WorkerSsh(HOSTNAME, source="/etc/hosts", dest=dest.name,
                               handler=None)
            self._task.schedule(worker)
            self._task.resume()
            self.assertEqual(self._task.max_retcode(), 0)
            self.assertTrue(os.path.exists(os.path.join(dest.name, "hosts")))
        finally:
            os.unlink(os.path.join(dest.name, "hosts"))
            dest.cleanup()
        # clear options after test
        task_cleanup()
        self.assertEqual(task_self().info("scp_path"), None)

    def testLocalhostExplicitSshCopyDir(self):
        dtmp_src = make_temp_dir('src')
        dtmp_dst = make_temp_dir('testLocalhostExplicitSshCopyDir')
        try:
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_b"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a", "lev2"))
            worker = WorkerSsh(HOSTNAME, source=dtmp_src.name,
                               dest=dtmp_dst.name, handler=None)
            self._task.schedule(worker)
            self._task.resume()
            path = os.path.join(dtmp_dst.name, os.path.basename(dtmp_src.name),
                                "lev1_a", "lev2")
            self.assertTrue(os.path.exists(path))
        finally:
            dtmp_dst.cleanup()
            dtmp_src.cleanup()

    def testLocalhostExplicitSshCopyDirPreserve(self):
        dtmp_src = make_temp_dir('src')
        dtmp_dst = make_temp_dir('testLocalhostExplicitSshCopyDirPreserve')
        try:
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_b"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a", "lev2"))
            worker = WorkerSsh(HOSTNAME, source=dtmp_src.name,
                               dest=dtmp_dst.name, handler=None, timeout=10,
                               preserve=True)
            self._task.schedule(worker)
            self._task.resume()
            self.assertTrue(os.path.exists(os.path.join(dtmp_dst.name,
                            os.path.basename(dtmp_src.name), "lev1_a", "lev2")))
        finally:
            dtmp_dst.cleanup()
            dtmp_src.cleanup()

    def testExplicitSshWorker(self):
        # init worker
        worker = WorkerSsh(HOSTNAME, command="/bin/echo alright", handler=None)
        self._task.schedule(worker)
        # run task
        self._task.resume()
        # test output
        self.assertEqual(worker.node_buffer(HOSTNAME), b"alright")

    def testExplicitSshWorkerWithOptions(self):
        self._task.set_info("ssh_path", "/usr/bin/ssh -C")
        self._task.set_info("ssh_options", "-oLogLevel=QUIET")
        worker = WorkerSsh(HOSTNAME, command="/bin/echo alright", handler=None)
        self._task.schedule(worker)
        # run task
        self._task.resume()
        # test output
        self.assertEqual(worker.node_buffer(HOSTNAME), b"alright")
        # clear options after test
        task_cleanup()
        self.assertEqual(task_self().info("ssh_path"), None)

    def testExplicitSshWorkerStdErr(self):
        # init worker
        worker = WorkerSsh(HOSTNAME, command="/bin/echo alright 1>&2",
                           handler=None, stderr=True)
        self._task.schedule(worker)
        # run task
        self._task.resume()
        # test output
        self.assertEqual(worker.node_error_buffer(HOSTNAME), b"alright")

        # Re-test with stderr=False
        worker = WorkerSsh(HOSTNAME, command="/bin/echo alright 1>&2",
                           handler=None, stderr=False)
        self._task.schedule(worker)
        # run task
        self._task.resume()
        # test output
        self.assertEqual(worker.node_error_buffer(HOSTNAME), None)

    class TEventHandlerChecker(EventHandler):
        """simple event trigger validator"""
        def __init__(self, test):
            self.test = test
            self.flags = 0
            self.read_count = 0
            self.written_count = 0
        def ev_start(self, worker):
            self.test.assertEqual(self.flags, 0)
            self.flags |= EV_START
        def ev_pickup(self, worker, node):
            self.test.assertTrue(self.flags & EV_START)
            self.flags |= EV_PICKUP
            self.last_node = node
        def ev_read(self, worker, node, sname, msg):
            self.test.assertEqual(self.flags, EV_START | EV_PICKUP)
            self.flags |= EV_READ
            self.last_node = node
            self.last_read = msg
        def ev_written(self, worker, node, sname, size):
            self.test.assertTrue(self.flags & (EV_START | EV_PICKUP))
            self.flags |= EV_WRITTEN
        def ev_hup(self, worker, node, rc):
            self.test.assertTrue(self.flags & (EV_START | EV_PICKUP))
            self.flags |= EV_HUP
            self.last_node = node
            self.last_rc = rc
        def ev_close(self, worker, timedout):
            self.test.assertTrue(self.flags & EV_START)
            self.test.assertTrue(self.flags & EV_CLOSE == 0)
            if timedout:
                self.flags |= EV_TIMEOUT
            self.flags |= EV_CLOSE

    def testShellEvents(self):
        # init worker
        test_eh = self.__class__.TEventHandlerChecker(self)
        worker = self._task.shell("/bin/hostname", nodes=HOSTNAME, handler=test_eh)
        # run task
        self._task.resume()
        # test events received: start, read, hup, close
        self.assertEqual(test_eh.flags, EV_START | EV_PICKUP | EV_READ | EV_HUP | EV_CLOSE)

    def testShellEventsWithTimeout(self):
        # init worker
        test_eh = self.__class__.TEventHandlerChecker(self)
        worker = self._task.shell("/bin/echo alright && /bin/sleep 10",
                                  nodes=HOSTNAME, handler=test_eh, timeout=2)
        self.assertTrue(worker != None)
        # run task
        self._task.resume()
        # test events received: start, read, timeout, close
        self.assertEqual(test_eh.flags, EV_START | EV_PICKUP | EV_READ | EV_TIMEOUT | EV_CLOSE)
        self.assertEqual(worker.node_buffer(HOSTNAME), b"alright")
        self.assertEqual(worker.num_timeout(), 1)
        self.assertEqual(self._task.num_timeout(), 1)
        count = 0
        for node in self._task.iter_keys_timeout():
            count += 1
            self.assertEqual(node, HOSTNAME)
        self.assertEqual(count, 1)
        count = 0
        for node in worker.iter_keys_timeout():
            count += 1
            self.assertEqual(node, HOSTNAME)
        self.assertEqual(count, 1)

    def testShellEventsWithTimeout2(self):
        # init worker
        test_eh1 = self.__class__.TEventHandlerChecker(self)
        worker1 = self._task.shell("/bin/echo alright && /bin/sleep 10",
                                   nodes=HOSTNAME, handler=test_eh1, timeout=2)
        test_eh2 = self.__class__.TEventHandlerChecker(self)
        worker2 = self._task.shell("/bin/echo okay && /bin/sleep 10",
                                   nodes=HOSTNAME, handler=test_eh2, timeout=3)
        # run task
        self._task.resume()
        # test events received: start, read, timeout, close
        self.assertEqual(test_eh1.flags, EV_START | EV_PICKUP | EV_READ | EV_TIMEOUT | EV_CLOSE)
        self.assertEqual(test_eh2.flags, EV_START | EV_PICKUP | EV_READ | EV_TIMEOUT | EV_CLOSE)
        self.assertEqual(worker1.node_buffer(HOSTNAME), b"alright")
        self.assertEqual(worker2.node_buffer(HOSTNAME), b"okay")
        self.assertEqual(worker1.num_timeout(), 1)
        self.assertEqual(worker2.num_timeout(), 1)
        self.assertEqual(self._task.num_timeout(), 2)

    def testShellEventsReadNoEOL(self):
        # init worker
        test_eh = self.__class__.TEventHandlerChecker(self)
        worker = self._task.shell("/bin/echo -n okay", nodes=HOSTNAME, handler=test_eh)
        # run task
        self._task.resume()
        # test events received: start, close
        self.assertEqual(test_eh.flags, EV_START | EV_PICKUP | EV_READ | EV_HUP | EV_CLOSE)
        self.assertEqual(worker.node_buffer(HOSTNAME), b"okay")

    def testShellEventsNoReadNoTimeout(self):
        # init worker
        test_eh = self.__class__.TEventHandlerChecker(self)
        worker = self._task.shell("/bin/sleep 2", nodes=HOSTNAME, handler=test_eh)
        # run task
        self._task.resume()
        # test events received: start, close
        self.assertEqual(test_eh.flags, EV_START | EV_PICKUP | EV_HUP | EV_CLOSE)
        self.assertEqual(worker.node_buffer(HOSTNAME), None)

    def testLocalhostCommandFanout(self):
        fanout = self._task.info("fanout")
        self._task.set_info("fanout", 2)
        # init worker
        for i in range(0, 10):
            worker = self._task.shell("/bin/echo %d" % i, nodes=HOSTNAME)
        # run task
        self._task.resume()
        # restore fanout value
        self._task.set_info("fanout", fanout)

    def testWorkerBuffers(self):
        # Warning: if you modify this test, please also modify testWorkerErrorBuffers()
        task = task_self()
        worker = task.shell("/usr/bin/printf 'foo\nbar\nxxx\n'",
                            nodes=HOSTNAME)
        task.resume()
        # test iter_buffers() by worker...
        cnt = 2
        for buf, nodes in worker.iter_buffers():
            cnt -= 1
            if buf == b"foo\nbar\nxxx\n":
                self.assertEqual(len(nodes), 1)
                self.assertEqual(str(nodes), HOSTNAME)
        self.assertEqual(cnt, 1)
        # new check in 1.7 to ensure match_keys is not a string
        testgen = worker.iter_buffers(HOSTNAME)
        # cast to list to effectively iterate
        self.assertRaises(TypeError, list, testgen)
        # and also fixed an issue when match_keys was an empty list
        for buf, nodes in worker.iter_buffers([]):
            self.assertFalse("Found buffer with empty match_keys?!")
        for buf, nodes in worker.iter_buffers([HOSTNAME]):
            cnt -= 1
            if buf == b"foo\nbar\nxxx\n":
                self.assertEqual(len(nodes), 1)
                self.assertEqual(str(nodes), HOSTNAME)
        self.assertEqual(cnt, 0)
        # test flushing buffers by worker
        worker.flush_buffers()
        self.assertEqual(list(worker.iter_buffers()), [])

    def testWorkerErrorBuffers(self):
        # Warning: if you modify this test, please also modify testWorkerBuffers()
        task = task_self()
        worker = task.shell("/usr/bin/printf 'foo\nbar\nxxx\n' 1>&2",
                            nodes=HOSTNAME, stderr=True)
        task.resume()
        # test iter_errors() by worker...
        cnt = 2
        for buf, nodes in worker.iter_errors():
            cnt -= 1
            if buf == b"foo\nbar\nxxx\n":
                self.assertEqual(len(nodes), 1)
                self.assertEqual(str(nodes), HOSTNAME)
        self.assertEqual(cnt, 1)
        # new check in 1.7 to ensure match_keys is not a string
        testgen = worker.iter_errors(HOSTNAME)
        # cast to list to effectively iterate
        self.assertRaises(TypeError, list, testgen)
        # and also fixed an issue when match_keys was an empty list
        for buf, nodes in worker.iter_errors([]):
            self.assertFalse("Found error buffer with empty match_keys?!")
        for buf, nodes in worker.iter_errors([HOSTNAME]):
            cnt -= 1
            if buf == b"foo\nbar\nxxx\n":
                self.assertEqual(len(nodes), 1)
                self.assertEqual(str(nodes), HOSTNAME)
        self.assertEqual(cnt, 0)
        # test flushing error buffers by worker
        worker.flush_errors()
        self.assertEqual(list(worker.iter_errors()), [])

    def testWorkerNodeBuffers(self):
        task = task_self()

        worker = task.shell("/usr/bin/printf 'foo\nbar\nxxx\n'",
                            nodes=HOSTNAME)

        task.resume()

        cnt = 1
        for node, buf in worker.iter_node_buffers():
            cnt -= 1
            if buf == b"foo\nbar\nxxx\n":
                self.assertEqual(node, HOSTNAME)
        self.assertEqual(cnt, 0)

    def testWorkerNodeErrors(self):
        task = task_self()

        worker = task.shell("/usr/bin/printf 'foo\nbar\nxxx\n' 1>&2",
                            nodes=HOSTNAME, stderr=True)

        task.resume()

        cnt = 1
        for node, buf in worker.iter_node_errors():
            cnt -= 1
            if buf == b"foo\nbar\nxxx\n":
                self.assertEqual(node, HOSTNAME)
        self.assertEqual(cnt, 0)

    def testWorkerRetcodes(self):
        task = task_self()

        worker = task.shell("/bin/sh -c 'exit 3'", nodes=HOSTNAME)

        task.resume()

        cnt = 2
        for rc, keys in worker.iter_retcodes():
            cnt -= 1
            self.assertEqual(rc, 3)
            self.assertEqual(len(keys), 1)
            self.assertEqual(keys[0], HOSTNAME)

        self.assertEqual(cnt, 1)

        for rc, keys in worker.iter_retcodes(HOSTNAME):
            cnt -= 1
            self.assertEqual(rc, 3)
            self.assertEqual(len(keys), 1)
            self.assertEqual(keys[0], HOSTNAME)

        self.assertEqual(cnt, 0)

        # test node_retcode
        self.assertEqual(worker.node_retcode(HOSTNAME), 3)   # 1.2.91+
        self.assertEqual(worker.node_rc(HOSTNAME), 3)

        # test node_retcode failure
        self.assertRaises(KeyError, worker.node_retcode, "dummy")

        # test max retcode API
        self.assertEqual(task.max_retcode(), 3)

    def testWorkerNodeRetcodes(self):
        task = task_self()

        worker = task.shell("/bin/sh -c 'exit 3'", nodes=HOSTNAME)

        task.resume()

        cnt = 1
        for node, rc in worker.iter_node_retcodes():
            cnt -= 1
            self.assertEqual(rc, 3)
            self.assertEqual(node, HOSTNAME)

        self.assertEqual(cnt, 0)

    def testEscape(self):
        cmd = r"export CSTEST=foobar; /bin/echo \$CSTEST | sed 's/\ foo/bar/'"
        worker = self._task.shell(cmd, nodes=HOSTNAME)
        # execute
        self._task.resume()
        # read result
        self.assertEqual(worker.node_buffer(HOSTNAME), b"$CSTEST")

    def testEscape2(self):
        cmd = r"export CSTEST=foobar; /bin/echo $CSTEST | sed 's/\ foo/bar/'"
        worker = self._task.shell(cmd, nodes=HOSTNAME)
        # execute
        self._task.resume()
        # read result
        self.assertEqual(worker.node_buffer(HOSTNAME), b"foobar")

    def testSshUserOption(self):
        ssh_user_orig = self._task.info("ssh_user")
        self._task.set_info("ssh_user", pwd.getpwuid(os.getuid())[0])
        worker = self._task.shell("/bin/echo foobar", nodes=HOSTNAME)
        self._task.resume()
        # restore original ssh_user (None)
        self.assertEqual(ssh_user_orig, None)
        self._task.set_info("ssh_user", ssh_user_orig)

    def testSshUserOptionForScp(self):
        ssh_user_orig = self._task.info("ssh_user")
        self._task.set_info("ssh_user", pwd.getpwuid(os.getuid())[0])
        dest = make_temp_filename('testLocalhostCopyU')
        worker = self._task.copy("/etc/hosts", dest, nodes=HOSTNAME)
        self._task.resume()
        # restore original ssh_user (None)
        self.assertEqual(ssh_user_orig, None)
        self._task.set_info("ssh_user", ssh_user_orig)
        os.unlink(dest)

    def testSshOptionsOption(self):
        ssh_options_orig = self._task.info("ssh_options")
        try:
            self._task.set_info("ssh_options", "-oLogLevel=QUIET")
            worker = self._task.shell("/bin/echo foobar", nodes=HOSTNAME)
            self._task.resume()
            self.assertEqual(worker.node_buffer(HOSTNAME), b"foobar")
            # test 3 options
            self._task.set_info("ssh_options", \
                "-oLogLevel=QUIET -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=no")
            worker = self._task.shell("/bin/echo foobar3", nodes=HOSTNAME)
            self._task.resume()
            self.assertEqual(worker.node_buffer(HOSTNAME), b"foobar3")
        finally:
            # restore original ssh_user (None)
            self.assertEqual(ssh_options_orig, None)
            self._task.set_info("ssh_options", ssh_options_orig)

    def testSshOptionsOptionForScp(self):
        ssh_options_orig = self._task.info("ssh_options")
        testfile = None
        try:
            testfile = make_temp_filename('testLocalhostCopyO')
            if os.path.exists(testfile):
                os.remove(testfile)
            self._task.set_info("ssh_options", \
                "-oLogLevel=QUIET -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=no")
            worker = self._task.copy("/etc/hosts", testfile, nodes=HOSTNAME)
            self._task.resume()
            self.assertTrue(os.path.exists(testfile))
        finally:
            os.unlink(testfile)
            # restore original ssh_user (None)
            self.assertEqual(ssh_options_orig, None)
            self._task.set_info("ssh_options", ssh_options_orig)

    def testShellStderrWithHandler(self):
        class StdErrHandler(EventHandler):
            def ev_read(self, worker, node, sname, msg):
                if sname == worker.SNAME_STDERR:
                    assert msg == b"something wrong"

        worker = self._task.shell("echo something wrong 1>&2", nodes=HOSTNAME,
                                  handler=StdErrHandler(), stderr=True)
        self._task.resume()
        for buf, nodes in worker.iter_errors():
            self.assertEqual(buf, b"something wrong")
        for buf, nodes in worker.iter_errors([HOSTNAME]):
            self.assertEqual(buf, b"something wrong")

    def testShellWriteSimple(self):
        worker = self._task.shell("cat", nodes=HOSTNAME)
        worker.write(b"this is a test\n")
        worker.set_write_eof()
        self._task.resume()
        self.assertEqual(worker.node_buffer(HOSTNAME), b"this is a test")

    def testShellWriteHandler(self):
        class WriteOnReadHandler(EventHandler):
            def __init__(self, target_worker):
                self.target_worker = target_worker
            def ev_read(self, worker, node, sname, msg):
                self.target_worker.write(node.encode() + b':' + msg + b'\n')
                self.target_worker.set_write_eof()

        reader = self._task.shell("cat", nodes=HOSTNAME)
        worker = self._task.shell("sleep 1; echo foobar", nodes=HOSTNAME,
                                  handler=WriteOnReadHandler(reader))
        self._task.resume()
        res = "%s:foobar" % HOSTNAME
        self.assertEqual(reader.node_buffer(HOSTNAME), res.encode())

    def testSshBadArgumentOption(self):
        # Check code < 1.4 compatibility
        self.assertRaises(WorkerBadArgumentError, WorkerSsh, HOSTNAME, None,
                          None)
        # As of 1.4, ValueError is raised for missing parameter
        self.assertRaises(ValueError, WorkerSsh, HOSTNAME, None, None) # 1.4+

    def testCopyEvents(self):
        test_eh = self.__class__.TEventHandlerChecker(self)
        dest = make_temp_filename('testLocalhostCopyEvents')
        worker = self._task.copy("/etc/hosts", dest, nodes=HOSTNAME,
                                 handler=test_eh)
        # run task
        self._task.resume()
        os.unlink(dest)
        self.assertEqual(test_eh.flags, EV_START | EV_PICKUP | EV_HUP | EV_CLOSE)

    def testWorkerAbort(self):
        task = task_self()

        # Test worker.abort() in an event handler.
        class AbortOnTimer(EventHandler):
            def __init__(self, worker):
                EventHandler.__init__(self)
                self.ext_worker = worker
                self.testtimer = False
            def ev_timer(self, timer):
                self.ext_worker.abort()
                self.ext_worker.abort()  # safe but no effect
                self.testtimer = True

        aot = AbortOnTimer(task.shell("sleep 10", nodes=HOSTNAME))
        self.assertEqual(aot.testtimer, False)
        task.timer(1.5, handler=aot)
        task.resume()
        self.assertEqual(aot.testtimer, True)

    def testWorkerAbortSanity(self):
        task = task_self()
        worker = task.shell("sleep 1", nodes=HOSTNAME)
        worker.abort()

        # test noop abort() on unscheduled worker
        worker = WorkerSsh(HOSTNAME, command="sleep 1", handler=None,
                           timeout=None)
        worker.abort()

    def testLocalhostRCopy(self):
        try:
            dest = make_temp_dir('testLocalhostRCopy')
            # use fake node 'aaa' to test rank > 0
            worker = self._task.rcopy("/etc/hosts", dest.name, "aaa,%s" % HOSTNAME,
                                      handler=None, timeout=10)
            self._task.resume()
            self.assertEqual(worker.source, "/etc/hosts")
            self.assertEqual(worker.dest, dest.name)
            self.assertTrue(os.path.exists(os.path.join(dest.name,
                                                        "hosts.%s" % HOSTNAME)))
        finally:
            dest.cleanup()

    def testLocalhostExplicitSshReverseCopy(self):
        dest = make_temp_dir('testLocalhostExplicitSshRCopy')
        try:
            worker = WorkerSsh(HOSTNAME, source="/etc/hosts", dest=dest.name,
                               handler=None, timeout=10, reverse=True)
            self._task.schedule(worker)
            self._task.resume()
            self.assertEqual(worker.source, "/etc/hosts")
            self.assertEqual(worker.dest, dest.name)
            self.assertTrue(os.path.exists(os.path.join(dest.name,
                                                        "hosts.%s" % HOSTNAME)))
        finally:
            dest.cleanup()

    def testLocalhostExplicitSshReverseCopyDir(self):
        dtmp_src = make_temp_dir('src')
        dtmp_dst = make_temp_dir('testLocalhostExplicitSshReverseCopyDir')
        try:
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_b"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a", "lev2"))
            worker = WorkerSsh(HOSTNAME, source=dtmp_src.name,
                               dest=dtmp_dst.name, handler=None, timeout=30,
                               reverse=True)
            self._task.schedule(worker)
            self._task.resume()
            self.assertTrue(os.path.exists(os.path.join(dtmp_dst.name, "%s.%s" % \
                    (os.path.basename(dtmp_src.name), HOSTNAME), "lev1_a", "lev2")))
        finally:
            dtmp_dst.cleanup()
            dtmp_src.cleanup()

    def testLocalhostExplicitSshReverseCopyDirPreserve(self):
        dtmp_src = make_temp_dir('src')
        dtmp_dst = make_temp_dir('testLocalhostExplicitSshReverseCpDirPreserve')
        try:
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_b"))
            os.mkdir(os.path.join(dtmp_src.name, "lev1_a", "lev2"))
            worker = WorkerSsh(HOSTNAME, source=dtmp_src.name,
                               dest=dtmp_dst.name, handler=None, timeout=30,
                               reverse=True)
            self._task.schedule(worker)
            self._task.resume()
            self.assertTrue(os.path.exists(os.path.join(dtmp_dst.name, "%s.%s" % \
                    (os.path.basename(dtmp_src.name), HOSTNAME), "lev1_a", "lev2")))
        finally:
            dtmp_dst.cleanup()
            dtmp_src.cleanup()

    def testErroneousSshPath(self):
        try:
            self._task.set_info("ssh_path", "/wrong/path/to/ssh")
            # init worker
            worker = self._task.shell("/bin/echo ok", nodes=HOSTNAME)
            # run task
            self._task.resume()
            self.assertEqual(self._task.max_retcode(), 255)
        finally:
            # restore fanout value
            self._task.set_info("ssh_path", None)

    class TEventHandlerEvCountChecker(EventHandler):
        """simple event count validator"""

        def __init__(self):
            self.start_count = 0
            self.pickup_count = 0
            self.hup_count = 0
            self.close_count = 0

        def ev_start(self, worker):
            self.start_count += 1

        def ev_pickup(self, worker, node):
            self.pickup_count += 1

        def ev_hup(self, worker, node, rc):
            self.hup_count += 1

        def ev_close(self, worker, timedout):
            self.close_count += 1

    @unittest.skipIf(HOSTNAME == 'localhost', "does not work with hostname set to 'localhost'")
    def testWorkerEventCount(self):
        test_eh = self.__class__.TEventHandlerEvCountChecker()
        nodes = "localhost,%s" % HOSTNAME
        worker = self._task.shell("/bin/hostname", nodes=nodes, handler=test_eh)
        self._task.resume()
        # test event count
        self.assertEqual(test_eh.pickup_count, 2)
        self.assertEqual(test_eh.hup_count, 2)
        self.assertEqual(test_eh.start_count, 1)
        self.assertEqual(test_eh.close_count, 1)