2014-07-03 05:35:50 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 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 kubelet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
)
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
type fakeHTTPClient struct {
|
2014-07-03 05:35:50 +00:00
|
|
|
req string
|
|
|
|
res http.Response
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
func (f *fakeHTTPClient) Get(url string) (*http.Response, error) {
|
2014-07-03 05:35:50 +00:00
|
|
|
f.req = url
|
|
|
|
return &f.res, f.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpHealth(t *testing.T) {
|
2014-07-10 12:26:24 +00:00
|
|
|
fakeClient := fakeHTTPClient{
|
2014-07-03 05:35:50 +00:00
|
|
|
res: http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
check := HTTPHealthChecker{
|
|
|
|
client: &fakeClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
container := api.Container{
|
2014-07-09 23:53:31 +00:00
|
|
|
LivenessProbe: &api.LivenessProbe{
|
|
|
|
HTTPGet: &api.HTTPGetProbe{
|
2014-07-03 05:35:50 +00:00
|
|
|
Port: "8080",
|
|
|
|
Path: "/foo/bar",
|
|
|
|
},
|
|
|
|
Type: "http",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:02:59 +00:00
|
|
|
ok, err := check.HealthCheck(container)
|
|
|
|
if ok != CheckHealthy {
|
2014-07-03 05:35:50 +00:00
|
|
|
t.Error("Unexpected unhealthy")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
if fakeClient.req != "http://localhost:8080/foo/bar" {
|
|
|
|
t.Errorf("Unexpected url: %s", fakeClient.req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindPort(t *testing.T) {
|
|
|
|
container := api.Container{
|
|
|
|
Ports: []api.Port{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
HostPort: 8080,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
HostPort: 9000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
check := HTTPHealthChecker{}
|
|
|
|
validatePort(t, check.findPort(container, "foo"), 8080)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePort(t *testing.T, port int64, expectedPort int64) {
|
|
|
|
if port != expectedPort {
|
|
|
|
t.Errorf("Unexpected port: %d, expected: %d", port, expectedPort)
|
|
|
|
}
|
|
|
|
}
|