File size: 17,894 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import configparser
import os
import sys
import typing

import pytest  # type: ignore

import redshift_connector

conf = configparser.ConfigParser()
root_path = os.path.dirname(os.path.abspath(__file__))
conf.read(root_path + "/config.ini")


# @pytest.hookimpl(tryfirst=True, hookwrapper=True)
# def pytest_runtest_makereport(item, call):
#     # execute all other hooks to obtain the report object
#     outcome = yield
#     rep = outcome.get_result()
#
#     # we only look at actual failing test calls, not setup/teardown
#     if rep.when == "call" and rep.failed:
#         mode = "a" if os.path.exists("failures") else "w"
#         with open("failures", mode) as f:
#             f.write(rep.longreprtext + "\n")


def _get_default_connection_args() -> typing.Dict[str, typing.Union[str, bool, int]]:
    """
    Helper function defining default database connection parameter values.
    Returns
    -------

    """
    return {
        "database": conf.get("ci-cluster", "database", fallback="mock_database"),
        "host": conf.get("ci-cluster", "host", fallback="mock_host"),
        "port": conf.getint("default-test", "port", fallback="mock_port"),
        "user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("ci-cluster", "test_password", fallback="mock_test_password"),
        "ssl": conf.getboolean("default-test", "ssl", fallback="mock_ssl"),
        "sslmode": conf.get("default-test", "sslmode", fallback="mock_sslmode"),
        "region": conf.get("ci-cluster", "region", fallback="mock_region"),
        "cluster_identifier": conf.get("ci-cluster", "cluster_identifier", fallback="mock_cluster_identifier"),
    }


def _get_default_iam_connection_args() -> typing.Dict[str, typing.Union[str, bool, int]]:
    args = _get_default_connection_args()
    del args["host"]
    del args["port"]
    args["password"] = ""
    return args


@pytest.fixture(scope="class")
def db_kwargs() -> typing.Dict[str, typing.Union[str, bool, int]]:
    return _get_default_connection_args()


def db_groups() -> typing.List[str]:
    return conf.get("cluster-setup", "groups", fallback="mock_groups").split(sep=",")


@pytest.fixture(scope="class")
def perf_db_kwargs() -> typing.Dict[str, typing.Union[str, bool]]:
    db_connect = {
        "database": conf.get("performance-database", "database", fallback="mock_database"),
        "host": conf.get("performance-database", "host", fallback="mock_host"),
        "user": conf.get("performance-database", "user", fallback="mock_user"),
        "password": conf.get("performance-database", "password", fallback="mock_password"),
        "ssl": conf.getboolean("performance-database", "ssl", fallback="mock_ssl"),
        "sslmode": conf.get("performance-database", "sslmode", fallback="mock_sslmode"),
    }

    return db_connect


@pytest.fixture(scope="class")
def serverless_native_db_kwargs() -> typing.Dict[str, str]:
    db_connect = {
        "database": conf.get("redshift-serverless", "database", fallback="mock_database"),
        "host": conf.get(
            "redshift-serverless", "host", fallback="testwg1.012345678901.us-east-2.redshift-serverless.amazonaws.com"
        ),
        "user": conf.get("redshift-serverless", "user", fallback="mock_user"),
        "password": conf.get("redshift-serverless", "password", fallback="mock_password"),
    }

    return db_connect


@pytest.fixture(scope="class")
def serverless_iam_db_kwargs() -> typing.Dict[str, typing.Union[str, bool]]:
    db_connect = {
        "database": conf.get("redshift-serverless", "database", fallback="mock_database"),
        "iam": conf.getboolean("redshift-serverless", "iam", fallback=True),
        "access_key_id": conf.get("redshift-serverless", "access_key_id", fallback="mock_access_key_id"),
        "secret_access_key": conf.get("redshift-serverless", "secret_access_key", fallback="mock_secret_access_key"),
        "session_token": conf.get("redshift-serverless", "session_token", fallback="mock_session_token"),
        "host": conf.get(
            "redshift-serverless", "host", fallback="testwg1.012345678901.us-east-2.redshift-serverless.amazonaws.com"
        ),
    }

    return db_connect  # type: ignore


