mirror of https://github.com/k3s-io/k3s
commit
8ffc8ef4dd
|
@ -39,7 +39,13 @@ func New() Prober {
|
||||||
|
|
||||||
// NewWithTLSConfig takes tls config as parameter.
|
// NewWithTLSConfig takes tls config as parameter.
|
||||||
func NewWithTLSConfig(config *tls.Config) Prober {
|
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}
|
return httpProber{transport}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -32,6 +33,62 @@ import (
|
||||||
|
|
||||||
const FailureCode int = -1
|
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) {
|
func TestHTTPProbeChecker(t *testing.T) {
|
||||||
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
|
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
Loading…
Reference in New Issue