| 1 |
#! /usr/bin/env python3 |
| 2 |
# Copyright © 2020, RedHat Inc. |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU Lesser General Public |
| 6 |
# License as published by the Free Software Foundation; either |
| 7 |
# version 2.1 of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public |
| 15 |
# License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# Authors: |
| 17 |
# Benjamin Berg <[email protected]> |
| 18 |
|
| 19 |
import os |
| 20 |
import sys |
| 21 |
import fcntl |
| 22 |
import io |
| 23 |
import re |
| 24 |
import time |
| 25 |
import threading |
| 26 |
import select |
| 27 |
import errno |
| 28 |
|
| 29 |
class OutputChecker(object): |
| 30 |
|
| 31 |
def __init__(self, out=sys.stdout): |
| 32 |
self._output = out |
| 33 |
self._pipe_fd_r, self._pipe_fd_w = os.pipe() |
| 34 |
self._partial_buf = b'' |
| 35 |
self._lines_sem = threading.Semaphore() |
| 36 |
self._lines = [] |
| 37 |
self._reader_io = io.StringIO() |
| 38 |
|
| 39 |
# Just to be sure, shouldn't be a problem even if we didn't set it |
| 40 |
fcntl.fcntl(self._pipe_fd_r, fcntl.F_SETFL, |
| 41 |
fcntl.fcntl(self._pipe_fd_r, fcntl.F_GETFL) | os.O_CLOEXEC | os.O_NONBLOCK) |
| 42 |
fcntl.fcntl(self._pipe_fd_w, fcntl.F_SETFL, |
| 43 |
fcntl.fcntl(self._pipe_fd_w, fcntl.F_GETFL) | os.O_CLOEXEC) |
| 44 |
|
| 45 |
# Start copier thread |
| 46 |
self._thread = threading.Thread(target=self._copy, daemon=True) |
| 47 |
self._thread.start() |
| 48 |
|
| 49 |
def _copy(self): |
| 50 |
p = select.poll() |
| 51 |
p.register(self._pipe_fd_r) |
| 52 |
while True: |
| 53 |
try: |
| 54 |
# Be lazy and wake up occasionally in case _pipe_fd_r became invalid |
| 55 |
# The reason to do this is because os.read() will *not* return if the |
| 56 |
# FD is forcefully closed. |
| 57 |
p.poll(0.1) |
| 58 |
|
| 59 |
r = os.read(self._pipe_fd_r, 1024) |
| 60 |
if not r: |
| 61 |
os.close(self._pipe_fd_r) |
| 62 |
self._pipe_fd_r = -1 |
| 63 |
self._lines_sem.release() |
| 64 |
return |
| 65 |
except OSError as e: |
| 66 |
if e.errno == errno.EWOULDBLOCK: |
| 67 |
continue |
| 68 |
|
| 69 |
# We get a bad file descriptor error when the outside closes the FD |
| 70 |
if self._pipe_fd_r >= 0: |
| 71 |
os.close(self._pipe_fd_r) |
| 72 |
self._pipe_fd_r = -1 |
| 73 |
self._lines_sem.release() |
| 74 |
return |
| 75 |
|
| 76 |
l = r.split(b'\n') |
| 77 |
l[0] = self._partial_buf + l[0] |
| 78 |
self._lines.extend(l[:-1]) |
| 79 |
self._partial_buf = l[-1] |
| 80 |
|
| 81 |
self._lines_sem.release() |
| 82 |
|
| 83 |
os.write(self._output.fileno(), r) |
| 84 |
|
| 85 |
def check_line_re(self, needle_re, timeout=0, failmsg=None): |
| 86 |
deadline = time.time() + timeout |
| 87 |
|
| 88 |
if isinstance(needle_re, str): |
| 89 |
needle_re = needle_re.encode('ascii') |
| 90 |
|
| 91 |
r = re.compile(needle_re) |
| 92 |
ret = [] |
| 93 |
|
| 94 |
while True: |
| 95 |
try: |
| 96 |
l = self._lines.pop(0) |
| 97 |
except IndexError: |
| 98 |
# EOF, throw error |
| 99 |
if self._pipe_fd_r == -1: |
| 100 |
if failmsg: |
| 101 |
raise AssertionError("No further messages: " % failmsg) from None |
| 102 |
else: |
| 103 |
raise AssertionError('No client waiting for needle %s' % (str(needle_re))) from None |
| 104 |
|
| 105 |
# Check if should wake up |
| 106 |
if not self._lines_sem.acquire(timeout = deadline - time.time()): |
| 107 |
if failmsg: |
| 108 |
raise AssertionError(failmsg) from None |
| 109 |
else: |
| 110 |
raise AssertionError('Timed out waiting for needle %s (timeout: %0.2f)' % (str(needle_re), timeout)) from None |
| 111 |
continue |
| 112 |
|
| 113 |
ret.append(l) |
| 114 |
if r.search(l): |
| 115 |
return ret |
| 116 |
|
| 117 |
def check_line(self, needle, timeout=0, failmsg=None): |
| 118 |
if isinstance(needle, str): |
| 119 |
needle = needle.encode('ascii') |
| 120 |
|
| 121 |
needle_re = re.escape(needle) |
| 122 |
|
| 123 |
return self.check_line_re(needle_re, timeout=timeout, failmsg=failmsg) |
| 124 |
|
| 125 |
def check_no_line_re(self, needle_re, wait=0, failmsg=None): |
| 126 |
deadline = time.time() + wait |
| 127 |
|
| 128 |
if isinstance(needle_re, str): |
| 129 |
needle_re = needle_re.encode('ascii') |
| 130 |
|
| 131 |
r = re.compile(needle_re) |
| 132 |
ret = [] |
| 133 |
|
| 134 |
while True: |
| 135 |
try: |
| 136 |
l = self._lines.pop(0) |
| 137 |
except IndexError: |
| 138 |
# EOF, so everything good |
| 139 |
if self._pipe_fd_r == -1: |
| 140 |
break |
| 141 |
|
| 142 |
# Check if should wake up |
| 143 |
if not self._lines_sem.acquire(timeout = deadline - time.time()): |
| 144 |
# Timed out, so everything is good |
| 145 |
break |
| 146 |
continue |
| 147 |
|
| 148 |
ret.append(l) |
| 149 |
if r.search(l): |
| 150 |
if failmsg: |
| 151 |
raise AssertionError(failmsg) |
| 152 |
else: |
| 153 |
raise AssertionError('Found needle %s but shouldn\'t have been there (timeout: %0.2f)' % (str(needle_re), wait)) |
| 154 |
|
| 155 |
return ret |
| 156 |
|
| 157 |
def check_no_line(self, needle, wait=0, failmsg=None): |
| 158 |
if isinstance(needle, str): |
| 159 |
needle = needle.encode('ascii') |
| 160 |
|
| 161 |
needle_re = re.escape(needle) |
| 162 |
|
| 163 |
return self.check_no_line_re(needle_re, wait=wait, failmsg=failmsg) |
| 164 |
|
| 165 |
def clear(self): |
| 166 |
ret = self._lines |
| 167 |
self._lines = [] |
| 168 |
return ret |
| 169 |
|
| 170 |
def assert_closed(self, timeout=1): |
| 171 |
self._thread.join(timeout) |
| 172 |
|
| 173 |
if self._thread.is_alive() != False: |
| 174 |
raise AssertionError("OutputCheck: Write side has not been closed yet!") |
| 175 |
|
| 176 |
def force_close(self): |
| 177 |
|
| 178 |
fd = self._pipe_fd_r |
| 179 |
self._pipe_fd_r = -1 |
| 180 |
if fd >= 0: |
| 181 |
os.close(fd) |
| 182 |
|
| 183 |
self._thread.join() |
| 184 |
|
| 185 |
@property |
| 186 |
def fd(self): |
| 187 |
return self._pipe_fd_w |
| 188 |
|
| 189 |
def writer_attached(self): |
| 190 |
os.close(self._pipe_fd_w) |
| 191 |
self._pipe_fd_w = -1 |
| 192 |
|
| 193 |
def __del__(self): |
| 194 |
if self._pipe_fd_r >= 0: |
| 195 |
os.close(self._pipe_fd_r) |
| 196 |
self._pipe_fd_r = -1 |
| 197 |
if self._pipe_fd_w >= 0: |
| 198 |
os.close(self._pipe_fd_w) |
| 199 |
self._pipe_fd_w = -1 |