1
1
#! /usr/bin/env bats
2
2
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"
6
4
load " test-helper"
5
+ load " aws-helper"
7
6
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"
20
12
readonly 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"
42
14
43
- # Configure the server so we can run a mock EC2 metadata endpoint on port 80 with the metadata endpoint's special IP.
44
15
function 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 "
69
24
}
70
25
71
26
function 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
81
28
}
82
29
83
30
@test " aws_get_instance_private_ip" {
84
31
run aws_get_instance_private_ip
85
32
assert_success
86
- assert_output " $meta_data_local_ipv4 "
33
+ assert_output " $local_ipv4 "
87
34
}
88
35
89
36
@test " aws_get_instance_public_ip" {
90
37
run aws_get_instance_public_ip
91
38
assert_success
92
- assert_output " $meta_data_public_ipv4 "
39
+ assert_output " $public_ipv4 "
93
40
}
94
41
95
42
@test " aws_get_instance_private_hostname" {
96
43
run aws_get_instance_private_hostname
97
44
assert_success
98
- assert_output " $meta_data_local_hostname "
45
+ assert_output " $local_hostname "
99
46
}
100
47
101
48
@test " aws_get_instance_public_hostname" {
102
49
run aws_get_instance_public_hostname
103
50
assert_success
104
- assert_output " $meta_data_public_hostname "
51
+ assert_output " $public_hostname "
105
52
}
106
53
107
54
@test " aws_get_instance_id" {
108
55
run aws_get_instance_id
109
56
assert_success
110
- assert_output " $meta_data_instance_id "
57
+ assert_output " $instance_id "
111
58
}
112
59
113
60
@test " aws_get_instance_region" {
@@ -119,6 +66,6 @@ function teardown {
119
66
@test " aws_get_ec2_instance_availability_zone" {
120
67
run aws_get_ec2_instance_availability_zone
121
68
assert_success
122
- assert_output " $meta_data_placement__availability_zone "
69
+ assert_output " $availability_zone "
123
70
}
124
71
0 commit comments