11#! /usr/bin/env bats
22
3- readonly bash_commons_src_path=" $BATS_TEST_DIRNAME /../modules/bash-commons/src"
4- source " $bash_commons_src_path /aws.sh"
5- source " $bash_commons_src_path /string.sh"
3+ source " $BATS_TEST_DIRNAME /../modules/bash-commons/src/aws.sh"
64load " test-helper"
5+ load " aws-helper"
76
8- readonly EC2_METADATA_MOCK_APP_PATH=" $BATS_TEST_DIRNAME /ec2-metadata-mock/ec2-metadata-mock.py"
9- readonly EC2_METADATA_MOCK_TMP_DIR=" /tmp/ec2-metadata-mock"
10- readonly EC2_METADATA_MOCK_PID_PATH=" $EC2_METADATA_MOCK_TMP_DIR /ec2-metadata-mock.pid"
11- readonly EC2_METADATA_MOCK_LOG_FILE_PATH=" $EC2_METADATA_MOCK_TMP_DIR /ec2-metadata-mock.log"
12-
13- # Set env vars for ec2-metadata-mock
14- export meta_data_local_ipv4=" 11.22.33.44"
15- export meta_data_public_ipv4=" 55.66.77.88"
16- export meta_data_local_hostname=" ip-10-251-50-12.ec2.internal"
17- export meta_data_public_hostname=" ec2-203-0-113-25.compute-1.amazonaws.com"
18- export meta_data_instance_id=" i-1234567890abcdef0"
19-
7+ readonly local_ipv4=" 11.22.33.44"
8+ readonly public_ipv4=" 55.66.77.88"
9+ readonly local_hostname=" ip-10-251-50-12.ec2.internal"
10+ readonly public_hostname=" ec2-203-0-113-25.compute-1.amazonaws.com"
11+ readonly instance_id=" i-1234567890abcdef0"
2012readonly mock_region=" us-west-1"
21- export meta_data_placement__availability_zone=" ${mock_region} b"
22- export dynamic_data_instance_identity__document=$( cat << END_HEREDOC
23- {
24- "devpayProductCodes" : null,
25- "marketplaceProductCodes" : [ "1abc2defghijklm3nopqrs4tu" ],
26- "availabilityZone" : "$meta_data_placement__availability_zone ",
27- "privateIp" : "$meta_data_local_ipv4 ",
28- "version" : "2017-09-30",
29- "instanceId" : "$meta_data_instance_id ",
30- "billingProducts" : null,
31- "instanceType" : "t2.micro",
32- "accountId" : "123456789012",
33- "imageId" : "ami-5fb8c835",
34- "pendingTime" : "2016-11-19T16:32:11Z",
35- "architecture" : "x86_64",
36- "kernelId" : null,
37- "ramdiskId" : null,
38- "region" : "$mock_region "
39- }
40- END_HEREDOC
41- )
13+ readonly availability_zone=" ${mock_region} b"
4214
43- # Configure the server so we can run a mock EC2 metadata endpoint on port 80 with the metadata endpoint's special IP.
4415function setup {
45- local config
46- config=$( ifconfig)
47-
48- mkdir -p " $EC2_METADATA_MOCK_TMP_DIR "
49-
50- # Use ifconfig and iptables to allow us to run a mock server on 169.254.169.254 and on port 80. These steps are
51- # based on https://github.com/NYTimes/mock-ec2-metadata. Note #1: we can't use that project directly as it doesn't
52- # support most EC2 metadata endpoints. Note #2: try to make this code idempotent so we don't try to create the same
53- # configuration multiple times.
54- if ! string_multiline_contains " $config " " lo:1" ; then
55- ifconfig lo:1 inet 169.254.169.254 netmask 255.255.255.255 up
56- echo 1 > /proc/sys/net/ipv4/ip_forward
57- iptables -t nat -A OUTPUT -p tcp -d 169.254.169.254/32 --dport 80 -j DNAT --to-destination 169.254.169.254:8111
58- iptables-save
59- fi
60-
61- # Start ec2-metadata-mock if it isn't already running
62- if [[ ! -f " $EC2_METADATA_MOCK_PID_PATH " ]]; then
63- FLASK_APP=" $EC2_METADATA_MOCK_APP_PATH " flask run --host=0.0.0.0 --port=8111 2>&1 > " $EC2_METADATA_MOCK_LOG_FILE_PATH " &
64- echo " $! " > " $EC2_METADATA_MOCK_PID_PATH "
65-
66- # Sleep a bit to give Flask a chance to start
67- sleep 1
68- fi
16+ start_ec2_metadata_mock \
17+ " $local_ipv4 " \
18+ " $public_ipv4 " \
19+ " $local_hostname " \
20+ " $public_hostname " \
21+ " $instance_id " \
22+ " $mock_region " \
23+ " $availability_zone "
6924}
7025
7126function teardown {
72- # Stop ec2-metadata-mock if it's running
73- if [[ -f " $EC2_METADATA_MOCK_PID_PATH " ]]; then
74- local readonly pid=$( cat " $EC2_METADATA_MOCK_PID_PATH " )
75- kill " $pid " 2>&1 > " $EC2_METADATA_MOCK_LOG_FILE_PATH "
76- rm -f " $EC2_METADATA_MOCK_PID_PATH "
77-
78- # Sleep a bit to give Flask a chance to stop
79- sleep 1
80- fi
27+ stop_ec2_metadata_mock
8128}
8229
8330@test " aws_get_instance_private_ip" {
8431 run aws_get_instance_private_ip
8532 assert_success
86- assert_output " $meta_data_local_ipv4 "
33+ assert_output " $local_ipv4 "
8734}
8835
8936@test " aws_get_instance_public_ip" {
9037 run aws_get_instance_public_ip
9138 assert_success
92- assert_output " $meta_data_public_ipv4 "
39+ assert_output " $public_ipv4 "
9340}
9441
9542@test " aws_get_instance_private_hostname" {
9643 run aws_get_instance_private_hostname
9744 assert_success
98- assert_output " $meta_data_local_hostname "
45+ assert_output " $local_hostname "
9946}
10047
10148@test " aws_get_instance_public_hostname" {
10249 run aws_get_instance_public_hostname
10350 assert_success
104- assert_output " $meta_data_public_hostname "
51+ assert_output " $public_hostname "
10552}
10653
10754@test " aws_get_instance_id" {
10855 run aws_get_instance_id
10956 assert_success
110- assert_output " $meta_data_instance_id "
57+ assert_output " $instance_id "
11158}
11259
11360@test " aws_get_instance_region" {
@@ -119,6 +66,6 @@ function teardown {
11966@test " aws_get_ec2_instance_availability_zone" {
12067 run aws_get_ec2_instance_availability_zone
12168 assert_success
122- assert_output " $meta_data_placement__availability_zone "
69+ assert_output " $availability_zone "
12370}
12471
0 commit comments