github.com/ezoic/ws@v1.0.4-0.20220713205711-5c1d69e074c5/autobahn/script/test.sh (about)

     1  #!/bin/bash
     2  
     3  FOLLOW_LOGS=0
     4  
     5  while [[ $# -gt 0 ]]; do
     6  	key="$1"
     7  	case $key in
     8  		--network)
     9  		NETWORK="$2"
    10  		shift
    11  		;;
    12  
    13  		--build)
    14  		case "$2" in
    15  			autobahn)
    16  				docker build . --file autobahn/docker/autobahn/Dockerfile --tag autobahn
    17  				shift
    18  			;;
    19  			server)
    20  				docker build . --file autobahn/docker/server/Dockerfile --tag server
    21  				shift
    22  			;;
    23  			*)
    24  				docker build . --file autobahn/docker/autobahn/Dockerfile --tag autobahn
    25  				docker build . --file autobahn/docker/server/Dockerfile --tag server
    26  			;;
    27  		esac
    28  		;;
    29  
    30  		--run)
    31  		docker run \
    32  			--interactive \
    33  			--tty \
    34  			${@:2}
    35  		exit $?
    36  		;;
    37  
    38  		--follow-logs)
    39  		FOLLOW_LOGS=1
    40  		shift
    41  		;;
    42  	esac
    43  	shift
    44  done
    45  
    46  with_prefix() {
    47  	local p="$1"
    48  	shift
    49  	
    50  	local out=$(mktemp -u ws.fifo.out.XXXX)
    51  	local err=$(mktemp -u ws.fifo.err.XXXX)
    52  	mkfifo $out $err
    53  	if [ $? -ne 0 ]; then
    54  		exit 1
    55  	fi
    56  	
    57  	# Start two background sed processes.
    58  	sed "s/^/$p/" <$out &
    59  	sed "s/^/$p/" <$err >&2 &
    60  	
    61  	# Run the program
    62  	"$@" >$out 2>$err
    63  	rm $out $err
    64  }
    65  
    66  random=$(xxd -l 4 -p /dev/random)
    67  server="${random}_server"
    68  autobahn="${random}_autobahn"
    69  
    70  network="ws-$random"
    71  docker network create --driver bridge "$network"
    72  if [ $? -ne 0 ]; then
    73  	exit 1
    74  fi
    75  
    76  docker run \
    77  	--interactive \
    78  	--tty \
    79  	--detach \
    80  	--network="$network" \
    81  	--network-alias="server" \
    82  	-v $(pwd)/autobahn/report:/report \
    83  	--name="$server" \
    84  	"server"
    85  
    86  docker run \
    87  	--interactive \
    88  	--tty \
    89  	--detach \
    90  	--network="$network" \
    91  	-v $(pwd)/autobahn/config:/config \
    92  	-v $(pwd)/autobahn/report:/report \
    93     	--name="$autobahn" \
    94  	"autobahn"
    95  
    96  
    97  if [[ $FOLLOW_LOGS -eq 1 ]]; then
    98  	(with_prefix "$(tput setaf 3)[autobahn]: $(tput sgr0)" docker logs --follow "$autobahn")&
    99  	(with_prefix "$(tput setaf 5)[server]:   $(tput sgr0)" docker logs --follow "$server")&
   100  fi
   101  
   102  trap ctrl_c INT
   103  ctrl_c () {
   104  	echo "SIGINT received; cleaning up"
   105  	docker kill --signal INT "$autobahn" >/dev/null
   106  	docker kill --signal INT "$server" >/dev/null
   107  	cleanup
   108  	exit 130
   109  } 
   110  
   111  cleanup() {
   112  	docker rm "$server" >/dev/null
   113  	docker rm "$autobahn" >/dev/null
   114  	docker network rm "$network"
   115  }
   116  
   117  docker wait "$autobahn" >/dev/null
   118  docker stop "$server" >/dev/null
   119  
   120  cleanup