2014-06-23 18:32:11 +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.
|
|
|
|
*/
|
|
|
|
|
2014-06-08 23:10:29 +00:00
|
|
|
package kubelet
|
|
|
|
|
|
|
|
import (
|
2014-06-19 20:43:35 +00:00
|
|
|
"encoding/json"
|
2014-06-08 23:10:29 +00:00
|
|
|
"fmt"
|
2014-09-24 21:27:10 +00:00
|
|
|
"io"
|
2014-06-08 23:10:29 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-07-15 07:04:30 +00:00
|
|
|
"net/http/httputil"
|
2014-06-08 23:10:29 +00:00
|
|
|
"reflect"
|
2014-07-15 07:04:30 +00:00
|
|
|
"strings"
|
2014-06-08 23:10:29 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-14 23:22:21 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
2014-07-14 21:48:51 +00:00
|
|
|
"github.com/google/cadvisor/info"
|
2014-06-08 23:10:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeKubelet struct {
|
2015-01-07 15:18:56 +00:00
|
|
|
podByNameFunc func(namespace, name string) (*api.BoundPod, bool)
|
2015-01-14 02:11:24 +00:00
|
|
|
statusFunc func(name string) (api.PodStatus, error)
|
2015-01-14 23:22:21 +00:00
|
|
|
containerInfoFunc func(podFullName string, uid types.UID, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error)
|
2014-09-24 21:27:10 +00:00
|
|
|
rootInfoFunc func(query *info.ContainerInfoRequest) (*info.ContainerInfo, error)
|
|
|
|
machineInfoFunc func() (*info.MachineInfo, error)
|
2014-10-22 23:52:38 +00:00
|
|
|
boundPodsFunc func() ([]api.BoundPod, error)
|
2014-09-24 21:27:10 +00:00
|
|
|
logFunc func(w http.ResponseWriter, req *http.Request)
|
2015-01-14 23:22:21 +00:00
|
|
|
runFunc func(podFullName string, uid types.UID, containerName string, cmd []string) ([]byte, error)
|
2014-09-24 21:27:10 +00:00
|
|
|
containerLogsFunc func(podFullName, containerName, tail string, follow bool, stdout, stderr io.Writer) error
|
2014-06-08 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 15:18:56 +00:00
|
|
|
func (fk *fakeKubelet) GetPodByName(namespace, name string) (*api.BoundPod, bool) {
|
|
|
|
return fk.podByNameFunc(namespace, name)
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:11:24 +00:00
|
|
|
func (fk *fakeKubelet) GetPodStatus(name string, uid types.UID) (api.PodStatus, error) {
|
|
|
|
return fk.statusFunc(name)
|
2014-06-08 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 23:22:21 +00:00
|
|
|
func (fk *fakeKubelet) GetContainerInfo(podFullName string, uid types.UID, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
|
2015-01-14 21:53:43 +00:00
|
|
|
return fk.containerInfoFunc(podFullName, uid, containerName, req)
|
2014-07-01 21:05:10 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 22:40:02 +00:00
|
|
|
func (fk *fakeKubelet) GetRootInfo(req *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
|
|
|
|
return fk.rootInfoFunc(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fk *fakeKubelet) GetMachineInfo() (*info.MachineInfo, error) {
|
|
|
|
return fk.machineInfoFunc()
|
2014-06-19 20:22:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 23:52:38 +00:00
|
|
|
func (fk *fakeKubelet) GetBoundPods() ([]api.BoundPod, error) {
|
|
|
|
return fk.boundPodsFunc()
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:04:30 +00:00
|
|
|
func (fk *fakeKubelet) ServeLogs(w http.ResponseWriter, req *http.Request) {
|
|
|
|
fk.logFunc(w, req)
|
|
|
|
}
|
|
|
|
|
2014-09-22 20:14:23 +00:00
|
|
|
func (fk *fakeKubelet) GetKubeletContainerLogs(podFullName, containerName, tail string, follow bool, stdout, stderr io.Writer) error {
|
|
|
|
return fk.containerLogsFunc(podFullName, containerName, tail, follow, stdout, stderr)
|
2014-09-15 16:20:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 23:22:21 +00:00
|
|
|
func (fk *fakeKubelet) RunInContainer(podFullName string, uid types.UID, containerName string, cmd []string) ([]byte, error) {
|
2015-01-14 21:53:43 +00:00
|
|
|
return fk.runFunc(podFullName, uid, containerName, cmd)
|
2014-08-26 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 23:10:29 +00:00
|
|
|
type serverTestFramework struct {
|
2014-07-15 20:24:41 +00:00
|
|
|
updateChan chan interface{}
|
2014-06-20 16:31:18 +00:00
|
|
|
updateReader *channelReader
|
2014-07-16 12:26:12 +00:00
|
|
|
serverUnderTest *Server
|
2014-06-08 23:10:29 +00:00
|
|
|
fakeKubelet *fakeKubelet
|
2014-07-10 12:26:24 +00:00
|
|
|
testHTTPServer *httptest.Server
|
2014-06-08 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 04:27:19 +00:00
|
|
|
func newServerTest() *serverTestFramework {
|
2014-06-08 23:10:29 +00:00
|
|
|
fw := &serverTestFramework{
|
2014-07-15 20:24:41 +00:00
|
|
|
updateChan: make(chan interface{}),
|
2014-06-08 23:10:29 +00:00
|
|
|
}
|
2014-06-20 16:31:18 +00:00
|
|
|
fw.updateReader = startReading(fw.updateChan)
|
2015-01-07 15:18:56 +00:00
|
|
|
fw.fakeKubelet = &fakeKubelet{
|
|
|
|
podByNameFunc: func(namespace, name string) (*api.BoundPod, bool) {
|
|
|
|
return &api.BoundPod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: name,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ConfigSourceAnnotationKey: "etcd",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, true
|
|
|
|
},
|
|
|
|
}
|
2014-12-18 01:03:59 +00:00
|
|
|
server := NewServer(fw.fakeKubelet, true)
|
2014-08-20 18:24:51 +00:00
|
|
|
fw.serverUnderTest = &server
|
2014-07-10 12:26:24 +00:00
|
|
|
fw.testHTTPServer = httptest.NewServer(fw.serverUnderTest)
|
2014-06-08 23:10:29 +00:00
|
|
|
return fw
|
|
|
|
}
|
|
|
|
|
2014-11-13 15:38:13 +00:00
|
|
|
// encodeJSON returns obj marshalled as a JSON string, panicing on any errors
|
|
|
|
func encodeJSON(obj interface{}) string {
|
|
|
|
data, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
2014-06-08 23:10:29 +00:00
|
|
|
func readResp(resp *http.Response) (string, error) {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
return string(body), err
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:11:24 +00:00
|
|
|
func TestPodStatus(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fw := newServerTest()
|
2015-01-14 02:11:24 +00:00
|
|
|
expected := api.PodStatus{
|
|
|
|
Info: map[string]api.ContainerStatus{
|
|
|
|
"goodpod": {},
|
|
|
|
},
|
2014-09-16 20:21:04 +00:00
|
|
|
}
|
2015-01-14 02:11:24 +00:00
|
|
|
fw.fakeKubelet.statusFunc = func(name string) (api.PodStatus, error) {
|
2014-10-08 19:56:02 +00:00
|
|
|
if name == "goodpod.default.etcd" {
|
2014-06-08 23:10:29 +00:00
|
|
|
return expected, nil
|
|
|
|
}
|
2015-01-14 02:11:24 +00:00
|
|
|
return api.PodStatus{}, fmt.Errorf("bad pod %s", name)
|
2014-06-08 23:10:29 +00:00
|
|
|
}
|
2014-10-17 16:09:01 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/podInfo?podID=goodpod&podNamespace=default")
|
2014-06-08 23:10:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
got, err := readResp(resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading body: %v", err)
|
|
|
|
}
|
2014-07-01 02:46:10 +00:00
|
|
|
expectedBytes, err := json.Marshal(expected)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected marshal error %v", err)
|
|
|
|
}
|
|
|
|
if got != string(expectedBytes) {
|
2014-06-08 23:10:29 +00:00
|
|
|
t.Errorf("Expected: '%v', got: '%v'", expected, got)
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 20:43:35 +00:00
|
|
|
|
2014-07-15 22:40:02 +00:00
|
|
|
func TestContainerInfo(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fw := newServerTest()
|
2014-09-22 18:22:40 +00:00
|
|
|
expectedInfo := &info.ContainerInfo{}
|
2014-10-08 00:21:24 +00:00
|
|
|
podID := "somepod"
|
2014-10-08 19:56:02 +00:00
|
|
|
expectedPodID := "somepod" + ".default.etcd"
|
2014-06-19 20:43:35 +00:00
|
|
|
expectedContainerName := "goodcontainer"
|
2015-01-14 23:22:21 +00:00
|
|
|
fw.fakeKubelet.containerInfoFunc = func(podID string, uid types.UID, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
|
2014-07-01 21:05:10 +00:00
|
|
|
if podID != expectedPodID || containerName != expectedContainerName {
|
|
|
|
return nil, fmt.Errorf("bad podID or containerName: podID=%v; containerName=%v", podID, containerName)
|
2014-06-19 20:43:35 +00:00
|
|
|
}
|
2014-07-15 22:40:02 +00:00
|
|
|
return expectedInfo, nil
|
2014-06-19 20:43:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 00:21:24 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + fmt.Sprintf("/stats/%v/%v", podID, expectedContainerName))
|
2014-07-01 21:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2014-07-15 22:40:02 +00:00
|
|
|
var receivedInfo info.ContainerInfo
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&receivedInfo)
|
2014-07-01 21:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("received invalid json data: %v", err)
|
|
|
|
}
|
2014-07-15 22:40:02 +00:00
|
|
|
if !reflect.DeepEqual(&receivedInfo, expectedInfo) {
|
|
|
|
t.Errorf("received wrong data: %#v", receivedInfo)
|
2014-07-01 21:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:17:25 +00:00
|
|
|
func TestContainerInfoWithUidNamespace(t *testing.T) {
|
|
|
|
fw := newServerTest()
|
|
|
|
expectedInfo := &info.ContainerInfo{}
|
|
|
|
podID := "somepod"
|
|
|
|
expectedNamespace := "custom"
|
|
|
|
expectedPodID := "somepod" + "." + expectedNamespace + ".etcd"
|
|
|
|
expectedContainerName := "goodcontainer"
|
|
|
|
expectedUid := "9b01b80f-8fb4-11e4-95ab-4200af06647"
|
2015-01-14 23:22:21 +00:00
|
|
|
fw.fakeKubelet.containerInfoFunc = func(podID string, uid types.UID, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
|
2015-01-14 21:53:43 +00:00
|
|
|
if podID != expectedPodID || string(uid) != expectedUid || containerName != expectedContainerName {
|
2015-01-03 02:17:25 +00:00
|
|
|
return nil, fmt.Errorf("bad podID or uid or containerName: podID=%v; uid=%v; containerName=%v", podID, uid, containerName)
|
|
|
|
}
|
|
|
|
return expectedInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + fmt.Sprintf("/stats/%v/%v/%v/%v", expectedNamespace, podID, expectedUid, expectedContainerName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var receivedInfo info.ContainerInfo
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&receivedInfo)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("received invalid json data: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&receivedInfo, expectedInfo) {
|
|
|
|
t.Errorf("received wrong data: %#v", receivedInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 22:40:02 +00:00
|
|
|
func TestRootInfo(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fw := newServerTest()
|
2014-09-22 18:22:40 +00:00
|
|
|
expectedInfo := &info.ContainerInfo{}
|
2014-07-15 22:40:02 +00:00
|
|
|
fw.fakeKubelet.rootInfoFunc = func(req *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
|
|
|
|
return expectedInfo, nil
|
2014-07-01 21:05:10 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/stats")
|
2014-06-19 20:43:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2014-07-15 22:40:02 +00:00
|
|
|
var receivedInfo info.ContainerInfo
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&receivedInfo)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("received invalid json data: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&receivedInfo, expectedInfo) {
|
|
|
|
t.Errorf("received wrong data: %#v", receivedInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMachineInfo(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fw := newServerTest()
|
2014-07-15 22:40:02 +00:00
|
|
|
expectedInfo := &info.MachineInfo{
|
|
|
|
NumCores: 4,
|
|
|
|
MemoryCapacity: 1024,
|
|
|
|
}
|
|
|
|
fw.fakeKubelet.machineInfoFunc = func() (*info.MachineInfo, error) {
|
|
|
|
return expectedInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/spec")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var receivedInfo info.MachineInfo
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&receivedInfo)
|
2014-06-19 20:43:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("received invalid json data: %v", err)
|
|
|
|
}
|
2014-07-15 22:40:02 +00:00
|
|
|
if !reflect.DeepEqual(&receivedInfo, expectedInfo) {
|
|
|
|
t.Errorf("received wrong data: %#v", receivedInfo)
|
2014-06-19 20:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 07:04:30 +00:00
|
|
|
|
|
|
|
func TestServeLogs(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fw := newServerTest()
|
2014-07-15 07:04:30 +00:00
|
|
|
|
|
|
|
content := string(`<pre><a href="kubelet.log">kubelet.log</a><a href="google.log">google.log</a></pre>`)
|
|
|
|
|
|
|
|
fw.fakeKubelet.logFunc = func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
w.Write([]byte(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/logs/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := httputil.DumpResponse(resp, true)
|
|
|
|
if err != nil {
|
|
|
|
// copying the response body did not work
|
|
|
|
t.Errorf("Cannot copy resp: %#v", err)
|
|
|
|
}
|
|
|
|
result := string(body)
|
|
|
|
if !strings.Contains(result, "kubelet.log") || !strings.Contains(result, "google.log") {
|
|
|
|
t.Errorf("Received wrong data: %s", result)
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 19:57:44 +00:00
|
|
|
|
|
|
|
func TestServeRunInContainer(t *testing.T) {
|
|
|
|
fw := newServerTest()
|
|
|
|
output := "foo bar"
|
2014-10-17 20:12:26 +00:00
|
|
|
podNamespace := "other"
|
2014-08-26 19:57:44 +00:00
|
|
|
podName := "foo"
|
2014-10-17 20:12:26 +00:00
|
|
|
expectedPodName := podName + "." + podNamespace + ".etcd"
|
2014-08-26 19:57:44 +00:00
|
|
|
expectedContainerName := "baz"
|
|
|
|
expectedCommand := "ls -a"
|
2015-01-14 23:22:21 +00:00
|
|
|
fw.fakeKubelet.runFunc = func(podFullName string, uid types.UID, containerName string, cmd []string) ([]byte, error) {
|
2014-08-26 19:57:44 +00:00
|
|
|
if podFullName != expectedPodName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedPodName, podFullName)
|
|
|
|
}
|
|
|
|
if containerName != expectedContainerName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedContainerName, containerName)
|
|
|
|
}
|
|
|
|
if strings.Join(cmd, " ") != expectedCommand {
|
|
|
|
t.Errorf("expected: %s, got %v", expectedCommand, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(output), nil
|
|
|
|
}
|
|
|
|
|
2014-10-17 20:12:26 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/run/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?cmd=ls%20-a")
|
2014-08-26 19:57:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
// copying the response body did not work
|
|
|
|
t.Errorf("Cannot copy resp: %#v", err)
|
|
|
|
}
|
|
|
|
result := string(body)
|
|
|
|
if result != output {
|
|
|
|
t.Errorf("expected %s, got %s", output, result)
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 09:49:11 +00:00
|
|
|
|
2015-01-14 21:53:43 +00:00
|
|
|
func TestServeRunInContainerWithUID(t *testing.T) {
|
2014-09-05 09:49:11 +00:00
|
|
|
fw := newServerTest()
|
|
|
|
output := "foo bar"
|
2014-10-17 20:12:26 +00:00
|
|
|
podNamespace := "other"
|
2014-09-05 09:49:11 +00:00
|
|
|
podName := "foo"
|
2014-10-17 20:12:26 +00:00
|
|
|
expectedPodName := podName + "." + podNamespace + ".etcd"
|
2015-01-14 21:53:43 +00:00
|
|
|
expectedUID := "7e00838d_-_3523_-_11e4_-_8421_-_42010af0a720"
|
2014-09-05 09:49:11 +00:00
|
|
|
expectedContainerName := "baz"
|
|
|
|
expectedCommand := "ls -a"
|
2015-01-14 23:22:21 +00:00
|
|
|
fw.fakeKubelet.runFunc = func(podFullName string, uid types.UID, containerName string, cmd []string) ([]byte, error) {
|
2014-09-05 09:49:11 +00:00
|
|
|
if podFullName != expectedPodName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedPodName, podFullName)
|
|
|
|
}
|
2015-01-14 21:53:43 +00:00
|
|
|
if string(uid) != expectedUID {
|
|
|
|
t.Errorf("expected %s, got %s", expectedUID, uid)
|
2014-09-05 09:49:11 +00:00
|
|
|
}
|
|
|
|
if containerName != expectedContainerName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedContainerName, containerName)
|
|
|
|
}
|
|
|
|
if strings.Join(cmd, " ") != expectedCommand {
|
|
|
|
t.Errorf("expected: %s, got %v", expectedCommand, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(output), nil
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:53:43 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/run/" + podNamespace + "/" + podName + "/" + expectedUID + "/" + expectedContainerName + "?cmd=ls%20-a")
|
2014-09-05 09:49:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
// copying the response body did not work
|
|
|
|
t.Errorf("Cannot copy resp: %#v", err)
|
|
|
|
}
|
|
|
|
result := string(body)
|
|
|
|
if result != output {
|
|
|
|
t.Errorf("expected %s, got %s", output, result)
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 12:08:20 +00:00
|
|
|
|
|
|
|
func TestContainerLogs(t *testing.T) {
|
2014-09-24 21:27:10 +00:00
|
|
|
fw := newServerTest()
|
|
|
|
output := "foo bar"
|
2014-10-17 19:51:57 +00:00
|
|
|
podNamespace := "other"
|
2014-09-24 21:27:10 +00:00
|
|
|
podName := "foo"
|
2014-10-17 19:51:57 +00:00
|
|
|
expectedPodName := podName + ".other.etcd"
|
2014-09-24 21:27:10 +00:00
|
|
|
expectedContainerName := "baz"
|
|
|
|
expectedTail := ""
|
|
|
|
expectedFollow := false
|
|
|
|
fw.fakeKubelet.containerLogsFunc = func(podFullName, containerName, tail string, follow bool, stdout, stderr io.Writer) error {
|
|
|
|
if podFullName != expectedPodName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedPodName, podFullName)
|
|
|
|
}
|
|
|
|
if containerName != expectedContainerName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedContainerName, containerName)
|
|
|
|
}
|
|
|
|
if tail != expectedTail {
|
|
|
|
t.Errorf("expected %s, got %s", expectedTail, tail)
|
|
|
|
}
|
|
|
|
if follow != expectedFollow {
|
|
|
|
t.Errorf("expected %t, got %t", expectedFollow, follow)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-17 19:51:57 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName)
|
2014-09-24 21:27:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading container logs: %v", err)
|
|
|
|
}
|
|
|
|
result := string(body)
|
|
|
|
if result != string(body) {
|
|
|
|
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
|
|
}
|
2014-09-18 12:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerLogsWithTail(t *testing.T) {
|
2014-09-24 21:27:10 +00:00
|
|
|
fw := newServerTest()
|
|
|
|
output := "foo bar"
|
2014-10-17 19:51:57 +00:00
|
|
|
podNamespace := "other"
|
2014-09-24 21:27:10 +00:00
|
|
|
podName := "foo"
|
2014-10-17 19:51:57 +00:00
|
|
|
expectedPodName := podName + ".other.etcd"
|
2014-09-24 21:27:10 +00:00
|
|
|
expectedContainerName := "baz"
|
|
|
|
expectedTail := "5"
|
|
|
|
expectedFollow := false
|
|
|
|
fw.fakeKubelet.containerLogsFunc = func(podFullName, containerName, tail string, follow bool, stdout, stderr io.Writer) error {
|
|
|
|
if podFullName != expectedPodName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedPodName, podFullName)
|
|
|
|
}
|
|
|
|
if containerName != expectedContainerName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedContainerName, containerName)
|
|
|
|
}
|
|
|
|
if tail != expectedTail {
|
|
|
|
t.Errorf("expected %s, got %s", expectedTail, tail)
|
|
|
|
}
|
|
|
|
if follow != expectedFollow {
|
|
|
|
t.Errorf("expected %t, got %t", expectedFollow, follow)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-17 19:51:57 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?tail=5")
|
2014-09-24 21:27:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading container logs: %v", err)
|
|
|
|
}
|
|
|
|
result := string(body)
|
|
|
|
if result != string(body) {
|
|
|
|
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
|
|
}
|
2014-09-18 12:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerLogsWithFollow(t *testing.T) {
|
2014-09-24 21:27:10 +00:00
|
|
|
fw := newServerTest()
|
|
|
|
output := "foo bar"
|
2014-10-17 19:51:57 +00:00
|
|
|
podNamespace := "other"
|
2014-09-24 21:27:10 +00:00
|
|
|
podName := "foo"
|
2014-10-17 19:51:57 +00:00
|
|
|
expectedPodName := podName + ".other.etcd"
|
2014-09-24 21:27:10 +00:00
|
|
|
expectedContainerName := "baz"
|
|
|
|
expectedTail := ""
|
|
|
|
expectedFollow := true
|
|
|
|
fw.fakeKubelet.containerLogsFunc = func(podFullName, containerName, tail string, follow bool, stdout, stderr io.Writer) error {
|
|
|
|
if podFullName != expectedPodName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedPodName, podFullName)
|
|
|
|
}
|
|
|
|
if containerName != expectedContainerName {
|
|
|
|
t.Errorf("expected %s, got %s", expectedContainerName, containerName)
|
|
|
|
}
|
|
|
|
if tail != expectedTail {
|
|
|
|
t.Errorf("expected %s, got %s", expectedTail, tail)
|
|
|
|
}
|
|
|
|
if follow != expectedFollow {
|
|
|
|
t.Errorf("expected %t, got %t", expectedFollow, follow)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-17 19:51:57 +00:00
|
|
|
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?follow=1")
|
2014-09-24 21:27:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error GETing: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading container logs: %v", err)
|
|
|
|
}
|
|
|
|
result := string(body)
|
|
|
|
if result != string(body) {
|
|
|
|
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
|
|
}
|
2014-09-18 12:08:20 +00:00
|
|
|
}
|