2020-11-05 14:51:49 -08:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
2024-09-25 15:25:54 -04:00
require_relative '../ec2-ruby-example-elastic-ips'
2020-11-05 14:51:49 -08:00
2024-09-25 15:25:54 -04:00
describe '#instance_exists?' do
let ( :instance_id ) { 'i-033c48ef067af3dEX' }
2020-11-05 14:51:49 -08:00
let ( :ec2_client ) do
Aws :: EC2 :: Client . new (
stub_responses : {
describe_instances : {
reservations : [
instances : [
{
instance_id : instance_id
}
]
]
}
}
)
end
2024-09-25 15:25:54 -04:00
it 'checks whether the instance exists' do
2020-11-05 14:51:49 -08:00
expect ( instance_exists? ( ec2_client , instance_id ) ) . to be ( true )
end
end
2024-09-25 15:25:54 -04:00
describe '#allocate_elastic_ip_address' do
let ( :instance_id ) { 'i-033c48ef067af3dEX' }
let ( :allocation_id ) { 'eipalloc-0e7e1c46c5ee5f8EX' }
2020-11-05 14:51:49 -08:00
let ( :ec2_client ) do
Aws :: EC2 :: Client . new (
stub_responses : {
allocate_address : {
allocation_id : allocation_id
}
}
)
end
2024-09-25 15:25:54 -04:00
it 'checks for the Elastic IP address allocation ID' do
2020-11-05 14:51:49 -08:00
expect ( allocate_elastic_ip_address ( ec2_client ) ) . to eq ( allocation_id )
end
end
2024-09-25 15:25:54 -04:00
describe '#associate_elastic_ip_address_with_instance' do
let ( :instance_id ) { 'i-033c48ef067af3dEX' }
let ( :allocation_id ) { 'eipalloc-0e7e1c46c5ee5f8EX' }
let ( :association_id ) { 'eipassoc-010e2d189043030EX' }
2020-11-05 14:51:49 -08:00
let ( :ec2_client ) do
Aws :: EC2 :: Client . new (
stub_responses : {
associate_address : {
association_id : association_id
}
}
)
end
2024-09-25 15:25:54 -04:00
it 'checks for the Elastic IP address allocation ID' do
2020-11-05 14:51:49 -08:00
expect (
associate_elastic_ip_address_with_instance (
ec2_client ,
allocation_id ,
instance_id
)
) . to eq ( association_id )
end
end
2024-09-25 15:25:54 -04:00
describe '#describe_addresses_for_instance' do
let ( :instance_id ) { 'i-033c48ef067af3dEX' }
2020-11-05 14:51:49 -08:00
let ( :ec2_client ) do
Aws :: EC2 :: Client . new (
stub_responses : {
describe_addresses : {
addresses : [
{
2024-09-25 15:25:54 -04:00
public_ip : '203.0.113.0' ,
private_ip_address : '10.0.1.241'
2020-11-05 14:51:49 -08:00
}
]
}
}
)
end
2024-09-25 15:25:54 -04:00
it 'lists information about the instance' do
2020-11-05 14:51:49 -08:00
expect { describe_addresses_for_instance ( ec2_client , instance_id ) } . not_to raise_error
end
end
2024-09-25 15:25:54 -04:00
describe '#elastic_ip_address_released?' do
let ( :instance_id ) { 'i-033c48ef067af3dEX' }
let ( :allocation_id ) { 'eipalloc-0e7e1c46c5ee5f8EX' }
2020-11-05 14:51:49 -08:00
let ( :ec2_client ) do
Aws :: EC2 :: Client . new (
stub_responses : {
release_address : { }
}
)
end
2024-09-25 15:25:54 -04:00
it 'releases the Elastic IP address' do
2020-11-05 14:51:49 -08:00
expect ( elastic_ip_address_released? ( ec2_client , allocation_id ) ) . to be ( true )
end
end