-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconftest.py
163 lines (141 loc) · 5.41 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
import logging
from SCAutolib.utils import load_user, ipa_factory, load_token, \
ca_factory
from fixtures import *
from pathlib import Path
log = logging.getLogger("PyTest")
log.setLevel(logging.DEBUG)
ipa_user = None
ipa_server = None
local_user = None
tokens = None
def load_tokens(user, token_list, update_sssd):
log.info("Loading tokens")
for index, token in enumerate(token_list):
log.debug("Loading %s. token", index)
setattr(
user, f"card_{index}", load_token(token, update_sssd = update_sssd)
)
log.debug(f"Token %s is loaded", index)
def update_ca(user, token_list):
log.info("Loading local CA")
for index, token in enumerate(token_list):
card_name = f"card_{index}"
card = getattr(user, card_name, None)
ca = ca_factory(ca_name=card.ca_name)
ca.update_ca_db()
log.debug("CA database is updated")
def pytest_configure(config):
global ipa_user
global ipa_server
global local_user
global tokens
user_type = config.getoption("user_type")
tokens = config.getoption("tokens")
# workaround to set default token as parser.addoption defining tokens
# is a list that needs to be empty by default
if not tokens:
tokens = ["virt-card-1"]
if user_type in ["ipa", "all"]:
log.debug("Loading IPA client")
ipa_server = ipa_factory()
log.debug("IPA client is loaded")
log.debug("Loading IPA user")
ipa_user = load_user(
config.getoption("ipa_username"),
ipa_server=ipa_server)
assert ipa_user.user_type == "ipa"
log.debug("IPA user is loaded")
load_tokens(ipa_user, tokens, config.getoption("keep_sssd"))
ipa_user.card = ipa_user.card_0
ipa_user.pin = ipa_user.card.pin
if user_type in ["local", "all"]:
log.debug("Loading local user")
local_user = load_user(config.getoption(
"local_username"))
assert local_user.user_type == "local"
log.debug("Local user is loaded")
load_tokens(local_user, tokens, config.getoption("keep_sssd"))
# backwards compatibility fix. Older tests expected one virtual card
# as attribute of user - i.e. user.card and approached card this way.
# As of now we expect user can have multiple cards, they are marked
# user.card_0, user.card_1, ... however, for backwards compatibility,
# we need to provide user.card:
local_user.card = local_user.card_0
# pin used to be user attribute. as we can currently have multiple cards
# pin was moved to card. For backwards compatibility:
local_user.pin = local_user.card.pin
update_ca(local_user, tokens)
def pytest_addoption(parser):
"""
Specification of CLI options.
"""
parser.addoption(
"--ipa-username",
action="store",
default='ipa-user',
help="Username of IPA user to be used in tests",
dest="ipa_username"
)
parser.addoption(
"--local-username",
action="store",
default='local-user',
help="Username of local user to be used in tests",
dest="local_username"
)
parser.addoption(
"--with-user-type",
action="store",
default='local',
dest="user_type",
help="Type of user to be used in tests",
choices=['local', 'ipa', 'all']
)
parser.addoption(
"--with-tokens",
action="append",
default=[],
dest="tokens",
help="List of tokens to be prepared"
)
parser.addoption(
"--keep-sssd",
action="store_false",
dest="keep_sssd",
help="Prevents the forced change of sssd.conf"
)
def pytest_generate_tests(metafunc):
"""
Injecting fixtures into tests.
This function would set 'user' argument in test (if present) for users that
we want to test.
For example, if we want to execute the test only with local user, we
need to se `--with-user-type local` in pytest command. Default name for
local user is "local-user". If the system is configured to a user with
different name, set this name with `--local-username NAME` option.
Similar options are available for IPA user. If we want to test with both,
use `--with-user-type all`. This would set 'user' argument to both local
and IPA user using names specified by `--local-username` and
`--ipa-username` (or default names if not specified).
To avoid executing test that are not relevant for the user type (e.g. test
that is relevant only for local user), this test has to specify explicitly
user type by adding `local_user` argument instead of `user` argument.
Similar is true for `ipa_user` argument.
"""
user_type = metafunc.config.getoption("user_type")
if 'user' in metafunc.fixturenames:
users = []
if user_type in ["local", "all"]:
users.append(local_user)
if user_type in ["ipa", "all"]:
users.append(ipa_user)
metafunc.parametrize("user", users)
if 'ipa_user' in metafunc.fixturenames:
metafunc.parametrize("ipa_user", [ipa_user])
if 'local_user' in metafunc.fixturenames:
metafunc.parametrize("local_user", [local_user])
if "ipa_server" in metafunc.fixturenames:
metafunc.parametrize("ipa_server", [ipa_server])
if "tokens" in metafunc.fixturenames:
metafunc.parametrize("tokens", [tokens])