2021-10-21 22:00:32 +00:00
import json
2023-11-30 15:55:51 +00:00
import boto3
import pytest
2023-07-18 15:33:16 +00:00
from botocore . exceptions import ClientError
2023-11-30 15:55:51 +00:00
2024-01-07 12:03:33 +00:00
from moto import mock_aws
2022-08-13 09:49:43 +00:00
from moto . core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
2021-10-21 22:00:32 +00:00
from moto . ec2 import utils as ec2_utils
from tests import EXAMPLE_AMI_ID
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_list_account_settings_initial ( ) :
client = boto3 . client ( " ecs " , region_name = " eu-west-1 " )
resp = client . list_account_settings ( )
2023-07-18 15:33:16 +00:00
assert resp [ " settings " ] == [ ]
2021-10-21 22:00:32 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
@pytest.mark.parametrize (
" name " ,
[ " containerInstanceLongArnFormat " , " serviceLongArnFormat " , " taskLongArnFormat " ] ,
)
@pytest.mark.parametrize ( " value " , [ " enabled " , " disabled " ] )
def test_put_account_setting ( name , value ) :
client = boto3 . client ( " ecs " , region_name = " eu-west-1 " )
resp = client . put_account_setting ( name = name , value = value )
2023-07-18 15:33:16 +00:00
assert resp [ " setting " ] == { " name " : name , " value " : value }
2021-10-21 22:00:32 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_list_account_setting ( ) :
client = boto3 . client ( " ecs " , region_name = " eu-west-1 " )
client . put_account_setting ( name = " containerInstanceLongArnFormat " , value = " enabled " )
client . put_account_setting ( name = " serviceLongArnFormat " , value = " disabled " )
client . put_account_setting ( name = " taskLongArnFormat " , value = " enabled " )
resp = client . list_account_settings ( )
2023-07-18 15:33:16 +00:00
assert len ( resp [ " settings " ] ) == 3
assert { " name " : " containerInstanceLongArnFormat " , " value " : " enabled " } in resp [
" settings "
]
assert { " name " : " serviceLongArnFormat " , " value " : " disabled " } in resp [ " settings " ]
assert { " name " : " taskLongArnFormat " , " value " : " enabled " } in resp [ " settings " ]
2021-10-21 22:00:32 +00:00
resp = client . list_account_settings ( name = " serviceLongArnFormat " )
2023-07-18 15:33:16 +00:00
assert len ( resp [ " settings " ] ) == 1
assert { " name " : " serviceLongArnFormat " , " value " : " disabled " } in resp [ " settings " ]
2021-10-21 22:00:32 +00:00
resp = client . list_account_settings ( value = " enabled " )
2023-07-18 15:33:16 +00:00
assert len ( resp [ " settings " ] ) == 2
assert { " name " : " containerInstanceLongArnFormat " , " value " : " enabled " } in resp [
" settings "
]
assert { " name " : " taskLongArnFormat " , " value " : " enabled " } in resp [ " settings " ]
2021-10-21 22:00:32 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_list_account_settings_wrong_name ( ) :
client = boto3 . client ( " ecs " , region_name = " eu-west-1 " )
with pytest . raises ( ClientError ) as exc :
client . list_account_settings ( name = " unknown " )
err = exc . value . response [ " Error " ]
2023-07-18 15:33:16 +00:00
assert err [ " Code " ] == " InvalidParameterException "
assert (
err [ " Message " ]
== " unknown should be one of [serviceLongArnFormat,taskLongArnFormat,containerInstanceLongArnFormat,containerLongArnFormat,awsvpcTrunking,containerInsights,dualStackIPv6] "
2021-10-21 22:00:32 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_delete_account_setting ( ) :
client = boto3 . client ( " ecs " , region_name = " eu-west-1 " )
client . put_account_setting ( name = " containerInstanceLongArnFormat " , value = " enabled " )
client . put_account_setting ( name = " serviceLongArnFormat " , value = " enabled " )
client . put_account_setting ( name = " taskLongArnFormat " , value = " enabled " )
resp = client . list_account_settings ( )
2023-07-18 15:33:16 +00:00
assert len ( resp [ " settings " ] ) == 3
2021-10-21 22:00:32 +00:00
client . delete_account_setting ( name = " serviceLongArnFormat " )
resp = client . list_account_settings ( )
2023-07-18 15:33:16 +00:00
assert len ( resp [ " settings " ] ) == 2
assert { " name " : " containerInstanceLongArnFormat " , " value " : " enabled " } in resp [
" settings "
]
assert { " name " : " taskLongArnFormat " , " value " : " enabled " } in resp [ " settings " ]
2021-10-21 22:00:32 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_put_account_setting_changes_service_arn ( ) :
client = boto3 . client ( " ecs " , region_name = " eu-west-1 " )
2022-01-18 20:53:31 +00:00
client . put_account_setting ( name = " serviceLongArnFormat " , value = " disabled " )
2021-10-21 22:00:32 +00:00
_ = client . create_cluster ( clusterName = " dummy-cluster " )
_ = client . register_task_definition (
family = " test_ecs_task " ,
containerDefinitions = [
{
" name " : " hello_world " ,
" image " : " docker/hello-world:latest " ,
" cpu " : 1024 ,
" memory " : 400 ,
}
] ,
)
client . create_service (
cluster = " dummy-cluster " ,
serviceName = " test-ecs-service " ,
taskDefinition = " test_ecs_task " ,
desiredCount = 2 ,
launchType = " FARGATE " ,
tags = [ { " key " : " ResourceOwner " , " value " : " Dummy " } ] ,
)
2022-01-18 20:53:31 +00:00
# Initial response is short (setting serviceLongArnFormat=disabled)
2021-10-21 22:00:32 +00:00
response = client . list_services ( cluster = " dummy-cluster " , launchType = " FARGATE " )
service_arn = response [ " serviceArns " ] [ 0 ]
2023-07-18 15:33:16 +00:00
assert service_arn == f " arn:aws:ecs:eu-west-1: { ACCOUNT_ID } :service/test-ecs-service "
2021-10-21 22:00:32 +00:00
2022-01-18 20:53:31 +00:00
# Second invocation returns long ARN's by default, after deleting the preference
2021-10-21 22:00:32 +00:00
client . delete_account_setting ( name = " serviceLongArnFormat " )
response = client . list_services ( cluster = " dummy-cluster " , launchType = " FARGATE " )
service_arn = response [ " serviceArns " ] [ 0 ]
2023-07-18 15:33:16 +00:00
assert (
service_arn
== f " arn:aws:ecs:eu-west-1: { ACCOUNT_ID } :service/dummy-cluster/test-ecs-service "
2021-10-21 22:00:32 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_put_account_setting_changes_containerinstance_arn ( ) :
ecs_client = boto3 . client ( " ecs " , region_name = " us-east-1 " )
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
test_cluster_name = " test_ecs_cluster "
ecs_client . create_cluster ( clusterName = test_cluster_name )
test_instance = ec2 . create_instances (
ImageId = EXAMPLE_AMI_ID , MinCount = 1 , MaxCount = 1
) [ 0 ]
instance_id_document = json . dumps (
ec2_utils . generate_instance_identity_document ( test_instance )
)
2022-01-18 20:53:31 +00:00
# Initial ARN should be long
2021-10-21 22:00:32 +00:00
response = ecs_client . register_container_instance (
cluster = test_cluster_name , instanceIdentityDocument = instance_id_document
)
full_arn = response [ " containerInstance " ] [ " containerInstanceArn " ]
2023-07-18 15:33:16 +00:00
assert full_arn . startswith (
f " arn:aws:ecs:us-east-1: { ACCOUNT_ID } :container-instance/ { test_cluster_name } / "
2021-10-21 22:00:32 +00:00
)
2022-01-18 20:53:31 +00:00
# Now disable long-format
2021-10-21 22:00:32 +00:00
ecs_client . put_account_setting (
2022-01-18 20:53:31 +00:00
name = " containerInstanceLongArnFormat " , value = " disabled "
2021-10-21 22:00:32 +00:00
)
response = ecs_client . register_container_instance (
cluster = test_cluster_name , instanceIdentityDocument = instance_id_document
)
full_arn = response [ " containerInstance " ] [ " containerInstanceArn " ]
2023-07-18 15:33:16 +00:00
assert full_arn . startswith (
f " arn:aws:ecs:us-east-1: { ACCOUNT_ID } :container-instance/ "
2021-10-21 22:00:32 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-21 22:00:32 +00:00
def test_run_task_default_cluster_new_arn_format ( ) :
client = boto3 . client ( " ecs " , region_name = " us-east-1 " )
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
test_cluster_name = " default "
client . create_cluster ( clusterName = test_cluster_name )
test_instance = ec2 . create_instances (
ImageId = EXAMPLE_AMI_ID , MinCount = 1 , MaxCount = 1
) [ 0 ]
instance_id_document = json . dumps (
ec2_utils . generate_instance_identity_document ( test_instance )
)
client . register_container_instance (
cluster = test_cluster_name , instanceIdentityDocument = instance_id_document
)
client . register_task_definition (
family = " test_ecs_task " ,
containerDefinitions = [
{
" name " : " hello_world " ,
" image " : " docker/hello-world:latest " ,
" cpu " : 1024 ,
" memory " : 400 ,
}
] ,
)
2022-01-18 20:53:31 +00:00
# Initial ARN is long-format
client . put_account_setting ( name = " taskLongArnFormat " , value = " enabled " )
2021-10-21 22:00:32 +00:00
response = client . run_task (
launchType = " FARGATE " ,
overrides = { } ,
taskDefinition = " test_ecs_task " ,
count = 1 ,
startedBy = " moto " ,
)
2023-07-18 15:33:16 +00:00
assert response [ " tasks " ] [ 0 ] [ " taskArn " ] . startswith (
f " arn:aws:ecs:us-east-1: { ACCOUNT_ID } :task/ { test_cluster_name } / "
2021-10-21 22:00:32 +00:00
)
2022-01-18 20:53:31 +00:00
# Enable short-format for the next task
client . put_account_setting ( name = " taskLongArnFormat " , value = " disabled " )
2021-10-21 22:00:32 +00:00
response = client . run_task (
launchType = " FARGATE " ,
overrides = { } ,
taskDefinition = " test_ecs_task " ,
count = 1 ,
startedBy = " moto " ,
)
2023-07-18 15:33:16 +00:00
assert response [ " tasks " ] [ 0 ] [ " taskArn " ] . startswith (
f " arn:aws:ecs:us-east-1: { ACCOUNT_ID } :task/ "
2021-10-21 22:00:32 +00:00
)