1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+
4+ SCRIPTPATH=" $( cd " $( dirname " $0 " ) " ; pwd -P) "
5+ BUILD_DIR=" ${SCRIPTPATH} /../../build"
6+ TEST_FAILURES_LOG=" ${BUILD_DIR} /test-failures.log"
7+
8+ AEIS=" ${BUILD_DIR} /ec2-instance-selector"
9+
10+ # # Print to stderr
11+ function echoerr() {
12+ echo " $@ " 1>&2 ;
13+ }
14+
15+ # # Sort a bash array
16+ function sort_array() {
17+ local input=( " $@ " )
18+ IFS=$' \n '
19+ local sorted=($( sort <<< " ${input[*]}" ) )
20+ unset IFS
21+ echo " ${sorted[*]} "
22+ }
23+
24+ # # Checks if expected items (consumed as an array in args)
25+ # # are all contained in the actual list (consumed on stdin)
26+ function assert_contains_instance_types() {
27+ local expected=( " $@ " )
28+ local actual=()
29+ while read actual_input; do
30+ actual+=($actual_input )
31+ done
32+ [[ 0 -eq " ${# actual[@]} " ]] && return 1
33+ local actual_sorted=($( sort_array " ${actual[@]} " ) )
34+ local expected_sorted=($( sort_array " ${expected[@]} " ) )
35+
36+ for expectation in " ${expected_sorted[@]} " ; do
37+ if [[ ! " ${actual_sorted[*]} " =~ ${expectation} ]]; then
38+ echoerr -e " \t🔺ACTUAL: ${actual_sorted[*]} \n\t🔺Expected: ${expected_sorted[*]} "
39+ return 1
40+ fi
41+ done
42+ }
43+
44+ # # Executes an expected vs actual test execution of amazon-ec2-instance-selector
45+ # # Test success or failure is output to stdout and failures are also logged to a file ($TEST_FAILURES_LOG)
46+ # # $1 = test name string
47+ # # shift; $@ = params to amazon-ec2-instance-selector (i.e. --vcpus=2)
48+ # # STDIN = expected list
49+ function execute_test() {
50+ local test_name=$1
51+ shift
52+ local params=( " $@ " )
53+ local expected=()
54+ while read expected_input; do
55+ expected+=($expected_input )
56+ done
57+ [[ 0 -eq " ${# expected[@]} " ]] && return 1
58+
59+ echo " =========================== Test: ${test_name} ==========================="
60+
61+ for p in " ${params[@]} " ; do
62+ if $AEIS ${p} | assert_contains_instance_types " ${expected[*]} " ; then
63+ echo " ✅ ${test_name} \" $p \" passed!"
64+ else
65+ echo " ❌ Failed ${test_name} \" $p \" " | tee " ${TEST_FAILURES_LOG} "
66+ fi
67+ done
68+ echo -e " ========================= End Test: ${test_name} ===========================\n\n"
69+ }
70+
71+ # # Clean up previous test failures
72+ rm -f " ${TEST_FAILURES_LOG} "
73+
74+ # ############################################### TESTS ################################################
75+
76+ expected=(t3a.micro t3.micro t2.micro)
77+ params=(
78+ " --memory=1"
79+ " --memory=1GiB"
80+ " --memory=1 GiB"
81+ " --memory=1gb"
82+ " --memory=1.0"
83+ " --memory-min=1 --memory-max=1"
84+ " --memory=1024m"
85+ )
86+ echo " ${expected[*]} " | execute_test " Memory 1 GiB" " ${params[@]} "
87+
88+ expected=(i3en.6xlarge inf1.6xlarge z1d.6xlarge)
89+ params=(
90+ " --vcpus=24"
91+ " --vcpus-min=24 --vcpus-max=24"
92+ )
93+ echo " ${expected[*]} " | execute_test " 24 VCPUs" " ${params[@]} "
94+
95+ expected=(g2.8xlarge g3.16xlarge g4dn.12xlarge p3.8xlarge)
96+ params=(
97+ " --gpus=4"
98+ " --gpus-min=4 --gpus-max=4"
99+ )
100+ echo " ${expected[*]} " | execute_test " 4 GPUs" " ${params[@]} "
101+
102+
103+ expected=(p2.16xlarge)
104+ params=(
105+ " --vcpus-to-memory-ratio=1:12"
106+ )
107+ echo " ${expected[*]} " | execute_test " 1:12 vcpus-to-memory-ratio" " ${params[@]} "
108+
109+
110+ expected=(p2.8xlarge)
111+ params=(
112+ " --gpu-memory-total=96"
113+ " --gpu-memory-total=96gb"
114+ " --gpu-memory-total=96GiB"
115+ " --gpu-memory-total=98304m"
116+ " --gpu-memory-total-min=96 --gpu-memory-total-max=96"
117+ )
118+ echo " ${expected[*]} " | execute_test " 96 GiB gpu-memory-total" " ${params[@]} "
119+
120+
121+ expected=(a1.large c3.large c4.large c5.large c5a.large c5d.large c5n.large c6g.large c6gd.large \
122+ g4dn.2xlarge g4dn.4xlarge g4dn.xlarge i3.large i3en.large m1.large m3.large m5.large m5a.large m5ad.large m5d.large)
123+ params=(
124+ " --network-interfaces=3"
125+ " --network-interfaces-min=3 --network-interfaces-max=3"
126+ " --network-interfaces 3 --memory-min=1 --vcpus-min=1"
127+ )
128+ echo " ${expected[*]} " | execute_test " 3 network interfaces" " ${params[@]} "
129+
130+
131+ expected=(c5n.18xlarge c5n.metal g4dn.metal i3en.24xlarge i3en.metal inf1.24xlarge m5dn.24xlarge m5n.24xlarge p3dn.24xlarge r5dn.24xlarge r5n.24xlarge)
132+ params=(
133+ " --network-performance=100"
134+ " --network-performance-min=100 --network-performance-max=100"
135+ " --network-performance=100 --vcpus-min 1 --memory-min=1"
136+ )
137+ echo " ${expected[*]} " | execute_test " 100 Gib/s Networking Performance" " ${params[@]} "
138+
139+ expected=(t3.micro)
140+ params=(
141+ " --allow-list=^t3\.micro$"
142+ " --allow-list=t3.micro"
143+ " --allow-list=t[03].mic"
144+ " --allow-list=t3.mi"
145+ )
146+ echo " ${expected[*]} " | execute_test " Allow List" " ${params[@]} "
147+
148+ expected=(t1.micro t2.micro t3.micro t3a.micro)
149+ params=(
150+ " --deny-list=^[a-z].*\.[0-9]*(sm|me|la|na|xl).*"
151+ " --deny-list=^[a-z].*\.[0-9]*(sm|me|la|na|xl).* --allow-list=t.*"
152+ )
153+ echo " ${expected[*]} " | execute_test " Deny List" " ${params[@]} "
154+
155+
156+ expected=(t2.micro t2.nano t2.small)
157+ params=(
158+ " --burst-support --vcpus-max=1"
159+ " --burst-support --vcpus-max=1 --hypervisor=xen"
160+ " --burst-support --vcpus-max=1 --hibernation-support"
161+ " --burst-support --vcpus-max=1 --usage-class=on-demand"
162+ )
163+ echo " ${expected[*]} " | execute_test " Burst Support" " ${params[@]} "
164+
165+
166+ expected=(f1.16xlarge f1.2xlarge f1.4xlarge)
167+ params=(
168+ " --fpga-support"
169+ " --fpga-support --hypervisor=xen"
170+ " --fpga-support --cpu-architecture=x86_64"
171+ " --fpga-support --vcpus-min 1"
172+ )
173+ echo " ${expected[*]} " | execute_test " FPGAs" " ${params[@]} "
174+
175+
176+
177+ if [[ -f " ${TEST_FAILURES_LOG} " ]]; then
178+ echo -e " \n\n\n=========================== FAILURE SUMMARY ===========================\n"
179+ cat " ${TEST_FAILURES_LOG} "
180+ echo -e " \n========================= END FAILURE SUMMARY ========================="
181+ exit 1
182+ fi
0 commit comments