@pytest.fixture(scope="class")
def provisioned_cname_db_kwargs() -> typing.Dict[str, str]:
    db_connect = {
        "database": conf.get("redshift-provisioned-cname", "database", fallback="mock_database"),
        "host": conf.get("redshift-provisioned-cname", "host", fallback="cname.mytest.com"),
        "db_user": conf.get("redshift-provisioned-cname", "db_user", fallback="mock_user"),
        "password": conf.get("redshift-provisioned-cname", "password", fallback="mock_password"),
    }

    return db_connect


@pytest.fixture(scope="class")
def serverless_cname_db_kwargs() -> typing.Dict[str, typing.Union[str, bool]]:
    db_connect = {
        "database": conf.get("redshift-serverless-cname", "database", fallback="mock_database"),
        "host": conf.get("redshift-serverless-cname", "host", fallback="cname.mytest.com"),
        "db_user": conf.get("redshift-serverless-cname", "db_user", fallback="mock_user"),
        "password": conf.get("redshift-serverless-cname", "password", fallback="mock_password"),
        "is_serverless": conf.getboolean("redshift-serverless-cname", "is_serverless", fallback="mockboolean"),
    }

    return db_connect


@pytest.fixture(scope="class")
def ds_consumer_db_kwargs() -> typing.Dict[str, str]:
    db_connect = {
        "database": conf.get("redshift-ds-consumer", "database", fallback="mock_database"),
        "host": conf.get("redshift-ds-consumer", "host", fallback="cname.mytest.com"),
        "user": conf.get("redshift-ds-consumer", "user", fallback="mock_user"),
        "password": conf.get("redshift-ds-consumer", "password", fallback="mock_password"),
        "extra": conf.get("redshift-ds-consumer", "extra", fallback="mock_extra"),
    }

    return db_connect


@pytest.fixture(scope="class")
def ds_producer_db_kwargs() -> typing.Dict[str, str]:
    db_connect = {
        "database": conf.get("redshift-ds-producer", "database", fallback="mock_database"),
        "host": conf.get("redshift-ds-producer", "host", fallback="cname.mytest.com"),
        "user": conf.get("redshift-ds-producer", "user", fallback="mock_user"),
        "password": conf.get("redshift-ds-producer", "password", fallback="mock_password"),
    }

    return db_connect


