This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
conftest.py
425 lines (348 loc) · 12.4 KB
/
conftest.py
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python2
# Author(s): Milan Falesnik <mfalesni@redhat.com>
# James Laska <jlaska@redhat.com>
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2012 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing
# to use, modify, copy, or redistribute it subject to the terms
# and conditions of the GNU General Public License version 2.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY 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, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
""" This module contains generator functions for variable injecting of py.test framework.
Some of them are cached, some not. I don't know how to make multi-level dependency yet.
Update 2013-08-19: Cahnged to newer styled fixtures. Dependency now should work O.K. :)
"""
import pytest
import os
import re
import subprocess
import yaml
# This loads the PluginProxy, no need to write this inside tests
import plugins
from ConfigParser import ConfigParser
try:
import json
except ImportError:
import simplejson as json
def pytest_addoption(parser):
parser.addoption("--parametrize-file", action="store", default="default", help="Where to load parametrizing data from.")
parser.addoption("--RPM-skip-sig-check", action="store", default="no", help="Whether to skip RPM signature tests.")
@pytest.fixture(scope="session")
def parametrize_data(request):
with open("parametrized/%s.yaml" % request.config.getoption("--parametrize-file")) as input_file:
data = yaml.load(input_file)
return data
@pytest.fixture(scope="session")
def services_to_test(parametrize_data):
""" Reads all rules from file parametrized/services for purposes of parametrizing of the testing
:raises: ``IOError``
"""
services = parametrize_data.get("services", {})
for service in services:
for runlevel, enablement in services[service].iteritems():
yield service, runlevel, enablement
@pytest.fixture(scope="session")
def filesystem_ignore_patterns(parametrize_data):
return parametrize_data.get("filesystem", {}).get("ignore_patterns", [])
@pytest.fixture(scope="session")
def filesystem_world_writable_whitelist(parametrize_data):
return parametrize_data.get("filesystem", {}).get("world_writable_whitelist", [])
@pytest.fixture(scope="session")
def packages_must_be_installed(parametrize_data):
return parametrize_data.get("packages", {}).get("must_be_installed", [])
@pytest.fixture
def audreyvars():
"""Setups variables for testing
:returns: All Audrey-relevant environment variables.
:rtype: dict
"""
result = {}
for key in os.environ:
if key.startswith("AUDREY_VAR_KATELLO_REGISTER_"):
result[re.sub("^AUDREY_VAR_KATELLO_REGISTER_", "", key)] = os.environ[key]
return result
@pytest.fixture
def katello_discoverable(request):
"""Returns boolean (True of False) to indicate whether the provided katello
server is accessible
:param request: py.test.request
:returns: Accesibility of Katello server
:rtype: ``bool``
"""
cmd = "ping -q -c5 %s" % request.getfuncargvalue("audreyvars")["KATELLO_HOST"]
print "# %s" % cmd
return subprocess.call(cmd.split()) == 0
@pytest.fixture
def tunnel_requested(request):
"""Determines whether setting up SSH tunnel is requested
:param request: py.test request.
:returns: Whether was tunnel requested.
:rtype: ``bool``
"""
audreyvars = request.getfuncargvalue("audreyvars")
ec2_deployment = request.getfuncargvalue("ec2_deployment")
ssh = audreyvars.get("SSH_TUNNEL_ENABLED", "Auto")
# Did we ask for a tunnel to be setup
if ssh.lower() == "true":
return True
# Or if we are 'auto' and running in ec2
if ssh.lower() == "auto" and ec2_deployment:
return True
# Otherwise, it wasn't requested
return False
@pytest.fixture
def system_groups():
"""Determine applicable system groups for the current system
:returns: All Audrey-relevant environment variables.
:rtype: dict
"""
group_names = []
# A group for the current system platform (aka $basearch)
group_names.append(Test.Yum.get_yum_variable('basearch'))
# A group for the current system release (aka $releasever)
# Per katello rules, replace any non-alpha-numeric character with a '_'
group_names.append(re.sub(r'\W', '_', Test.Yum.get_yum_variable('releasever')))
# A group to indicate which provider the instance is deployed to
if is_rhev_deployment():
group_names.append('provider_rhev')
if is_vsphere_deployment():
group_names.append('provider_vsphere')
if ec2_deployment():
group_names.append('provider_ec2')
# Add a group name for the ec2 region
result = Test.Run.command('curl --fail http://169.254.169.254/latest/dynamic/instance-identity/document')
buf, rc = result.stdout, result.rc
if rc == 0:
ec2_data = json.loads(buf)
if ec2_data.has_key('region'):
group_names.append('ec2-%s' % ec2_data.get('region'))
return group_names
@pytest.fixture(scope="session")
def is_rhev_deployment():
"""Setups cached variable whether it's RHEV deployment or not.
:returns: Whether is this RHEV deployment (cached).
:rtype: ``bool``
"""
try:
Test.RPM.query('rhev-agent')
return True
except AssertionError:
pass
try:
assert Test.Run.bash("grep -qi rhev /sys/class/virtio-ports/*/name")
return True
except AssertionError:
pass
return False
@pytest.fixture(scope="session")
def is_vsphere_deployment():
"""Setups cached variable whether it's vsphere deployment or not.
:returns: Whether is this vsphere deployment (cached).
:rtype: ``bool``
"""
try:
Test.RPM.query('open-vm-tools')
return True
except AssertionError:
pass
try:
assert Test.Run.bash("grep -qi vmware /sys/bus/scsi/devices/*/vendor")
return True
except AssertionError:
pass
@pytest.fixture(scope="session")
def ec2_deployment():
"""Setups cached variable whether it's ec2 deployment or not.
:returns: Whether is this EC2 deployment (cached).
:rtype: ``bool``
"""
# The --fail curl argument will cause curl to exit with rc=22 if a server
# failure occurs (e.g. 403 or 404)
cmd = 'curl --fail http://169.254.169.254/latest/dynamic/instance-identity/document'
print "# %s" % cmd
return subprocess.call(cmd.split()) == 0
@pytest.fixture
def subscription_manager_version():
"""Setups cached variable of version of sub-man
:returns: SM version from cache
:rtype: 2-tuple
"""
sm_rpm_ver = Test.Run.bash("rpm -q --queryformat %{VERSION} subscription-manager")
assert sm_rpm_ver
sm_rpm_ver = sm_rpm_ver.stdout
sm_ver_maj, sm_ver_min, sm_ver_rest = sm_rpm_ver.split(".", 2)
return int(sm_ver_maj), int(sm_ver_min)
@pytest.fixture
def system_uuid():
""" Returns system UUID from subscription-manager
:returns: System UUID
:rtype: ``str``
"""
facts = Test.Run.command("subscription-manager facts --list")
assert facts
facts = facts.stdout.strip().split("\n")
for fact in facts:
name, value = fact.split(":", 1)
if name == "system.uuid":
return value.lstrip()
@pytest.fixture
def selinux_enabled():
""" Detects whether is SElinux enabled or not
:returns: SElinux status
:rtype: ``bool``
"""
return Test.SELinux.enabled
@pytest.fixture
def selinux_getenforce():
""" Returns current enforcing mode of SELinux
:returns: SElinux enforcing status
:rtype: ``str``
"""
return Test.SELinux.getenforce
@pytest.fixture
def selinux_getenforce_conf():
""" Returns current enforcing mode of SELinux from config file
:returns: SElinux enforcing status
:rtype: ``str``
"""
f = open("/etc/sysconfig/selinux", "r")
lines = []
for line in f.readlines():
if line.startswith("SELINUX="):
lines.append(line)
f.close()
# Check whether is only one
assert len(lines) == 1
return lines[0].split("=")[1].strip()
@pytest.fixture
def selinux_type():
""" Returns current SELINUX type from config file
:returns: SElinux type
:rtype: ``str``
"""
f = open("/etc/sysconfig/selinux", "r")
lines = []
for line in f.readlines():
if line.startswith("SELINUXTYPE="):
lines.append(line)
f.close()
# Check whether is only one
assert len(lines) == 1
return lines[0].split("=")[1].strip()
@pytest.fixture
def rpm_package_list():
""" Returns list of all installed packages in computer.
:returns: List of all installed packages in computer.
:rtype: ``list``
"""
return Test.RPM.query()
@pytest.fixture
def rpm_package_list_names():
""" Returns list of all installed packages in computer.
:returns: List of all installed packages in computer.
:rtype: ``list``
"""
return Test.RPM.query(format="%{NAME}")
@pytest.fixture
def rhel_release():
"""Returns RHEL version
:returns: RHEL version
:rtype: ``tuple``
"""
redhat_release_content = Test.Run.command("cat /etc/redhat-release")
assert redhat_release_content
release = None
if redhat_release_content.stdout.startswith("Fedora"):
release = [int(redhat_release_content.stdout.strip().split(" ")[2]), 0] # Major/minor (0)
release.append("Fedora")
else:
release = redhat_release_content.stdout.strip().split(" ")[6].split(".", 1)
release.append("RHEL")
class RedhatRelease(object):
def __init__(self, major, minor, distro="RHEL"):
self.major = int(major)
self.minor = int(minor)
self.distro = distro
# Compatibility layer
def __getitem__(self, position):
if position == 0:
return str(self.major)
elif position == 1:
return str(self.minor)
else:
raise KeyError("only 0 and 1 supported")
#return tuple(redhat_version_field.split(".", 1))
return RedhatRelease(*release)
@pytest.fixture
def PATH():
""" PATH environment variable
:returns: List of directories in $PATH
:rtype: ``list``
"""
return os.environ["PATH"].split(":")
@pytest.fixture
def gpgcheck_enabled():
""" Whether is GPG check enabled in yum
:returns: GPG check status
:rtype: ``bool``
"""
cfg = ConfigParser()
cfg.read(["/etc/yum.conf"])
return int(cfg.get("main", "gpgcheck")) == 1
@pytest.fixture
def chkconfig_list():
""" Returns list of all services with enablement in each runlevel
:returns: All services.
:rtype: ``dict``
"""
result = {}
chkconfig = Test.Run.command("chkconfig --list")
assert chkconfig
for line in chkconfig.stdout.strip().split("\n"):
line = re.sub("[[:blank:]]+", "\t", line)
fields = line.split("\t")
servicename = fields[0].strip()
fields = fields[1:]
result[servicename] = {}
for field in fields:
(runlevel, status) = field.split(":")
runlevel = int(runlevel)
if status.lower() == "on":
status = True
elif status.lower() == "off":
status = False
else:
pytest.fail(msg="Bad parsing of chkconfig --list")
result[servicename][runlevel] = status
return result
@pytest.fixture
def service_check():
""" Produces service-checking fixture """
class ServiceChecker(object):
def __init__(self, services):
self.services = services
def __call__(self, service, runlevel, active=True):
return common.services.service_active_in_runlevel(self.services, service, runlevel, active)
return ServiceChecker(chkconfig_list())
@pytest.fixture
def is_systemd():
"""
Checks for systemd presence
"""
try:
Test.RPM.query("systemd")
return True
except AssertionError:
return False