mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
kubelet: refactor server containerLogs test to table driven test
Signed-off-by: zhuangqh <zhuangqhc@gmail.com>
This commit is contained in:
parent
84fe3db5cf
commit
057caf7fcf
@ -93,6 +93,7 @@ go_test(
|
|||||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||||
"//vendor/github.com/stretchr/testify/require:go_default_library",
|
"//vendor/github.com/stretchr/testify/require:go_default_library",
|
||||||
"//vendor/golang.org/x/net/websocket:go_default_library",
|
"//vendor/golang.org/x/net/websocket:go_default_library",
|
||||||
|
"//vendor/k8s.io/utils/pointer:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,6 +51,7 @@ import (
|
|||||||
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
|
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
|
statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
|
||||||
|
"k8s.io/utils/pointer"
|
||||||
|
|
||||||
// Do some initialization to decode the query parameters correctly.
|
// Do some initialization to decode the query parameters correctly.
|
||||||
_ "k8s.io/kubernetes/pkg/apis/core/install"
|
_ "k8s.io/kubernetes/pkg/apis/core/install"
|
||||||
@ -948,110 +949,45 @@ func setGetContainerLogsFunc(fw *serverTestFramework, t *testing.T, expectedPodN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: I really want to be a table driven test
|
|
||||||
func TestContainerLogs(t *testing.T) {
|
func TestContainerLogs(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
defer fw.testHTTPServer.Close()
|
defer fw.testHTTPServer.Close()
|
||||||
output := "foo bar"
|
|
||||||
podNamespace := "other"
|
|
||||||
podName := "foo"
|
|
||||||
expectedPodName := getPodName(podName, podNamespace)
|
|
||||||
expectedContainerName := "baz"
|
|
||||||
setPodByNameFunc(fw, podNamespace, podName, expectedContainerName)
|
|
||||||
setGetContainerLogsFunc(fw, t, expectedPodName, expectedContainerName, &v1.PodLogOptions{}, output)
|
|
||||||
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Got error GETing: %v", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
tests := map[string]struct {
|
||||||
if err != nil {
|
query string
|
||||||
t.Errorf("Error reading container logs: %v", err)
|
podLogOption *v1.PodLogOptions
|
||||||
|
}{
|
||||||
|
"without tail": {"", &v1.PodLogOptions{}},
|
||||||
|
"with tail": {"?tailLines=5", &v1.PodLogOptions{TailLines: pointer.Int64Ptr(5)}},
|
||||||
|
"with legacy tail": {"?tail=5", &v1.PodLogOptions{TailLines: pointer.Int64Ptr(5)}},
|
||||||
|
"with tail all": {"?tail=all", &v1.PodLogOptions{}},
|
||||||
|
"with follow": {"?follow=1", &v1.PodLogOptions{Follow: true}},
|
||||||
}
|
}
|
||||||
result := string(body)
|
|
||||||
if result != output {
|
|
||||||
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContainerLogsWithTail(t *testing.T) {
|
for desc, test := range tests {
|
||||||
fw := newServerTest()
|
t.Run(desc, func(t *testing.T) {
|
||||||
defer fw.testHTTPServer.Close()
|
output := "foo bar"
|
||||||
output := "foo bar"
|
podNamespace := "other"
|
||||||
podNamespace := "other"
|
podName := "foo"
|
||||||
podName := "foo"
|
expectedPodName := getPodName(podName, podNamespace)
|
||||||
expectedPodName := getPodName(podName, podNamespace)
|
expectedContainerName := "baz"
|
||||||
expectedContainerName := "baz"
|
setPodByNameFunc(fw, podNamespace, podName, expectedContainerName)
|
||||||
expectedTail := int64(5)
|
setGetContainerLogsFunc(fw, t, expectedPodName, expectedContainerName, test.podLogOption, output)
|
||||||
setPodByNameFunc(fw, podNamespace, podName, expectedContainerName)
|
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + test.query)
|
||||||
setGetContainerLogsFunc(fw, t, expectedPodName, expectedContainerName, &v1.PodLogOptions{TailLines: &expectedTail}, output)
|
if err != nil {
|
||||||
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?tailLines=5")
|
t.Errorf("Got error GETing: %v", err)
|
||||||
if err != nil {
|
}
|
||||||
t.Errorf("Got error GETing: %v", err)
|
defer resp.Body.Close()
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error reading container logs: %v", err)
|
t.Errorf("Error reading container logs: %v", err)
|
||||||
}
|
}
|
||||||
result := string(body)
|
result := string(body)
|
||||||
if result != output {
|
if result != output {
|
||||||
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestContainerLogsWithLegacyTail(t *testing.T) {
|
|
||||||
fw := newServerTest()
|
|
||||||
defer fw.testHTTPServer.Close()
|
|
||||||
output := "foo bar"
|
|
||||||
podNamespace := "other"
|
|
||||||
podName := "foo"
|
|
||||||
expectedPodName := getPodName(podName, podNamespace)
|
|
||||||
expectedContainerName := "baz"
|
|
||||||
expectedTail := int64(5)
|
|
||||||
setPodByNameFunc(fw, podNamespace, podName, expectedContainerName)
|
|
||||||
setGetContainerLogsFunc(fw, t, expectedPodName, expectedContainerName, &v1.PodLogOptions{TailLines: &expectedTail}, output)
|
|
||||||
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?tail=5")
|
|
||||||
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 != output {
|
|
||||||
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContainerLogsWithTailAll(t *testing.T) {
|
|
||||||
fw := newServerTest()
|
|
||||||
defer fw.testHTTPServer.Close()
|
|
||||||
output := "foo bar"
|
|
||||||
podNamespace := "other"
|
|
||||||
podName := "foo"
|
|
||||||
expectedPodName := getPodName(podName, podNamespace)
|
|
||||||
expectedContainerName := "baz"
|
|
||||||
setPodByNameFunc(fw, podNamespace, podName, expectedContainerName)
|
|
||||||
setGetContainerLogsFunc(fw, t, expectedPodName, expectedContainerName, &v1.PodLogOptions{}, output)
|
|
||||||
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?tail=all")
|
|
||||||
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 != output {
|
|
||||||
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1075,32 +1011,6 @@ func TestContainerLogsWithInvalidTail(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerLogsWithFollow(t *testing.T) {
|
|
||||||
fw := newServerTest()
|
|
||||||
defer fw.testHTTPServer.Close()
|
|
||||||
output := "foo bar"
|
|
||||||
podNamespace := "other"
|
|
||||||
podName := "foo"
|
|
||||||
expectedPodName := getPodName(podName, podNamespace)
|
|
||||||
expectedContainerName := "baz"
|
|
||||||
setPodByNameFunc(fw, podNamespace, podName, expectedContainerName)
|
|
||||||
setGetContainerLogsFunc(fw, t, expectedPodName, expectedContainerName, &v1.PodLogOptions{Follow: true}, output)
|
|
||||||
resp, err := http.Get(fw.testHTTPServer.URL + "/containerLogs/" + podNamespace + "/" + podName + "/" + expectedContainerName + "?follow=1")
|
|
||||||
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 != output {
|
|
||||||
t.Errorf("Expected: '%v', got: '%v'", output, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServeExecInContainerIdleTimeout(t *testing.T) {
|
func TestServeExecInContainerIdleTimeout(t *testing.T) {
|
||||||
ss, err := newTestStreamingServer(100 * time.Millisecond)
|
ss, err := newTestStreamingServer(100 * time.Millisecond)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user