-
Notifications
You must be signed in to change notification settings - Fork 119
/
noxfile.py
347 lines (267 loc) · 11.5 KB
/
noxfile.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
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Configuration for test sessions.
This is a configuration file for use with `nox <https://nox.thea.codes/>`__.
This configuration is modelled after the one for the `google-cloud-biguery
<https://github.com/googleapis/python-bigquery/blob/master/noxfile.py>`__
package.
"""
import os
import random
import nox
# Python version used for linting.
DEFAULT_PYTHON_VERSION = "3.10"
# Python versions used for testing. TODO: Add 3.11 and 3.12 when issue-1321 is complete.
PYTHON_VERSIONS = ["3.8", "3.9", "3.10"]
BLACK_PATHS = (
"data_validation",
"samples",
"tests",
"third_party",
"noxfile.py",
"setup.py",
)
LINT_PACKAGES = ["flake8", "black==22.3.0"]
UNIT_PACKAGES = ["pyfakefs==4.6.2", "freezegun"]
def _setup_session_requirements(session, extra_packages=[]):
"""Install requirements for nox tests."""
session.install("--upgrade", "pip", "pytest", "pytest-cov", "wheel")
session.install("--no-cache-dir", "-e", ".")
if extra_packages:
session.install(*extra_packages)
@nox.session(python=PYTHON_VERSIONS, venv_backend="venv")
def unit(session):
# Install all test dependencies, then install local packages in-place.
_setup_session_requirements(session, extra_packages=UNIT_PACKAGES)
# Run py.test against the unit tests.
session.run(
"py.test",
"--quiet",
"--cov=data_validation",
"--cov=tests.unit",
"--cov-append",
"--cov-config=.coveragerc",
"--cov-report=term",
os.path.join("tests", "unit"),
env={"PSO_DV_CONN_HOME": ""},
*session.posargs,
)
@nox.session(venv_backend="venv")
def unit_small(session):
unit(session)
@nox.session(python=PYTHON_VERSIONS, venv_backend="venv")
def samples(session):
"""Run the snippets test suite."""
# Sanity check: Only run snippets tests if the environment variable is set.
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
session.skip("Credentials must be set via environment variable.")
# Install all test dependencies, then install local packages in place.
_setup_session_requirements(session)
# Run pytest against the samples tests.
session.run("pytest", "samples", *session.posargs)
@nox.session(python=DEFAULT_PYTHON_VERSION, venv_backend="venv")
def lint(session):
"""Run linters.
Returns a failure if the linters find linting errors or sufficiently
serious code quality issues.
"""
_setup_session_requirements(session, extra_packages=LINT_PACKAGES)
session.install("--upgrade", "pip", "wheel")
session.run("flake8", "data_validation")
session.run("flake8", "tests")
session.run("black", "--check", *BLACK_PATHS)
session.run("python", "setup.py", "check", "--strict")
@nox.session(python=DEFAULT_PYTHON_VERSION, venv_backend="venv")
def blacken(session):
"""Run black.
Format code to uniform standard.
"""
# Pin a specific version of black, so that the linter doesn't conflict with
# contributors.
_setup_session_requirements(session, extra_packages=LINT_PACKAGES)
session.run("black", *BLACK_PATHS)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_mysql(session):
"""Run MySQL integration tests.
Ensure MySQL validation is running as expected.
"""
# Pin a specific version of black, so that the linter doesn't conflict with
# contributors.
_setup_session_requirements(session, extra_packages=[])
test_path = "tests/system/data_sources/test_mysql.py"
expected_env_vars = ["PROJECT_ID", "MYSQL_PASSWORD", "CLOUD_SQL_CONNECTION"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", test_path, *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_postgres(session):
"""Run Postgres integration tests.
Ensure Postgres validation is running as expected.
"""
# Pin a specific version of black, so that the linter doesn't conflict with
# contributors.
_setup_session_requirements(session, extra_packages=[])
test_path = "tests/system/data_sources/test_postgres.py"
expected_env_vars = ["PROJECT_ID", "POSTGRES_PASSWORD", "CLOUD_SQL_CONNECTION"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", test_path, *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_sql_server(session):
"""Run SQL Server integration tests.
Ensure SQL Server validation is running as expected.
"""
# Pin a specific version of black, so that the linter doesn't conflict with
# contributors.
_setup_session_requirements(session, extra_packages=["pyodbc"])
test_path = "tests/system/data_sources/test_sql_server.py"
expected_env_vars = ["PROJECT_ID", "SQL_SERVER_PASSWORD", "CLOUD_SQL_CONNECTION"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", test_path, *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_bigquery(session):
"""Run BigQuery integration tests.
Ensure BigQuery validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=[])
test_path = "tests/system/data_sources/test_bigquery.py"
env_vars = {"PROJECT_ID": os.environ.get("PROJECT_ID", "pso-kokoro-resources")}
for env_var in env_vars:
if not env_vars[env_var]:
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", test_path, env=env_vars, *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_spanner(session):
"""Run Spanner integration tests.
Ensure Spanner validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=[])
expected_env_vars = ["PROJECT_ID"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", "tests/system/data_sources/test_spanner.py", *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_teradata(session):
"""Run Teradata integration tests.
Ensure Teradata validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=["teradatasql"])
expected_env_vars = ["PROJECT_ID", "TERADATA_PASSWORD", "TERADATA_HOST"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run(
"pytest", "tests/system/data_sources/test_teradata.py", *session.posargs
)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_state(session):
"""Run StateManager integration tests.
Ensure the StateManager is running as expected.
"""
_setup_session_requirements(session, extra_packages=[])
test_path = "tests/system/test_state_manager.py"
session.run("pytest", test_path, *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_oracle(session):
"""Run Oracle integration tests.
Ensure Oracle validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=["cx_Oracle"])
expected_env_vars = [
"PROJECT_ID",
"ORACLE_PASSWORD",
"ORACLE_HOST",
"POSTGRES_PASSWORD",
"CLOUD_SQL_CONNECTION",
]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", "tests/system/data_sources/test_oracle.py", *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_hive(session):
"""Run Hive integration tests.
Ensure Hive validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=["PyHive", "hdfs"])
expected_env_vars = ["PROJECT_ID", "HIVE_HOST"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", "tests/system/data_sources/test_hive.py", *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_snowflake(session):
"""Run Snowflake integration tests.
Ensure Snowflake validation is running as expected.
"""
_setup_session_requirements(
session, extra_packages=["snowflake-sqlalchemy", "snowflake-connector-python"]
)
expected_env_vars = [
"PROJECT_ID",
"SNOWFLAKE_ACCOUNT",
"SNOWFLAKE_USER",
"SNOWFLAKE_PASSWORD",
]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run(
"pytest", "tests/system/data_sources/test_snowflake.py", *session.posargs
)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_db2(session):
"""Run DB2 integration tests.
Ensure DB2 validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=["ibm-db-sa"])
expected_env_vars = [
"PROJECT_ID",
"DB2_HOST",
"DB2_PASSWORD",
]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", "tests/system/data_sources/test_db2.py", *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_secrets(session):
"""
Run SecretManager integration tests.
Ensure the SecretManager is running as expected.
"""
_setup_session_requirements(session, extra_packages=[])
expected_env_vars = ["PROJECT_ID"]
for env_var in expected_env_vars:
if not os.environ.get(env_var, ""):
raise Exception("Expected Env Var: %s" % env_var)
test_path = "tests/system/test_secret_manager.py"
session.run("pytest", test_path, *session.posargs)
@nox.session(python=random.choice(PYTHON_VERSIONS), venv_backend="venv")
def integration_filesystem(session):
"""Run Filesystem integration tests.
Ensure Filesystem validation is running as expected.
"""
_setup_session_requirements(session, extra_packages=["gcsfs"])
test_path = "tests/system/data_sources/test_filesystem.py"
env_vars = {"PROJECT_ID": os.environ.get("PROJECT_ID", "pso-kokoro-resources")}
for env_var in env_vars:
if not env_vars[env_var]:
raise Exception("Expected Env Var: %s" % env_var)
session.run("pytest", test_path, env=env_vars, *session.posargs)