aboutsummaryrefslogtreecommitdiff
blob: 3f5d31eb4c226a64d13817ec36158e76df81e078 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#	vim:fileencoding=utf-8
# (c) 2011 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.

import copy, random, re
from gentoopm.util import ABCObject

from abc import ABCMeta, abstractmethod, abstractproperty

# XXX: move to some consts module?
phase_func_names = [
	'pkg_pretend', 'pkg_setup', 'src_unpack', 'src_prepare',
	'src_configure', 'src_compile', 'src_install',
	'pkg_preinst', 'pkg_postinst'
]

""" Names of all phase functions supported in EAPIs. """

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

EAPI=%d

inherit %s

'''

""" A common ebuild header. """

pn_re = re.compile('([^A-Z])([A-Z])')

def cleanup_test_case_name(classname):
	"""
	Create the ebuild PN from classname.

	>>> cleanup_test_case_name('Testzor')
	'testzor'
	>>> cleanup_test_case_name('TestzorTest')
	'testzor'
	>>> cleanup_test_case_name('TestZorTest')
	'test-zor'
	>>> cleanup_test_case_name('veryRandomCamelCaseTest')
	'very-random-camel-case'
	>>> cleanup_test_case_name('RDependTest')
	'rdepend'

	@param classname: the class name to clean up
	@type classname: string
	"""

	if classname.endswith('Test'):
		classname = classname[:-4]
	return pn_re.sub('\\1-\\2', classname).lower()

class TestCase(ABCObject):
	"""
	Base class for a test case.

	@ivar _finalized: has the case initialization been finished yet?
		Set by L{_finalize()}.
	@type _finalized: bool
	"""

	_finalized = False

	def _finalize(self):
		"""
		Do any final modifications to test case data. Mark it finalized.
		This function shall be called at most once per object.
		"""
		self._finalized = True

	@abstractmethod
	def get_output_files(self):
		"""
		Get a dict of files to output in the repository for the test case.

		@return: a dict where keys are file paths (relative to the repository
			root), and values evaluate to file contents
		@rtype: dict (string -> stringifiable)
		"""
		pass

	@abstractmethod
	def clean(self, pm):
		"""
		Schedule cleaning the test.

		@param pm: the package manager instance
		@type pm: L{PackageManager}
		"""
		pass

	@abstractmethod
	def start(self, pm):
		"""
		Schedule starting the test.

		@param pm: the package manager instance
		@type pm: L{PackageManager}
		"""
		pass

	@abstractmethod
	def check_result(self, pm):
		"""
		Check the correctness of the result of test execution.

		@param pm: the package manager instance
		@type pm: L{PackageManager}

		@return: True if the test succeeded, False otherwise
		@rtype: bool
		"""
		pass

class EbuildTestCase(TestCase):
	"""
	Test case using a single ebuild (per EAPI).

	The __init__() function is going to clone (deepcopy()) all the instance
	variables to allow modifying them easily.

	@ivar ebuild_vars: additional global variables
	@type ebuild_vars: dict (string -> string)
	@ivar expect_failure: if set to False (the default), the test is supposed
		to merge successfully. Otherwise, the test ebuild is supposed to fail
		to merge (die)
	@type expect_failure: bool
	@ivar inherits: additional eclasses to inherit
	@type inherits: list (string)
	@ivar phase_funcs: phase function contents
	@type phase_funcs: dict (string -> list(string))
	"""

	ebuild_vars = {}
	expect_failure = False
	inherits = []
	phase_funcs = {}

	@classmethod
	def _eval_prop(cls, prop_or_obj):
		"""
		Evaluate and return the value of a property when passed a property
		object, or return the passed value otherwise.

		@param prop_or_obj: the object to process
		@type prop_or_obj: property/object
		@return: value of the property
		@rtype: object
		"""

		if isinstance(prop_or_obj, property):
			return prop_or_obj.fget(cls)
		else:
			return prop_or_obj

	@property
	def supported_eapis(cls):
		"""
		A list of EAPIs for which the test is able to run and gives
		predictible results. Defaults to all available EAPIs.

		For example, if a feature is required in EAPI 3+ and optional
		in earlier EAPIs, this variable should contain (3,4).

		@type: iterable
		"""
		return (0, 1, 2, 3, 4)

	@property
	def relevant_eapis(cls):
		"""
		A list of EAPIs for which the test should be run by default (in
		non-thorough mode). This should list all the EAPIs where
		a change of behaviour occurs.

		When unset, defaults to a random element of .supported_eapis().

		@type: iterable
		"""
		return (random.choice(cls._eval_prop(cls.supported_eapis)),)

	@classmethod
	def inst_all(cls, thorough = False):
		"""
		Instantiate the test case for all relevant EAPIs. If in thorough
		mode, assume all supported EAPIs are relevant.

		@return: an iterable over test case instances
		@rtype: generator(L{EbuildTestCase})
		"""

		if thorough:
			eapis = cls.supported_eapis
		else:
			eapis = cls.relevant_eapis

		for eapi in cls._eval_prop(eapis):
			yield cls(eapi = eapi)

	@property
	def pn(self):
		return cleanup_test_case_name(self.__class__.__name__)

	@property
	def pv(self):
		return self.eapi

	@property
	def p(self):
		return '%s-%s' % (self.pn, self.pv)

	@property
	def cpv(self):
		""" Return CPV for the test. """
		return 'pms-test/%s' % self.p

	def atom(self, pm):
		""" Return atom for the test. """
		return pm.Atom('=%s' % self.cpv)

	@property
	def _stripped_docstring(self):
		descdoc = ' '.join(self.__doc__.split())
		return descdoc.rstrip('.')

	def _finalize(self):
		TestCase._finalize(self)

		if 'DESCRIPTION' not in self.ebuild_vars:
			self.ebuild_vars['DESCRIPTION'] = self._stripped_docstring

	def __str__(self):
		""" Return freetext test description. """
		return '%s (%s)' % (self.p, self._stripped_docstring)

	def __init__(self, eapi):
		"""
		Instiantate the test case for a particular EAPI.

		@param eapi: the EAPI
		@type eapi: string
		"""
		self.eapi = eapi

		for v in ('ebuild_vars', 'inherits', 'phase_funcs'):
			setattr(self, v, copy.deepcopy(getattr(self, v)))
		for pf in phase_func_names:
			if pf not in self.phase_funcs:
				self.phase_funcs[pf] = []

		# add KEYWORDS to the ebuild
		self.ebuild_vars['KEYWORDS'] = 'alpha amd64 arm hppa ia64 ' + \
				'm68k ~mips ppc ppc64 s390 sh sparc x86'

	def get_output_files(self):
		class EbuildTestCaseEbuildFile(object):
			""" Lazy ebuild contents evaluator for EbuildTestCase. """
			def __init__(self, parent):
				""" Instantiate the evaluator for test case <parent>. """
				assert(isinstance(parent, EbuildTestCase))
				self._parent = parent

			def __str__(self):
				""" Return the ebuild contents as string. """
				contents = [ebuild_header % (self._parent.eapi,
					' '.join(['pms-test'] + self._parent.inherits))]

				for k, v in self._parent.ebuild_vars.items():
					contents.append('%s="%s"\n' % (k, v)) # XXX: escaping

				for f, lines in self._parent.phase_funcs.items():
					if not lines:
						continue

					contents.append('\n%s() {\n' % f)
					for l in lines:
						contents.append('\t%s\n' % l) # XXX: smarter tabs
					contents.append('}\n')

				return ''.join(contents)

		if not self._finalized:
			self._finalize()

		fn = 'pms-test/%s/%s.ebuild' % (self.pn, self.p)

		return {fn: EbuildTestCaseEbuildFile(self)}

	def clean(self, pm):
		if self.atom(pm) in pm.installed:
			pm.unmerge(self.cpv)

	def start(self, pm):
		pm.merge(self.cpv)

	def check_result(self, pm):
		"""
		Check the correctness of the result of test execution. By default,
		checks whether the ebuild was actually merged.
		"""

		merged = self.atom(pm) in pm.installed
		return (merged != self.expect_failure)