Skip to content

Commit

Permalink
[pr-test] add exist 0 for Jenkins pipeline (#142)
Browse files Browse the repository at this point in the history
* [pr-test] add exist 0 for Jekins pipeline

* [pr-test] change to use echo.go for only listen 9095 port
  • Loading branch information
benja-wu authored Jul 24, 2021
1 parent ed2a6ed commit 5cb82af
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
37 changes: 25 additions & 12 deletions build/test/httpserver_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ WRITER01DIR=$EXAMPLEDIR"/writer-001"

# target file related define.
server="writer-001/bin/easegress-server"
backend="$EXAMPLEDIR/backend-service/mirror/mirror.go"
mirror_port=10080
backend="$EXAMPLEDIR/backend-service/echo/echo.go"
httpsvr_port=10080
echo_port=9095
eg_apiport=12381

# color define.
Expand All @@ -49,12 +50,12 @@ function clean()

# clean the go mirror backend
if [ "$1" != "" ];then
echo -e "\n${COLOR_INFO}finish mirror running pid=$1${COLOR_NONE}"
echo -e "\n${COLOR_INFO}finish echo-svr running pid=$1${COLOR_NONE}"
child_pid=`pgrep -P $1`

if [ "$child_pid" != "" ]; then
kill -9 $child_pid
echo -e "\n${COLOR_INFO}finish mirror running child process pid=$child_pid${COLOR_NONE}"
echo -e "\n${COLOR_INFO}finish echo-svr running child process pid=$child_pid${COLOR_NONE}"
fi

kill -9 $1
Expand Down Expand Up @@ -117,11 +118,22 @@ filters:
loadBalance:
policy: roundRobin' | $WRITER01DIR/egctl.sh object create

while ! nc -z localhost $httpsvr_port </dev/null
do
sleep 5
try_time=$(($try_time+1))
if [[ $try_time -ge 3 ]]; then
echo -e "\n{COLOR_ERROR}start mirror server failed${COLOR_NONE}"
clean
exit 3
fi
done

# run the backend.
(go run $backend &)
try_time=0
# wait the mirror backend ready, it will retry three times.
while ! nc -z localhost $mirror_port </dev/null
while ! nc -z localhost $echo_port </dev/null
do
sleep 5
try_time=$(($try_time+1))
Expand All @@ -133,26 +145,27 @@ do
done

# check the mirror backend running status.
mirror_pid=`ps -eo pid,args|grep mirror.go |grep -v grep |awk '{print $1}'`
if [ "$mirror_pid" = "" ]; then
echo_pid=`ps -eo pid,args|grep $backend |grep -v grep |awk '{print $1}'`
if [ "$echo_pid" = "" ]; then
echo -e "\n${COLOR_ERROR}start test backend server failed, command=go run $backend${COLOR_NONE}"
clean
exit 4
else
echo -e "\n${COLOR_INFO}start mirror, its pid=$mirror_pid${COLOR_NONE}"
echo -e "\n${COLOR_INFO}start mirror, its pid=$echo_pid${COLOR_NONE}"
fi

# test backend routed by HTTPServer and Pipeline with curl.
response=$(curl --write-out '%{http_code}' --silent --output /dev/null https://localhost:$mirror_port/pipeline -d'hello easegress')
response=$(curl --write-out '%{http_code}' --silent --output /dev/null https://localhost:$httpsvr_port/pipeline -d'hello easegress')
if [ "$response" != "200" ]; then
echo "curl http server failed, response code "$response
clean $mirror_pid
echo "curl http server failed, response code :$response url is https://localhost:$httpsvr_port/pipeline"
clean $echo_pid
exit 5
else
echo -e "\n${COLOR_INFO}test succ${COLOR_NONE}"
fi

# clean all created resources.
clean $mirror_pid
clean $echo_pid

popd > /dev/null
exit 0
62 changes: 62 additions & 0 deletions example/backend-service/echo/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
"time"
)

// TeeWriter is an io.Writer wapper.
type TeeWriter struct {
writers []io.Writer
}

// NewTeeWriter returns a TeeWriter.
func NewTeeWriter(writers ...io.Writer) *TeeWriter {
return &TeeWriter{writers: writers}
}

// Write writes the data.
func (tw *TeeWriter) Write(p []byte) (n int, err error) {
for _, w := range tw.writers {
w.Write(p)
}
return len(p), nil
}

func main() {
echoHandler := func(w http.ResponseWriter, req *http.Request) {
time.Sleep(10 * time.Millisecond)
body, err := io.ReadAll(req.Body)
if err != nil {
body = []byte(fmt.Sprintf("<read failed: %v>", err))
}

tw := NewTeeWriter(w, os.Stdout)

url := req.URL.Path
if req.URL.Query().Encode() != "" {
url += "?" + req.URL.Query().Encode()
}

fmt.Fprintln(tw, "Your Request")
fmt.Fprintln(tw, "==============")
fmt.Fprintln(tw, "Method:", req.Method)
fmt.Fprintln(tw, "URL :", url)

fmt.Fprintln(tw, "Header:")
for k, v := range req.Header {
fmt.Fprintf(tw, " %s: %v\n", k, v)
}

fmt.Fprintln(tw, "Body :", string(body))
}

http.HandleFunc("/", echoHandler)
http.HandleFunc("/pipeline", echoHandler)

http.ListenAndServe(":9095", nil)
fmt.Println("listen and serve failed")
}
2 changes: 1 addition & 1 deletion example/clean_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ SCRIPTFILE=`basename $0`
for MEMBER_PATH in writer-00{1,2,3} reader-00{4,5}
do
${SCRIPTPATH}/${MEMBER_PATH}/stop.sh -f
rm -fr ${SCRIPTPATH}/${MEMBER_PATH}/{nohup.out,log,member,data,data_bak,easegress.pid}
rm -fr ${SCRIPTPATH}/${MEMBER_PATH}/{nohup.out,log,member,data,data_bak,easegress.pid,running_objects.yaml}
done

0 comments on commit 5cb82af

Please sign in to comment.