2015-04-28 23:05:26 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-04-28 23:05:26 +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 prober
|
|
|
|
|
|
|
|
import (
|
2015-09-18 00:04:20 +00:00
|
|
|
"errors"
|
2015-04-28 23:05:26 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-18 00:04:20 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/probe"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
|
|
|
"k8s.io/kubernetes/pkg/util/exec"
|
2015-04-28 23:05:26 +00:00
|
|
|
)
|
|
|
|
|
2015-06-25 17:53:41 +00:00
|
|
|
func TestFormatURL(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
scheme string
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
path string
|
|
|
|
result string
|
|
|
|
}{
|
|
|
|
{"http", "localhost", 93, "", "http://localhost:93"},
|
|
|
|
{"https", "localhost", 93, "/path", "https://localhost:93/path"},
|
|
|
|
}
|
|
|
|
for _, test := range testCases {
|
|
|
|
url := formatURL(test.scheme, test.host, test.port, test.path)
|
|
|
|
if url.String() != test.result {
|
|
|
|
t.Errorf("Expected %s, got %s", test.result, url.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 23:05:26 +00:00
|
|
|
func TestFindPortByName(t *testing.T) {
|
|
|
|
container := api.Container{
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
2015-08-14 02:06:59 +00:00
|
|
|
Name: "foo",
|
|
|
|
ContainerPort: 8080,
|
2015-04-28 23:05:26 +00:00
|
|
|
},
|
|
|
|
{
|
2015-08-14 02:06:59 +00:00
|
|
|
Name: "bar",
|
|
|
|
ContainerPort: 9000,
|
2015-04-28 23:05:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
want := 8080
|
2015-06-25 17:53:41 +00:00
|
|
|
got, err := findPortByName(container, "foo")
|
|
|
|
if got != want || err != nil {
|
|
|
|
t.Errorf("Expected %v, got %v, err: %v", want, got, err)
|
2015-04-28 23:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetURLParts(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
probe *api.HTTPGetAction
|
|
|
|
ok bool
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
path string
|
|
|
|
}{
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromInt(-1), Path: ""}, false, "", -1, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString(""), Path: ""}, false, "", -1, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("-1"), Path: ""}, false, "", -1, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("not-found"), Path: ""}, false, "", -1, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("found"), Path: ""}, true, "127.0.0.1", 93, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromInt(76), Path: ""}, true, "127.0.0.1", 76, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("118"), Path: ""}, true, "127.0.0.1", 118, ""},
|
|
|
|
{&api.HTTPGetAction{Host: "hostname", Port: util.NewIntOrStringFromInt(76), Path: "path"}, true, "hostname", 76, "path"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
state := api.PodStatus{PodIP: "127.0.0.1"}
|
|
|
|
container := api.Container{
|
2015-08-14 02:06:59 +00:00
|
|
|
Ports: []api.ContainerPort{{Name: "found", ContainerPort: 93}},
|
2015-04-28 23:05:26 +00:00
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
HTTPGet: test.probe,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-06-25 17:53:41 +00:00
|
|
|
|
|
|
|
scheme := test.probe.Scheme
|
|
|
|
if scheme == "" {
|
|
|
|
scheme = api.URISchemeHTTP
|
|
|
|
}
|
|
|
|
host := test.probe.Host
|
|
|
|
if host == "" {
|
|
|
|
host = state.PodIP
|
|
|
|
}
|
|
|
|
port, err := extractPort(test.probe.Port, container)
|
2015-04-28 23:05:26 +00:00
|
|
|
if test.ok && err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2015-06-25 17:53:41 +00:00
|
|
|
path := test.probe.Path
|
|
|
|
|
2015-04-28 23:05:26 +00:00
|
|
|
if !test.ok && err == nil {
|
2015-06-25 17:53:41 +00:00
|
|
|
t.Errorf("Expected error for %+v, got %s%s:%d/%s", test, scheme, host, port, path)
|
2015-04-28 23:05:26 +00:00
|
|
|
}
|
|
|
|
if test.ok {
|
|
|
|
if host != test.host || port != test.port || path != test.path {
|
|
|
|
t.Errorf("Expected %s:%d/%s, got %s:%d/%s",
|
|
|
|
test.host, test.port, test.path, host, port, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTCPAddrParts(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
probe *api.TCPSocketAction
|
|
|
|
ok bool
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
}{
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromInt(-1)}, false, "", -1},
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("")}, false, "", -1},
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("-1")}, false, "", -1},
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("not-found")}, false, "", -1},
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("found")}, true, "1.2.3.4", 93},
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromInt(76)}, true, "1.2.3.4", 76},
|
|
|
|
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("118")}, true, "1.2.3.4", 118},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
host := "1.2.3.4"
|
|
|
|
container := api.Container{
|
2015-08-14 02:06:59 +00:00
|
|
|
Ports: []api.ContainerPort{{Name: "found", ContainerPort: 93}},
|
2015-04-28 23:05:26 +00:00
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
TCPSocket: test.probe,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
port, err := extractPort(test.probe.Port, container)
|
|
|
|
if !test.ok && err == nil {
|
|
|
|
t.Errorf("Expected error for %+v, got %s:%d", test, host, port)
|
|
|
|
}
|
|
|
|
if test.ok && err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if test.ok {
|
|
|
|
if host != test.host || port != test.port {
|
|
|
|
t.Errorf("Expected %s:%d, got %s:%d", test.host, test.port, host, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 00:04:20 +00:00
|
|
|
// TestProbeContainer tests the functionality of probeContainer.
|
|
|
|
// Test cases are:
|
|
|
|
//
|
|
|
|
// No probe.
|
|
|
|
// Only LivenessProbe.
|
|
|
|
// Only ReadinessProbe.
|
|
|
|
// Both probes.
|
|
|
|
//
|
|
|
|
// Also, for each probe, there will be several cases covering whether the initial
|
|
|
|
// delay has passed, whether the probe handler will return Success, Failure,
|
|
|
|
// Unknown or error.
|
|
|
|
//
|
|
|
|
// PLEASE READ THE PROBE DOCS BEFORE CHANGING THIS TEST IF YOU ARE UNSURE HOW PROBES ARE SUPPOSED TO WORK:
|
|
|
|
// (See https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/user-guide/pod-states.md#pod-conditions)
|
|
|
|
func TestProbeContainer(t *testing.T) {
|
|
|
|
prober := &prober{
|
2015-08-25 17:39:41 +00:00
|
|
|
refManager: kubecontainer.NewRefManager(),
|
|
|
|
recorder: &record.FakeRecorder{},
|
2015-09-18 00:04:20 +00:00
|
|
|
}
|
2015-10-07 17:58:05 +00:00
|
|
|
containerID := kubecontainer.ContainerID{"test", "foobar"}
|
2015-09-18 00:04:20 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
testContainer api.Container
|
|
|
|
expectError bool
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness probe.Result
|
|
|
|
expectedReadiness probe.Result
|
2015-09-18 00:04:20 +00:00
|
|
|
}{
|
|
|
|
// No probes.
|
|
|
|
{
|
|
|
|
testContainer: api.Container{},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
// Only LivenessProbe. expectedReadiness should always be true here.
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
2015-10-19 22:15:59 +00:00
|
|
|
LivenessProbe: &api.Probe{},
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Unknown,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Failure,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Unknown,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Unknown,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
// // Only ReadinessProbe. expectedLiveness should always be probe.Success here.
|
2015-09-18 00:04:20 +00:00
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
2015-10-19 22:15:59 +00:00
|
|
|
ReadinessProbe: &api.Probe{},
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Unknown,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
ReadinessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
ReadinessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
ReadinessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
ReadinessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
// Both LivenessProbe and ReadinessProbe.
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
2015-10-19 22:15:59 +00:00
|
|
|
LivenessProbe: &api.Probe{},
|
|
|
|
ReadinessProbe: &api.Probe{},
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Unknown,
|
|
|
|
expectedReadiness: probe.Unknown,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
2015-10-19 22:15:59 +00:00
|
|
|
ReadinessProbe: &api.Probe{},
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Failure,
|
|
|
|
expectedReadiness: probe.Unknown,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testContainer: api.Container{
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReadinessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-25 17:39:41 +00:00
|
|
|
expectedLiveness: probe.Success,
|
|
|
|
expectedReadiness: probe.Success,
|
2015-09-18 00:04:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
if test.expectError {
|
2015-08-25 17:39:41 +00:00
|
|
|
prober.exec = fakeExecProber{test.expectedLiveness, errors.New("exec error")}
|
2015-09-18 00:04:20 +00:00
|
|
|
} else {
|
2015-08-25 17:39:41 +00:00
|
|
|
prober.exec = fakeExecProber{test.expectedLiveness, nil}
|
2015-09-18 00:04:20 +00:00
|
|
|
}
|
2015-08-25 17:39:41 +00:00
|
|
|
|
2015-10-19 22:15:59 +00:00
|
|
|
liveness, err := prober.probeLiveness(&api.Pod{}, api.PodStatus{}, test.testContainer, containerID)
|
2015-09-18 00:04:20 +00:00
|
|
|
if test.expectError && err == nil {
|
2015-08-25 17:39:41 +00:00
|
|
|
t.Errorf("[%d] Expected liveness probe error but no error was returned.", i)
|
2015-09-18 00:04:20 +00:00
|
|
|
}
|
|
|
|
if !test.expectError && err != nil {
|
2015-08-25 17:39:41 +00:00
|
|
|
t.Errorf("[%d] Didn't expect liveness probe error but got: %v", i, err)
|
2015-09-18 00:04:20 +00:00
|
|
|
}
|
2015-08-25 17:39:41 +00:00
|
|
|
if test.expectedLiveness != liveness {
|
|
|
|
t.Errorf("[%d] Expected liveness result to be %v but was %v", i, test.expectedLiveness, liveness)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Test readiness errors
|
|
|
|
prober.exec = fakeExecProber{test.expectedReadiness, nil}
|
2015-10-19 22:15:59 +00:00
|
|
|
readiness, err := prober.probeReadiness(&api.Pod{}, api.PodStatus{}, test.testContainer, containerID)
|
2015-08-25 17:39:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] Unexpected readiness probe error: %v", i, err)
|
2015-09-18 00:04:20 +00:00
|
|
|
}
|
2015-08-25 17:39:41 +00:00
|
|
|
if test.expectedReadiness != readiness {
|
|
|
|
t.Errorf("[%d] Expected readiness result to be %v but was %v", i, test.expectedReadiness, readiness)
|
2015-09-18 00:04:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeExecProber struct {
|
2015-04-28 23:05:26 +00:00
|
|
|
result probe.Result
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2015-09-18 00:04:20 +00:00
|
|
|
func (p fakeExecProber) Probe(_ exec.Cmd) (probe.Result, string, error) {
|
|
|
|
return p.result, "", p.err
|
2015-04-28 23:05:26 +00:00
|
|
|
}
|