2222from sagemaker .utils import unique_name_from_base
2323from tests .integ import AUTO_ML_DEFAULT_TIMEMOUT_MINUTES , DATA_DIR , auto_ml_utils
2424from tests .integ .timeout import timeout
25+ from tests .conftest import CUSTOM_S3_OBJECT_KEY_PREFIX
2526
2627ROLE = "SageMakerRole"
2728PREFIX = "sagemaker/beta-automl-xgboost"
3839BASE_JOB_NAME = "auto-ml"
3940MODE = "ENSEMBLING"
4041
41- # use a succeeded AutoML job to test describe and list candidates method, otherwise tests will run too long
42- AUTO_ML_JOB_NAME = "python-sdk-integ-test-base-job"
4342DEFAULT_MODEL_NAME = "python-sdk-automl"
4443
4544
4948}
5049
5150
51+ # use a succeeded AutoML job to test describe and list candidates method, otherwise tests will run too long
52+ # test-session-job will be created once per session if it doesn't exist, and be reused in relevant tests.
53+ @pytest .fixture (scope = "module" )
54+ def test_session_job_name ():
55+ job_name = unique_name_from_base ("test-session-job" , max_length = 32 )
56+ return job_name
57+
58+
5259@pytest .mark .slow_test
5360@pytest .mark .skipif (
5461 tests .integ .test_region () in tests .integ .NO_AUTO_ML_REGIONS ,
@@ -63,7 +70,7 @@ def test_auto_ml_fit(sagemaker_session):
6370 max_candidates = 1 ,
6471 )
6572
66- job_name = unique_name_from_base ("auto-ml" , max_length = 32 )
73+ job_name = unique_name_from_base (BASE_JOB_NAME , max_length = 32 )
6774 inputs = sagemaker_session .upload_data (path = TRAINING_DATA , key_prefix = PREFIX + "/input" )
6875 with timeout (minutes = AUTO_ML_DEFAULT_TIMEMOUT_MINUTES ):
6976 auto_ml .fit (inputs , job_name = job_name )
@@ -82,7 +89,7 @@ def test_auto_ml_fit_local_input(sagemaker_session):
8289 )
8390
8491 inputs = TRAINING_DATA
85- job_name = unique_name_from_base ("auto-ml" , max_length = 32 )
92+ job_name = unique_name_from_base (BASE_JOB_NAME , max_length = 32 )
8693 with timeout (minutes = AUTO_ML_DEFAULT_TIMEMOUT_MINUTES ):
8794 auto_ml .fit (inputs , job_name = job_name )
8895
@@ -99,7 +106,7 @@ def test_auto_ml_input_object_fit(sagemaker_session):
99106 max_candidates = 1 ,
100107 generate_candidate_definitions_only = True ,
101108 )
102- job_name = unique_name_from_base ("auto-ml" , max_length = 32 )
109+ job_name = unique_name_from_base (BASE_JOB_NAME , max_length = 32 )
103110 s3_input = sagemaker_session .upload_data (path = TRAINING_DATA , key_prefix = PREFIX + "/input" )
104111 inputs = AutoMLInput (inputs = s3_input , target_attribute_name = TARGET_ATTRIBUTE_NAME )
105112 with timeout (minutes = AUTO_ML_DEFAULT_TIMEMOUT_MINUTES ):
@@ -118,7 +125,7 @@ def test_auto_ml_input_object_list_fit(sagemaker_session):
118125 max_candidates = 1 ,
119126 mode = MODE ,
120127 )
121- job_name = unique_name_from_base ("auto-ml" , max_length = 32 )
128+ job_name = unique_name_from_base (BASE_JOB_NAME , max_length = 32 )
122129 s3_input_training = sagemaker_session .upload_data (
123130 path = TRAINING_DATA , key_prefix = PREFIX + "/input"
124131 )
@@ -178,7 +185,7 @@ def test_auto_ml_invalid_target_attribute(sagemaker_session):
178185 auto_ml = AutoML (
179186 role = ROLE , target_attribute_name = "y" , sagemaker_session = sagemaker_session , max_candidates = 1
180187 )
181- job_name = unique_name_from_base ("auto-ml" , max_length = 32 )
188+ job_name = unique_name_from_base (BASE_JOB_NAME , max_length = 32 )
182189 inputs = sagemaker_session .upload_data (path = TRAINING_DATA , key_prefix = PREFIX + "/input" )
183190 with pytest .raises (
184191 ClientError ,
@@ -192,14 +199,14 @@ def test_auto_ml_invalid_target_attribute(sagemaker_session):
192199 tests .integ .test_region () in tests .integ .NO_AUTO_ML_REGIONS ,
193200 reason = "AutoML is not supported in the region yet." ,
194201)
195- def test_auto_ml_describe_auto_ml_job (sagemaker_session ):
202+ def test_auto_ml_describe_auto_ml_job (sagemaker_session , test_session_job_name ):
196203 expected_default_input_config = [
197204 {
198205 "DataSource" : {
199206 "S3DataSource" : {
200207 "S3DataType" : "S3Prefix" ,
201- "S3Uri" : "s3://{}/{}/input/iris_training.csv" .format (
202- sagemaker_session .default_bucket (), PREFIX
208+ "S3Uri" : "s3://{}/{}/{}/ input/iris_training.csv" .format (
209+ sagemaker_session .default_bucket (), CUSTOM_S3_OBJECT_KEY_PREFIX , PREFIX
203210 ),
204211 }
205212 },
@@ -209,16 +216,18 @@ def test_auto_ml_describe_auto_ml_job(sagemaker_session):
209216 }
210217 ]
211218 expected_default_output_config = {
212- "S3OutputPath" : "s3://{}/" .format (sagemaker_session .default_bucket ())
219+ "S3OutputPath" : "s3://{}/{}/" .format (
220+ sagemaker_session .default_bucket (), CUSTOM_S3_OBJECT_KEY_PREFIX
221+ )
213222 }
214223
215- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
224+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
216225 auto_ml = AutoML (
217226 role = ROLE , target_attribute_name = TARGET_ATTRIBUTE_NAME , sagemaker_session = sagemaker_session
218227 )
219228
220- desc = auto_ml .describe_auto_ml_job (job_name = AUTO_ML_JOB_NAME )
221- assert desc ["AutoMLJobName" ] == AUTO_ML_JOB_NAME
229+ desc = auto_ml .describe_auto_ml_job (job_name = test_session_job_name )
230+ assert desc ["AutoMLJobName" ] == test_session_job_name
222231 assert desc ["AutoMLJobStatus" ] == "Completed"
223232 assert isinstance (desc ["BestCandidate" ], dict )
224233 assert desc ["InputDataConfig" ] == expected_default_input_config
@@ -230,14 +239,14 @@ def test_auto_ml_describe_auto_ml_job(sagemaker_session):
230239 tests .integ .test_region () in tests .integ .NO_AUTO_ML_REGIONS ,
231240 reason = "AutoML is not supported in the region yet." ,
232241)
233- def test_auto_ml_attach (sagemaker_session ):
242+ def test_auto_ml_attach (sagemaker_session , test_session_job_name ):
234243 expected_default_input_config = [
235244 {
236245 "DataSource" : {
237246 "S3DataSource" : {
238247 "S3DataType" : "S3Prefix" ,
239- "S3Uri" : "s3://{}/{}/input/iris_training.csv" .format (
240- sagemaker_session .default_bucket (), PREFIX
248+ "S3Uri" : "s3://{}/{}/{}/ input/iris_training.csv" .format (
249+ sagemaker_session .default_bucket (), CUSTOM_S3_OBJECT_KEY_PREFIX , PREFIX
241250 ),
242251 }
243252 },
@@ -247,16 +256,18 @@ def test_auto_ml_attach(sagemaker_session):
247256 }
248257 ]
249258 expected_default_output_config = {
250- "S3OutputPath" : "s3://{}/" .format (sagemaker_session .default_bucket ())
259+ "S3OutputPath" : "s3://{}/{}/" .format (
260+ sagemaker_session .default_bucket (), CUSTOM_S3_OBJECT_KEY_PREFIX
261+ )
251262 }
252263
253- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
264+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
254265
255266 attached_automl_job = AutoML .attach (
256- auto_ml_job_name = AUTO_ML_JOB_NAME , sagemaker_session = sagemaker_session
267+ auto_ml_job_name = test_session_job_name , sagemaker_session = sagemaker_session
257268 )
258269 attached_desc = attached_automl_job .describe_auto_ml_job ()
259- assert attached_desc ["AutoMLJobName" ] == AUTO_ML_JOB_NAME
270+ assert attached_desc ["AutoMLJobName" ] == test_session_job_name
260271 assert attached_desc ["AutoMLJobStatus" ] == "Completed"
261272 assert isinstance (attached_desc ["BestCandidate" ], dict )
262273 assert attached_desc ["InputDataConfig" ] == expected_default_input_config
@@ -268,28 +279,28 @@ def test_auto_ml_attach(sagemaker_session):
268279 tests .integ .test_region () in tests .integ .NO_AUTO_ML_REGIONS ,
269280 reason = "AutoML is not supported in the region yet." ,
270281)
271- def test_list_candidates (sagemaker_session ):
272- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
282+ def test_list_candidates (sagemaker_session , test_session_job_name ):
283+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
273284
274285 auto_ml = AutoML (
275286 role = ROLE , target_attribute_name = TARGET_ATTRIBUTE_NAME , sagemaker_session = sagemaker_session
276287 )
277288
278- candidates = auto_ml .list_candidates (job_name = AUTO_ML_JOB_NAME )
289+ candidates = auto_ml .list_candidates (job_name = test_session_job_name )
279290 assert len (candidates ) == 3
280291
281292
282293@pytest .mark .skipif (
283294 tests .integ .test_region () in tests .integ .NO_AUTO_ML_REGIONS ,
284295 reason = "AutoML is not supported in the region yet." ,
285296)
286- def test_best_candidate (sagemaker_session ):
287- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
297+ def test_best_candidate (sagemaker_session , test_session_job_name ):
298+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
288299
289300 auto_ml = AutoML (
290301 role = ROLE , target_attribute_name = TARGET_ATTRIBUTE_NAME , sagemaker_session = sagemaker_session
291302 )
292- best_candidate = auto_ml .best_candidate (job_name = AUTO_ML_JOB_NAME )
303+ best_candidate = auto_ml .best_candidate (job_name = test_session_job_name )
293304 assert len (best_candidate ["InferenceContainers" ]) == 3
294305 assert len (best_candidate ["CandidateSteps" ]) == 4
295306 assert best_candidate ["CandidateStatus" ] == "Completed"
@@ -300,13 +311,13 @@ def test_best_candidate(sagemaker_session):
300311 reason = "AutoML is not supported in the region yet." ,
301312)
302313@pytest .mark .release
303- def test_deploy_best_candidate (sagemaker_session , cpu_instance_type ):
304- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
314+ def test_deploy_best_candidate (sagemaker_session , cpu_instance_type , test_session_job_name ):
315+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
305316
306317 auto_ml = AutoML (
307318 role = ROLE , target_attribute_name = TARGET_ATTRIBUTE_NAME , sagemaker_session = sagemaker_session
308319 )
309- best_candidate = auto_ml .best_candidate (job_name = AUTO_ML_JOB_NAME )
320+ best_candidate = auto_ml .best_candidate (job_name = test_session_job_name )
310321 endpoint_name = unique_name_from_base ("sagemaker-auto-ml-best-candidate-test" )
311322
312323 with timeout (minutes = AUTO_ML_DEFAULT_TIMEMOUT_MINUTES ):
@@ -331,14 +342,16 @@ def test_deploy_best_candidate(sagemaker_session, cpu_instance_type):
331342@pytest .mark .skip (
332343 reason = "" ,
333344)
334- def test_candidate_estimator_default_rerun_and_deploy (sagemaker_session , cpu_instance_type ):
335- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
345+ def test_candidate_estimator_default_rerun_and_deploy (
346+ sagemaker_session , cpu_instance_type , test_session_job_name
347+ ):
348+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
336349
337350 auto_ml = AutoML (
338351 role = ROLE , target_attribute_name = TARGET_ATTRIBUTE_NAME , sagemaker_session = sagemaker_session
339352 )
340353
341- candidates = auto_ml .list_candidates (job_name = AUTO_ML_JOB_NAME )
354+ candidates = auto_ml .list_candidates (job_name = test_session_job_name )
342355 candidate = candidates [1 ]
343356
344357 candidate_estimator = CandidateEstimator (candidate , sagemaker_session )
@@ -364,13 +377,13 @@ def test_candidate_estimator_default_rerun_and_deploy(sagemaker_session, cpu_ins
364377 tests .integ .test_region () in tests .integ .NO_AUTO_ML_REGIONS ,
365378 reason = "AutoML is not supported in the region yet." ,
366379)
367- def test_candidate_estimator_get_steps (sagemaker_session ):
368- auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session )
380+ def test_candidate_estimator_get_steps (sagemaker_session , test_session_job_name ):
381+ auto_ml_utils .create_auto_ml_job_if_not_exist (sagemaker_session , test_session_job_name )
369382
370383 auto_ml = AutoML (
371384 role = ROLE , target_attribute_name = TARGET_ATTRIBUTE_NAME , sagemaker_session = sagemaker_session
372385 )
373- candidates = auto_ml .list_candidates (job_name = AUTO_ML_JOB_NAME )
386+ candidates = auto_ml .list_candidates (job_name = test_session_job_name )
374387 candidate = candidates [1 ]
375388
376389 candidate_estimator = CandidateEstimator (candidate , sagemaker_session )
0 commit comments