2015-01-25 02:48:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2015-01-29 04:35:49 +00:00
|
|
|
"time"
|
2015-01-25 02:48:55 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormatURL(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
path string
|
|
|
|
result string
|
|
|
|
}{
|
|
|
|
{"localhost", 93, "", "http://localhost:93"},
|
|
|
|
{"localhost", 93, "/path", "http://localhost:93/path"},
|
|
|
|
}
|
|
|
|
for _, test := range testCases {
|
|
|
|
url := formatURL(test.host, test.port, test.path)
|
|
|
|
if url != test.result {
|
|
|
|
t.Errorf("Expected %s, got %s", test.result, url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPProbeChecker(t *testing.T) {
|
2015-01-29 04:35:49 +00:00
|
|
|
handleReq := func(s int) func(w http.ResponseWriter) {
|
|
|
|
return func(w http.ResponseWriter) { w.WriteHeader(s) }
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:00:21 +00:00
|
|
|
prober := New()
|
2015-01-25 02:48:55 +00:00
|
|
|
testCases := []struct {
|
2015-01-29 04:35:49 +00:00
|
|
|
handler func(w http.ResponseWriter)
|
2015-01-31 01:03:57 +00:00
|
|
|
health probe.Result
|
2015-01-25 02:48:55 +00:00
|
|
|
}{
|
|
|
|
// The probe will be filled in below. This is primarily testing that an HTTP GET happens.
|
2015-01-29 04:35:49 +00:00
|
|
|
{handleReq(http.StatusOK), probe.Success},
|
|
|
|
{handleReq(-1), probe.Failure},
|
|
|
|
{func(w http.ResponseWriter) { time.Sleep(3 * time.Second) }, probe.Failure},
|
2015-01-25 02:48:55 +00:00
|
|
|
}
|
|
|
|
for _, test := range testCases {
|
2015-01-29 04:35:49 +00:00
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
test.handler(w)
|
2015-01-25 02:48:55 +00:00
|
|
|
}))
|
2015-01-29 04:35:49 +00:00
|
|
|
u, err := url.Parse(server.URL)
|
2015-01-25 02:48:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
host, port, err := net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
p, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2015-01-29 04:35:49 +00:00
|
|
|
health, err := prober.Probe(host, p, "", 1*time.Second)
|
2015-01-25 02:48:55 +00:00
|
|
|
if test.health == probe.Unknown && err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
|
|
|
if test.health != probe.Unknown && err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if health != test.health {
|
|
|
|
t.Errorf("Expected %v, got %v", test.health, health)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|