2015-01-25 02:48:55 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-01-25 02:48:55 +00:00
|
|
|
|
|
|
|
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 tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2015-01-29 04:35:49 +00:00
|
|
|
"time"
|
2015-01-25 02:48:55 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/probe"
|
2015-01-25 02:48:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTcpHealthChecker(t *testing.T) {
|
2015-11-04 02:29:15 +00:00
|
|
|
// Setup a test server that responds to probing correctly
|
2015-01-25 02:48:55 +00:00
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
2016-04-21 11:50:55 +00:00
|
|
|
defer server.Close()
|
2015-11-04 02:29:15 +00:00
|
|
|
tHost, tPortStr, err := net.SplitHostPort(server.Listener.Addr().String())
|
2015-01-25 02:48:55 +00:00
|
|
|
if err != nil {
|
2015-11-04 02:29:15 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
2015-01-25 02:48:55 +00:00
|
|
|
}
|
2015-11-04 02:29:15 +00:00
|
|
|
tPort, err := strconv.Atoi(tPortStr)
|
2015-01-25 02:48:55 +00:00
|
|
|
if err != nil {
|
2015-11-04 02:29:15 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
2015-01-25 02:48:55 +00:00
|
|
|
}
|
2015-11-04 02:29:15 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
|
|
|
|
expectedStatus probe.Result
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
// A connection is made and probing would succeed
|
2017-04-18 20:24:18 +00:00
|
|
|
{tHost, tPort, probe.Success, nil},
|
2015-11-04 02:29:15 +00:00
|
|
|
// No connection can be made and probing would fail
|
2017-04-18 20:24:18 +00:00
|
|
|
{tHost, -1, probe.Failure, nil},
|
2015-11-04 02:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prober := New()
|
|
|
|
for i, tt := range tests {
|
2017-04-18 20:24:18 +00:00
|
|
|
status, _, err := prober.Probe(tt.host, tt.port, 1*time.Second)
|
2015-11-04 02:29:15 +00:00
|
|
|
if status != tt.expectedStatus {
|
|
|
|
t.Errorf("#%d: expected status=%v, get=%v", i, tt.expectedStatus, status)
|
2015-01-25 02:48:55 +00:00
|
|
|
}
|
2015-11-04 02:29:15 +00:00
|
|
|
if err != tt.expectedError {
|
|
|
|
t.Errorf("#%d: expected error=%v, get=%v", i, tt.expectedError, err)
|
2015-01-25 02:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|