@pytest.fixture(scope="class")
def okta_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("okta-idp", "password", fallback="mock_password"),
        "iam": conf.getboolean("okta-idp", "iam", fallback="mock_iam"),
        "idp_host": conf.get("okta-idp", "idp_host", fallback="mock_idp_host"),
        "user": conf.get("okta-idp", "user", fallback="mock_user"),
        "app_id": conf.get("okta-idp", "app_id", fallback="mock_app_id"),
        "app_name": conf.get("okta-idp", "app_name", fallback="mock_app_name"),
        "credentials_provider": conf.get("okta-idp", "credentials_provider", fallback="OktaCredentialsProvider"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def okta_browser_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("okta-browser-idp", "password", fallback="mock_password"),
        "iam": conf.getboolean("okta-browser-idp", "iam", fallback="mock_iam"),
        "user": conf.get("okta-browser-idp", "user", fallback="mock_user"),
        "credentials_provider": conf.get(
            "okta-browser-idp", "credentials_provider", fallback="SamlCredentialsProvider"
        ),
        "login_url": conf.get("okta-browser-idp", "login_url", fallback="mock_login_url"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def azure_browser_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("azure-browser-idp", "password", fallback="mock_password"),
        "iam": conf.getboolean("azure-browser-idp", "iam", fallback="mock_iam"),
        "user": conf.get("azure-browser-idp", "user", fallback="mock_user"),
        "credentials_provider": conf.get(
            "azure-browser-idp", "credentials_provider", fallback="BrowserAzureCredentialsProvider"
        ),
        "idp_tenant": conf.get("azure-browser-idp", "idp_tenant", fallback="mock_idp_tenant"),
        "client_id": conf.get("azure-browser-idp", "client_id", fallback="mock_client_id"),
        "client_secret": conf.get("azure-browser-idp", "client_secret", fallback="mock_client_secret"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def jumpcloud_browser_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("jumpcloud-browser-idp", "password", fallback="mock_password"),
        "iam": conf.getboolean("jumpcloud-browser-idp", "iam", fallback="mock_iam"),
        "user": conf.get("jumpcloud-browser-idp", "user", fallback="mock_user"),
        "credentials_provider": conf.get(
            "jumpcloud-browser-idp", "credentials_provider", fallback="SamlCredentialsProvider"
        ),
        "login_url": conf.get("jumpcloud-browser-idp", "login_url", fallback="mock_login_url"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def ping_browser_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "iam": conf.getboolean("ping-one-idp", "iam", fallback="mock_iam"),
        "credentials_provider": conf.get("ping-one-idp", "credentials_provider", fallback="PingCredentialsProvider"),
        "login_url": conf.get("ping-one-idp", "login_url", fallback="mock_login_url"),
        "listen_port": conf.getint("ping-one-idp", "listen_port", fallback="mock_listen_port"),
        "idp_response_timeout": conf.getint(
            "ping-one-idp", "idp_response_timeout", fallback="mock_idp_response_timeout"
        ),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def azure_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("azure-idp", "password", fallback="mock_password"),
        "iam": conf.getboolean("azure-idp", "iam", fallback="mock_iam"),
        "user": conf.get("azure-idp", "user", fallback="mock_user"),
        "credentials_provider": conf.get("azure-idp", "credentials_provider", fallback="AzureCredentialsProvider"),
        "idp_tenant": conf.get("azure-idp", "idp_tenant", fallback="mock_idp_tenant"),
        "client_id": conf.get("azure-idp", "client_id", fallback="mock_client_id"),
        "client_secret": conf.get("azure-idp", "client_secret", fallback="mock_client_secret"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def adfs_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "password": conf.get("adfs-idp", "password", fallback="mock_password"),
        "iam": conf.getboolean("adfs-idp", "iam", fallback="mock_iam"),
        "user": conf.get("adfs-idp", "user", fallback="mock_user"),
        "credentials_provider": conf.get("adfs-idp", "credentials_provider", fallback="AdfsCredentialsProvider"),
        "idp_host": conf.get("adfs-idp", "idp_host", fallback="mock_idp_host"),
        "cluster_identifier": conf.get("adfs-idp", "cluster_identifier", fallback="mock_adfs_cluster_identifier"),
        "region": conf.get("adfs-idp", "region", fallback="mock-region"),
        "ssl_insecure": conf.getboolean("adfs-idp", "ssl_insecure", fallback=False),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def jwt_google_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "iam": conf.getboolean("jwt-google-idp", "iam", fallback="mock_iam"),
        "password": conf.get("jwt-google-idp", "password", fallback="mock_password"),
        "credentials_provider": conf.get(
            "jwt-google-idp", "credentials_provider", fallback="BasicJwtCredentialsProvider"
        ),
        "web_identity_token": conf.get("jwt-google-idp", "web_identity_token", fallback="mock_web_identity_token"),
        "role_arn": conf.get("jwt-google-idp", "role_arn", fallback="mock_role_arn"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def jwt_azure_v2_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "db_user": conf.get("ci-cluster", "test_user", fallback="mock_test_user"),
        "iam": conf.getboolean("jwt-azure-v2-idp", "iam", fallback="mock_iam"),
        "password": conf.get("jwt-azure-v2-idp", "password", fallback="mock_password"),
        "credentials_provider": conf.get(
            "jwt-azure-v2-idp", "credentials_provider", fallback="BasicJwtCredentialsProvider"
        ),
        "web_identity_token": conf.get("jwt-azure-v2-idp", "web_identity_token", fallback="mock_web_identity_token"),
        "role_arn": conf.get("jwt-azure-v2-idp", "role_arn", fallback="mock_role_arn"),
    }
    return {**_get_default_iam_connection_args(), **db_connect}


@pytest.fixture(scope="class")
def redshift_native_browser_azure_oauth2_idp() -> typing.Dict[str, typing.Union[str, bool, int]]:
    db_connect = {
        "host": conf.get("redshift-native-browser-azure-oauth2", "host", fallback="mock_host"),
        "port": conf.getint("default-test", "port", fallback="mock_port"),
        "database": conf.get("ci-cluster", "database", fallback="mock_database"),
        "credentials_provider": conf.get(
            "redshift-native-browser-azure-oauth2", "credentials_provider", fallback="BasicJwtCredentialsProvider"
        ),
        "scope": conf.get("redshift-native-browser-azure-oauth2", "scope", fallback="mock_scope"),
        "client_id": conf.get("redshift-native-browser-azure-oauth2", "client_id", fallback="mock_client_id"),
        "idp_tenant": conf.get("redshift-native-browser-azure-oauth2", "idp_tenant", fallback="mock_idp_tenant"),
        "cluster_identifier": conf.get("adfs-idp", "cluster_identifier", fallback="mock_adfs_cluster_identifier"),
        "region": conf.get("adfs-idp", "region", fallback="mock-region"),
        "iam": conf.getboolean("jwt-google-idp", "iam", fallback="mock_iam"),
    }
    return db_connect


@pytest.fixture(scope="class")
def redshift_idp_token_auth_plugin() -> typing.Dict[str, typing.Optional[str]]:
    db_connect = {
        "host": conf.get("redshift-idp-token-auth-plugin", "host", fallback=None),
        "region": conf.get("redshift-idp-token-auth-plugin", "region", fallback=None),
        "database": conf.get("redshift-idp-token-auth-plugin", "database", fallback="dev"),
        "credentials_provider": conf.get(
            "redshift-idp-token-auth-plugin", "credentials_provider", fallback="IdpTokenAuthPlugin"
        ),
        "token": conf.get("redshift-idp-token-auth-plugin", "token", fallback=None),
        "token_type": conf.get("redshift-idp-token-auth-plugin", "token_type", fallback=None),
        "identity_namespace": conf.get("redshift-idp-token-auth-plugin", "identity_namespace", fallback=None),
    }
    return db_connect


@pytest.fixture(scope="class")
def redshift_browser_idc() -> typing.Dict[str, typing.Union[str, typing.Optional[bool], int]]:
    db_connect = {
        "host": conf.get("redshift-browser-idc", "host", fallback=None),
        "region": conf.get("redshift-browser-idc", "region", fallback=None),
        "database": conf.get("redshift-browser-idc", "database", fallback="dev"),
        "credentials_provider": conf.get(
            "redshift-browser-idc", "credentials_provider", fallback="BrowserIdcAuthPlugin"
        ),
        "issuer_url": conf.get("redshift-browser-idc", "issuer_url", fallback=None),
        "idc_region": conf.get("redshift-browser-idc", "idc_region", fallback=None),
        "idp_response_timeout": conf.getint("redshift-browser-idc", "idp_response_timeout", fallback=120),
        "listen_port": conf.get("redshift-browser-idc", "listen_port", fallback=7890),
        "idc_client_display_name": conf.get(
            "redshift-browser-idc", "idc_client_display_name", fallback="Amazon Redshift Python connector"
        ),
    }
    return db_connect


@pytest.fixture
def con(request, db_kwargs) -> redshift_connector.Connection:
    conn: redshift_connector.Connection = redshift_connector.connect(**db_kwargs)

    def fin() -> None:
        conn.rollback()
        try:
            conn.close()
        except redshift_connector.InterfaceError:
            pass

    request.addfinalizer(fin)
    return conn


@pytest.fixture
def cursor(request, con) -> redshift_connector.Cursor:
    cursor: redshift_connector.Cursor = con.cursor()

    def fin() -> None:
        cursor.close()

    request.addfinalizer(fin)
    return cursor


@pytest.fixture
def idp_arg(request) -> typing.Dict[str, typing.Union[str, bool, int]]:
    return request.getfixturevalue(request.param)


@pytest.fixture
def is_java() -> bool:
    return "java" in sys.platform.lower()