Skip to content

Commit bc7e3ba

Browse files
rrrkharsenargokul
authored andcommitted
fix: Prevent RunContext overlap between test_run tests (aws#5083)
Co-authored-by: Gokul Anantha Narayanan <[email protected]>
1 parent 6408c0c commit bc7e3ba

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

tests/integ/sagemaker/experiments/helpers.py

+16
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
from __future__ import absolute_import
1414

1515
from contextlib import contextmanager
16+
import pytest
17+
import logging
1618

1719
from sagemaker import utils
1820
from sagemaker.experiments.experiment import Experiment
21+
from sagemaker.experiments._run_context import _RunContext
1922

2023
EXP_INTEG_TEST_NAME_PREFIX = "experiments-integ"
2124

@@ -40,3 +43,16 @@ def cleanup_exp_resources(exp_names, sagemaker_session):
4043
for exp_name in exp_names:
4144
exp = Experiment.load(experiment_name=exp_name, sagemaker_session=sagemaker_session)
4245
exp._delete_all(action="--force")
46+
47+
@pytest.fixture
48+
def clear_run_context():
49+
current_run = _RunContext.get_current_run()
50+
if current_run == None:
51+
return
52+
53+
logging.info(
54+
f"RunContext already populated by run {current_run.run_name}"
55+
f" in experiment {current_run.experiment_name}."
56+
" Clearing context manually"
57+
)
58+
_RunContext.drop_current_run()

tests/integ/sagemaker/experiments/test_run.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from sagemaker.experiments.trial_component import _TrialComponent
3333
from sagemaker.sklearn import SKLearn
3434
from sagemaker.utils import retry_with_backoff, unique_name_from_base
35-
from tests.integ.sagemaker.experiments.helpers import name, cleanup_exp_resources
35+
from tests.integ.sagemaker.experiments.helpers import name, cleanup_exp_resources, clear_run_context
3636
from sagemaker.experiments.run import (
3737
RUN_NAME_BASE,
3838
DELIMITER,
@@ -55,7 +55,7 @@ def artifact_file_path(tempdir):
5555
metric_name = "Test-Local-Init-Log-Metric"
5656

5757

58-
def test_local_run_with_load(sagemaker_session, artifact_file_path):
58+
def test_local_run_with_load(sagemaker_session, artifact_file_path, clear_run_context):
5959
exp_name = f"My-Local-Exp-{name()}"
6060
with cleanup_exp_resources(exp_names=[exp_name], sagemaker_session=sagemaker_session):
6161
# Run name is not provided, will create a new TC
@@ -86,7 +86,9 @@ def verify_load_run():
8686
retry_with_backoff(verify_load_run, 4)
8787

8888

89-
def test_two_local_run_init_with_same_run_name_and_different_exp_names(sagemaker_session):
89+
def test_two_local_run_init_with_same_run_name_and_different_exp_names(
90+
sagemaker_session, clear_run_context
91+
):
9092
exp_name1 = f"my-two-local-exp1-{name()}"
9193
exp_name2 = f"my-two-local-exp2-{name()}"
9294
run_name = "test-run"
@@ -124,7 +126,9 @@ def test_two_local_run_init_with_same_run_name_and_different_exp_names(sagemaker
124126
("my-test4", "test-run", "run-display-name-test"), # with supplied display name
125127
],
126128
)
127-
def test_run_name_vs_trial_component_name_edge_cases(sagemaker_session, input_names):
129+
def test_run_name_vs_trial_component_name_edge_cases(
130+
sagemaker_session, input_names, clear_run_context
131+
):
128132
exp_name, run_name, run_display_name = input_names
129133
with cleanup_exp_resources(exp_names=[exp_name], sagemaker_session=sagemaker_session):
130134
with Run(
@@ -177,6 +181,7 @@ def test_run_from_local_and_train_job_and_all_exp_cfg_match(
177181
execution_role,
178182
sagemaker_client_config,
179183
sagemaker_metrics_config,
184+
clear_run_context,
180185
):
181186
# Notes:
182187
# 1. The 1st Run created locally and its exp config was auto passed to the job
@@ -277,6 +282,7 @@ def test_run_from_local_and_train_job_and_exp_cfg_not_match(
277282
execution_role,
278283
sagemaker_client_config,
279284
sagemaker_metrics_config,
285+
clear_run_context,
280286
):
281287
# Notes:
282288
# 1. The 1st Run created locally and its exp config was auto passed to the job
@@ -363,6 +369,7 @@ def test_run_from_train_job_only(
363369
execution_role,
364370
sagemaker_client_config,
365371
sagemaker_metrics_config,
372+
clear_run_context,
366373
):
367374
# Notes:
368375
# 1. No Run created locally or specified in experiment config
@@ -413,6 +420,7 @@ def test_run_from_processing_job_and_override_default_exp_config(
413420
execution_role,
414421
sagemaker_client_config,
415422
sagemaker_metrics_config,
423+
clear_run_context,
416424
):
417425
# Notes:
418426
# 1. The 1st Run (run) created locally
@@ -492,6 +500,7 @@ def test_run_from_transform_job(
492500
execution_role,
493501
sagemaker_client_config,
494502
sagemaker_metrics_config,
503+
clear_run_context,
495504
):
496505
# Notes:
497506
# 1. The 1st Run (run) created locally
@@ -573,6 +582,7 @@ def test_load_run_auto_pass_in_exp_config_to_job(
573582
execution_role,
574583
sagemaker_client_config,
575584
sagemaker_metrics_config,
585+
clear_run_context,
576586
):
577587
# Notes:
578588
# 1. In local side, load the Run created previously and invoke a job under the load context
@@ -621,7 +631,7 @@ def test_load_run_auto_pass_in_exp_config_to_job(
621631
)
622632

623633

624-
def test_list(run_obj, sagemaker_session):
634+
def test_list(run_obj, sagemaker_session, clear_run_context):
625635
tc1 = _TrialComponent.create(
626636
trial_component_name=f"non-run-tc1-{name()}",
627637
sagemaker_session=sagemaker_session,
@@ -643,7 +653,7 @@ def test_list(run_obj, sagemaker_session):
643653
assert run_tcs[0].experiment_config == run_obj.experiment_config
644654

645655

646-
def test_list_twice(run_obj, sagemaker_session):
656+
def test_list_twice(run_obj, sagemaker_session, clear_run_context):
647657
tc1 = _TrialComponent.create(
648658
trial_component_name=f"non-run-tc1-{name()}",
649659
sagemaker_session=sagemaker_session,

0 commit comments

Comments
 (0)