aboutsummaryrefslogtreecommitdiff
blob: a76bba42cfaee2f305a6a07d4b7cab9973820191 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#	vim:fileencoding=utf-8
# (c) 2011 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.

import dbus.service

from pmstestsuite.library.case import EbuildTestCase
from pmstestsuite.library.depend_case import EbuildDependencyTestCase, \
		EclassDependencyTestCase
from pmstestsuite.library.eclass_case import EclassTestCase

from pmstestsuite.dbus_handler import DBusHandler, dbus_interface_name, dbus_object_prefix

dbus_handler = DBusHandler()

pms_dbus_eclass = """# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

DEPEND="sys-apps/dbus"

EXPORT_FUNCTIONS pkg_setup

pms-test-dbus_call() {
	local method=${1}
	shift

	PMS_TEST_DBUS_P=${P//-/_}
	dbus-send \\
		--system \\
		--print-reply \\
		--type=method_call \\
		--dest=org.gentoo.pmstestsuite \\
		/org/gentoo/pmstestsuite/${PMS_TEST_DBUS_P} \\
		org.gentoo.pmstestsuite.${method} \\
		"${@}"
}

pms-test-dbus_pkg_setup() {
	pms-test-dbus_call test_started
}

pms-test-dbus_append_result() {
	pms-test-dbus_call append_output \\
		string:"${*}" \\
		|| die 'Failed to append the test result using dbus'
}
"""

class RunningTest(dbus.service.Object):
	""" A class encapsulating a running test. """

	def __init__(self, test):
		"""
		Initialize the D-Bus object for test <test>.
		"""
		self.test = test
		self.reset()
		dbus.service.Object.__init__(
				self,
				dbus_handler.bus,
				'%s/%s' % (dbus_object_prefix, test.p.replace('-', '_'))
		)

	def reset(self):
		"""
		Reset test results.
		"""
		self.test.dbus_output = []
		self.test.dbus_started = False

	@dbus.service.method(
			dbus_interface=dbus_interface_name,
			in_signature='', out_signature='')
	def test_started(self):
		""" Notify the test suite that a particular test has been started. """
		self.test.dbus_started = True

	@dbus.service.method(
			dbus_interface=dbus_interface_name,
			in_signature='s', out_signature='')
	def append_output(self, l):
		""" Append the line <l> to the test output. """
		self.test.dbus_output.append(str(l))

class DBusBaseTestCase(object):
	""" A base D-Bus test case class. """

	inherits = ['pms-test-dbus']

	def __init__(self):
		self._dbusobj = RunningTest(self)

	def _finalize(self):
		""" Finalize the object, ensuring pkg_setup() will be called. """
		if self.phase_funcs['pkg_setup']:
			self.phase_funcs['pkg_setup'].insert(0, 'pms-test-dbus_pkg_setup')

	def check_dbus_result(self, output, pm):
		"""
		Check whether the <output> sent through D-Bus matches expected test
		output.

		Return True if it does, False otherwise.

		The default implementation simply checks whether the test was merged
		alike EbuildTestCase.check_result().
		"""
		pass

	def _pop_dbus_output(self):
		ret = self.dbus_output
		self._dbusobj.reset()
		return ret

	def check_result(self, pm):
		self.assertTrue(self.dbus_started, 'build started')
		self.check_dbus_result(self._pop_dbus_output(), pm)

class DBusEbuildTestCase(DBusBaseTestCase, EbuildTestCase):
	""" D-Bus capable base test case. """

	def __init__(self, *args, **kwargs):
		""" Initialize the test case and the D-Bus object for it. """
		EbuildTestCase.__init__(self, *args, **kwargs)
		DBusBaseTestCase.__init__(self)

	def check_dbus_result(self, output, pm):
		EbuildTestCase.check_result(self, pm)

class DBusEclassTestCase(DBusBaseTestCase, EclassTestCase):
	""" D-Bus capable eclass test case. """

	def __init__(self, *args, **kwargs):
		""" Initialize the test case and the D-Bus object for it. """
		EclassTestCase.__init__(self, *args, **kwargs)
		DBusBaseTestCase.__init__(self)

	def check_dbus_result(self, output, pm):
		EclassTestCase.check_result(self, pm)

class DBusEbuildDependencyTestCase(DBusBaseTestCase, EbuildDependencyTestCase):
	""" D-Bus capable dependency test case. """

	def __init__(self, *args, **kwargs):
		""" Initialize the test case and the D-Bus object for it. """
		EbuildDependencyTestCase.__init__(self, *args, **kwargs)
		DBusBaseTestCase.__init__(self)

	def check_dbus_result(self, output, pm):
		EbuildDependencyTestCase.check_result(self, pm)

	def check_result(self, pm):
		started = self.dbus_started
		self.check_dbus_result(self._pop_dbus_output(), pm)
		self.assertBool(not self.expect_failure, started, 'build started')

class DBusEclassDependencyTestCase(DBusBaseTestCase, EclassDependencyTestCase):
	""" D-Bus capable eclass dependency test case. """

	def __init__(self, *args, **kwargs):
		""" Initialize the test case and the D-Bus object for it. """
		EclassDependencyTestCase.__init__(self, *args, **kwargs)
		DBusBaseTestCase.__init__(self)

	def check_dbus_result(self, output, pm):
		EclassDependencyTestCase.check_result(self, pm)

	def check_result(self, pm):
		started = self.dbus_started
		self.check_dbus_result(self._pop_dbus_output(), pm)
		self.assertBool(not self.expect_failure, started, 'build started')