2019-08-14 21:11:05 +00:00
import boto3
2020-10-06 05:54:49 +00:00
import pytest
2019-08-14 21:11:05 +00:00
2023-07-17 09:31:05 +00:00
from botocore . client import ClientError
2021-10-05 17:11:07 +00:00
from moto import mock_ec2 , settings
from uuid import uuid4
2019-08-14 21:11:05 +00:00
2023-03-30 15:53:26 +00:00
from tests import EXAMPLE_AMI_ID
2019-08-19 23:01:44 +00:00
2019-08-14 21:11:05 +00:00
@mock_ec2
def test_launch_template_create ( ) :
2019-08-14 21:32:01 +00:00
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2019-08-14 21:11:05 +00:00
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
resp = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-14 21:11:05 +00:00
# the absolute minimum needed to create a template without other resources
LaunchTemplateData = {
" TagSpecifications " : [
{
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " test " , " Value " : " value " } ] ,
}
2019-10-31 15:44:26 +00:00
]
2019-08-14 21:11:05 +00:00
} ,
)
2023-07-17 09:31:05 +00:00
assert " LaunchTemplate " in resp
2019-08-14 21:11:05 +00:00
lt = resp [ " LaunchTemplate " ]
2023-07-17 09:31:05 +00:00
assert lt [ " LaunchTemplateName " ] == template_name
assert lt [ " DefaultVersionNumber " ] == 1
assert lt [ " LatestVersionNumber " ] == 1
2019-08-14 21:11:05 +00:00
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2019-08-14 21:11:05 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-14 21:11:05 +00:00
LaunchTemplateData = {
" TagSpecifications " : [
{
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " test " , " Value " : " value " } ] ,
}
2019-10-31 15:44:26 +00:00
]
2019-08-14 21:11:05 +00:00
} ,
)
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== " An error occurred (InvalidLaunchTemplateName.AlreadyExistsException) when calling the CreateLaunchTemplate operation: Launch template name already in use. "
2019-08-14 21:11:05 +00:00
)
2019-08-19 22:57:39 +00:00
2023-02-14 13:43:28 +00:00
@mock_ec2
def test_create_launch_template__dryrun ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
with pytest . raises ( ClientError ) as exc :
cli . create_launch_template (
DryRun = True ,
LaunchTemplateName = template_name ,
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
TagSpecifications = [
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " key " , " Value " : " value " } ] }
] ,
)
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " DryRunOperation "
assert (
err [ " Message " ]
== " An error occurred (DryRunOperation) when calling the CreateLaunchTemplate operation: Request would have succeeded, but DryRun flag is set "
2023-02-14 13:43:28 +00:00
)
2019-08-14 21:11:05 +00:00
@mock_ec2
def test_describe_launch_template_versions ( ) :
template_data = {
" ImageId " : " ami-abc123 " ,
" DisableApiTermination " : False ,
" TagSpecifications " : [
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " test " , " Value " : " value " } ] }
] ,
" SecurityGroupIds " : [ " sg-1234 " , " sg-ab5678 " ] ,
}
2019-08-14 21:32:01 +00:00
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2019-08-14 21:11:05 +00:00
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
create_resp = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = template_data
2019-08-14 21:11:05 +00:00
)
# test using name
resp = cli . describe_launch_template_versions (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , Versions = [ " 1 " ]
2019-08-14 21:11:05 +00:00
)
templ = resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ]
2023-07-17 09:31:05 +00:00
assert templ == template_data
2019-08-14 21:11:05 +00:00
# test using id
resp = cli . describe_launch_template_versions (
LaunchTemplateId = create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ] ,
Versions = [ " 1 " ] ,
)
templ = resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ]
2023-07-17 09:31:05 +00:00
assert templ == template_data
2019-08-14 21:11:05 +00:00
2023-01-11 19:31:14 +00:00
# test using $Latest version
resp = cli . describe_launch_template_versions (
LaunchTemplateName = template_name , Versions = [ " $Latest " ]
)
templ = resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ]
2023-07-17 09:31:05 +00:00
assert templ == template_data
2023-01-11 19:31:14 +00:00
# test using $Default version
resp = cli . describe_launch_template_versions (
LaunchTemplateName = template_name , Versions = [ " $Default " ]
)
templ = resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ]
2023-07-17 09:31:05 +00:00
assert templ == template_data
2023-01-11 19:31:14 +00:00
2019-08-19 23:01:44 +00:00
2022-09-16 10:01:43 +00:00
@mock_ec2
def test_describe_launch_template_versions_by_name_when_absent ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = " foo "
# test using name
with pytest . raises (
ClientError ,
match = f " The specified launch template, with template name { template_name } , does not exist " ,
) :
cli . describe_launch_template_versions ( LaunchTemplateName = template_name )
2023-07-04 09:36:34 +00:00
# test default response
resp = cli . describe_launch_template_versions ( )
assert resp [ " LaunchTemplateVersions " ] == [ ]
# test using $Latest version
resp = cli . describe_launch_template_versions ( Versions = [ " $Latest " ] )
assert resp [ " LaunchTemplateVersions " ] == [ ]
2022-09-16 10:01:43 +00:00
2019-08-14 21:11:05 +00:00
@mock_ec2
def test_create_launch_template_version ( ) :
2019-08-14 21:32:01 +00:00
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2019-08-14 21:11:05 +00:00
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
create_resp = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-14 21:11:05 +00:00
)
version_resp = cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-14 21:11:05 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2023-07-17 09:31:05 +00:00
assert " LaunchTemplateVersion " in version_resp
2019-08-14 21:11:05 +00:00
version = version_resp [ " LaunchTemplateVersion " ]
2023-07-17 09:31:05 +00:00
assert version [ " DefaultVersion " ] is False
assert (
version [ " LaunchTemplateId " ] == create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ]
2019-08-14 21:11:05 +00:00
)
2023-07-17 09:31:05 +00:00
assert version [ " VersionDescription " ] == " new ami "
assert version [ " VersionNumber " ] == 2
2019-08-14 21:11:05 +00:00
2019-08-19 23:01:44 +00:00
2023-02-14 13:43:28 +00:00
@mock_ec2
def test_create_launch_template_version__dryrun ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
cli . create_launch_template (
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
with pytest . raises ( ClientError ) as exc :
cli . create_launch_template_version (
DryRun = True ,
LaunchTemplateName = template_name ,
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " DryRunOperation "
assert (
err [ " Message " ]
== " An error occurred (DryRunOperation) when calling the CreateLaunchTemplateVersion operation: Request would have succeeded, but DryRun flag is set "
2023-02-14 13:43:28 +00:00
)
2019-08-14 21:11:05 +00:00
@mock_ec2
2019-08-19 22:58:48 +00:00
def test_create_launch_template_version_by_id ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-19 22:58:48 +00:00
create_resp = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-19 22:58:48 +00:00
)
version_resp = cli . create_launch_template_version (
LaunchTemplateId = create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ] ,
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2023-07-17 09:31:05 +00:00
assert " LaunchTemplateVersion " in version_resp
2019-08-19 22:58:48 +00:00
version = version_resp [ " LaunchTemplateVersion " ]
2023-07-17 09:31:05 +00:00
assert version [ " DefaultVersion " ] is False
assert (
version [ " LaunchTemplateId " ] == create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ]
2019-08-19 22:58:48 +00:00
)
2023-07-17 09:31:05 +00:00
assert version [ " VersionDescription " ] == " new ami "
assert version [ " VersionNumber " ] == 2
2019-08-19 22:58:48 +00:00
@mock_ec2
def test_describe_launch_template_versions_with_multiple_versions ( ) :
2019-08-14 21:32:01 +00:00
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2019-08-14 21:11:05 +00:00
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-14 21:11:05 +00:00
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-14 21:11:05 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2021-10-05 17:11:07 +00:00
resp = cli . describe_launch_template_versions ( LaunchTemplateName = template_name )
2019-08-14 21:11:05 +00:00
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplateVersions " ] ) == 2
assert (
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-abc123 "
2019-08-14 21:11:05 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-def456 "
2019-08-14 21:11:05 +00:00
)
2019-08-19 22:57:39 +00:00
@mock_ec2
def test_describe_launch_template_versions_with_versions_option ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-19 22:57:39 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-19 22:57:39 +00:00
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
resp = cli . describe_launch_template_versions (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , Versions = [ " 2 " , " 3 " ]
2019-08-19 23:01:44 +00:00
)
2019-08-19 22:57:39 +00:00
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplateVersions " ] ) == 2
assert (
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-def456 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-hij789 "
2019-08-19 22:57:39 +00:00
)
@mock_ec2
def test_describe_launch_template_versions_with_min ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-19 22:57:39 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-19 22:57:39 +00:00
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
resp = cli . describe_launch_template_versions (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , MinVersion = " 2 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplateVersions " ] ) == 2
assert (
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-def456 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-hij789 "
2019-08-19 22:57:39 +00:00
)
@mock_ec2
def test_describe_launch_template_versions_with_max ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-19 22:57:39 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-19 22:57:39 +00:00
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
resp = cli . describe_launch_template_versions (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , MaxVersion = " 2 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplateVersions " ] ) == 2
assert (
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-abc123 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-def456 "
2019-08-19 22:57:39 +00:00
)
@mock_ec2
def test_describe_launch_template_versions_with_min_and_max ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-19 22:57:39 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-19 22:57:39 +00:00
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
cli . create_launch_template_version (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 22:57:39 +00:00
LaunchTemplateData = { " ImageId " : " ami-345abc " } ,
VersionDescription = " new ami, because why not " ,
)
resp = cli . describe_launch_template_versions (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , MinVersion = " 2 " , MaxVersion = " 3 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplateVersions " ] ) == 2
assert (
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-def456 "
2019-08-19 22:57:39 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ]
== " ami-hij789 "
2019-08-19 22:57:39 +00:00
)
2022-08-10 19:17:30 +00:00
@mock_ec2
def test_describe_launch_templates_with_non_existent_name ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
with pytest . raises ( ClientError ) as ex :
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== " An error occurred (InvalidLaunchTemplateName.NotFoundException) when calling the DescribeLaunchTemplates operation: At least one of the launch templates specified in the request does not exist. "
2022-08-10 19:17:30 +00:00
)
2019-08-14 21:11:05 +00:00
@mock_ec2
def test_describe_launch_templates ( ) :
2019-08-14 21:32:01 +00:00
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2019-08-14 21:11:05 +00:00
lt_ids = [ ]
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
r = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-14 21:11:05 +00:00
)
lt_ids . append ( r [ " LaunchTemplate " ] [ " LaunchTemplateId " ] )
2021-10-05 17:11:07 +00:00
template_name2 = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
r = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name2 , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-14 21:11:05 +00:00
)
lt_ids . append ( r [ " LaunchTemplate " ] [ " LaunchTemplateId " ] )
# general call, all templates
2021-10-05 17:11:07 +00:00
if not settings . TEST_SERVER_MODE :
# Bug: Only 15 launch templates are returned, ever
# ServerMode may have many more templates, created in parallel
all_templates = retrieve_all_templates ( cli )
my_templates = [ t for t in all_templates if t [ " LaunchTemplateId " ] in lt_ids ]
2023-07-17 09:31:05 +00:00
assert my_templates [ 0 ] [ " LaunchTemplateName " ] == template_name
assert my_templates [ 1 ] [ " LaunchTemplateName " ] == template_name2
2019-08-14 21:11:05 +00:00
# filter by names
resp = cli . describe_launch_templates (
2021-10-05 17:11:07 +00:00
LaunchTemplateNames = [ template_name2 , template_name ]
2019-08-14 21:11:05 +00:00
)
2023-07-17 09:31:05 +00:00
assert " LaunchTemplates " in resp
assert len ( resp [ " LaunchTemplates " ] ) == 2
assert resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] == template_name2
assert resp [ " LaunchTemplates " ] [ 1 ] [ " LaunchTemplateName " ] == template_name
2019-08-14 21:11:05 +00:00
# filter by ids
resp = cli . describe_launch_templates ( LaunchTemplateIds = lt_ids )
2023-07-17 09:31:05 +00:00
assert " LaunchTemplates " in resp
assert len ( resp [ " LaunchTemplates " ] ) == 2
assert resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] == template_name
assert resp [ " LaunchTemplates " ] [ 1 ] [ " LaunchTemplateName " ] == template_name2
2019-08-14 21:11:05 +00:00
2019-08-19 23:01:44 +00:00
2019-08-14 21:11:05 +00:00
@mock_ec2
def test_describe_launch_templates_with_filters ( ) :
2019-08-14 21:32:01 +00:00
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2019-08-14 21:11:05 +00:00
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
r = cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
2019-08-14 21:11:05 +00:00
)
2021-10-05 17:11:07 +00:00
tag_value = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
cli . create_tags (
Resources = [ r [ " LaunchTemplate " ] [ " LaunchTemplateId " ] ] ,
Tags = [
2021-10-05 17:11:07 +00:00
{ " Key " : " tag1 " , " Value " : tag_value } ,
2019-08-14 21:11:05 +00:00
{ " Key " : " another-key " , " Value " : " this value " } ,
] ,
)
2021-10-05 17:11:07 +00:00
template_name_no_tags = str ( uuid4 ( ) )
2019-08-14 21:11:05 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name_no_tags ,
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
2019-08-14 21:11:05 +00:00
)
resp = cli . describe_launch_templates (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " tag:tag1 " , " Values " : [ tag_value ] } ]
2019-08-14 21:11:05 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplates " ] ) == 1
assert resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] == template_name
2019-08-14 21:11:05 +00:00
resp = cli . describe_launch_templates (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " launch-template-name " , " Values " : [ template_name_no_tags ] } ]
2019-08-14 21:11:05 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " LaunchTemplates " ] ) == 1
assert resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] == template_name_no_tags
2019-08-14 21:11:05 +00:00
2019-08-19 22:58:19 +00:00
@mock_ec2
def test_create_launch_template_with_tag_spec ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
2021-10-05 17:11:07 +00:00
template_name = str ( uuid4 ( ) )
2019-08-19 22:58:19 +00:00
cli . create_launch_template (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name ,
2019-08-19 23:01:44 +00:00
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
2019-08-19 22:58:19 +00:00
TagSpecifications = [
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " key " , " Value " : " value " } ] }
] ,
)
resp = cli . describe_launch_template_versions (
2021-10-05 17:11:07 +00:00
LaunchTemplateName = template_name , Versions = [ " 1 " ]
2019-08-19 22:58:19 +00:00
)
version = resp [ " LaunchTemplateVersions " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert " TagSpecifications " in version [ " LaunchTemplateData " ]
assert len ( version [ " LaunchTemplateData " ] [ " TagSpecifications " ] ) == 1
assert version [ " LaunchTemplateData " ] [ " TagSpecifications " ] [ 0 ] == {
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " key " , " Value " : " value " } ] ,
}
2021-10-05 17:11:07 +00:00
2023-03-30 15:53:26 +00:00
@mock_ec2
def test_get_launch_template_data ( ) :
client = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
reservation = client . run_instances ( ImageId = EXAMPLE_AMI_ID , MinCount = 1 , MaxCount = 1 )
instance = reservation [ " Instances " ] [ 0 ]
launch_template_data = client . get_launch_template_data (
InstanceId = instance [ " InstanceId " ]
) [ " LaunchTemplateData " ]
# Ensure launch template data matches instance
2023-07-17 09:31:05 +00:00
assert launch_template_data [ " ImageId " ] == instance [ " ImageId " ]
assert launch_template_data [ " InstanceType " ] == instance [ " InstanceType " ]
2023-03-30 15:53:26 +00:00
# Ensure a launch template can be created from this data
client . create_launch_template (
LaunchTemplateName = str ( uuid4 ( ) ) ,
LaunchTemplateData = launch_template_data ,
)
2022-03-29 21:46:06 +00:00
@mock_ec2
def test_delete_launch_template__dryrun ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
cli . create_launch_template (
LaunchTemplateName = template_name ,
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
TagSpecifications = [
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " key " , " Value " : " value " } ] }
] ,
)
2023-07-17 09:31:05 +00:00
assert (
len (
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] ) [
" LaunchTemplates "
]
)
== 1
)
2022-03-29 21:46:06 +00:00
with pytest . raises ( ClientError ) as exc :
cli . delete_launch_template ( DryRun = True , LaunchTemplateName = template_name )
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " DryRunOperation "
assert (
err [ " Message " ]
== " An error occurred (DryRunOperation) when calling the DeleteLaunchTemplate operation: Request would have succeeded, but DryRun flag is set "
2023-02-14 13:43:28 +00:00
)
2022-03-29 21:46:06 +00:00
# Template still exists
2023-07-17 09:31:05 +00:00
assert (
len (
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] ) [
" LaunchTemplates "
]
)
== 1
)
2022-03-29 21:46:06 +00:00
@mock_ec2
def test_delete_launch_template__by_name ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
cli . create_launch_template (
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2023-07-17 09:31:05 +00:00
assert (
len (
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] ) [
" LaunchTemplates "
]
)
== 1
)
2022-03-29 21:46:06 +00:00
cli . delete_launch_template ( LaunchTemplateName = template_name )
2022-12-02 13:17:06 +00:00
with pytest . raises ( ClientError ) as exc :
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] ) [
" LaunchTemplates "
]
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " InvalidLaunchTemplateName.NotFoundException "
assert (
err [ " Message " ]
== " At least one of the launch templates specified in the request does not exist. "
2022-12-02 13:17:06 +00:00
)
# Ensure deleted template name can be reused
cli . create_launch_template (
LaunchTemplateName = template_name ,
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
)
2022-03-29 21:46:06 +00:00
@mock_ec2
def test_delete_launch_template__by_id ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
2022-12-02 13:17:06 +00:00
with pytest . raises ( ClientError ) as exc :
cli . delete_launch_template ( )
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " MissingParameter "
assert (
err [ " Message " ]
== " The request must contain the parameter launch template ID or launch template name "
2022-12-02 13:17:06 +00:00
)
2022-03-29 21:46:06 +00:00
template_id = cli . create_launch_template (
LaunchTemplateName = template_name , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
) [ " LaunchTemplate " ] [ " LaunchTemplateId " ]
2023-07-17 09:31:05 +00:00
assert (
len (
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] ) [
" LaunchTemplates "
]
)
== 1
)
2022-03-29 21:46:06 +00:00
cli . delete_launch_template ( LaunchTemplateId = template_id )
2022-12-02 13:17:06 +00:00
with pytest . raises ( ClientError ) as exc :
2023-07-17 09:31:05 +00:00
cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] )
2022-12-02 13:17:06 +00:00
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " InvalidLaunchTemplateName.NotFoundException "
assert (
err [ " Message " ]
== " At least one of the launch templates specified in the request does not exist. "
2022-12-02 13:17:06 +00:00
)
# Ensure deleted template name can be reused
cli . create_launch_template (
LaunchTemplateName = template_name ,
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
)
2022-03-29 21:46:06 +00:00
2021-10-18 19:44:29 +00:00
def retrieve_all_templates ( client , filters = [ ] ) : # pylint: disable=W0102
2021-10-05 17:11:07 +00:00
resp = client . describe_launch_templates ( Filters = filters )
all_templates = resp [ " LaunchTemplates " ]
next_token = resp . get ( " NextToken " )
while next_token :
resp = client . describe_launch_templates ( Filters = filters , NextToken = next_token )
all_templates . extend ( resp [ " LaunchTemplates " ] )
next_token = resp . get ( " NextToken " )
return all_templates
2022-08-23 14:52:42 +00:00
@mock_ec2
def test_launch_template_create_with_tags ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
resp = cli . create_launch_template (
LaunchTemplateName = template_name ,
# the absolute minimum needed to create a template without other resources
LaunchTemplateData = {
" TagSpecifications " : [
{
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " test " , " Value " : " value " } ] ,
}
]
} ,
TagSpecifications = [
{
" ResourceType " : " launch-template " ,
" Tags " : [ { " Key " : " test1 " , " Value " : " value1 " } ] ,
}
] ,
)
2023-07-17 09:31:05 +00:00
assert " LaunchTemplate " in resp
2022-08-23 14:52:42 +00:00
lt = resp [ " LaunchTemplate " ]
2023-07-17 09:31:05 +00:00
assert lt [ " LaunchTemplateName " ] == template_name
assert lt [ " DefaultVersionNumber " ] == 1
assert lt [ " LatestVersionNumber " ] == 1
assert len ( lt [ " Tags " ] ) == 1
assert lt [ " Tags " ] [ 0 ] == { " Key " : " test1 " , " Value " : " value1 " }
2022-08-23 14:52:42 +00:00
@mock_ec2
def test_launch_template_describe_with_tags ( ) :
cli = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
template_name = str ( uuid4 ( ) )
cli . create_launch_template (
LaunchTemplateName = template_name ,
# the absolute minimum needed to create a template without other resources
LaunchTemplateData = {
" TagSpecifications " : [
{
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " test " , " Value " : " value " } ] ,
}
]
} ,
TagSpecifications = [
{
" ResourceType " : " launch-template " ,
" Tags " : [ { " Key " : " test1 " , " Value " : " value1 " } ] ,
}
] ,
)
lt = cli . describe_launch_templates ( LaunchTemplateNames = [ template_name ] ) [
" LaunchTemplates "
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert lt [ " LaunchTemplateName " ] == template_name
assert lt [ " DefaultVersionNumber " ] == 1
assert lt [ " LatestVersionNumber " ] == 1
assert len ( lt [ " Tags " ] ) == 1
assert lt [ " Tags " ] [ 0 ] == { " Key " : " test1 " , " Value " : " value1 " }