When node got proxy settings like "http_proxy=xxx".

The http probe would use that settings. This could cause probe failure.
This patch fix it.
pull/564/head
WanLinghao 2018-09-27 11:45:18 +08:00
parent 9b9f5f949d
commit 29f7e537a9
2 changed files with 64 additions and 1 deletions

View File

@ -39,7 +39,13 @@ func New() Prober {
// NewWithTLSConfig takes tls config as parameter.
func NewWithTLSConfig(config *tls.Config) Prober {
transport := utilnet.SetTransportDefaults(&http.Transport{TLSClientConfig: config, DisableKeepAlives: true})
// We do not want the probe use node's local proxy set.
transport := utilnet.SetTransportDefaults(
&http.Transport{
TLSClientConfig: config,
DisableKeepAlives: true,
Proxy: http.ProxyURL(nil),
})
return httpProber{transport}
}

View File

@ -22,6 +22,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
@ -32,6 +33,62 @@ import (
const FailureCode int = -1
func setEnv(key, value string) func() {
originalValue := os.Getenv(key)
os.Setenv(key, value)
if len(originalValue) > 0 {
return func() {
os.Setenv(key, originalValue)
}
}
return func() {}
}
func unsetEnv(key string) func() {
originalValue := os.Getenv(key)
os.Unsetenv(key)
if len(originalValue) > 0 {
return func() {
os.Setenv(key, originalValue)
}
}
return func() {}
}
func TestHTTPProbeProxy(t *testing.T) {
res := "welcome to http probe proxy"
localProxy := "http://127.0.0.1:9098/"
defer setEnv("http_proxy", localProxy)()
defer setEnv("HTTP_PROXY", localProxy)()
defer unsetEnv("no_proxy")()
defer unsetEnv("NO_PROXY")()
prober := New()
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, res)
})
err := http.ListenAndServe(":9098", nil)
if err != nil {
t.Errorf("Failed to start foo server: localhost:9098")
}
}()
// take some time to wait server boot
time.Sleep(2 * time.Second)
url, err := url.Parse("http://example.com")
if err != nil {
t.Errorf("proxy test unexpected error: %v", err)
}
_, response, _ := prober.Probe(url, http.Header{}, time.Second*3)
if response == res {
t.Errorf("proxy test unexpected error: the probe is using proxy")
}
}
func TestHTTPProbeChecker(t *testing.T) {
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {