Gentoo Archives: gentoo-commits

From: Zac Medico <zmedico@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/tests/process/
Date: Sat, 26 May 2018 10:48:02
Message-Id: 1527331478.3b0c52b9ef364dc8e69208ec5341255ac94d41d8.zmedico@gentoo
1 commit: 3b0c52b9ef364dc8e69208ec5341255ac94d41d8
2 Author: Zac Medico <zmedico <AT> gentoo <DOT> org>
3 AuthorDate: Sat May 26 10:44:38 2018 +0000
4 Commit: Zac Medico <zmedico <AT> gentoo <DOT> org>
5 CommitDate: Sat May 26 10:44:38 2018 +0000
6 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=3b0c52b9
7
8 PipeReaderTestCase: cover sockets and named pipes
9
10 pym/portage/tests/process/test_poll.py | 74 ++++++++++++++++++++++------------
11 1 file changed, 49 insertions(+), 25 deletions(-)
12
13 diff --git a/pym/portage/tests/process/test_poll.py b/pym/portage/tests/process/test_poll.py
14 index 596ea3088..d71c9b59c 100644
15 --- a/pym/portage/tests/process/test_poll.py
16 +++ b/pym/portage/tests/process/test_poll.py
17 @@ -1,11 +1,16 @@
18 -# Copyright 1998-2013 Gentoo Foundation
19 +# Copyright 1998-2018 Gentoo Foundation
20 # Distributed under the terms of the GNU General Public License v2
21
22 +import functools
23 +import pty
24 +import shutil
25 +import socket
26 +import sys
27 import subprocess
28 +import tempfile
29
30 from portage import os
31 from portage.tests import TestCase
32 -from portage.util._pty import _create_pty_or_pipe
33 from portage.util._async.PopenProcess import PopenProcess
34 from portage.util._eventloop.global_event_loop import global_event_loop
35 from _emerge.PipeReader import PipeReader
36 @@ -13,28 +18,47 @@ from _emerge.PipeReader import PipeReader
37 class PipeReaderTestCase(TestCase):
38
39 _use_array = False
40 - _use_pty = False
41 _echo_cmd = "echo -n '%s'"
42
43 - def _testPipeReader(self, test_string):
44 + def test_pipe(self):
45 + def make_pipes():
46 + return os.pipe(), None
47 + self._do_test(make_pipes)
48 +
49 + def test_pty_device(self):
50 + def make_pipes():
51 + try:
52 + return pty.openpty(), None
53 + except EnvironmentError:
54 + self.skipTest('pty not available')
55 + self._do_test(make_pipes)
56 +
57 + def test_domain_socket(self):
58 + def make_pipes():
59 + if sys.version_info >= (3, 2):
60 + read_end, write_end = socket.socketpair()
61 + return (read_end.detach(), write_end.detach()), None
62 + else:
63 + self.skipTest('socket detach not supported')
64 + self._do_test(make_pipes)
65 +
66 + def test_named_pipe(self):
67 + def make_pipes():
68 + tempdir = tempfile.mkdtemp()
69 + fifo_path = os.path.join(tempdir, 'fifo')
70 + os.mkfifo(fifo_path)
71 + return ((os.open(fifo_path, os.O_NONBLOCK|os.O_RDONLY),
72 + os.open(fifo_path, os.O_NONBLOCK|os.O_WRONLY)),
73 + functools.partial(shutil.rmtree, tempdir))
74 + self._do_test(make_pipes)
75 +
76 + def _testPipeReader(self, master_fd, slave_fd, test_string):
77 """
78 Use a poll loop to read data from a pipe and assert that
79 the data written to the pipe is identical to the data
80 read from the pipe.
81 """
82
83 - if self._use_pty:
84 - got_pty, master_fd, slave_fd = _create_pty_or_pipe()
85 - if not got_pty:
86 - os.close(slave_fd)
87 - os.close(master_fd)
88 - skip_reason = "pty not acquired"
89 - self.portage_skip = skip_reason
90 - self.fail(skip_reason)
91 - return
92 - else:
93 - master_fd, slave_fd = os.pipe()
94 -
95 # WARNING: It is very important to use unbuffered mode here,
96 # in order to avoid issue 5380 with python3.
97 master_file = os.fdopen(master_fd, 'rb', 0)
98 @@ -60,15 +84,18 @@ class PipeReaderTestCase(TestCase):
99
100 return consumer.getvalue().decode('ascii', 'replace')
101
102 - def testPipeReader(self):
103 + def _do_test(self, make_pipes):
104 for x in (1, 2, 5, 6, 7, 8, 2**5, 2**10, 2**12, 2**13, 2**14):
105 test_string = x * "a"
106 - output = self._testPipeReader(test_string)
107 - self.assertEqual(test_string, output,
108 - "x = %s, len(output) = %s" % (x, len(output)))
109 + (read_end, write_end), cleanup = make_pipes()
110 + try:
111 + output = self._testPipeReader(read_end, write_end, test_string)
112 + self.assertEqual(test_string, output,
113 + "x = %s, len(output) = %s" % (x, len(output)))
114 + finally:
115 + if cleanup is not None:
116 + cleanup()
117
118 -class PipeReaderPtyTestCase(PipeReaderTestCase):
119 - _use_pty = True
120
121 class PipeReaderArrayTestCase(PipeReaderTestCase):
122
123 @@ -81,6 +108,3 @@ class PipeReaderArrayTestCase(PipeReaderTestCase):
124 # https://bugs.python.org/issue5380
125 # https://bugs.pypy.org/issue956
126 self.todo = True
127 -
128 -class PipeReaderPtyArrayTestCase(PipeReaderArrayTestCase):
129 - _use_pty = True