2021-10-28 20:21:20 +00:00
|
|
|
import boto3
|
2023-08-08 10:06:51 +00:00
|
|
|
import pytest
|
2023-11-30 15:55:51 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2021-10-28 20:21:20 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2021-10-28 20:21:20 +00:00
|
|
|
|
|
|
|
TEST_REGION_NAME = "us-east-1"
|
|
|
|
|
|
|
|
|
2022-10-04 16:28:30 +00:00
|
|
|
@pytest.fixture(name="sagemaker_client")
|
|
|
|
def fixture_sagemaker_client():
|
2024-01-07 12:03:33 +00:00
|
|
|
with mock_aws():
|
2022-10-04 16:28:30 +00:00
|
|
|
yield boto3.client("sagemaker", region_name=TEST_REGION_NAME)
|
2021-10-28 20:21:20 +00:00
|
|
|
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
def test_search(sagemaker_client):
|
|
|
|
experiment_name = "experiment_name"
|
|
|
|
trial_component_name = "trial_component_name"
|
|
|
|
trial_name = "trial_name"
|
|
|
|
_set_up_trial_component(
|
|
|
|
sagemaker_client,
|
|
|
|
experiment_name=experiment_name,
|
|
|
|
trial_component_name=trial_component_name,
|
|
|
|
trial_name=trial_name,
|
2021-10-28 20:21:20 +00:00
|
|
|
)
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.search(Resource="ExperimentTrialComponent")
|
2021-10-28 20:21:20 +00:00
|
|
|
assert len(resp["Results"]) == 2
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.describe_trial_component(
|
|
|
|
TrialComponentName=trial_component_name
|
|
|
|
)
|
2021-10-28 20:21:20 +00:00
|
|
|
trial_component_arn = resp["TrialComponentArn"]
|
|
|
|
|
|
|
|
tags = [{"Key": "key-name", "Value": "some-value"}]
|
2022-04-27 11:56:08 +00:00
|
|
|
sagemaker_client.add_tags(ResourceArn=trial_component_arn, Tags=tags)
|
2021-10-28 20:21:20 +00:00
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.search(
|
2021-10-28 20:21:20 +00:00
|
|
|
Resource="ExperimentTrialComponent",
|
|
|
|
SearchExpression={
|
|
|
|
"Filters": [
|
|
|
|
{"Name": "Tags.key-name", "Operator": "Equals", "Value": "some-value"}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(resp["Results"]) == 1
|
|
|
|
assert (
|
|
|
|
resp["Results"][0]["TrialComponent"]["TrialComponentName"]
|
|
|
|
== trial_component_name
|
|
|
|
)
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.search(Resource="Experiment")
|
2021-10-28 20:21:20 +00:00
|
|
|
assert len(resp["Results"]) == 1
|
|
|
|
assert resp["Results"][0]["Experiment"]["ExperimentName"] == experiment_name
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.search(Resource="ExperimentTrial")
|
2021-10-28 20:21:20 +00:00
|
|
|
assert len(resp["Results"]) == 1
|
|
|
|
assert resp["Results"][0]["Trial"]["TrialName"] == trial_name
|
2021-11-01 22:30:07 +00:00
|
|
|
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
def test_search_trial_component_with_experiment_name(sagemaker_client):
|
|
|
|
experiment_name = "experiment_name"
|
|
|
|
trial_component_name = "trial_component_name"
|
|
|
|
_set_up_trial_component(
|
|
|
|
sagemaker_client,
|
|
|
|
experiment_name=experiment_name,
|
|
|
|
trial_component_name=trial_component_name,
|
2021-11-01 22:30:07 +00:00
|
|
|
)
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.search(Resource="ExperimentTrialComponent")
|
2021-11-01 22:30:07 +00:00
|
|
|
assert len(resp["Results"]) == 2
|
|
|
|
|
2022-04-27 11:56:08 +00:00
|
|
|
resp = sagemaker_client.describe_trial_component(
|
|
|
|
TrialComponentName=trial_component_name
|
|
|
|
)
|
2021-11-01 22:30:07 +00:00
|
|
|
trial_component_arn = resp["TrialComponentArn"]
|
|
|
|
|
|
|
|
tags = [{"Key": "key-name", "Value": "some-value"}]
|
2022-04-27 11:56:08 +00:00
|
|
|
sagemaker_client.add_tags(ResourceArn=trial_component_arn, Tags=tags)
|
2021-11-01 22:30:07 +00:00
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
2022-04-27 11:56:08 +00:00
|
|
|
sagemaker_client.search(
|
2021-11-01 22:30:07 +00:00
|
|
|
Resource="ExperimentTrialComponent",
|
|
|
|
SearchExpression={
|
|
|
|
"Filters": [
|
|
|
|
{
|
|
|
|
"Name": "ExperimentName",
|
|
|
|
"Operator": "Equals",
|
|
|
|
"Value": experiment_name,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-08-08 10:06:51 +00:00
|
|
|
assert ex.value.response["Error"]["Code"] == "ValidationException"
|
|
|
|
assert (
|
|
|
|
ex.value.response["Error"]["Message"] == "Unknown property name: ExperimentName"
|
2021-11-01 22:30:07 +00:00
|
|
|
)
|
2023-08-08 10:06:51 +00:00
|
|
|
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
2022-04-27 11:56:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _set_up_trial_component(
|
|
|
|
sagemaker_client,
|
|
|
|
experiment_name="some-experiment-name",
|
|
|
|
trial_component_name="some-trial-component-name",
|
|
|
|
trial_name="some-trial-name",
|
|
|
|
another_trial_component_name="another-trial-component-name",
|
|
|
|
):
|
|
|
|
sagemaker_client.create_experiment(ExperimentName=experiment_name)
|
|
|
|
sagemaker_client.create_trial(ExperimentName=experiment_name, TrialName=trial_name)
|
|
|
|
sagemaker_client.create_trial_component(TrialComponentName=trial_component_name)
|
|
|
|
sagemaker_client.create_trial_component(
|
|
|
|
TrialComponentName=another_trial_component_name
|
|
|
|
)
|