forked from interuss/dss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
231 lines (178 loc) · 7.03 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
import argparse
from typing import Callable, Optional
from monitoring.monitorlib.infrastructure import UTMClientSession, AsyncUTMTestSession
from monitoring.monitorlib import auth, rid, scd
from monitoring.prober.infrastructure import add_test_result, IDFactory, ResourceType, VersionString
import pytest
OPT_RID_AUTH = 'rid_auth'
OPT_RID_V2_AUTH = 'rid_v2_auth'
OPT_SCD_AUTH1 = 'scd_auth1'
OPT_SCD_AUTH2 = 'scd_auth2'
BASE_URL_RID = ''
BASE_URL_RID_V2 = '/rid/v2'
BASE_URL_SCD = '/dss/v1'
BASE_URL_AUX = '/aux/v1'
def str2bool(v) -> bool:
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def pytest_addoption(parser):
parser.addoption(
'--dss-endpoint',
help='Base URL of DSS to test',
metavar='URL',
dest='dss_endpoint')
parser.addoption(
'--rid-auth',
help='Auth spec (see Authorization section of README.md) for performing remote ID v1 actions in the DSS',
metavar='SPEC',
dest='rid_auth')
parser.addoption(
'--rid-v2-auth',
help='Auth spec (see Authorization section of README.md) for performing remote ID v2 actions in the DSS',
metavar='SPEC',
dest='rid_v2_auth')
parser.addoption(
'--scd-auth1',
help='Auth spec (see Authorization section of README.md) for performing primary strategic deconfliction actions in the DSS',
metavar='SPEC',
dest='scd_auth1')
parser.addoption(
'--scd-auth1-cp',
help='True if the USS specified in scd-auth1 has utm.constraint_processing privileges',
type=str2bool,
nargs='?',
default=True,
dest='scd_auth1_cp')
parser.addoption(
'--scd-auth1-cm',
help='True if the USS specified in scd-auth1 has utm.constraint_management privileges',
type=str2bool,
nargs='?',
default=True,
dest='scd_auth1_cm')
parser.addoption(
'--scd-auth2',
help='Auth spec (see Authorization section of README.md) for performing secondary strategic deconfliction actions (like observing primary actions, causing notification generation, etc) in the DSS',
metavar='SPEC',
dest='scd_auth2')
parser.addoption(
'--scd-api-version',
help='SCD API version to target',
choices=[scd.API_1_0_0],
default=scd.API_1_0_0,
dest='scd_api_version')
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
result = outcome.get_result()
if result.when == 'call':
add_test_result(item, result)
def make_session(pytestconfig, endpoint_suffix: str, auth_option: Optional[str] = None) -> Optional[UTMClientSession]:
dss_endpoint = pytestconfig.getoption('dss_endpoint')
if dss_endpoint is None:
pytest.skip('dss-endpoint option not set')
auth_adapter = None
if auth_option:
auth_spec = pytestconfig.getoption(auth_option)
if not auth_spec:
pytest.skip('%s option not set' % auth_option)
auth_adapter = auth.make_auth_adapter(auth_spec)
s = UTMClientSession(dss_endpoint + endpoint_suffix, auth_adapter)
return s
def make_session_async(pytestconfig, endpoint_suffix: str, auth_option: Optional[str] = None) -> Optional[AsyncUTMTestSession]:
dss_endpoint = pytestconfig.getoption('dss_endpoint')
if dss_endpoint is None:
pytest.skip('dss-endpoint option not set')
auth_adapter = None
if auth_option:
auth_spec = pytestconfig.getoption(auth_option)
if not auth_spec:
pytest.skip('%s option not set' % auth_option)
auth_adapter = auth.make_auth_adapter(auth_spec)
s = AsyncUTMTestSession(dss_endpoint + endpoint_suffix, auth_adapter)
return s
@pytest.fixture(scope='session')
def session_ridv1(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_RID, OPT_RID_AUTH)
@pytest.fixture(scope='session')
def session_ridv2(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_RID_V2, OPT_RID_V2_AUTH)
@pytest.fixture(scope='session')
def session_ridv1_async(pytestconfig):
session = make_session_async(pytestconfig, BASE_URL_RID, OPT_RID_AUTH)
yield session
session.close()
@pytest.fixture(scope='session')
def aux_session(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_AUX, OPT_RID_AUTH)
@pytest.fixture(scope='session')
def scd_session(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_SCD, OPT_SCD_AUTH1)
@pytest.fixture(scope='session')
def scd_session_async(pytestconfig):
session = make_session_async(pytestconfig, '/dss/v1', 'scd_auth1')
yield session
session.close()
@pytest.fixture(scope='session')
def scd_session_cp(pytestconfig) -> bool:
"""True iff SCD auth1 user is authorized for constraint processing"""
return pytestconfig.getoption('scd_auth1_cp')
@pytest.fixture(scope='session')
def scd_session_cm(pytestconfig) -> bool:
"""True iff SCD auth1 user is authorized for constraint management"""
return pytestconfig.getoption('scd_auth1_cm')
@pytest.fixture(scope='session')
def scd_session2(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_SCD, OPT_SCD_AUTH2)
@pytest.fixture()
def subscriber(pytestconfig) -> Optional[str]:
"""Subscriber of USS making UTM API calls"""
if pytestconfig.getoption(OPT_RID_AUTH):
session = make_session(pytestconfig, BASE_URL_RID, OPT_RID_AUTH)
session.get('/status', scope=rid.SCOPE_READ)
rid_sub = session.auth_adapter.get_sub()
if rid_sub:
return rid_sub
if pytestconfig.getoption(OPT_SCD_AUTH1):
scd_session = make_session(pytestconfig, BASE_URL_SCD, OPT_SCD_AUTH1)
scd_session.get('/status', scope=scd.SCOPE_SC)
scd_sub = scd_session.auth_adapter.get_sub()
if scd_sub:
return scd_sub
if pytestconfig.getoption(OPT_SCD_AUTH2):
scd_session2 = make_session(pytestconfig, BASE_URL_SCD, OPT_SCD_AUTH2)
scd_session2.get('/status', scope=scd.SCOPE_SC)
scd2_sub = scd_session2.auth_adapter.get_sub()
if scd2_sub:
return scd2_sub
return None
@pytest.fixture()
def ids(pytestconfig, subscriber) -> Callable[[ResourceType], str]:
"""Fixture that converts a ResourceType into an ID for that resource.
This fixture is a function that accepts a ResourceType as the argument and
returns a UUIDv4-format string containing an ID for that resource.
See register_resource_type in infrastructure.py for how to create
ResourceTypes to provide to this fixture, and also the "Resources" section of
the README.
"""
sub = subscriber
if sub is None:
sub = 'unknown'
factory = IDFactory(sub)
return lambda id_code: factory.make_id(id_code)
@pytest.fixture(scope='function')
def no_auth_session_ridv1(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_RID)
@pytest.fixture(scope='function')
def no_auth_session_ridv2(pytestconfig) -> UTMClientSession:
return make_session(pytestconfig, BASE_URL_RID_V2)
@pytest.fixture(scope='session')
def scd_api(pytestconfig) -> str:
api = pytestconfig.getoption('scd_api_version')
return VersionString(api)