1+ #! /bin/bash -e
2+
3+ # Load framework functions
4+ . ../framework.sh
5+
6+ # TEST VARIABLES
7+ NODE=runcvm-01-test
8+ NETWORK=" $NODE -network"
9+ IMAGE=" alpine"
10+ RUNTIME=" ${RUNTIME:- runcvm} "
11+
12+ # OVERRIDE FRAMEWORK FUNCTIONS
13+ nodes () { echo $NODE ; }
14+ networks () { echo $NETWORK ; }
15+
16+ # TEST DETAILS
17+ COMMAND=' env | sort'
18+ EXPECTED_OUTPUT=" $( echo -e ' HOME=/root\nHOSTNAME=runcvm-01-test\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nPWD=/\nSHLVL=1\n' ) "
19+
20+ # TEST FUNCTIONS
21+ # --------------
22+
23+ # Function to test output against expected values
24+ test_output () {
25+ local test_type=" $1 "
26+ local expected_output=" $2 "
27+ local output_to_test=" $3 "
28+
29+ if [ " $output_to_test " = " $expected_output " ]; then
30+ log " docker $test_type test: expected and received '$output_to_test ' - PASS"
31+ return 0
32+ fi
33+
34+ log " docker $test_type test: expected '$expected_output ', but got: '$output_to_test ' - FAIL"
35+ return 1
36+ }
37+
38+ # TEST PROCEDURE
39+ # --------------
40+
41+ # Create custom network
42+ log -n " Creating network '$NETWORK ' ..."
43+ docker network create $NETWORK
44+
45+ # Create and run the container
46+ log -n " Launching runcvm container with command '$COMMAND ' ..."
47+ docker run \
48+ -d \
49+ --rm \
50+ --runtime=$RUNTIME \
51+ --network=$NETWORK \
52+ --name=$NODE \
53+ --hostname=$NODE \
54+ --user=$USER_ID \
55+ --workdir=$WORK_DIR \
56+ --init \
57+ $IMAGE \
58+ sh -c " $COMMAND ; while true; do echo ===DONE===; sleep 1; done"
59+
60+ shopt -s lastpipe
61+ log " Container '$NODE ' output ..."
62+ docker logs -f $NODE 2>&1 | sed " s/^/($NODE ) > /; /===DONE===/q0;"
63+
64+ ERRORS=0
65+
66+ # Test docker run command:
67+ # - Retrieve first line of logs from container
68+ # - Strip carriage returns for now. as it's unclear why they are present and are not present in the expected output
69+ test_output " run" " $EXPECTED_OUTPUT " " $( docker logs $NODE | grep -v ' ===DONE===' | tr -d ' \015' ) " || ERRORS=$(( ERRORS+ 1 ))
70+
71+ # Test docker exec command:
72+ # - Retrieve output from exec command for exec test
73+ test_output " exec" " $EXPECTED_OUTPUT " " $( docker exec $NODE sh -c " $COMMAND " ) " || ERRORS=$(( ERRORS+ 1 ))
74+
75+ # Final output
76+ log " Tests completed with $ERRORS errors"
77+ exit $ERRORS
0 commit comments