1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# -*- coding: utf-8 -*-
#
# Copyright 2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Tests for the glib runner helper module."""
import subprocess
# pylint: disable=E0611
from gi.repository import GLib
# pylint: enable=E0611
from twisted.internet import defer
from ubuntu_sso.utils import runner
from ubuntu_sso.utils.runner import glib
from ubuntu_sso.utils.runner.tests.test_runner import SpawnProgramTestCase
class FakedSignal(object):
"""Fake a glib signal."""
def __init__(self, name):
self.name = name
self._handlers = []
def connect(self, handler):
"""Connect 'handler' with this signal."""
self._handlers.append(handler)
def emit(self, *args, **kwargs):
"""Emit this signal."""
for handler in self._handlers:
handler(*args, **kwargs)
class FakedProcess(object):
"""Fake a glib Process."""
_argv = _flags = None
_pid = 123456
_pids = []
_status_code = 0
SpawnFlags = GLib.SpawnFlags
GError = GLib.GError
def __init__(self):
self.pid = lambda: self._pid
self.started = FakedSignal('started')
self.finished = FakedSignal('finished')
self.error = FakedSignal('error')
def spawn_async(self, argv, flags):
"""Spwan a process."""
if 'None' in argv:
exc = GLib.GError()
exc.message = str('Can not handle None')
exc.code = 24
raise exc
self._argv = argv
self._flags = flags
try:
subprocess.call(argv)
except Exception, e:
exc = GLib.GError()
exc.message = str(e)
exc.code = 42
raise exc
self._pids.append(self._pid)
return (self._pid, None, None, None)
def child_watch_add(self, pid, child_watch):
"""Addd a child watch."""
if pid in self._pids:
child_watch(pid, self._status_code)
def spawn_close_pid(self, pid):
"""Close the 'pid'."""
self._pids.remove(pid)
class GLibSpawnProgramTestCase(SpawnProgramTestCase):
"""The test suite for the spawn_program method (using GLib)."""
use_reactor = False
@defer.inlineCallbacks
def setUp(self):
yield super(GLibSpawnProgramTestCase, self).setUp()
# Since we can't mix plan glib runner and the gireactor, we patch
# GLib.spawn_async and fake the conditions so the glib runner is chosen
self.process = FakedProcess()
self.patch(glib, 'GLib', self.process)
self.patch(runner, 'is_twisted_reactor_installed', lambda: False)
self.patch(runner, 'is_qt4_main_loop_installed', lambda: False)
# Access to a protected member _flags, _argv of a client class
# pylint: disable=W0212
@defer.inlineCallbacks
def test_flags_are_correct(self):
"""The flags are the correct ones."""
yield self.spawn_fn(self.args)
flags = GLib.SpawnFlags.DO_NOT_REAP_CHILD | \
GLib.SpawnFlags.SEARCH_PATH | \
GLib.SpawnFlags.STDOUT_TO_DEV_NULL | \
GLib.SpawnFlags.STDERR_TO_DEV_NULL
self.assertEqual(self.process._flags, flags)
@defer.inlineCallbacks
def test_argv_is_bytes(self):
"""The argv parameter is converted to bytes."""
yield self.spawn_fn(self.args)
bytes_args = [a.encode('utf-8') for a in self.args]
self.assertEqual(self.process._argv, bytes_args)
|