| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright (c) 2021-2022 Paul Mattes. |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# * Redistributions of source code must retain the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer. |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# * Neither the names of Paul Mattes nor the names of his contributors |
| 14 |
# may be used to endorse or promote products derived from this software |
| 15 |
# without specific prior written permission. |
| 16 |
# |
| 17 |
# THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 |
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 |
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 20 |
# EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 |
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 |
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 |
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 |
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
# |
| 28 |
# tcl3270 smoke tests |
| 29 |
|
| 30 |
import unittest |
| 31 |
from subprocess import Popen, PIPE, DEVNULL |
| 32 |
import sys |
| 33 |
if sys.platform != 'cygwin': |
| 34 |
import psutil |
| 35 |
import requests |
| 36 |
import os |
| 37 |
import signal |
| 38 |
import Common.Test.cti as cti |
| 39 |
|
| 40 |
@unittest.skipIf(sys.platform == "darwin", "macOS does not like tcl") |
| 41 |
@unittest.skipIf(sys.platform == 'cygwin', "Cygwin does not have psutil") |
| 42 |
class TestTcl3270Linger(cti.cti): |
| 43 |
|
| 44 |
# tcl3270 linger test, making sure s3270 exits when tcl3270 does. |
| 45 |
def test_tcl3270_linger1(self): |
| 46 |
|
| 47 |
# Start tcl3270. |
| 48 |
port, ts = cti.unused_port() |
| 49 |
tcl3270 = Popen(cti.vgwrap(["tcl3270", "tcl3270/Test/linger.tcl", "--", |
| 50 |
"-httpd", f"127.0.0.1:{port}"]), stdin=DEVNULL, stdout=DEVNULL) |
| 51 |
self.children.append(tcl3270) |
| 52 |
self.check_listen(port) |
| 53 |
ts.close() |
| 54 |
|
| 55 |
# Make sure it is blocked. |
| 56 |
def test(): |
| 57 |
j = requests.get(f'http://127.0.0.1:{port}/3270/rest/json/Query(Tasks)').json() |
| 58 |
return any('Wait(' in line for line in j['result']) |
| 59 |
self.try_until(test, 2, "emulator did not block") |
| 60 |
|
| 61 |
# Find the copy of s3270 it is running. |
| 62 |
tchildren = psutil.Process(tcl3270.pid).children() |
| 63 |
self.assertEqual(1, len(tchildren)) |
| 64 |
s3270 = tchildren[0] |
| 65 |
self.assertEqual('s3270', s3270.name()) |
| 66 |
|
| 67 |
# Kill tcl3270. |
| 68 |
tcl3270.kill() |
| 69 |
self.children.remove(tcl3270) |
| 70 |
self.vgwait(tcl3270, assertOnFailure=False) |
| 71 |
|
| 72 |
# Make sure s3270 is gone, too. |
| 73 |
def test2(): |
| 74 |
return 'terminated' in str(s3270) |
| 75 |
self.try_until(test2, 2, "s3270 did not exit") |
| 76 |
|
| 77 |
# tcl3270 linger test, making sure tcl3270 exits when s3270 does. |
| 78 |
def test_tcl3270_linger2(self): |
| 79 |
|
| 80 |
# Start tcl3270. |
| 81 |
port, ts = cti.unused_port() |
| 82 |
tcl3270 = Popen(cti.vgwrap(["tcl3270", "tcl3270/Test/linger2.tcl", "--", |
| 83 |
"-httpd", f"127.0.0.1:{port}"]), |
| 84 |
stdin=DEVNULL, stdout=DEVNULL) |
| 85 |
self.children.append(tcl3270) |
| 86 |
self.check_listen(port) |
| 87 |
ts.close() |
| 88 |
|
| 89 |
# Find the copy of s3270 it is running. |
| 90 |
tchildren = psutil.Process(tcl3270.pid).children() |
| 91 |
self.assertEqual(1, len(tchildren)) |
| 92 |
s3270 = tchildren[0] |
| 93 |
self.assertEqual('s3270', s3270.name()) |
| 94 |
|
| 95 |
# Kill s3270. |
| 96 |
os.kill(s3270.pid, signal.SIGTERM) |
| 97 |
|
| 98 |
# Make sure tcl3270 is gone, too. |
| 99 |
self.vgwait(tcl3270, assertOnFailure=False) |
| 100 |
exit_code = tcl3270.wait() |
| 101 |
self.assertEqual(98, exit_code) |
| 102 |
|
| 103 |
if __name__ == '__main__': |
| 104 |
unittest.main() |