summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTapple Fulmer <mfulmer@cisco.com>2023-02-27 21:26:41 +0000
committerTapple Fulmer <mfulmer@cisco.com>2023-02-27 21:26:41 +0000
commit571ca4b49d2c469aaaa57bfea6d8a28d8a91c4fc (patch)
tree1a46c661aac4168392bf10e1346425aaf48580d7
parentc9214888333865dd9f12c310d12ee40884d83fee (diff)
downloadpexpect-571ca4b49d2c469aaaa57bfea6d8a28d8a91c4fc.tar.gz
duplicated test_socket to test socket_spawn and fdspawn both
-rw-r--r--tests/test_socket.py40
-rw-r--r--tests/test_socket_fd.py64
2 files changed, 78 insertions, 26 deletions
diff --git a/tests/test_socket.py b/tests/test_socket.py
index 548d90a..b801b00 100644
--- a/tests/test_socket.py
+++ b/tests/test_socket.py
@@ -19,7 +19,7 @@ PEXPECT LICENSE
'''
import pexpect
-from pexpect import fdpexpect
+from pexpect import socket_pexpect
import unittest
from . import PexpectTestCase
import multiprocessing
@@ -133,12 +133,16 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
pass
exit(0)
+ def spawn(self, socket, timeout=30, use_poll=False):
+ """override me with other ways of spawning on a socket"""
+ return socket_pexpect.SocketSpawn(socket, timeout=timeout, use_poll=use_poll)
+
def socket_fn(self, timed_out, all_read):
result = 0
try:
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock, timeout=10)
+ session = self.spawn(sock, timeout=10)
# Get all data from server
session.read_nonblocking(size=4096)
all_read.set()
@@ -152,7 +156,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_socket(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
+ session = self.spawn(sock, timeout=10)
session.expect(self.prompt1)
self.assertEqual(session.before, self.motd)
session.send(self.enter)
@@ -166,7 +170,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_socket_with_write(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
+ session = self.spawn(sock, timeout=10)
session.expect(self.prompt1)
self.assertEqual(session.before, self.motd)
session.write(self.enter)
@@ -177,19 +181,11 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
session.expect(pexpect.EOF)
self.assertEqual(session.before, b'')
- def test_not_int(self):
- with self.assertRaises(pexpect.ExceptionPexpect):
- session = fdpexpect.fdspawn('bogus', timeout=10)
-
- def test_not_file_descriptor(self):
- with self.assertRaises(pexpect.ExceptionPexpect):
- session = fdpexpect.fdspawn(-1, timeout=10)
-
def test_timeout(self):
with self.assertRaises(pexpect.TIMEOUT):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock, timeout=10)
+ session = self.spawn(sock, timeout=10)
session.expect(b'Bogus response')
def test_interrupt(self):
@@ -223,7 +219,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_maxread(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
+ session = self.spawn(sock, timeout=10)
session.maxread = 1100
session.expect(self.prompt1)
self.assertEqual(session.before, self.motd)
@@ -238,7 +234,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_fd_isalive(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
+ session = self.spawn(sock, timeout=10)
assert session.isalive()
sock.close()
assert not session.isalive(), "Should not be alive after close()"
@@ -246,7 +242,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_fd_isalive_poll(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10, use_poll=True)
+ session = self.spawn(sock, timeout=10, use_poll=True)
assert session.isalive()
sock.close()
assert not session.isalive(), "Should not be alive after close()"
@@ -254,25 +250,17 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_fd_isatty(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
+ session = self.spawn(sock, timeout=10)
assert not session.isatty()
session.close()
def test_fd_isatty_poll(self):
sock = socket.socket(self.af, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock.fileno(), timeout=10, use_poll=True)
+ session = self.spawn(sock, timeout=10, use_poll=True)
assert not session.isatty()
session.close()
- def test_fileobj(self):
- sock = socket.socket(self.af, socket.SOCK_STREAM)
- sock.connect((self.host, self.port))
- session = fdpexpect.fdspawn(sock, timeout=10) # Should get the fileno from the socket
- session.expect(self.prompt1)
- session.close()
- assert not session.isalive()
- session.close() # Smoketest - should be able to call this again
if __name__ == '__main__':
unittest.main()
diff --git a/tests/test_socket_fd.py b/tests/test_socket_fd.py
new file mode 100644
index 0000000..5be733c
--- /dev/null
+++ b/tests/test_socket_fd.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+ This license is approved by the OSI and FSF as GPL-compatible.
+ http://opensource.org/licenses/isc-license.txt
+
+ Copyright (c) 2012, Noah Spurrier <noah@noah.org>
+ PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+ PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+ COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+from pexpect import fdpexpect
+import unittest
+from . import test_socket
+import multiprocessing
+import os
+import signal
+import socket
+import time
+import errno
+
+
+class SocketServerError(Exception):
+ pass
+
+
+class ExpectTestCase(test_socket.ExpectTestCase):
+ """ duplicate of test_socket, but using fdpexpect rather than socket_expect """
+
+ def spawn(self, socket, timeout=30, use_poll=False):
+ return fdpexpect.fdspawn(socket.fileno(), timeout=timeout, use_poll=use_poll)
+
+ def test_not_int(self):
+ with self.assertRaises(pexpect.ExceptionPexpect):
+ session = fdpexpect.fdspawn('bogus', timeout=10)
+
+ def test_not_file_descriptor(self):
+ with self.assertRaises(pexpect.ExceptionPexpect):
+ session = fdpexpect.fdspawn(-1, timeout=10)
+
+ def test_fileobj(self):
+ sock = socket.socket(self.af, socket.SOCK_STREAM)
+ sock.connect((self.host, self.port))
+ session = fdpexpect.fdspawn(sock, timeout=10) # Should get the fileno from the socket
+ session.expect(self.prompt1)
+ session.close()
+ assert not session.isalive()
+ session.close() # Smoketest - should be able to call this again
+
+
+if __name__ == '__main__':
+ unittest.main()
+
+suite = unittest.TestLoader().loadTestsFromTestCase(ExpectTestCase)