|
26 | 26 | ) |
27 | 27 | from src.sagemaker.experiments._utils import resolve_artifact_name |
28 | 28 | from src.sagemaker.session import Session |
| 29 | +from tests.unit import ( |
| 30 | + _test_default_bucket_and_prefix_combinations, |
| 31 | + DEFAULT_S3_OBJECT_KEY_PREFIX_NAME, |
| 32 | + DEFAULT_S3_BUCKET_NAME, |
| 33 | +) |
29 | 34 |
|
30 | 35 |
|
31 | 36 | @pytest.fixture |
@@ -193,3 +198,119 @@ def test_artifact_uploader_upload_object_artifact(tempdir, artifact_uploader): |
193 | 198 |
|
194 | 199 | expected_uri = "s3://{}/{}".format(artifact_uploader.artifact_bucket, expected_key) |
195 | 200 | assert expected_uri == s3_uri |
| 201 | + |
| 202 | + |
| 203 | +def test_upload_artifact__default_bucket_and_prefix_combinations(tempdir): |
| 204 | + path = os.path.join(tempdir, "exists") |
| 205 | + with open(path, "a") as f: |
| 206 | + f.write("boo") |
| 207 | + |
| 208 | + def with_user_input(sess): |
| 209 | + artifact_uploader = _ArtifactUploader( |
| 210 | + trial_component_name="trial_component_name", |
| 211 | + artifact_bucket="artifact_bucket", |
| 212 | + artifact_prefix="artifact_prefix", |
| 213 | + sagemaker_session=sess, |
| 214 | + ) |
| 215 | + artifact_uploader._s3_client.head_object.return_value = {"ETag": "etag_value"} |
| 216 | + s3_uri, etag = artifact_uploader.upload_artifact(path) |
| 217 | + s3_uri_2, etag_2 = artifact_uploader.upload_artifact(path) |
| 218 | + return s3_uri, s3_uri_2 |
| 219 | + |
| 220 | + def without_user_input(sess): |
| 221 | + artifact_uploader = _ArtifactUploader( |
| 222 | + trial_component_name="trial_component_name", |
| 223 | + sagemaker_session=sess, |
| 224 | + ) |
| 225 | + artifact_uploader._s3_client.head_object.return_value = {"ETag": "etag_value"} |
| 226 | + s3_uri, etag = artifact_uploader.upload_artifact(path) |
| 227 | + s3_uri_2, etag_2 = artifact_uploader.upload_artifact(path) |
| 228 | + return s3_uri, s3_uri_2 |
| 229 | + |
| 230 | + actual, expected = _test_default_bucket_and_prefix_combinations( |
| 231 | + function_with_user_input=with_user_input, |
| 232 | + function_without_user_input=without_user_input, |
| 233 | + expected__without_user_input__with_default_bucket_and_default_prefix=( |
| 234 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/{DEFAULT_S3_OBJECT_KEY_PREFIX_NAME}/" |
| 235 | + + "trial-component-artifacts/trial_component_name/exists", |
| 236 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/{DEFAULT_S3_OBJECT_KEY_PREFIX_NAME}/" |
| 237 | + + "trial-component-artifacts/trial_component_name/exists", |
| 238 | + ), |
| 239 | + expected__without_user_input__with_default_bucket_only=( |
| 240 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/trial-component-artifacts/trial_component_name/exists", |
| 241 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/trial-component-artifacts/trial_component_name/exists", |
| 242 | + ), |
| 243 | + expected__with_user_input__with_default_bucket_and_prefix=( |
| 244 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/exists", |
| 245 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/exists", |
| 246 | + ), |
| 247 | + expected__with_user_input__with_default_bucket_only=( |
| 248 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/exists", |
| 249 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/exists", |
| 250 | + ), |
| 251 | + ) |
| 252 | + assert actual == expected |
| 253 | + |
| 254 | + |
| 255 | +def test_upload_object_artifact__default_bucket_and_prefix_combinations(tempdir): |
| 256 | + path = os.path.join(tempdir, "exists") |
| 257 | + with open(path, "a") as f: |
| 258 | + f.write("boo") |
| 259 | + |
| 260 | + artifact_name = "my-artifact" |
| 261 | + artifact_object = {"key": "value"} |
| 262 | + file_extension = ".csv" |
| 263 | + |
| 264 | + def with_user_input(sess): |
| 265 | + artifact_uploader = _ArtifactUploader( |
| 266 | + trial_component_name="trial_component_name", |
| 267 | + artifact_bucket="artifact_bucket", |
| 268 | + artifact_prefix="artifact_prefix", |
| 269 | + sagemaker_session=sess, |
| 270 | + ) |
| 271 | + artifact_uploader._s3_client.head_object.return_value = {"ETag": "etag_value"} |
| 272 | + s3_uri, etag = artifact_uploader.upload_object_artifact( |
| 273 | + artifact_name, artifact_object, file_extension |
| 274 | + ) |
| 275 | + s3_uri_2, etag_2 = artifact_uploader.upload_object_artifact( |
| 276 | + artifact_name, artifact_object, file_extension |
| 277 | + ) |
| 278 | + return s3_uri, s3_uri_2 |
| 279 | + |
| 280 | + def without_user_input(sess): |
| 281 | + artifact_uploader = _ArtifactUploader( |
| 282 | + trial_component_name="trial_component_name", |
| 283 | + sagemaker_session=sess, |
| 284 | + ) |
| 285 | + artifact_uploader._s3_client.head_object.return_value = {"ETag": "etag_value"} |
| 286 | + s3_uri, etag = artifact_uploader.upload_object_artifact( |
| 287 | + artifact_name, artifact_object, file_extension |
| 288 | + ) |
| 289 | + s3_uri_2, etag_2 = artifact_uploader.upload_object_artifact( |
| 290 | + artifact_name, artifact_object, file_extension |
| 291 | + ) |
| 292 | + return s3_uri, s3_uri_2 |
| 293 | + |
| 294 | + actual, expected = _test_default_bucket_and_prefix_combinations( |
| 295 | + function_with_user_input=with_user_input, |
| 296 | + function_without_user_input=without_user_input, |
| 297 | + expected__without_user_input__with_default_bucket_and_default_prefix=( |
| 298 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/{DEFAULT_S3_OBJECT_KEY_PREFIX_NAME}/" |
| 299 | + + "trial-component-artifacts/trial_component_name/my-artifact.csv", |
| 300 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/{DEFAULT_S3_OBJECT_KEY_PREFIX_NAME}/" |
| 301 | + + "trial-component-artifacts/trial_component_name/my-artifact.csv", |
| 302 | + ), |
| 303 | + expected__without_user_input__with_default_bucket_only=( |
| 304 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/trial-component-artifacts/trial_component_name/my-artifact.csv", |
| 305 | + f"s3://{DEFAULT_S3_BUCKET_NAME}/trial-component-artifacts/trial_component_name/my-artifact.csv", |
| 306 | + ), |
| 307 | + expected__with_user_input__with_default_bucket_and_prefix=( |
| 308 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/my-artifact.csv", |
| 309 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/my-artifact.csv", |
| 310 | + ), |
| 311 | + expected__with_user_input__with_default_bucket_only=( |
| 312 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/my-artifact.csv", |
| 313 | + "s3://artifact_bucket/artifact_prefix/trial_component_name/my-artifact.csv", |
| 314 | + ), |
| 315 | + ) |
| 316 | + assert actual == expected |
0 commit comments