2019-08-14 21:11:05 +00:00
import boto3
import sure # noqa
from nose . tools import assert_raises
from botocore . client import ClientError
from moto import mock_ec2
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
resp = cli . create_launch_template (
LaunchTemplateName = " test-template " ,
# the absolute minimum needed to create a template without other resources
LaunchTemplateData = {
2019-10-31 15:44:26 +00:00
" TagSpecifications " : [
{
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " test " , " Value " : " value " } ] ,
}
]
2019-08-14 21:11:05 +00:00
} ,
)
resp . should . have . key ( " LaunchTemplate " )
lt = resp [ " LaunchTemplate " ]
lt [ " LaunchTemplateName " ] . should . equal ( " test-template " )
lt [ " DefaultVersionNumber " ] . should . equal ( 1 )
lt [ " LatestVersionNumber " ] . should . equal ( 1 )
with assert_raises ( ClientError ) as ex :
cli . create_launch_template (
LaunchTemplateName = " test-template " ,
LaunchTemplateData = {
2019-10-31 15:44:26 +00:00
" TagSpecifications " : [
{
" ResourceType " : " instance " ,
" Tags " : [ { " Key " : " test " , " Value " : " value " } ] ,
}
]
2019-08-14 21:11:05 +00:00
} ,
)
str ( ex . exception ) . should . equal (
2019-10-31 15:44:26 +00:00
" 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
2019-08-14 21:11:05 +00:00
@mock_ec2
def test_describe_launch_template_versions ( ) :
template_data = {
" ImageId " : " ami-abc123 " ,
" DisableApiTermination " : False ,
2019-10-31 15:44:26 +00:00
" TagSpecifications " : [
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " test " , " Value " : " value " } ] }
2019-08-14 21:11:05 +00:00
] ,
2019-10-31 15:44:26 +00:00
" SecurityGroupIds " : [ " sg-1234 " , " sg-ab5678 " ] ,
2019-08-14 21:11:05 +00:00
}
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
create_resp = cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = template_data
)
2019-08-14 21:11:05 +00:00
# test using name
resp = cli . describe_launch_template_versions (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , Versions = [ " 1 " ]
)
2019-08-14 21:11:05 +00:00
templ = resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ]
templ . should . equal ( template_data )
# test using id
resp = cli . describe_launch_template_versions (
LaunchTemplateId = create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ] ,
2019-10-31 15:44:26 +00:00
Versions = [ " 1 " ] ,
)
2019-08-14 21:11:05 +00:00
templ = resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ]
templ . should . equal ( template_data )
2019-08-19 23:01:44 +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
create_resp = cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-14 21:11:05 +00:00
version_resp = cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-14 21:11:05 +00:00
version_resp . should . have . key ( " LaunchTemplateVersion " )
version = version_resp [ " LaunchTemplateVersion " ]
version [ " DefaultVersion " ] . should . equal ( False )
2019-10-31 15:44:26 +00:00
version [ " LaunchTemplateId " ] . should . equal (
create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ]
)
2019-08-14 21:11:05 +00:00
version [ " VersionDescription " ] . should . equal ( " new ami " )
version [ " VersionNumber " ] . should . equal ( 2 )
2019-08-19 23:01:44 +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 " )
create_resp = cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-19 22:58:48 +00:00
version_resp = cli . create_launch_template_version (
LaunchTemplateId = create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ] ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-19 22:58:48 +00:00
version_resp . should . have . key ( " LaunchTemplateVersion " )
version = version_resp [ " LaunchTemplateVersion " ]
version [ " DefaultVersion " ] . should . equal ( False )
2019-10-31 15:44:26 +00:00
version [ " LaunchTemplateId " ] . should . equal (
create_resp [ " LaunchTemplate " ] [ " LaunchTemplateId " ]
)
2019-08-19 22:58:48 +00:00
version [ " VersionDescription " ] . should . equal ( " new ami " )
version [ " VersionNumber " ] . should . equal ( 2 )
@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
cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-14 21:11:05 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-14 21:11:05 +00:00
2019-10-31 15:44:26 +00:00
resp = cli . describe_launch_template_versions ( LaunchTemplateName = " test-template " )
2019-08-14 21:11:05 +00:00
resp [ " LaunchTemplateVersions " ] . should . have . length_of ( 2 )
2019-10-31 15:44:26 +00:00
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" ami-abc123 "
)
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" 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 " )
cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
2019-08-19 22:57:39 +00:00
resp = cli . describe_launch_template_versions (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , Versions = [ " 2 " , " 3 " ]
)
2019-08-19 22:57:39 +00:00
resp [ " LaunchTemplateVersions " ] . should . have . length_of ( 2 )
2019-10-31 15:44:26 +00:00
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" ami-def456 "
)
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" 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 " )
cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
2019-08-19 22:57:39 +00:00
resp = cli . describe_launch_template_versions (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , MinVersion = " 2 "
)
2019-08-19 22:57:39 +00:00
resp [ " LaunchTemplateVersions " ] . should . have . length_of ( 2 )
2019-10-31 15:44:26 +00:00
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" ami-def456 "
)
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" 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 " )
cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
2019-08-19 22:57:39 +00:00
resp = cli . describe_launch_template_versions (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , MaxVersion = " 2 "
)
2019-08-19 22:57:39 +00:00
resp [ " LaunchTemplateVersions " ] . should . have . length_of ( 2 )
2019-10-31 15:44:26 +00:00
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" ami-abc123 "
)
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" 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 " )
cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-def456 " } ,
VersionDescription = " new ami " ,
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-hij789 " } ,
VersionDescription = " new ami, again " ,
)
2019-08-19 22:57:39 +00:00
cli . create_launch_template_version (
LaunchTemplateName = " test-template " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-345abc " } ,
VersionDescription = " new ami, because why not " ,
)
2019-08-19 22:57:39 +00:00
resp = cli . describe_launch_template_versions (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , MinVersion = " 2 " , MaxVersion = " 3 "
)
2019-08-19 22:57:39 +00:00
resp [ " LaunchTemplateVersions " ] . should . have . length_of ( 2 )
2019-10-31 15:44:26 +00:00
resp [ " LaunchTemplateVersions " ] [ 0 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" ami-def456 "
)
resp [ " LaunchTemplateVersions " ] [ 1 ] [ " LaunchTemplateData " ] [ " ImageId " ] . should . equal (
" ami-hij789 "
)
2019-08-19 22:57:39 +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 = [ ]
r = cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-14 21:11:05 +00:00
lt_ids . append ( r [ " LaunchTemplate " ] [ " LaunchTemplateId " ] )
r = cli . create_launch_template (
LaunchTemplateName = " test-template2 " ,
2019-10-31 15:44:26 +00:00
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
)
2019-08-14 21:11:05 +00:00
lt_ids . append ( r [ " LaunchTemplate " ] [ " LaunchTemplateId " ] )
# general call, all templates
resp = cli . describe_launch_templates ( )
resp . should . have . key ( " LaunchTemplates " )
resp [ " LaunchTemplates " ] . should . have . length_of ( 2 )
resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] . should . equal ( " test-template " )
resp [ " LaunchTemplates " ] [ 1 ] [ " LaunchTemplateName " ] . should . equal ( " test-template2 " )
# filter by names
resp = cli . describe_launch_templates (
2019-10-31 15:44:26 +00:00
LaunchTemplateNames = [ " test-template2 " , " test-template " ]
)
2019-08-14 21:11:05 +00:00
resp . should . have . key ( " LaunchTemplates " )
resp [ " LaunchTemplates " ] . should . have . length_of ( 2 )
resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] . should . equal ( " test-template2 " )
resp [ " LaunchTemplates " ] [ 1 ] [ " LaunchTemplateName " ] . should . equal ( " test-template " )
# filter by ids
resp = cli . describe_launch_templates ( LaunchTemplateIds = lt_ids )
resp . should . have . key ( " LaunchTemplates " )
resp [ " LaunchTemplates " ] . should . have . length_of ( 2 )
resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] . should . equal ( " test-template " )
resp [ " LaunchTemplates " ] [ 1 ] [ " LaunchTemplateName " ] . should . equal ( " test-template2 " )
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
r = cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-14 21:11:05 +00:00
cli . create_tags (
Resources = [ r [ " LaunchTemplate " ] [ " LaunchTemplateId " ] ] ,
Tags = [
{ " Key " : " tag1 " , " Value " : " a value " } ,
{ " Key " : " another-key " , " Value " : " this value " } ,
2019-10-31 15:44:26 +00:00
] ,
)
2019-08-14 21:11:05 +00:00
cli . create_launch_template (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " no-tags " , LaunchTemplateData = { " ImageId " : " ami-abc123 " }
)
2019-08-14 21:11:05 +00:00
2019-10-31 15:44:26 +00:00
resp = cli . describe_launch_templates (
Filters = [ { " Name " : " tag:tag1 " , " Values " : [ " a value " ] } ]
)
2019-08-14 21:11:05 +00:00
resp [ " LaunchTemplates " ] . should . have . length_of ( 1 )
resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] . should . equal ( " test-template " )
2019-10-31 15:44:26 +00:00
resp = cli . describe_launch_templates (
Filters = [ { " Name " : " launch-template-name " , " Values " : [ " no-tags " ] } ]
)
2019-08-14 21:11:05 +00:00
resp [ " LaunchTemplates " ] . should . have . length_of ( 1 )
resp [ " LaunchTemplates " ] [ 0 ] [ " LaunchTemplateName " ] . should . equal ( " no-tags " )
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 " )
cli . create_launch_template (
LaunchTemplateName = " test-template " ,
2019-08-19 23:01:44 +00:00
LaunchTemplateData = { " ImageId " : " ami-abc123 " } ,
2019-10-31 15:44:26 +00:00
TagSpecifications = [
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " key " , " Value " : " value " } ] }
] ,
2019-08-19 22:58:19 +00:00
)
resp = cli . describe_launch_template_versions (
2019-10-31 15:44:26 +00:00
LaunchTemplateName = " test-template " , Versions = [ " 1 " ]
)
2019-08-19 22:58:19 +00:00
version = resp [ " LaunchTemplateVersions " ] [ 0 ]
version [ " LaunchTemplateData " ] . should . have . key ( " TagSpecifications " )
version [ " LaunchTemplateData " ] [ " TagSpecifications " ] . should . have . length_of ( 1 )
2019-10-31 15:44:26 +00:00
version [ " LaunchTemplateData " ] [ " TagSpecifications " ] [ 0 ] . should . equal (
{ " ResourceType " : " instance " , " Tags " : [ { " Key " : " key " , " Value " : " value " } ] }
)