X Tutup
Skip to content

Commit b2eda32

Browse files
ci: clean up env between tests with working_dir (spack#29807)
1 parent ff33978 commit b2eda32

File tree

2 files changed

+70
-98
lines changed

2 files changed

+70
-98
lines changed

lib/spack/spack/test/ci.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ def test_read_write_cdash_ids(config, tmp_scope, tmpdir, mock_packages):
264264
assert(str(read_cdashid) == orig_cdashid)
265265

266266

267-
def test_download_and_extract_artifacts(tmpdir, monkeypatch):
268-
os.environ['GITLAB_PRIVATE_TOKEN'] = 'faketoken'
267+
def test_download_and_extract_artifacts(tmpdir, monkeypatch, working_env):
268+
os.environ.update({
269+
'GITLAB_PRIVATE_TOKEN': 'faketoken',
270+
})
269271

270272
url = 'https://www.nosuchurlexists.itsfake/artifacts.zip'
271273
working_dir = os.path.join(tmpdir.strpath, 'repro')

lib/spack/spack/test/cmd/ci.py

Lines changed: 66 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,8 @@
4646

4747

4848
@pytest.fixture()
49-
def project_dir_env():
50-
def _set_project_dir(path):
51-
os.environ['CI_PROJECT_DIR'] = path
52-
53-
yield _set_project_dir
54-
55-
if 'CI_PROJECT_DIR' in os.environ:
56-
os.environ.pop('CI_PROJECT_DIR')
57-
58-
59-
def set_env_var(key, val):
60-
os.environ[key] = val
49+
def ci_base_environment(working_env, tmpdir):
50+
os.environ['CI_PROJECT_DIR'] = tmpdir.strpath
6151

6252

6353
def test_specs_staging(config):
@@ -120,11 +110,10 @@ def test_specs_staging(config):
120110

121111

122112
def test_ci_generate_with_env(tmpdir, mutable_mock_env_path,
123-
install_mockery, mock_packages, project_dir_env,
124-
mock_binary_index):
113+
install_mockery, mock_packages,
114+
ci_base_environment, mock_binary_index):
125115
"""Make sure we can get a .gitlab-ci.yml from an environment file
126116
which has the gitlab-ci, cdash, and mirrors sections."""
127-
project_dir_env(tmpdir.strpath)
128117
mirror_url = 'https://my.fake.mirror'
129118
filename = str(tmpdir.join('spack.yaml'))
130119
with open(filename, 'w') as f:
@@ -217,10 +206,9 @@ def _validate_needs_graph(yaml_contents, needs_graph, artifacts):
217206

