github.com/rootless-containers/rootlesskit/v2@v2.3.4/hack/integration-port.sh (about)

     1  #!/bin/bash
     2  source $(realpath $(dirname $0))/common.inc.sh
     3  
     4  # test_port PORT_DRIVER CURL_URL EXPECTATION [ROOTLESSKIT ARGS...]
     5  function test_port() {
     6  	args="$@"
     7  	port_driver="$1"
     8  	curl_url="$2"
     9  	expectation="$3"
    10  	shift
    11  	shift
    12  	shift
    13  	rootlesskit_args="$@"
    14  	INFO "Testing port_driver=\"${port_driver}\" curl_url=\"${curl_url}\" expectation=\"${expectation}\" rootlesskit_args=\"${rootlesskit_args}\""
    15  	tmp=$(mktemp -d)
    16  	state_dir=${tmp}/state
    17  	html_dir=${tmp}/html
    18  	mkdir -p ${html_dir}
    19  	echo "test_port ($args)" >${html_dir}/index.html
    20  	$ROOTLESSKIT \
    21  		--state-dir=${state_dir} \
    22  		--net=slirp4netns \
    23  		--disable-host-loopback \
    24  		--copy-up=/etc \
    25  		--port-driver=${port_driver} \
    26  		${rootlesskit_args} \
    27  		busybox httpd -f -v -p 80 -h ${html_dir} \
    28  		2>&1 &
    29  	pid=$!
    30  	sleep 1
    31  
    32  	set +e
    33  	curl -fsSL ${curl_url}
    34  	code=$?
    35  	set -e
    36  	if [ "${expectation}" = "should success" ]; then
    37  		if [ ${code} != 0 ]; then
    38  			ERROR "curl exited with ${code}"
    39  			exit ${code}
    40  		fi
    41  	elif [ "${expectation}" = "should fail" ]; then
    42  		if [ ${code} = 0 ]; then
    43  			ERROR "curl should not success"
    44  			exit 1
    45  		fi
    46  	else
    47  		ERROR "internal error"
    48  		exit 1
    49  	fi
    50  
    51  	INFO "Test pasing, stopping httpd (\"exit status 255\" is negligible here)"
    52  	kill -SIGTERM $(cat ${state_dir}/child_pid)
    53  	wait $pid >/dev/null 2>&1 || true
    54  	rm -rf $tmp
    55  }
    56  
    57  INFO "===== Port driver: builtin ====="
    58  INFO "=== protocol \"tcp\" listens on both v4 and v6 ==="
    59  test_port builtin http://127.0.0.1:8080 "should success" -p 0.0.0.0:8080:80/tcp
    60  test_port builtin http://[::1]:8080 "should success" -p 0.0.0.0:8080:80/tcp
    61  
    62  INFO "=== protocol \"tcp4\" is strictly v4-only ==="
    63  test_port builtin http://127.0.0.1:8080 "should success" -p 0.0.0.0:8080:80/tcp4
    64  test_port builtin http://[::1]:8080 "should fail" -p 0.0.0.0:8080:80/tcp4
    65  
    66  INFO "=== protocol \"tcp6\" is strictly v6-only ==="
    67  test_port builtin http://127.0.0.1:8080 "should fail" -p [::]:8080:80/tcp6
    68  test_port builtin http://[::1]:8080 "should success" -p [::]:8080:80/tcp6
    69  
    70  INFO "=== v6-to-v6 ==="
    71  test_port builtin http://[::1]:8080 "should success" -p [::]:8080:[::1]:80/tcp6
    72  test_port builtin http://[::1]:8080 "should success" -p [::]:8080:[::1]:80/tcp
    73  
    74  INFO "=== v6-to-v4 ==="
    75  test_port builtin http://[::1]:8080 "should success" -p [::]:8080:[127.0.0.1]:80/tcp6
    76  test_port builtin http://[::1]:8080 "should success" -p [::]:8080:[127.0.0.1]:80/tcp
    77  
    78  INFO "=== v4-to-v6 ==="
    79  test_port builtin http://127.0.0.1:8080 "should success" -p 0.0.0.0:8080:[::1]:80/tcp4
    80  test_port builtin http://127.0.0.1:8080 "should success" -p 0.0.0.0:8080:[::1]:80/tcp
    81  
    82  INFO "=== \"tcp4\" and \"tcp6\" do not conflict ==="
    83  test_port builtin http://127.0.0.1:8080 "should success" -p 0.0.0.0:8080:80/tcp4 -p [::]:8080:80/tcp6
    84  
    85  INFO "===== Port driver: slirp4netns (IPv4 only)====="
    86  INFO "=== protocol \"tcp\" listens on v4 ==="
    87  test_port slirp4netns http://127.0.0.1:8080 "should success" -p 0.0.0.0:8080:80/tcp
    88  
    89  INFO "=== protocol \"tcp4\" is strictly v4-only ==="
    90  test_port slirp4netns http://[::1]:8080 "should fail" -p 0.0.0.0:8080:80/tcp4
    91  
    92  INFO "===== PASSING ====="