3875d9f579a0082727e25a2d8fec40c80a240b88
[vpp.git] / extras / vcl-ldpreload / test / common / nginx_test.sh
1 #! /bin/bash
2 #
3 # nginx_test.sh
4 #
5 #   Run specified app using LD_PRELOAD to fetch a page from
6 #   nginx running in vpp1 net-namespace.
7 #
8 # Specify the following environment variables:
9 #
10 # STRACE_ONLY - Run app with strace instead of LD_PRELOAD.
11 # TEST_APP    - App to run (default: curl)
12
13
14 # Configuration variables.
15 #
16 vpp_dk_name="VPP-TEST-NGINX"
17 # Comment out the next line to run the VPP release version
18 debug="_debug"
19 vpp_app="$WS_ROOT/build-root/install-vpp${debug}-native/vpp/bin/vpp"
20
21 check_for_vpp() {
22     local grep_for_vpp="ps -eaf|grep -v grep|grep \"bin/vpp\""
23     running_vpp="$(eval $grep_for_vpp)"
24 }
25
26 # Verify Environment.
27 if [ -z "$WS_ROOT" ] ; then
28     echo "ERROR: WS_ROOT environment variable not set!" >&2
29     echo "       Please set WS_ROOT to VPP workspace root directory." >&2
30     exit 1
31 fi
32 if [ -z "$LDP_DIR" ] ; then
33     echo "WARNING: LDP_DIR environment variable is not set!"
34     echo "         Sourcing $WS_ROOT/extras/vcl-ldpreload/env.sh"
35     source $WS_ROOT/extras/vcl-ldpreload/env.sh
36 fi
37
38 TEST_APP="${TEST_APP:-curl}"
39 LDP_TEST_DIR="${LDP_TEST_DIR:-${LDP_DIR}/test}"
40 LDP_LIB="${LDP_LIB:-${VCL_LDPRELOAD_LIB_DIR}/libvcl_ldpreload.so.0.0.0}"
41
42 if [ ! -f "$LDP_LIB" ] ; then
43     echo "ERROR: Missing VCL-LDPRELOAD Library: $LDP_LIB"
44     echo "       See $LDP_DIR/README.md for build instructions!"
45     exit 1
46 fi
47
48 if [ -z "$(docker ps -qf name=$vpp_dk_name)" ] ; then
49     echo "Starting NGINX in docker container ($vpp_dk_name)"
50     docker run --rm --name $vpp_dk_name -v $LDP_TEST_DIR/common/nginx_welcome.html:/usr/share/nginx/html/index.html:ro -d nginx
51
52     echo "Configuring network interfaces"
53     sudo ip link del dev vpp_dk
54     sudo ip link add name vpp_dk type veth peer name vpp1
55     sudo ip link set dev vpp_dk up
56     sudo ethtool --offload vpp_dk rx off tx off
57     sudo ip link set dev vpp1 up
58     sudo ethtool --offload vpp1 rx off tx off
59     sudo ip link set dev lo up
60     sudo brctl addif docker0 vpp_dk
61 fi
62
63 export LD_LIBRARY_PATH="$WS_ROOT/build-root/install-vpp${debug}-native/vpp/lib64/:$LDP_DIR/src/.libs:"
64
65 # Extract nginx IPv4 address from docker bridge
66 #
67 nginx_addr=$(docker network inspect bridge | grep IPv4Address | awk -e '{print $2}' | sed -e 's,/16,,' -e 's,",,g' -e 's/,//')
68
69 if [ -z "$nginx_addr" ] ; then
70     echo "ERROR: Unable to determine docker container address!"
71     exit 1
72 fi
73
74 if [ -n "$STRACE_ONLY" ] ; then
75     echo "Running strace -tt $TEST_APP http://$nginx_addr"
76     strace -tt $TEST_APP http://$nginx_addr
77 else
78     check_for_vpp
79     if [ -z "$running_vpp" ] ; then
80         echo "Starting VPP ('telnet 0 5002' to connect to cli)"
81         sudo $vpp_app unix { cli-listen localhost:5002 exec $LDP_TEST_DIR/common/vpp_docker.conf } api-segment { gid $(id -g) }
82         sleep 2
83     fi
84     echo "Running LD_PRELOAD=$LDP_LIB $TEST_APP http://$nginx_addr"
85     LD_PRELOAD=$LDP_LIB $TEST_APP http://$nginx_addr
86 fi