218207
def test_ci_generate_bootstrap_gcc(tmpdir, mutable_mock_env_path,
219208
install_mockery,
220-
mock_packages, project_dir_env):
209+
mock_packages, ci_base_environment):
221210
"""Test that we can bootstrap a compiler and use it as the
222211
compiler for a spec in the environment"""
223-
project_dir_env(tmpdir.strpath)
224212
filename = str(tmpdir.join('spack.yaml'))
225213
with open(filename, 'w') as f:
226214
f.write("""\
@@ -281,10 +269,9 @@ def test_ci_generate_bootstrap_artifacts_buildcache(tmpdir,
281269
mutable_mock_env_path,
282270
install_mockery,
283271
mock_packages,
284-
project_dir_env):
272+
ci_base_environment):
285273
"""Test that we can bootstrap a compiler when artifacts buildcache
286274
is turned on"""
287-
project_dir_env(tmpdir.strpath)
288275
filename = str(tmpdir.join('spack.yaml'))
289276
with open(filename, 'w') as f:
290277
f.write("""\
@@ -346,10 +333,9 @@ def test_ci_generate_bootstrap_artifacts_buildcache(tmpdir,
346333

347334
def test_ci_generate_with_env_missing_section(tmpdir, mutable_mock_env_path,
348335
install_mockery,
349-
mock_packages, project_dir_env,
336+
mock_packages, ci_base_environment,
350337
mock_binary_index):
351338
"""Make sure we get a reasonable message if we omit gitlab-ci section"""
352-
project_dir_env(tmpdir.strpath)
353339
filename = str(tmpdir.join('spack.yaml'))
354340
with open(filename, 'w') as f:
355341
f.write("""\
@@ -372,10 +358,12 @@ def test_ci_generate_with_env_missing_section(tmpdir, mutable_mock_env_path,
372358

373359
def test_ci_generate_with_cdash_token(tmpdir, mutable_mock_env_path,
374360
install_mockery,
375-
mock_packages, project_dir_env,
361+
mock_packages, ci_base_environment,
376362
mock_binary_index):
377363
"""Make sure we it doesn't break if we configure cdash"""
378-
project_dir_env(tmpdir.strpath)
364+
os.environ.update({
365+
'SPACK_CDASH_AUTH_TOKEN': 'notreallyatokenbutshouldnotmatter',
366+
})
379367
filename = str(tmpdir.join('spack.yaml'))
380368
with open(filename, 'w') as f:
381369
f.write("""\
@@ -404,13 +392,8 @@ def test_ci_generate_with_cdash_token(tmpdir, mutable_mock_env_path,
404392
env_cmd('create', 'test', './spack.yaml')
405393

406394
with ev.read('test'):
407-
fake_token = 'notreallyatokenbutshouldnotmatter'
408-
os.environ['SPACK_CDASH_AUTH_TOKEN'] = fake_token
409395
copy_to_file = str(tmpdir.join('backup-ci.yml'))
410-
try:
411-
output = ci_cmd('generate', '--copy-to', copy_to_file, output=str)
412-
finally:
413-
del os.environ['SPACK_CDASH_AUTH_TOKEN']
396+
output = ci_cmd('generate', '--copy-to', copy_to_file, output=str)
414397
# That fake token should still have resulted in being unable to
415398
# register build group with cdash, but the workload should
416399
# still have been generated.
@@ -429,9 +412,8 @@ def test_ci_generate_with_cdash_token(tmpdir, mutable_mock_env_path,
429412
def test_ci_generate_with_custom_scripts(tmpdir, mutable_mock_env_path,
430413
install_mockery,
431414
mock_packages, monkeypatch,
432-
project_dir_env, mock_binary_index):
415+
ci_base_environment, mock_binary_index):
433416
"""Test use of user-provided scripts"""
434-
project_dir_env(tmpdir.strpath)
435417
filename = str(tmpdir.join('spack.yaml'))
436418
with open(filename, 'w') as f:
437419
f.write("""\
@@ -519,9 +501,8 @@ def test_ci_generate_with_custom_scripts(tmpdir, mutable_mock_env_path,
519501

520502
def test_ci_generate_pkg_with_deps(tmpdir, mutable_mock_env_path,
521503
install_mockery,
522-
mock_packages, project_dir_env):
504+
mock_packages, ci_base_environment):
523505
"""Test pipeline generation for a package w/ dependencies"""
524-
project_dir_env(tmpdir.strpath)
525506
filename = str(tmpdir.join('spack.yaml'))
526507
with open(filename, 'w') as f:
527508
f.write("""\
@@ -574,11 +555,14 @@ def test_ci_generate_pkg_with_deps(tmpdir, mutable_mock_env_path,
574555
def test_ci_generate_for_pr_pipeline(tmpdir, mutable_mock_env_path,
575556
install_mockery,
576557
mock_packages, monkeypatch,
577-
project_dir_env):
558+
ci_base_environment):
578559
"""Test that PR pipelines do not include a final stage job for
579560
rebuilding the mirror index, even if that job is specifically
580561
configured"""
581-
project_dir_env(tmpdir.strpath)
562+
os.environ.update({
563+
'SPACK_PIPELINE_TYPE': 'spack_pull_request',
564+
'SPACK_PR_BRANCH': 'fake-test-branch',
565+
})
582566
filename = str(tmpdir.join('spack.yaml'))
583567
with open(filename, 'w') as f:
584568
f.write("""\
@@ -611,17 +595,11 @@ def test_ci_generate_for_pr_pipeline(tmpdir, mutable_mock_env_path,
611595
outputfile = str(tmpdir.join('.gitlab-ci.yml'))
612596

613597
with ev.read('test'):
614-
os.environ['SPACK_PIPELINE_TYPE'] = 'spack_pull_request'
615-
os.environ['SPACK_PR_BRANCH'] = 'fake-test-branch'
616598
monkeypatch.setattr(
617599
ci, 'SPACK_PR_MIRRORS_ROOT_URL', r"file:///fake/mirror")
618600
monkeypatch.setattr(
619601
ci, 'SPACK_SHARED_PR_MIRROR_URL', r"file:///fake/mirror_two")
620-
try:
621-
ci_cmd('generate', '--output-file', outputfile)
622-
finally:
623-
del os.environ['SPACK_PIPELINE_TYPE']
624-
del os.environ['SPACK_PR_BRANCH']
602+
ci_cmd('generate', '--output-file', outputfile)
625603

626604
with open(outputfile) as f:
627605
contents = f.read()
@@ -638,9 +616,8 @@ def test_ci_generate_for_pr_pipeline(tmpdir, mutable_mock_env_path,
638616
def test_ci_generate_with_external_pkg(tmpdir, mutable_mock_env_path,
639617
install_mockery,
640618
mock_packages, monkeypatch,
641-
project_dir_env):
619+
ci_base_environment):
642620
"""Make sure we do not generate jobs for external pkgs"""
643-
project_dir_env(tmpdir.strpath)
644621
filename = str(tmpdir.join('spack.yaml'))
645622
with open(filename, 'w') as f:
646623
f.write("""\
@@ -682,9 +659,8 @@ def test_ci_generate_with_external_pkg(tmpdir, mutable_mock_env_path,
682659
@pytest.mark.xfail(reason='fails intermittently and covered by gitlab ci')
683660
def test_ci_rebuild(tmpdir, mutable_mock_env_path,
684661
install_mockery, mock_packages, monkeypatch,
685-
mock_gnupghome, mock_fetch, project_dir_env,
662+
mock_gnupghome, mock_fetch, ci_base_environment,
686663
mock_binary_index):
687-
project_dir_env(tmpdir.strpath)
688664
working_dir = tmpdir.join('working_dir')
689665

690666
log_dir = os.path.join(working_dir.strpath, 'logs')
@@ -778,23 +754,25 @@ def fake_cdash_register(build_name, base_url, project, site, track):
778754
env_cmd('activate', '--without-view', '--sh', '-d', '.')
779755

780756
# Create environment variables as gitlab would do it
781-
set_env_var('SPACK_ARTIFACTS_ROOT', working_dir.strpath)
782-
set_env_var('SPACK_JOB_LOG_DIR', log_dir)
783-
set_env_var('SPACK_JOB_REPRO_DIR', repro_dir)
784-
set_env_var('SPACK_LOCAL_MIRROR_DIR', mirror_dir.strpath)
785-
set_env_var('SPACK_CONCRETE_ENV_DIR', env_dir.strpath)
786-
set_env_var('CI_PIPELINE_ID', '7192')
787-
set_env_var('SPACK_SIGNING_KEY', signing_key)
788-
set_env_var('SPACK_ROOT_SPEC', root_spec_build_hash)
789-
set_env_var('SPACK_JOB_SPEC_DAG_HASH', job_spec_dag_hash)
790-
set_env_var('SPACK_JOB_SPEC_PKG_NAME', 'archive-files')
791-
set_env_var('SPACK_COMPILER_ACTION', 'NONE')
792-
set_env_var('SPACK_CDASH_BUILD_NAME', '(specs) archive-files')
793-
set_env_var('SPACK_RELATED_BUILDS_CDASH', '')
794-
set_env_var('SPACK_REMOTE_MIRROR_URL', mirror_url)
795-
set_env_var('SPACK_PIPELINE_TYPE', 'spack_protected_branch')
796-
set_env_var('CI_JOB_URL', ci_job_url)
797-
set_env_var('CI_PIPELINE_URL', ci_pipeline_url)
757+
os.environ.update({
758+
'SPACK_ARTIFACTS_ROOT': working_dir.strpath,
759+
'SPACK_JOB_LOG_DIR': log_dir,
760+
'SPACK_JOB_REPRO_DIR': repro_dir,
761+
'SPACK_LOCAL_MIRROR_DIR': mirror_dir.strpath,
762+
'SPACK_CONCRETE_ENV_DIR': env_dir.strpath,
763+
'CI_PIPELINE_ID': '7192',
764+
'SPACK_SIGNING_KEY': signing_key,
765+
'SPACK_ROOT_SPEC': root_spec_build_hash,
766+
'SPACK_JOB_SPEC_DAG_HASH': job_spec_dag_hash,
767+
'SPACK_JOB_SPEC_PKG_NAME': 'archive-files',
768+
'SPACK_COMPILER_ACTION': 'NONE',
769+
'SPACK_CDASH_BUILD_NAME': '(specs) archive-files',
770+
'SPACK_RELATED_BUILDS_CDASH': '',
771+
'SPACK_REMOTE_MIRROR_URL': mirror_url,
772+
'SPACK_PIPELINE_TYPE': 'spack_protected_branch',
773+
'CI_JOB_URL': ci_job_url,
774+
'CI_PIPELINE_URL': ci_pipeline_url,
775+
})
798776

799777
ci_cmd('rebuild', fail_on_error=False)
800778

@@ -841,8 +819,7 @@ def mystrip(s):
841819

842820
def test_ci_nothing_to_rebuild(tmpdir, mutable_mock_env_path,
843821
install_mockery, mock_packages, monkeypatch,
844-
mock_fetch, project_dir_env, mock_binary_index):
845-
project_dir_env(tmpdir.strpath)
822+
mock_fetch, ci_base_environment, mock_binary_index):
846823
working_dir = tmpdir.join('working_dir')
847824

848825
mirror_dir = working_dir.join('mirror')
@@ -888,16 +865,18 @@ def test_ci_nothing_to_rebuild(tmpdir, mutable_mock_env_path,
888865
job_spec_dag_hash = s.dag_hash()
889866

890867
# Create environment variables as gitlab would do it
891-
set_env_var('SPACK_ARTIFACTS_ROOT', working_dir.strpath)
892-
set_env_var('SPACK_JOB_LOG_DIR', 'log_dir')
893-
set_env_var('SPACK_JOB_REPRO_DIR', 'repro_dir')
894-
set_env_var('SPACK_LOCAL_MIRROR_DIR', mirror_dir.strpath)
895-
set_env_var('SPACK_CONCRETE_ENV_DIR', tmpdir.strpath)
896-
set_env_var('SPACK_ROOT_SPEC', root_spec_build_hash)
897-
set_env_var('SPACK_JOB_SPEC_DAG_HASH', job_spec_dag_hash)
898-
set_env_var('SPACK_JOB_SPEC_PKG_NAME', 'archive-files')
899-
set_env_var('SPACK_COMPILER_ACTION', 'NONE')
900-
set_env_var('SPACK_REMOTE_MIRROR_URL', mirror_url)
868+
os.environ.update({
869+
'SPACK_ARTIFACTS_ROOT': working_dir.strpath,
870+
'SPACK_JOB_LOG_DIR': 'log_dir',
871+
'SPACK_JOB_REPRO_DIR': 'repro_dir',
872+
'SPACK_LOCAL_MIRROR_DIR': mirror_dir.strpath,
873+
'SPACK_CONCRETE_ENV_DIR': tmpdir.strpath,
874+
'SPACK_ROOT_SPEC': root_spec_build_hash,
875+
'SPACK_JOB_SPEC_DAG_HASH': job_spec_dag_hash,
876+
'SPACK_JOB_SPEC_PKG_NAME': 'archive-files',
877+
'SPACK_COMPILER_ACTION': 'NONE',
878+
'SPACK_REMOTE_MIRROR_URL': mirror_url,
879+
})
901880

902881
def fake_dl_method(spec, *args, **kwargs):
903882
print('fake download buildcache {0}'.format(spec.name))
@@ -917,8 +896,7 @@ def fake_dl_method(spec, *args, **kwargs):
917896
def test_push_mirror_contents(tmpdir, mutable_mock_env_path,
918897
install_mockery_mutable_config, mock_packages,
919898
mock_fetch, mock_stage, mock_gnupghome,
920-
project_dir_env):
921-
project_dir_env(tmpdir.strpath)
899+
ci_base_environment):
922900
working_dir = tmpdir.join('working_dir')
923901

924902
mirror_dir = working_dir.join('mirror')
@@ -1083,12 +1061,11 @@ def failing_access(*args, **kwargs):
10831061
def test_ci_generate_override_runner_attrs(tmpdir, mutable_mock_env_path,
10841062
install_mockery,
10851063
mock_packages, monkeypatch,
1086-
project_dir_env):
1064+
ci_base_environment):
10871065
"""Test that we get the behavior we want with respect to the provision
10881066
of runner attributes like tags, variables, and scripts, both when we
10891067
inherit them from the top level, as well as when we override one or
10901068
more at the runner level"""
1091-
project_dir_env(tmpdir.strpath)
10921069
filename = str(tmpdir.join('spack.yaml'))
10931070
with open(filename, 'w') as f:
10941071
f.write("""\
@@ -1228,9 +1205,8 @@ def test_ci_generate_override_runner_attrs(tmpdir, mutable_mock_env_path,
12281205
def test_ci_generate_with_workarounds(tmpdir, mutable_mock_env_path,
12291206
install_mockery,
12301207
mock_packages, monkeypatch,
1231-
project_dir_env):
1208+
ci_base_environment):
12321209
"""Make sure the post-processing cli workarounds do what they should"""
1233-
project_dir_env(tmpdir.strpath)
12341210
filename = str(tmpdir.join('spack.yaml'))
12351211
with open(filename, 'w') as f:
12361212
f.write("""\
@@ -1331,14 +1307,13 @@ def test_ci_rebuild_index(tmpdir, mutable_mock_env_path,
13311307
def test_ci_generate_bootstrap_prune_dag(
13321308
install_mockery_mutable_config, mock_packages, mock_fetch,
13331309
mock_archive, mutable_config, monkeypatch, tmpdir,
1334-
mutable_mock_env_path, project_dir_env):
1310+
mutable_mock_env_path, ci_base_environment):
13351311
"""Test compiler bootstrapping with DAG pruning. Specifically, make
13361312
sure that if we detect the bootstrapped compiler needs to be rebuilt,
13371313
we ensure the spec we want to build with that compiler is scheduled
13381314
for rebuild as well."""
13391315

13401316
# Create a temp mirror directory for buildcache usage
1341-
project_dir_env(tmpdir.strpath)
13421317
mirror_dir = tmpdir.join('mirror_dir')
13431318
mirror_url = 'file://{0}'.format(mirror_dir.strpath)
13441319

@@ -1464,10 +1439,12 @@ def fake_get_mirrors_for_spec(spec=None, full_hash_match=False,
14641439

14651440
def test_ci_generate_prune_untouched(tmpdir, mutable_mock_env_path,
14661441
install_mockery, mock_packages,
1467-
project_dir_env, monkeypatch):
1442+
ci_base_environment, monkeypatch):
14681443
"""Test pipeline generation with pruning works to eliminate
14691444
specs that were not affected by a change"""
1470-
project_dir_env(tmpdir.strpath)
1445+
os.environ.update({
1446+
'SPACK_PRUNE_UNTOUCHED': 'TRUE', # enables pruning of untouched specs
1447+
})
14711448
mirror_url = 'https://my.fake.mirror'
14721449
filename = str(tmpdir.join('spack.yaml'))
14731450
with open(filename, 'w') as f:
@@ -1495,12 +1472,9 @@ def fake_compute_affected(r1=None, r2=None):
14951472
return ['libdwarf']
14961473

14971474
with ev.read('test'):
1498-
# This kind of pruning is enabled with the following env var
1499-
os.environ['SPACK_PRUNE_UNTOUCHED'] = 'TRUE'
15001475
monkeypatch.setattr(
15011476
ci, 'compute_affected_packages', fake_compute_affected)
15021477
ci_cmd('generate', '--output-file', outputfile)
1503-
os.environ.pop('SPACK_PRUNE_UNTOUCHED')
15041478

15051479
with open(outputfile) as f:
15061480
contents = f.read()
@@ -1514,10 +1488,9 @@ def fake_compute_affected(r1=None, r2=None):
15141488

15151489
def test_ci_subcommands_without_mirror(tmpdir, mutable_mock_env_path,
15161490
mock_packages,
1517-
install_mockery, project_dir_env,
1491+
install_mockery, ci_base_environment,
15181492
mock_binary_index):
15191493
"""Make sure we catch if there is not a mirror and report an error"""
1520-
project_dir_env(tmpdir.strpath)
15211494
filename = str(tmpdir.join('spack.yaml'))
15221495
with open(filename, 'w') as f:
15231496
f.write("""\
@@ -1594,9 +1567,8 @@ def test_ensure_only_one_temporary_storage():
15941567
def test_ci_generate_temp_storage_url(tmpdir, mutable_mock_env_path,
15951568
install_mockery,
15961569
mock_packages, monkeypatch,
1597-
project_dir_env, mock_binary_index):
1570+
ci_base_environment, mock_binary_index):
15981571
"""Verify correct behavior when using temporary-storage-url-prefix"""
1599-
project_dir_env(tmpdir.strpath)
16001572
filename = str(tmpdir.join('spack.yaml'))
16011573
with open(filename, 'w') as f:
16021574
f.write("""\
@@ -1650,9 +1622,8 @@ def test_ci_generate_temp_storage_url(tmpdir, mutable_mock_env_path,
16501622
def test_ci_generate_read_broken_specs_url(tmpdir, mutable_mock_env_path,
16511623
install_mockery,
16521624
mock_packages, monkeypatch,
1653-
project_dir_env):
1625+
ci_base_environment):
16541626
"""Verify that `broken-specs-url` works as intended"""
1655-
project_dir_env(tmpdir.strpath)
16561627
spec_a = Spec('a')
16571628
spec_a.concretize()
16581629
a_full_hash = spec_a.full_hash()
@@ -1708,8 +1679,7 @@ def test_ci_generate_read_broken_specs_url(tmpdir, mutable_mock_env_path,
17081679

17091680
def test_ci_reproduce(tmpdir, mutable_mock_env_path,
17101681
install_mockery, mock_packages, monkeypatch,
1711-
last_two_git_commits, project_dir_env, mock_binary_index):
1712-
project_dir_env(tmpdir.strpath)
1682+
last_two_git_commits, ci_base_environment, mock_binary_index):
17131683
working_dir = tmpdir.join('repro_dir')
17141684
image_name = 'org/image:tag'
17151685

0 commit comments

Comments
 (0)
X Tutup