File
Blob: src/workerd/server/tests/compile-tests/compile-test.sh
| 1 | #!/bin/bash |
| 2 | |
| 3 | set -o errexit |
| 4 | set -o nounset |
| 5 | set -o pipefail |
| 6 | |
| 7 | # $1 -> workerd binary path |
| 8 | # $2 -> path to desired file to compile |
| 9 | # $3 -> port to curl |
| 10 | # $4 -> desired output |
| 11 | |
| 12 | # Help Function |
| 13 | function show_help() { |
| 14 | echo " |
| 15 | The Compile Test script is designed to aid in testing compiled workerd binaries. |
| 16 | It works by verifying that the result of a curl command on a running compiled binary matches an expected output. |
| 17 | usage: compile-test.sh [-d] [-h] <workerd command> <file-to-compile> <port-to-curl> <expected-output-file> |
| 18 | options: |
| 19 | -d print out where tmp files are created and do not delete them |
| 20 | -h this help message |
| 21 | |
| 22 | Note: all flags must occur before arguments |
| 23 | " |
| 24 | } |
| 25 | |
| 26 | while getopts "h" option; do |
| 27 | case ${option} in |
| 28 | h) |
| 29 | show_help |
| 30 | exit |
| 31 | ;; |
| 32 | esac |
| 33 | done |
| 34 | |
| 35 | shift $(expr $OPTIND - 1) |
| 36 | WORKERD_BINARY=$1 |
| 37 | CAPNP_SOURCE=$2 |
| 38 | EXPECTED=$3 |
| 39 | |
| 40 | CAPNP_BINARY=$(mktemp) |
| 41 | PORT_FILE=$(mktemp) |
| 42 | |
| 43 | # Compile the app |
| 44 | $WORKERD_BINARY compile $CAPNP_SOURCE > $CAPNP_BINARY |
| 45 | |
| 46 | # Run the app |
| 47 | $CAPNP_BINARY -shttp=localhost:0 --control-fd=1 > $PORT_FILE & |
| 48 | KILL=$! |
| 49 | |
| 50 | # Make intermediate files before trying to wait on the bindings |
| 51 | OUTPUT=$(mktemp) |
| 52 | FIXED_OUTPUT=$(mktemp) |
| 53 | FIXED_EXPECTED=$(mktemp) |
| 54 | |
| 55 | # Wait on the port bindings to occur |
| 56 | while ! grep \"socket\"\:\"http\" $PORT_FILE; do |
| 57 | sleep .1 |
| 58 | done |
| 59 | |
| 60 | # Identify the port chosen by the binary |
| 61 | PORT=`grep \"socket\"\:\"http\" $PORT_FILE | sed 's/^.*\"port\"://g' | sed 's/\}//g' |head -n 1` |
| 62 | |
| 63 | # Request output |
| 64 | curl localhost:$PORT -o $OUTPUT |
| 65 | |
| 66 | # Compare the tests to the expected output |
| 67 | sed -e's/[[:space:]]*$//' $OUTPUT > $FIXED_OUTPUT |
| 68 | sed -e's/[[:space:]]*$//' $EXPECTED > $FIXED_EXPECTED |
| 69 | diff $FIXED_OUTPUT $FIXED_EXPECTED |
| 70 | |
| 71 | # Clean up running workerd |
| 72 | kill -9 $KILL |
| 73 | |
| 74 | # Clean up temp files |
| 75 | rm -f $CAPNP_BINARY |
| 76 | rm -f $PORT_FILE |
| 77 | rm -f $OUTPUT |
| 78 | rm -f $FIXED_OUTPUT |
| 79 | rm -f $FIXED_EXPECTED |