2016-06-03 13:57:33 +00:00
import boto3
2017-04-28 15:56:32 +00:00
import botocore
2020-10-06 05:54:49 +00:00
import pytest
2023-07-07 09:10:52 +00:00
import requests
2023-07-08 21:53:09 +00:00
from botocore . exceptions import ClientError
2023-07-07 09:10:52 +00:00
from moto import mock_ec2 , mock_route53 , settings
2013-11-14 19:14:14 +00:00
2021-09-23 10:27:09 +00:00
@mock_route53
2022-04-18 20:44:56 +00:00
def test_create_hosted_zone ( ) :
2021-09-23 10:27:09 +00:00
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
response = conn . create_hosted_zone (
Name = " testdns.aws.com. " , CallerReference = str ( hash ( " foo " ) )
)
firstzone = response [ " HostedZone " ]
2023-07-08 21:53:09 +00:00
assert " /hostedzone/ " in firstzone [ " Id " ]
assert firstzone [ " Name " ] == " testdns.aws.com. "
assert firstzone [ " Config " ] == { " PrivateZone " : False }
assert firstzone [ " ResourceRecordSetCount " ] == 2
2021-09-23 10:27:09 +00:00
delegation = response [ " DelegationSet " ]
2023-07-08 21:53:09 +00:00
assert len ( delegation [ " NameServers " ] ) == 4
assert " ns-2048.awsdns-64.com " in delegation [ " NameServers " ]
assert " ns-2049.awsdns-65.net " in delegation [ " NameServers " ]
assert " ns-2050.awsdns-66.org " in delegation [ " NameServers " ]
assert " ns-2051.awsdns-67.co.uk " in delegation [ " NameServers " ]
2021-09-23 10:27:09 +00:00
2023-07-07 09:10:52 +00:00
location = response [ " Location " ]
if not settings . TEST_SERVER_MODE :
assert " <Name>testdns.aws.com.</Name> " in requests . get ( location ) . text
2021-09-23 10:27:09 +00:00
2023-10-15 21:39:27 +00:00
@mock_route53
def test_get_hosted_zone ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
name = " testdns.aws.com. "
caller_ref = str ( hash ( " foo " ) )
zone = conn . create_hosted_zone ( Name = name , CallerReference = caller_ref ) [ " HostedZone " ]
res = conn . get_hosted_zone ( Id = zone [ " Id " ] )
assert res [ " HostedZone " ] [ " Name " ] == name
assert res [ " HostedZone " ] [ " CallerReference " ] == caller_ref
2021-09-23 10:27:09 +00:00
@mock_route53
def test_list_hosted_zones ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
res = conn . list_hosted_zones ( ) [ " HostedZones " ]
2023-07-08 21:53:09 +00:00
assert len ( res ) == 0
2021-09-23 10:27:09 +00:00
zone1 = conn . create_hosted_zone (
Name = " testdns1.aws.com. " , CallerReference = str ( hash ( " foo " ) )
) [ " HostedZone " ]
zone2 = conn . create_hosted_zone (
Name = " testdns2.aws.com. " , CallerReference = str ( hash ( " foo " ) )
) [ " HostedZone " ]
res = conn . list_hosted_zones ( ) [ " HostedZones " ]
2023-07-08 21:53:09 +00:00
assert len ( res ) == 2
2021-09-23 10:27:09 +00:00
2023-07-08 21:53:09 +00:00
assert zone1 in res
assert zone2 in res
2021-09-23 10:27:09 +00:00
@mock_route53
def test_delete_hosted_zone ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone1 = conn . create_hosted_zone (
Name = " testdns1.aws.com. " , CallerReference = str ( hash ( " foo " ) )
) [ " HostedZone " ]
conn . create_hosted_zone ( Name = " testdns2.aws.com. " , CallerReference = str ( hash ( " foo " ) ) )
conn . delete_hosted_zone ( Id = zone1 [ " Id " ] )
res = conn . list_hosted_zones ( ) [ " HostedZones " ]
2023-07-08 21:53:09 +00:00
assert len ( res ) == 1
2021-09-23 10:27:09 +00:00
2022-06-12 17:53:27 +00:00
@mock_route53
def test_delete_hosted_zone_with_change_sets ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone_id = conn . create_hosted_zone (
Name = " testdns.aws.com. " , CallerReference = str ( hash ( " foo " ) )
) [ " HostedZone " ] [ " Id " ]
conn . change_resource_record_sets (
HostedZoneId = zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " foo.bar.testdns.aws.com " ,
" Type " : " A " ,
" ResourceRecords " : [ { " Value " : " 1.2.3.4 " } ] ,
} ,
}
]
} ,
)
with pytest . raises ( ClientError ) as exc :
conn . delete_hosted_zone ( Id = zone_id )
err = exc . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " HostedZoneNotEmpty "
assert (
err [ " Message " ]
== " The hosted zone contains resource records that are not SOA or NS records. "
2022-06-12 17:53:27 +00:00
)
2022-01-30 13:25:36 +00:00
@mock_route53
def test_get_hosted_zone_count_no_zones ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone_count = conn . get_hosted_zone_count ( )
2023-07-08 21:53:09 +00:00
assert zone_count [ " HostedZoneCount " ] == 0
2022-01-30 13:25:36 +00:00
@mock_route53
def test_get_hosted_zone_count_one_zone ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone = " a "
zone_name = f " test. { zone } .com. "
conn . create_hosted_zone (
Name = zone_name ,
CallerReference = str ( hash ( " foo " ) ) ,
HostedZoneConfig = dict ( PrivateZone = False , Comment = f " test { zone } com " ) ,
)
zone_count = conn . get_hosted_zone_count ( )
2023-07-08 21:53:09 +00:00
assert zone_count [ " HostedZoneCount " ] == 1
2022-01-30 13:25:36 +00:00
@mock_route53
def test_get_hosted_zone_count_many_zones ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zones = { }
zone_indexes = [ ]
for char in range ( ord ( " a " ) , ord ( " d " ) + 1 ) :
for char2 in range ( ord ( " a " ) , ord ( " z " ) + 1 ) :
zone_indexes . append ( f " { chr ( char ) } { chr ( char2 ) } " )
# Create 100-ish zones and make sure we get 100 back. This works
# for 702 zones {a..zz}, but seemed a needless waste of
# time/resources.
for zone in zone_indexes :
zone_name = f " test. { zone } .com. "
zones [ zone ] = conn . create_hosted_zone (
Name = zone_name ,
CallerReference = str ( hash ( " foo " ) ) ,
HostedZoneConfig = dict ( PrivateZone = False , Comment = f " test { zone } com " ) ,
)
zone_count = conn . get_hosted_zone_count ( )
2023-07-08 21:53:09 +00:00
assert zone_count [ " HostedZoneCount " ] == len ( zone_indexes )
2022-01-30 13:25:36 +00:00
2021-09-23 10:27:09 +00:00
@mock_route53
def test_get_unknown_hosted_zone ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
with pytest . raises ( ClientError ) as ex :
conn . get_hosted_zone ( Id = " unknown " )
err = ex . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " NoSuchHostedZone "
assert err [ " Message " ] == " No hosted zone found with ID: unknown "
2021-09-23 10:27:09 +00:00
2022-06-19 13:43:57 +00:00
@mock_route53
def test_update_hosted_zone_comment ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
response = conn . create_hosted_zone (
Name = " testdns.aws.com. " , CallerReference = str ( hash ( " foo " ) )
)
zone_id = response [ " HostedZone " ] [ " Id " ] . split ( " / " ) [ - 1 ]
conn . update_hosted_zone_comment ( Id = zone_id , Comment = " yolo " )
resp = conn . get_hosted_zone ( Id = zone_id ) [ " HostedZone " ]
2023-07-08 21:53:09 +00:00
assert resp [ " Config " ] [ " Comment " ] == " yolo "
2022-06-19 13:43:57 +00:00
2021-09-23 10:27:09 +00:00
@mock_route53
def test_list_resource_record_set_unknown_zone ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
with pytest . raises ( ClientError ) as ex :
conn . list_resource_record_sets ( HostedZoneId = " abcd " )
err = ex . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " NoSuchHostedZone "
assert err [ " Message " ] == " No hosted zone found with ID: abcd "
2021-09-23 10:27:09 +00:00
@mock_route53
def test_list_resource_record_set_unknown_type ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone = conn . create_hosted_zone (
Name = " testdns1.aws.com. " , CallerReference = str ( hash ( " foo " ) )
) [ " HostedZone " ]
with pytest . raises ( ClientError ) as ex :
conn . list_resource_record_sets ( HostedZoneId = zone [ " Id " ] , StartRecordType = " A " )
err = ex . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " 400 "
assert err [ " Message " ] == " Bad Request "
2021-09-23 10:27:09 +00:00
@mock_route53
2022-04-18 20:44:56 +00:00
def test_use_health_check_in_resource_record_set ( ) :
2021-09-23 10:27:09 +00:00
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
check = conn . create_health_check (
CallerReference = " ? " ,
HealthCheckConfig = {
" IPAddress " : " 10.0.0.25 " ,
" Port " : 80 ,
" Type " : " HTTP " ,
" ResourcePath " : " / " ,
" RequestInterval " : 10 ,
" FailureThreshold " : 2 ,
} ,
) [ " HealthCheck " ]
check_id = check [ " Id " ]
zone = conn . create_hosted_zone (
Name = " testdns.aws.com " , CallerReference = str ( hash ( " foo " ) )
)
zone_id = zone [ " HostedZone " ] [ " Id " ]
conn . change_resource_record_sets (
HostedZoneId = zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " foo.bar.testdns.aws.com " ,
" Type " : " A " ,
" HealthCheckId " : check_id ,
" ResourceRecords " : [ { " Value " : " 1.2.3.4 " } ] ,
} ,
}
]
} ,
)
record_sets = conn . list_resource_record_sets ( HostedZoneId = zone_id ) [
" ResourceRecordSets "
]
2023-07-08 21:53:09 +00:00
assert record_sets [ 2 ] [ " Name " ] == " foo.bar.testdns.aws.com. "
assert record_sets [ 2 ] [ " HealthCheckId " ] == check_id
2021-09-23 10:27:09 +00:00
@mock_route53
2022-04-18 20:44:56 +00:00
def test_hosted_zone_comment_preserved ( ) :
2021-09-23 10:27:09 +00:00
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
firstzone = conn . create_hosted_zone (
Name = " testdns.aws.com " ,
CallerReference = str ( hash ( " foo " ) ) ,
HostedZoneConfig = { " Comment " : " test comment " } ,
)
zone_id = firstzone [ " HostedZone " ] [ " Id " ]
hosted_zone = conn . get_hosted_zone ( Id = zone_id )
2023-07-08 21:53:09 +00:00
assert hosted_zone [ " HostedZone " ] [ " Config " ] [ " Comment " ] == " test comment "
2021-09-23 10:27:09 +00:00
hosted_zones = conn . list_hosted_zones ( )
2023-07-08 21:53:09 +00:00
assert hosted_zones [ " HostedZones " ] [ 0 ] [ " Config " ] [ " Comment " ] == " test comment "
2021-09-23 10:27:09 +00:00
@mock_route53
2022-04-18 20:44:56 +00:00
def test_deleting_weighted_route ( ) :
2021-09-23 10:27:09 +00:00
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone = conn . create_hosted_zone (
Name = " testdns.aws.com " , CallerReference = str ( hash ( " foo " ) )
)
zone_id = zone [ " HostedZone " ] [ " Id " ]
for identifier in [ " success-test-foo " , " success-test-bar " ] :
conn . change_resource_record_sets (
HostedZoneId = zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " cname.testdns.aws.com " ,
" Type " : " CNAME " ,
" SetIdentifier " : identifier ,
" Weight " : 50 ,
} ,
}
]
} ,
)
cnames = conn . list_resource_record_sets (
HostedZoneId = zone_id , StartRecordName = " cname " , StartRecordType = " CNAME "
) [ " ResourceRecordSets " ]
2023-07-08 21:53:09 +00:00
assert len ( cnames ) == 4
2021-09-23 10:27:09 +00:00
conn . change_resource_record_sets (
HostedZoneId = zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " DELETE " ,
" ResourceRecordSet " : {
" Name " : " cname.testdns.aws.com " ,
" Type " : " CNAME " ,
" SetIdentifier " : " success-test-foo " ,
2022-12-10 11:07:30 +00:00
" Weight " : 50 ,
2021-09-23 10:27:09 +00:00
} ,
}
]
} ,
)
cnames = conn . list_resource_record_sets (
HostedZoneId = zone_id , StartRecordName = " cname " , StartRecordType = " CNAME "
) [ " ResourceRecordSets " ]
2023-07-08 21:53:09 +00:00
assert len ( cnames ) == 3
assert cnames [ - 1 ] [ " Name " ] == " cname.testdns.aws.com. "
assert cnames [ - 1 ] [ " SetIdentifier " ] == " success-test-bar "
2021-09-23 10:27:09 +00:00
@mock_route53
2022-04-18 20:44:56 +00:00
def test_deleting_latency_route ( ) :
2021-09-23 10:27:09 +00:00
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone = conn . create_hosted_zone (
Name = " testdns.aws.com " , CallerReference = str ( hash ( " foo " ) )
)
zone_id = zone [ " HostedZone " ] [ " Id " ]
for _id , region in [
( " success-test-foo " , " us-west-2 " ) ,
( " success-test-bar " , " us-west-1 " ) ,
] :
conn . change_resource_record_sets (
HostedZoneId = zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " cname.testdns.aws.com " ,
" Type " : " CNAME " ,
" SetIdentifier " : _id ,
" Region " : region ,
" ResourceRecords " : [ { " Value " : " example.com " } ] ,
} ,
}
]
} ,
)
cnames = conn . list_resource_record_sets (
HostedZoneId = zone_id , StartRecordName = " cname " , StartRecordType = " CNAME "
) [ " ResourceRecordSets " ]
2023-07-08 21:53:09 +00:00
assert len ( cnames ) == 4
2021-09-23 10:27:09 +00:00
foo_cname = [
2022-06-10 19:33:17 +00:00
cname
for cname in cnames
if cname . get ( " SetIdentifier " ) and cname [ " SetIdentifier " ] == " success-test-foo "
2021-09-23 10:27:09 +00:00
] [ 0 ]
2023-07-08 21:53:09 +00:00
assert foo_cname [ " Region " ] == " us-west-2 "
2021-09-23 10:27:09 +00:00
conn . change_resource_record_sets (
HostedZoneId = zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " DELETE " ,
" ResourceRecordSet " : {
" Name " : " cname.testdns.aws.com " ,
" Type " : " CNAME " ,
" SetIdentifier " : " success-test-foo " ,
2022-12-10 11:07:30 +00:00
" Region " : " us-west-2 " ,
" ResourceRecords " : [ { " Value " : " example.com " } ] ,
2021-09-23 10:27:09 +00:00
} ,
}
]
} ,
)
cnames = conn . list_resource_record_sets (
HostedZoneId = zone_id , StartRecordName = " cname " , StartRecordType = " CNAME "
) [ " ResourceRecordSets " ]
2023-07-08 21:53:09 +00:00
assert len ( cnames ) == 3
assert cnames [ - 1 ] [ " SetIdentifier " ] == " success-test-bar "
assert cnames [ - 1 ] [ " Region " ] == " us-west-1 "
2021-09-23 10:27:09 +00:00
2016-09-21 00:41:23 +00:00
@mock_route53
def test_list_or_change_tags_for_resource_request ( ) :
2017-02-24 00:43:48 +00:00
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
health_check = conn . create_health_check (
CallerReference = " foobar " ,
HealthCheckConfig = {
" IPAddress " : " 192.0.2.44 " ,
" Port " : 123 ,
" Type " : " HTTP " ,
" ResourcePath " : " / " ,
" RequestInterval " : 30 ,
" FailureThreshold " : 123 ,
" HealthThreshold " : 123 ,
} ,
)
healthcheck_id = health_check [ " HealthCheck " ] [ " Id " ]
2016-09-21 00:41:23 +00:00
2019-08-28 18:55:19 +00:00
# confirm this works for resources with zero tags
response = conn . list_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id
)
2023-07-08 21:53:09 +00:00
assert response [ " ResourceTagSet " ] [ " Tags " ] == [ ]
2019-08-28 18:55:19 +00:00
2016-09-21 00:41:23 +00:00
tag1 = { " Key " : " Deploy " , " Value " : " True " }
tag2 = { " Key " : " Name " , " Value " : " UnitTest " }
# Test adding a tag for a resource id
conn . change_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id , AddTags = [ tag1 , tag2 ]
)
# Check to make sure that the response has the 'ResourceTagSet' key
2017-02-24 02:37:43 +00:00
response = conn . list_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id
)
2023-07-08 21:53:09 +00:00
assert " ResourceTagSet " in response
2016-09-21 00:41:23 +00:00
# Validate that each key was added
2023-07-08 21:53:09 +00:00
assert tag1 in response [ " ResourceTagSet " ] [ " Tags " ]
assert tag2 in response [ " ResourceTagSet " ] [ " Tags " ]
2016-09-21 00:41:23 +00:00
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceTagSet " ] [ " Tags " ] ) == 2
2016-09-23 01:44:07 +00:00
2016-09-21 00:41:23 +00:00
# Try to remove the tags
conn . change_tags_for_resource (
ResourceType = " healthcheck " ,
ResourceId = healthcheck_id ,
RemoveTagKeys = [ tag1 [ " Key " ] ] ,
)
# Check to make sure that the response has the 'ResourceTagSet' key
2017-02-24 02:37:43 +00:00
response = conn . list_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id
)
2023-07-08 21:53:09 +00:00
assert " ResourceTagSet " in response
assert tag1 not in response [ " ResourceTagSet " ] [ " Tags " ]
assert tag2 in response [ " ResourceTagSet " ] [ " Tags " ]
2016-09-21 00:41:23 +00:00
# Remove the second tag
conn . change_tags_for_resource (
ResourceType = " healthcheck " ,
ResourceId = healthcheck_id ,
RemoveTagKeys = [ tag2 [ " Key " ] ] ,
)
2017-02-24 02:37:43 +00:00
response = conn . list_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id
)
2023-07-08 21:53:09 +00:00
assert tag2 not in response [ " ResourceTagSet " ] [ " Tags " ]
2016-09-21 00:41:23 +00:00
# Re-add the tags
conn . change_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id , AddTags = [ tag1 , tag2 ]
)
# Remove both
conn . change_tags_for_resource (
ResourceType = " healthcheck " ,
ResourceId = healthcheck_id ,
RemoveTagKeys = [ tag1 [ " Key " ] , tag2 [ " Key " ] ] ,
)
2017-02-24 02:37:43 +00:00
response = conn . list_tags_for_resource (
ResourceType = " healthcheck " , ResourceId = healthcheck_id
)
2023-07-08 21:53:09 +00:00
assert response [ " ResourceTagSet " ] [ " Tags " ] == [ ]
2017-03-13 14:09:51 +00:00
2022-01-27 11:28:31 +00:00
@mock_ec2
2017-03-13 14:09:51 +00:00
@mock_route53
def test_list_hosted_zones_by_name ( ) :
2022-01-27 11:28:31 +00:00
# Create mock VPC so we can get a VPC ID
ec2c = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
vpc_id = ec2c . create_vpc ( CidrBlock = " 10.1.0.0/16 " ) . get ( " Vpc " ) . get ( " VpcId " )
region = " us-east-1 "
conn = boto3 . client ( " route53 " , region_name = region )
zone_b = conn . create_hosted_zone (
2017-03-13 14:09:51 +00:00
Name = " test.b.com. " ,
CallerReference = str ( hash ( " foo " ) ) ,
HostedZoneConfig = dict ( PrivateZone = True , Comment = " test com " ) ,
2022-01-27 11:28:31 +00:00
VPC = { " VPCRegion " : region , " VPCId " : vpc_id } ,
2017-03-13 14:09:51 +00:00
)
2022-01-27 11:28:31 +00:00
zone_b = conn . list_hosted_zones_by_name ( DNSName = " test.b.com. " )
2023-07-08 21:53:09 +00:00
assert len ( zone_b [ " HostedZones " ] ) == 1
assert zone_b [ " HostedZones " ] [ 0 ] [ " Name " ] == " test.b.com. "
assert zone_b [ " HostedZones " ] [ 0 ] [ " Config " ] [ " PrivateZone " ]
2022-01-27 11:28:31 +00:00
# We declared this a a private hosted zone above, so let's make
# sure it really is!
zone_b_id = zone_b [ " HostedZones " ] [ 0 ] [ " Id " ] . split ( " / " ) [ - 1 ]
b_hosted_zone = conn . get_hosted_zone ( Id = zone_b_id )
# Pull the HostedZone block out and test it.
b_hz = b_hosted_zone [ " HostedZone " ]
2023-07-08 21:53:09 +00:00
assert b_hz [ " Config " ] [ " PrivateZone " ]
2022-01-27 11:28:31 +00:00
# Check for the VPCs block since this *should* be a VPC-Private Zone
2023-07-08 21:53:09 +00:00
assert len ( b_hosted_zone [ " VPCs " ] ) == 1
2022-01-27 11:28:31 +00:00
b_hz_vpcs = b_hosted_zone [ " VPCs " ] [ 0 ]
2023-07-08 21:53:09 +00:00
assert b_hz_vpcs [ " VPCId " ] == vpc_id
assert b_hz_vpcs [ " VPCRegion " ] == region
2022-01-27 11:28:31 +00:00
# Now create other zones and test them.
2017-03-13 14:09:51 +00:00
conn . create_hosted_zone (
Name = " test.a.org. " ,
CallerReference = str ( hash ( " bar " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " test org " ) ,
2017-03-13 14:09:51 +00:00
)
conn . create_hosted_zone (
Name = " test.a.org. " ,
CallerReference = str ( hash ( " bar " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " test org 2 " ) ,
2017-03-13 14:09:51 +00:00
)
2022-01-27 11:28:31 +00:00
# Now makes sure the other zones we created above are NOT private...
2017-03-13 14:09:51 +00:00
zones = conn . list_hosted_zones_by_name ( DNSName = " test.a.org. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 2
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " test.a.org. "
assert zones [ " HostedZones " ] [ 0 ] [ " Config " ] [ " PrivateZone " ] is False
2022-01-27 11:28:31 +00:00
2023-07-08 21:53:09 +00:00
assert zones [ " HostedZones " ] [ 1 ] [ " Name " ] == " test.a.org. "
assert zones [ " HostedZones " ] [ 1 ] [ " Config " ] [ " PrivateZone " ] is False
2017-03-13 14:09:51 +00:00
# test sort order
zones = conn . list_hosted_zones_by_name ( )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 3
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " test.b.com. "
assert zones [ " HostedZones " ] [ 1 ] [ " Name " ] == " test.a.org. "
assert zones [ " HostedZones " ] [ 2 ] [ " Name " ] == " test.a.org. "
2017-04-28 15:56:32 +00:00
2021-03-16 12:58:16 +00:00
@mock_route53
def test_list_hosted_zones_by_dns_name ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " test.b.com. " ,
CallerReference = str ( hash ( " foo " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " test com " ) ,
2021-03-16 12:58:16 +00:00
)
conn . create_hosted_zone (
Name = " test.a.org. " ,
CallerReference = str ( hash ( " bar " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " test org " ) ,
2021-03-16 12:58:16 +00:00
)
conn . create_hosted_zone (
Name = " test.a.org. " ,
CallerReference = str ( hash ( " bar " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " test org 2 " ) ,
2021-03-16 12:58:16 +00:00
)
conn . create_hosted_zone (
Name = " my.test.net. " ,
CallerReference = str ( hash ( " baz " ) ) ,
HostedZoneConfig = dict ( PrivateZone = False , Comment = " test net " ) ,
)
# test lookup
zones = conn . list_hosted_zones_by_name ( DNSName = " test.b.com. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " DNSName " ] == " test.b.com. "
2021-03-16 12:58:16 +00:00
zones = conn . list_hosted_zones_by_name ( DNSName = " test.a.org. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 2
assert zones [ " DNSName " ] == " test.a.org. "
2021-03-16 12:58:16 +00:00
zones = conn . list_hosted_zones_by_name ( DNSName = " my.test.net. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " DNSName " ] == " my.test.net. "
2021-03-16 12:58:16 +00:00
zones = conn . list_hosted_zones_by_name ( DNSName = " my.test.net " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " DNSName " ] == " my.test.net. "
2021-03-16 12:58:16 +00:00
# test sort order
zones = conn . list_hosted_zones_by_name ( )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 4
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " test.b.com. "
assert zones [ " HostedZones " ] [ 1 ] [ " Name " ] == " my.test.net. "
assert zones [ " HostedZones " ] [ 2 ] [ " Name " ] == " test.a.org. "
assert zones [ " HostedZones " ] [ 3 ] [ " Name " ] == " test.a.org. "
2021-03-16 12:58:16 +00:00
2017-04-28 15:56:32 +00:00
@mock_route53
def test_change_resource_record_sets_crud_valid ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " db " ) ,
2017-04-28 15:56:32 +00:00
)
zones = conn . list_hosted_zones_by_name ( DNSName = " db. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " db. "
2017-04-28 15:56:32 +00:00
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Create A Record.
a_record_endpoint_payload = {
2019-02-27 10:54:55 +00:00
" Comment " : " Create A record prod.redis.db " ,
2017-04-28 15:56:32 +00:00
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
2018-11-20 12:43:59 +00:00
" Name " : " prod.redis.db. " ,
2017-04-28 15:56:32 +00:00
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = a_record_endpoint_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 3
2022-12-14 23:58:55 +00:00
a_record_detail = response [ " ResourceRecordSets " ] [ 2 ]
2023-07-08 21:53:09 +00:00
assert a_record_detail [ " Name " ] == " prod.redis.db. "
assert a_record_detail [ " Type " ] == " A "
assert a_record_detail [ " TTL " ] == 10
assert a_record_detail [ " ResourceRecords " ] == [ { " Value " : " 127.0.0.1 " } ]
2017-04-28 15:56:32 +00:00
2019-02-27 10:54:55 +00:00
# Update A Record.
2017-04-28 15:56:32 +00:00
cname_record_endpoint_payload = {
2019-02-27 10:54:55 +00:00
" Comment " : " Update A record prod.redis.db " ,
2017-04-28 15:56:32 +00:00
" Changes " : [
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
2018-11-20 12:43:59 +00:00
" Name " : " prod.redis.db. " ,
2019-02-27 10:54:55 +00:00
" Type " : " A " ,
2017-04-28 15:56:32 +00:00
" TTL " : 60 ,
" ResourceRecords " : [ { " Value " : " 192.168.1.1 " } ] ,
} ,
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = cname_record_endpoint_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 3
2022-12-14 23:58:55 +00:00
cname_record_detail = response [ " ResourceRecordSets " ] [ 2 ]
2023-07-08 21:53:09 +00:00
assert cname_record_detail [ " Name " ] == " prod.redis.db. "
assert cname_record_detail [ " Type " ] == " A "
assert cname_record_detail [ " TTL " ] == 60
assert cname_record_detail [ " ResourceRecords " ] == [ { " Value " : " 192.168.1.1 " } ]
2017-04-28 15:56:32 +00:00
2018-06-22 02:09:04 +00:00
# Update to add Alias.
cname_alias_record_endpoint_payload = {
" Comment " : " Update to Alias prod.redis.db " ,
" Changes " : [
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
" Name " : " prod.redis.db. " ,
" Type " : " A " ,
" TTL " : 60 ,
" AliasTarget " : {
" HostedZoneId " : hosted_zone_id ,
" DNSName " : " prod.redis.alias. " ,
" EvaluateTargetHealth " : False ,
} ,
} ,
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = cname_alias_record_endpoint_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2022-12-14 23:58:55 +00:00
cname_alias_record_detail = response [ " ResourceRecordSets " ] [ 2 ]
2023-07-08 21:53:09 +00:00
assert cname_alias_record_detail [ " Name " ] == " prod.redis.db. "
assert cname_alias_record_detail [ " Type " ] == " A "
assert cname_alias_record_detail [ " TTL " ] == 60
assert cname_alias_record_detail [ " AliasTarget " ] == {
" HostedZoneId " : hosted_zone_id ,
" DNSName " : " prod.redis.alias. " ,
" EvaluateTargetHealth " : False ,
}
assert " ResourceRecords " not in cname_alias_record_detail
2018-06-22 02:09:04 +00:00
2019-06-17 13:53:32 +00:00
# Delete record.
delete_payload = {
" Comment " : " delete prod.redis.db " ,
" Changes " : [
{
" Action " : " DELETE " ,
2022-12-10 11:07:30 +00:00
" ResourceRecordSet " : {
" Name " : " prod.redis.db. " ,
" Type " : " A " ,
" TTL " : 60 ,
" ResourceRecords " : [ { " Value " : " 192.168.1.1 " } ] ,
} ,
2019-06-17 13:53:32 +00:00
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = delete_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 2
2019-10-31 15:44:26 +00:00
2017-04-28 15:56:32 +00:00
2020-11-18 07:23:49 +00:00
@mock_route53
def test_change_resource_record_sets_crud_valid_with_special_xml_chars ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " db " ) ,
2020-11-18 07:23:49 +00:00
)
zones = conn . list_hosted_zones_by_name ( DNSName = " db. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " db. "
2020-11-18 07:23:49 +00:00
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Create TXT Record.
txt_record_endpoint_payload = {
" Comment " : " Create TXT record prod.redis.db " ,
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " prod.redis.db. " ,
" Type " : " TXT " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " SomeInitialValue " } ] ,
} ,
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = txt_record_endpoint_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 3
2022-12-14 23:58:55 +00:00
a_record_detail = response [ " ResourceRecordSets " ] [ 2 ]
2023-07-08 21:53:09 +00:00
assert a_record_detail [ " Name " ] == " prod.redis.db. "
assert a_record_detail [ " Type " ] == " TXT "
assert a_record_detail [ " TTL " ] == 10
assert a_record_detail [ " ResourceRecords " ] == [ { " Value " : " SomeInitialValue " } ]
2020-11-18 07:23:49 +00:00
# Update TXT Record with XML Special Character &.
txt_record_with_special_char_endpoint_payload = {
" Comment " : " Update TXT record prod.redis.db " ,
" Changes " : [
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
" Name " : " prod.redis.db. " ,
" Type " : " TXT " ,
" TTL " : 60 ,
" ResourceRecords " : [ { " Value " : " SomeInitialValue&NewValue " } ] ,
} ,
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = txt_record_with_special_char_endpoint_payload ,
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 3
2022-12-14 23:58:55 +00:00
cname_record_detail = response [ " ResourceRecordSets " ] [ 2 ]
2023-07-08 21:53:09 +00:00
assert cname_record_detail [ " Name " ] == " prod.redis.db. "
assert cname_record_detail [ " Type " ] == " TXT "
assert cname_record_detail [ " TTL " ] == 60
assert cname_record_detail [ " ResourceRecords " ] == [
{ " Value " : " SomeInitialValue&NewValue " }
]
2020-11-18 07:23:49 +00:00
# Delete record.
delete_payload = {
" Comment " : " delete prod.redis.db " ,
" Changes " : [
{
" Action " : " DELETE " ,
2022-12-10 11:07:30 +00:00
" ResourceRecordSet " : {
" Name " : " prod.redis.db. " ,
" Type " : " TXT " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " SomeInitialValue " } ] ,
} ,
2020-11-18 07:23:49 +00:00
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = delete_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 2
2020-11-18 07:23:49 +00:00
2022-12-10 11:07:30 +00:00
@mock_route53
def test_change_resource_record_set__delete_should_match_create ( ) :
# To delete a resource record set, you must specify all the same values that you specified when you created it.
client = boto3 . client ( " route53 " , region_name = " us-east-1 " )
name = " example.com "
hosted_zone_id = client . create_hosted_zone ( Name = name , CallerReference = name ) [
" HostedZone "
] [ " Id " ]
create_call = client . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : name ,
" Type " : " A " ,
" TTL " : 300 ,
" ResourceRecords " : [ { " Value " : " 192.168.0.1 " } ] ,
} ,
}
]
} ,
)
waiter = client . get_waiter ( " resource_record_sets_changed " )
waiter . wait ( Id = create_call [ " ChangeInfo " ] [ " Id " ] )
with pytest . raises ( ClientError ) as exc :
client . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " DELETE " ,
" ResourceRecordSet " : {
" Name " : name ,
" Type " : " A "
# Missing TTL and ResourceRecords
} ,
}
]
} ,
)
err = exc . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " InvalidInput "
assert (
err [ " Message " ]
== " Invalid request: Expected exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId], but found none in Change with [Action=DELETE, Name=example.com, Type=A, SetIdentifier=null] "
2022-12-10 11:07:30 +00:00
)
2019-07-17 23:37:47 +00:00
@mock_route53
def test_change_weighted_resource_record_sets ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-2 " )
conn . create_hosted_zone (
Name = " test.vpc.internal. " , CallerReference = str ( hash ( " test " ) )
)
zones = conn . list_hosted_zones_by_name ( DNSName = " test.vpc.internal. " )
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Create 2 weighted records
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " test.vpc.internal " ,
" Type " : " A " ,
" SetIdentifier " : " test1 " ,
" Weight " : 50 ,
" AliasTarget " : {
" HostedZoneId " : " Z3AADJGX6KTTL2 " ,
" DNSName " : " internal-test1lb-447688172.us-east-2.elb.amazonaws.com. " ,
" EvaluateTargetHealth " : True ,
} ,
} ,
} ,
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " test.vpc.internal " ,
" Type " : " A " ,
" SetIdentifier " : " test2 " ,
" Weight " : 50 ,
" AliasTarget " : {
" HostedZoneId " : " Z3AADJGX6KTTL2 " ,
" DNSName " : " internal-testlb2-1116641781.us-east-2.elb.amazonaws.com. " ,
" EvaluateTargetHealth " : True ,
} ,
} ,
} ,
]
} ,
)
2022-12-14 23:58:55 +00:00
rr_sets = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id ) [
" ResourceRecordSets "
]
record = [ r for r in rr_sets if r [ " Type " ] == " A " ] [ 0 ]
2019-07-17 23:37:47 +00:00
# Update the first record to have a weight of 90
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
" Name " : record [ " Name " ] ,
" Type " : record [ " Type " ] ,
" SetIdentifier " : record [ " SetIdentifier " ] ,
" Weight " : 90 ,
" AliasTarget " : {
" HostedZoneId " : record [ " AliasTarget " ] [ " HostedZoneId " ] ,
" DNSName " : record [ " AliasTarget " ] [ " DNSName " ] ,
" EvaluateTargetHealth " : record [ " AliasTarget " ] [
" EvaluateTargetHealth "
2019-10-31 15:44:26 +00:00
] ,
2019-07-17 23:37:47 +00:00
} ,
} ,
2019-10-31 15:44:26 +00:00
}
2019-07-17 23:37:47 +00:00
]
} ,
)
2022-12-14 23:58:55 +00:00
record = [ r for r in rr_sets if r [ " Type " ] == " A " ] [ 1 ]
2019-07-17 23:37:47 +00:00
# Update the second record to have a weight of 10
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
" Name " : record [ " Name " ] ,
" Type " : record [ " Type " ] ,
" SetIdentifier " : record [ " SetIdentifier " ] ,
" Weight " : 10 ,
" AliasTarget " : {
" HostedZoneId " : record [ " AliasTarget " ] [ " HostedZoneId " ] ,
" DNSName " : record [ " AliasTarget " ] [ " DNSName " ] ,
" EvaluateTargetHealth " : record [ " AliasTarget " ] [
" EvaluateTargetHealth "
2019-10-31 15:44:26 +00:00
] ,
2019-07-17 23:37:47 +00:00
} ,
} ,
2019-10-31 15:44:26 +00:00
}
2019-07-17 23:37:47 +00:00
]
} ,
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
for record in response [ " ResourceRecordSets " ] :
2022-06-10 19:33:17 +00:00
if record . get ( " SetIdentifier " ) :
if record [ " SetIdentifier " ] == " test1 " :
2023-07-08 21:53:09 +00:00
assert record [ " Weight " ] == 90
2022-06-10 19:33:17 +00:00
if record [ " SetIdentifier " ] == " test2 " :
2023-07-08 21:53:09 +00:00
assert record [ " Weight " ] == 10
2019-07-17 23:37:47 +00:00
2017-04-28 15:56:32 +00:00
2020-03-02 12:46:15 +00:00
@mock_route53
def test_failover_record_sets ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-2 " )
2020-03-02 13:07:34 +00:00
conn . create_hosted_zone ( Name = " test.zone. " , CallerReference = str ( hash ( " test " ) ) )
2020-03-02 12:46:15 +00:00
zones = conn . list_hosted_zones_by_name ( DNSName = " test.zone. " )
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Create geolocation record
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " failover.test.zone. " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
2020-03-02 13:07:34 +00:00
" Failover " : " PRIMARY " ,
} ,
2020-03-02 12:46:15 +00:00
}
]
2020-03-02 13:07:34 +00:00
} ,
2020-03-02 12:46:15 +00:00
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2022-12-14 23:58:55 +00:00
record = response [ " ResourceRecordSets " ] [ 2 ]
2023-07-08 21:53:09 +00:00
assert record [ " Failover " ] == " PRIMARY "
2020-03-02 12:46:15 +00:00
@mock_route53
def test_geolocation_record_sets ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-2 " )
2020-03-02 13:07:34 +00:00
conn . create_hosted_zone ( Name = " test.zone. " , CallerReference = str ( hash ( " test " ) ) )
2020-03-02 12:46:15 +00:00
zones = conn . list_hosted_zones_by_name ( DNSName = " test.zone. " )
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Create geolocation record
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " georecord1.test.zone. " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
2020-03-02 13:07:34 +00:00
" GeoLocation " : { " ContinentCode " : " EU " } ,
} ,
2020-03-02 12:46:15 +00:00
} ,
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " georecord2.test.zone. " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.2 " } ] ,
2020-03-02 13:07:34 +00:00
" GeoLocation " : { " CountryCode " : " US " , " SubdivisionCode " : " NY " } ,
} ,
} ,
2020-03-02 12:46:15 +00:00
]
2020-03-02 13:07:34 +00:00
} ,
2020-03-02 12:46:15 +00:00
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
rrs = response [ " ResourceRecordSets " ]
2023-07-08 21:53:09 +00:00
assert rrs [ 2 ] [ " GeoLocation " ] == { " ContinentCode " : " EU " }
assert rrs [ 3 ] [ " GeoLocation " ] == { " CountryCode " : " US " , " SubdivisionCode " : " NY " }
2020-03-02 12:46:15 +00:00
2017-04-28 15:56:32 +00:00
@mock_route53
def test_change_resource_record_invalid ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " db " ) ,
2017-04-28 15:56:32 +00:00
)
zones = conn . list_hosted_zones_by_name ( DNSName = " db. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " db. "
2017-04-28 15:56:32 +00:00
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
invalid_a_record_payload = {
" Comment " : " this should fail " ,
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " prod.scooby.doo " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
] ,
}
2020-10-06 05:54:49 +00:00
with pytest . raises ( botocore . exceptions . ClientError ) :
2017-04-28 15:56:32 +00:00
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = invalid_a_record_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 2
2017-04-28 15:56:32 +00:00
invalid_cname_record_payload = {
" Comment " : " this should also fail " ,
" Changes " : [
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
" Name " : " prod.scooby.doo " ,
" Type " : " CNAME " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
] ,
}
2020-10-06 05:54:49 +00:00
with pytest . raises ( botocore . exceptions . ClientError ) :
2017-04-28 15:56:32 +00:00
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = invalid_cname_record_payload
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 2
2017-10-23 15:25:40 +00:00
2022-06-14 14:22:07 +00:00
@mock_route53
def test_change_resource_record_invalid_action_value ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
HostedZoneConfig = dict ( PrivateZone = False , Comment = " db " ) ,
)
zones = conn . list_hosted_zones_by_name ( DNSName = " db. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " db. "
2022-06-14 14:22:07 +00:00
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
invalid_a_record_payload = {
" Comment " : " this should fail " ,
" Changes " : [
{
" Action " : " INVALID_ACTION " ,
" ResourceRecordSet " : {
" Name " : " prod.scooby.doo " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
] ,
}
with pytest . raises ( botocore . exceptions . ClientError ) as exc :
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = invalid_a_record_payload
)
err = exc . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " InvalidInput "
assert (
err [ " Message " ]
== " Invalid XML ; cvc-enumeration-valid: Value ' INVALID_ACTION ' is not facet-valid with respect to enumeration ' [CREATE, DELETE, UPSERT] ' . It must be a value from the enumeration. "
2022-06-14 14:22:07 +00:00
)
response = conn . list_resource_record_sets ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 2
2022-06-14 14:22:07 +00:00
2022-11-30 23:35:20 +00:00
@mock_route53
2022-12-21 23:38:27 +00:00
def test_change_resource_record_set_create__should_fail_when_record_already_exists ( ) :
2022-11-30 23:35:20 +00:00
ZONE = " cname.local "
FQDN = f " test. { ZONE } "
FQDN_TARGET = " develop.domain.com "
client = boto3 . client ( " route53 " , region_name = " us-east-1 " )
zone_id = client . create_hosted_zone (
Name = ZONE , CallerReference = " ref " , DelegationSetId = " string "
) [ " HostedZone " ] [ " Id " ]
changes = {
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : FQDN ,
" Type " : " CNAME " ,
" TTL " : 600 ,
" ResourceRecords " : [ { " Value " : FQDN_TARGET } ] ,
} ,
}
]
}
client . change_resource_record_sets ( HostedZoneId = zone_id , ChangeBatch = changes )
with pytest . raises ( ClientError ) as exc :
client . change_resource_record_sets ( HostedZoneId = zone_id , ChangeBatch = changes )
2022-12-21 23:38:27 +00:00
2022-11-30 23:35:20 +00:00
err = exc . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " InvalidChangeBatch "
assert (
err [ " Message " ]
== " Tried to create resource record set [name= ' test.cname.local. ' , type= ' CNAME ' ] but it already exists "
2022-12-21 23:38:27 +00:00
)
@mock_route53
def test_change_resource_record_set__should_create_record_when_using_upsert ( ) :
route53_client = boto3 . client ( " route53 " , region_name = " us-east-1 " )
hosted_zone = route53_client . create_hosted_zone (
Name = " example.com " , CallerReference = " irrelevant "
) [ " HostedZone " ]
resource_record = {
" Name " : " test.example.com. " ,
" Type " : " CNAME " ,
" TTL " : 60 ,
" ResourceRecords " : [ { " Value " : " www.test.example.com " } ] ,
}
route53_client . change_resource_record_sets (
HostedZoneId = hosted_zone [ " Id " ] ,
ChangeBatch = {
" Changes " : [ { " Action " : " UPSERT " , " ResourceRecordSet " : resource_record } ] ,
} ,
)
response = route53_client . list_resource_record_sets ( HostedZoneId = hosted_zone [ " Id " ] )
# The 1st and 2nd records are NS and SOA records, respectively.
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 3
assert response [ " ResourceRecordSets " ] [ 2 ] == resource_record
2022-12-21 23:38:27 +00:00
# a subsequest UPSERT with the same ChangeBatch should succeed as well
route53_client . change_resource_record_sets (
HostedZoneId = hosted_zone [ " Id " ] ,
ChangeBatch = {
" Changes " : [ { " Action " : " UPSERT " , " ResourceRecordSet " : resource_record } ] ,
} ,
)
response = route53_client . list_resource_record_sets ( HostedZoneId = hosted_zone [ " Id " ] )
# The 1st and 2nd records are NS and SOA records, respectively.
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 3
assert response [ " ResourceRecordSets " ] [ 2 ] == resource_record
2022-11-30 23:35:20 +00:00
2017-10-23 15:25:40 +00:00
@mock_route53
def test_list_resource_record_sets_name_type_filters ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
create_hosted_zone_response = conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " db " ) ,
2017-10-23 15:25:40 +00:00
)
hosted_zone_id = create_hosted_zone_response [ " HostedZone " ] [ " Id " ]
def create_resource_record_set ( rec_type , rec_name ) :
payload = {
2022-11-17 22:41:08 +00:00
" Comment " : f " create { rec_type } record { rec_name } " ,
2017-10-23 15:25:40 +00:00
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : rec_name ,
" Type " : rec_type ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = payload
)
# record_type, record_name
all_records = [
2018-11-20 12:43:59 +00:00
( " A " , " a.a.db. " ) ,
( " A " , " a.b.db. " ) ,
( " A " , " b.b.db. " ) ,
( " CNAME " , " b.b.db. " ) ,
( " CNAME " , " b.c.db. " ) ,
( " CNAME " , " c.c.db. " ) ,
2017-10-23 15:25:40 +00:00
]
for record_type , record_name in all_records :
create_resource_record_set ( record_type , record_name )
start_with = 2
response = conn . list_resource_record_sets (
HostedZoneId = hosted_zone_id ,
StartRecordType = all_records [ start_with ] [ 0 ] ,
StartRecordName = all_records [ start_with ] [ 1 ] ,
)
2023-07-08 21:53:09 +00:00
assert response [ " IsTruncated " ] is False
2019-12-27 17:53:14 +00:00
2017-10-23 15:25:40 +00:00
returned_records = [
( record [ " Type " ] , record [ " Name " ] ) for record in response [ " ResourceRecordSets " ]
2019-10-31 15:44:26 +00:00
]
2023-07-08 21:53:09 +00:00
assert len ( returned_records ) == len ( all_records ) - start_with
2017-10-23 15:25:40 +00:00
for desired_record in all_records [ start_with : ] :
2023-07-08 21:53:09 +00:00
assert desired_record in returned_records
2020-11-20 07:21:05 +00:00
@mock_route53
def test_get_change ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-2 " )
change_id = " 123456 "
response = conn . get_change ( Id = change_id )
2023-07-08 21:53:09 +00:00
assert response [ " ChangeInfo " ] [ " Id " ] == change_id
assert response [ " ChangeInfo " ] [ " Status " ] == " INSYNC "
2021-12-10 09:47:27 +00:00
@mock_route53
def test_change_resource_record_sets_records_limit ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
2022-01-27 11:28:31 +00:00
HostedZoneConfig = dict ( PrivateZone = False , Comment = " db " ) ,
2021-12-10 09:47:27 +00:00
)
zones = conn . list_hosted_zones_by_name ( DNSName = " db. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " db. "
2021-12-10 09:47:27 +00:00
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Changes creating exactly 1,000 resource records.
changes = [ ]
for ci in range ( 4 ) :
resourcerecords = [ ]
for rri in range ( 250 ) :
2022-11-17 22:41:08 +00:00
resourcerecords . append ( { " Value " : f " 127.0.0. { rri } " } )
2021-12-10 09:47:27 +00:00
changes . append (
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
2022-11-17 22:41:08 +00:00
" Name " : f " foo { ci } .db. " ,
2021-12-10 09:47:27 +00:00
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : resourcerecords ,
} ,
}
)
create_1000_resource_records_payload = {
" Comment " : " Create four records with 250 resource records each " ,
" Changes " : changes ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = create_1000_resource_records_payload
)
# Changes creating over 1,000 resource records.
too_many_changes = create_1000_resource_records_payload [ " Changes " ] . copy ( )
too_many_changes . append (
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : " toomany.db. " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
)
create_1001_resource_records_payload = {
" Comment " : " Create four records with 250 resource records each, plus one more " ,
" Changes " : too_many_changes ,
}
with pytest . raises ( ClientError ) as exc :
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id ,
ChangeBatch = create_1001_resource_records_payload ,
)
err = exc . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " InvalidChangeBatch "
2021-12-10 09:47:27 +00:00
# Changes upserting exactly 500 resource records.
changes = [ ]
for ci in range ( 2 ) :
resourcerecords = [ ]
for rri in range ( 250 ) :
2022-11-17 22:41:08 +00:00
resourcerecords . append ( { " Value " : f " 127.0.0. { rri } " } )
2021-12-10 09:47:27 +00:00
changes . append (
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
2022-11-17 22:41:08 +00:00
" Name " : f " foo { ci } .db. " ,
2021-12-10 09:47:27 +00:00
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : resourcerecords ,
} ,
}
)
upsert_500_resource_records_payload = {
" Comment " : " Upsert two records with 250 resource records each " ,
" Changes " : changes ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = upsert_500_resource_records_payload
)
# Changes upserting over 1,000 resource records.
too_many_changes = upsert_500_resource_records_payload [ " Changes " ] . copy ( )
too_many_changes . append (
{
" Action " : " UPSERT " ,
" ResourceRecordSet " : {
" Name " : " toomany.db. " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
)
upsert_501_resource_records_payload = {
" Comment " : " Upsert two records with 250 resource records each, plus one more " ,
" Changes " : too_many_changes ,
}
with pytest . raises ( ClientError ) as exc :
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = upsert_501_resource_records_payload
)
err = exc . value . response [ " Error " ]
2023-07-08 21:53:09 +00:00
assert err [ " Code " ] == " InvalidChangeBatch "
assert err [ " Message " ] == " Number of records limit of 1000 exceeded. "
2022-01-31 00:53:05 +00:00
@mock_route53
def test_list_resource_recordset_pagination ( ) :
conn = boto3 . client ( " route53 " , region_name = " us-east-1 " )
conn . create_hosted_zone (
Name = " db. " ,
CallerReference = str ( hash ( " foo " ) ) ,
HostedZoneConfig = dict ( PrivateZone = True , Comment = " db " ) ,
)
zones = conn . list_hosted_zones_by_name ( DNSName = " db. " )
2023-07-08 21:53:09 +00:00
assert len ( zones [ " HostedZones " ] ) == 1
assert zones [ " HostedZones " ] [ 0 ] [ " Name " ] == " db. "
2022-01-31 00:53:05 +00:00
hosted_zone_id = zones [ " HostedZones " ] [ 0 ] [ " Id " ]
# Create A Record.
a_record_endpoint_payload = {
2022-04-27 11:58:59 +00:00
" Comment " : " Create 500 A records " ,
2022-01-31 00:53:05 +00:00
" Changes " : [
{
" Action " : " CREATE " ,
" ResourceRecordSet " : {
" Name " : f " env { idx } .redis.db. " ,
" Type " : " A " ,
" TTL " : 10 ,
" ResourceRecords " : [ { " Value " : " 127.0.0.1 " } ] ,
} ,
}
for idx in range ( 500 )
] ,
}
conn . change_resource_record_sets (
HostedZoneId = hosted_zone_id , ChangeBatch = a_record_endpoint_payload
)
response = conn . list_resource_record_sets (
HostedZoneId = hosted_zone_id , MaxItems = " 100 "
)
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 100
assert response [ " IsTruncated " ]
assert response [ " MaxItems " ] == " 100 "
assert response [ " NextRecordName " ] == " env187.redis.db. "
assert response [ " NextRecordType " ] == " A "
2022-01-31 00:53:05 +00:00
response = conn . list_resource_record_sets (
HostedZoneId = hosted_zone_id ,
StartRecordName = response [ " NextRecordName " ] ,
StartRecordType = response [ " NextRecordType " ] ,
)
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 300
assert response [ " IsTruncated " ] is True
assert response [ " MaxItems " ] == " 300 "
assert response [ " NextRecordName " ] == " env457.redis.db. "
assert response [ " NextRecordType " ] == " A "
2022-01-31 00:53:05 +00:00
response = conn . list_resource_record_sets (
HostedZoneId = hosted_zone_id ,
StartRecordName = response [ " NextRecordName " ] ,
StartRecordType = response [ " NextRecordType " ] ,
)
2023-07-08 21:53:09 +00:00
assert len ( response [ " ResourceRecordSets " ] ) == 102
assert response [ " IsTruncated " ] is False
assert response [ " MaxItems " ] == " 300 "
assert " NextRecordName " not in response
assert " NextRecordType " not in response
2022-06-19 13:43:57 +00:00
@mock_route53
def test_get_dns_sec ( ) :
client = boto3 . client ( " route53 " , region_name = " us-east-1 " )
hosted_zone_id = client . create_hosted_zone (
Name = " testdns.aws.com. " , CallerReference = str ( hash ( " foo " ) )
) [ " HostedZone " ] [ " Id " ]
dns_sec = client . get_dnssec ( HostedZoneId = hosted_zone_id )
2023-07-08 21:53:09 +00:00
assert dns_sec [ " Status " ] == { " ServeSignature " : " NOT_SIGNING " }