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