From 81d69dea54074339e9a5f3811dee11c481d7b3c7 Mon Sep 17 00:00:00 2001 From: memoryliu Date: Mon, 3 Aug 2020 23:33:26 +0800 Subject: [PATCH] Fix cover service listen port issue for #74 --- pkg/cover/instrument.go | 46 +++++++----- tests/agent.bats | 108 +++++++++++++++++++++++++++++ tests/run-ci-actions.sh | 2 + tests/samples/simple_agent/go.mod | 3 + tests/samples/simple_agent/main.go | 11 +++ 5 files changed, 153 insertions(+), 17 deletions(-) create mode 100755 tests/agent.bats create mode 100644 tests/samples/simple_agent/go.mod create mode 100644 tests/samples/simple_agent/main.go diff --git a/pkg/cover/instrument.go b/pkg/cover/instrument.go index 78a75dd..c5a2faa 100644 --- a/pkg/cover/instrument.go +++ b/pkg/cover/instrument.go @@ -153,7 +153,7 @@ func registerHandlers() { if resp, err := registerSelf(profileAddr); err != nil { log.Fatalf("register address %v failed, err: %v, response: %v", profileAddr, err, string(resp)) } - go genProfileAddr(host) + mux := http.NewServeMux() // Coverage reports the current code coverage as a fraction in the range [0, 1]. // If coverage is not enabled, Coverage returns 0. @@ -206,7 +206,7 @@ func registerHandlers() { mux.HandleFunc("/v1/cover/clear", func(w http.ResponseWriter, r *http.Request) { clearValues() w.WriteHeader(http.StatusOK) - fmt.Fprintln(w,"clear call successfully") + fmt.Fprintln(w, "clear call successfully") }) log.Fatal(http.Serve(ln, mux)) @@ -252,26 +252,37 @@ func isNetworkError(err error) bool { } func listen() (ln net.Listener, host string, err error) { - // 获取上次使用的监听地址 - if previousAddr := getPreviousAddr(); previousAddr != "" { - ss := strings.Split(previousAddr, ":") - // listen on all network interface - ln, err = net.Listen("tcp4", ":"+ss[len(ss)-1]) - if err == nil { - host = previousAddr - return - } - } agentPort := "{{.AgentPort }}" if agentPort != "" { - ln, err = net.Listen("tcp4", agentPort) + if ln, err = net.Listen("tcp4", agentPort); err != nil { + return + } + if host, err = getRealHost(ln); err != nil { + return + } } else { - ln, err = net.Listen("tcp4", ":0") - } - if err != nil { - return + // 获取上次使用的监听地址 + if previousAddr := getPreviousAddr(); previousAddr != "" { + ss := strings.Split(previousAddr, ":") + // listen on all network interface + ln, err = net.Listen("tcp4", ":"+ss[len(ss)-1]) + if err == nil { + host = previousAddr + return + } + } + if ln, err = net.Listen("tcp4", ":0"); err != nil { + return + } + if host, err = getRealHost(ln); err != nil { + return + } } + go genProfileAddr(host) + return +} +func getRealHost(ln net.Listener) (host string, err error) { adds, err := net.InterfaceAddrs() if err != nil { return @@ -293,6 +304,7 @@ func listen() (ln net.Listener, host string, err error) { } else { host = fmt.Sprintf("%s:%d", localIPV4, ln.Addr().(*net.TCPAddr).Port) } + return } diff --git a/tests/agent.bats b/tests/agent.bats new file mode 100755 index 0000000..43512c6 --- /dev/null +++ b/tests/agent.bats @@ -0,0 +1,108 @@ +#!/usr/bin/env bats +# Copyright 2020 Qiniu Cloud (七牛云) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load util.sh + +setup_file() { + if [ -e samples/simple_agent/register.port ]; then + rm samples/simple_agent/register.port + fi + if [ -e samples/simple_agent/simple-agent_profile_listen_addr ]; then + rm samples/simple_agent/simple-agent_profile_listen_addr + fi + # run centered server + goc server > samples/simple_agent/register.port & + GOC_PID=$! + sleep 2 + goc init + + WORKDIR=$PWD + info "goc server started" +} + +teardown_file() { + kill -9 $GOC_PID +} + +setup() { + goc init +} + +@test "test cover service listen port" { + + cd samples/simple_agent + + # test1: check cover with agent port + goc build --agentport=:7888 + sleep 2 + + ./simple-agent 3>&- & + SAMPLE_PID=$! + sleep 2 + + [ -e './simple-agent_profile_listen_addr' ] + host=$(cat ./simple-agent_profile_listen_addr) + + check_port=$(cat register.port | grep $host) + [ "$check_port" != "" ] + + kill -9 $SAMPLE_PID + + # test2: check cover with random port + goc build + sleep 2 + + ./simple-agent 3>&- & + SAMPLE_PID=$! + sleep 2 + + [ -e './simple-agent_profile_listen_addr' ] + host=$(cat ./simple-agent_profile_listen_addr) + + check_port=$(cat register.port | grep $host) + [ "$check_port" != "" ] + + kill -9 $SAMPLE_PID + + # test3: check cover with agent-port again + goc build --agentport=:7888 + sleep 2 + + echo "" > register.port + ./simple-agent 3>&- & + SAMPLE_PID=$! + sleep 2 + + check_port=$(cat register.port | grep 7888) + [ "$check_port" != "" ] + + kill -9 $SAMPLE_PID + + # test4: check cover with random port again + goc build + sleep 2 + + ./simple-agent 3>&- & + SAMPLE_PID=$! + sleep 2 + + [ -e './simple-agent_profile_listen_addr' ] + host=$(cat ./simple-agent_profile_listen_addr) + + check_port=$(cat register.port | grep $host) + [ "$check_port" != "" ] + + kill -9 $SAMPLE_PID +} \ No newline at end of file diff --git a/tests/run-ci-actions.sh b/tests/run-ci-actions.sh index 792b68d..48ac66f 100755 --- a/tests/run-ci-actions.sh +++ b/tests/run-ci-actions.sh @@ -41,4 +41,6 @@ bats -t diff.bats bats -t cover.bats +bats -t agent.bats + bash <(curl -s https://codecov.io/bash) -f 'filtered*' -F e2e-$GOVERSION \ No newline at end of file diff --git a/tests/samples/simple_agent/go.mod b/tests/samples/simple_agent/go.mod new file mode 100644 index 0000000..9952808 --- /dev/null +++ b/tests/samples/simple_agent/go.mod @@ -0,0 +1,3 @@ +module example.com/simple-agent + +go 1.14 diff --git a/tests/samples/simple_agent/main.go b/tests/samples/simple_agent/main.go new file mode 100644 index 0000000..f8e8cbc --- /dev/null +++ b/tests/samples/simple_agent/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + fmt.Println("hello") + time.Sleep(time.Second * 15) +}