tests: capture stdout

This commit is contained in:
Vadim Rutkovsky
2014-06-10 13:09:58 +02:00
parent 2c3485e613
commit 1d748182ff
2 changed files with 31 additions and 12 deletions

View File

@ -5,15 +5,15 @@ if isA11yEnabled() is False:
from time import time, sleep from time import time, sleep
from functools import wraps from functools import wraps
from os import strerror, errno, kill, system from os import strerror, errno, system
from signal import signal, alarm, SIGALRM, SIGKILL from signal import signal, alarm, SIGALRM
from subprocess import Popen from subprocess import Popen, PIPE
from behave import step from behave import step
from gi.repository import GLib, Gio from gi.repository import GLib, Gio
import fcntl, os
from dogtail.rawinput import keyCombo, absoluteMotion, pressKey from dogtail.rawinput import keyCombo, absoluteMotion, pressKey
from dogtail.tree import root from dogtail.tree import root
from dogtail.utils import run
from unittest import TestCase from unittest import TestCase
@ -125,7 +125,8 @@ class App(object):
self.a11yAppName = self.internCommand self.a11yAppName = self.internCommand
# Trap weird bus errors # Trap weird bus errors
for attempt in xrange(0, 10): for attempt in xrange(0, 30):
sleep(1)
try: try:
return self.a11yAppName in [x.name for x in root.applications()] return self.a11yAppName in [x.name for x in root.applications()]
except GLib.GError: except GLib.GError:
@ -140,7 +141,7 @@ class App(object):
keyCombo('<Control><Alt><Shift>R') keyCombo('<Control><Alt><Shift>R')
try: try:
kill(self.pid, SIGKILL) self.process.kill()
except: except:
# Fall back to killall # Fall back to killall
Popen("killall " + self.appCommand, shell=True).wait() Popen("killall " + self.appCommand, shell=True).wait()
@ -153,8 +154,11 @@ class App(object):
self.kill() self.kill()
assert not self.isRunning(), "Application cannot be stopped" assert not self.isRunning(), "Application cannot be stopped"
command = "%s %s" % (self.appCommand, self.parameters) #command = "%s %s" % (self.appCommand, self.parameters)
self.pid = run(command, timeout=5) #self.pid = run(command, timeout=5)
self.process = Popen(self.appCommand.split() + self.parameters.split(),
stdout=PIPE, stderr=PIPE, bufsize=0)
self.pid = self.process.pid
assert self.isRunning(), "Application failed to start" assert self.isRunning(), "Application failed to start"
return root.application(self.a11yAppName) return root.application(self.a11yAppName)
@ -222,3 +226,13 @@ def check_for_errors(context):
system("rm -rf %s > /dev/null" % folder) system("rm -rf %s > /dev/null" % folder)
raise RuntimeError("Error occurred: %s" % messages) raise RuntimeError("Error occurred: %s" % messages)
def non_block_read(output):
fd = output.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
return output.read()
except:
return ""

View File

@ -5,7 +5,7 @@ from dogtail.utils import isA11yEnabled, enableA11y
if not isA11yEnabled(): if not isA11yEnabled():
enableA11y(True) enableA11y(True)
from common_steps import App, dummy, cleanup from common_steps import App, dummy, cleanup, non_block_read
from dogtail.config import config from dogtail.config import config
import os import os
@ -57,9 +57,6 @@ def after_scenario(context, scenario):
Kill evolution (in order to make this reliable we send sigkill) Kill evolution (in order to make this reliable we send sigkill)
""" """
try: try:
# Stop evolution
context.app_class.kill()
# Attach journalctl logs # Attach journalctl logs
if hasattr(context, "embed"): if hasattr(context, "embed"):
os.system("pkexec journalctl /usr/bin/gnome-session --no-pager -o cat --since='%s'> /tmp/journal-session.log" % context.log_start_time) os.system("pkexec journalctl /usr/bin/gnome-session --no-pager -o cat --since='%s'> /tmp/journal-session.log" % context.log_start_time)
@ -67,6 +64,14 @@ def after_scenario(context, scenario):
if data: if data:
context.embed('text/plain', data) context.embed('text/plain', data)
context.app_class.kill()
stdout = non_block_read(context.app_class.process.stdout)
stderr = non_block_read(context.app_class.process.stderr)
context.embed('text/plain', '\n'.join(stdout))
context.embed('text/plain', '\n'.join(stderr))
# Make some pause after scenario # Make some pause after scenario
sleep(1) sleep(1)
except Exception as e: except Exception as e: