diff --git a/test/e2e/node/kubelet.go b/test/e2e/node/kubelet.go index 15304db4bc9..bd17a7a4a07 100644 --- a/test/e2e/node/kubelet.go +++ b/test/e2e/node/kubelet.go @@ -19,6 +19,7 @@ package node import ( "context" "fmt" + "os/exec" "path/filepath" "strings" "time" @@ -30,6 +31,7 @@ import ( "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" "k8s.io/kubernetes/test/e2e/framework" + e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl" e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet" e2enode "k8s.io/kubernetes/test/e2e/framework/node" e2epod "k8s.io/kubernetes/test/e2e/framework/pod" @@ -455,4 +457,82 @@ var _ = SIGDescribe("kubelet", func() { } }) }) + + //Test kubectl alpha node-logs commands + ginkgo.Describe("kubectl node-logs [Feature:add node log viewer]", func() { + var ( + numNodes int + nodeNames sets.String + ) + + ginkgo.BeforeEach(func() { + nodes, err := e2enode.GetBoundedReadySchedulableNodes(c, maxNodesToCheck) + numNodes = len(nodes.Items) + framework.ExpectNoError(err) + nodeNames = sets.NewString() + for i := 0; i < numNodes; i++ { + nodeNames.Insert(nodes.Items[i].Name) + } + }) + + /* + Test if kubectl node-logs + returns something or not! + */ + + ginkgo.It("should return the logs ", func() { + ginkgo.By("Starting the command") + tk := e2ekubectl.NewTestKubeconfig(framework.TestContext.CertDir, framework.TestContext.Host, framework.TestContext.KubeConfig, framework.TestContext.KubeContext, framework.TestContext.KubectlPath, ns) + + for nodeName := range nodeNames { + cmd := tk.KubectlCmd("alpha", "node-logs", nodeName) + runKubectlCommand(cmd, "") + } + }) + + /* + Test if kubectl node-logs --service kubelet + returns something or not! + */ + + ginkgo.It("should return the logs for the requested service", func() { + ginkgo.By("Starting the command") + tk := e2ekubectl.NewTestKubeconfig(framework.TestContext.CertDir, framework.TestContext.Host, framework.TestContext.KubeConfig, framework.TestContext.KubeContext, framework.TestContext.KubectlPath, ns) + + for nodeName := range nodeNames { + cmd := tk.KubectlCmd("alpha", "node-logs", nodeName, "--service", "kubelet") + runKubectlCommand(cmd, "--service") + } + }) + + /* + Test if kubectl node-logs --path pods + returns something or not! + */ + + ginkgo.It("should return the logs for the provided path", func() { + ginkgo.By("Starting the command") + tk := e2ekubectl.NewTestKubeconfig(framework.TestContext.CertDir, framework.TestContext.Host, framework.TestContext.KubeConfig, framework.TestContext.KubeContext, framework.TestContext.KubectlPath, ns) + + for nodeName := range nodeNames { + cmd := tk.KubectlCmd("alpha", "node-logs", nodeName, "--path", "pods") + runKubectlCommand(cmd, "--path") + } + }) + }) }) + +func runKubectlCommand(cmd *exec.Cmd, arg string) { + stdout, stderr, err := framework.StartCmdAndStreamOutput(cmd) + if err != nil { + framework.Failf("Failed to start kubectl command: %v", err) + } + defer stdout.Close() + defer stderr.Close() + defer framework.TryKill(cmd) + buf := make([]byte, 128) + if _, err = stdout.Read(buf); err != nil { + framework.Failf("Expected output from kubectl alpha node-logs %s: %v", arg, err) + } + framework.Logf("output: %s", buf) +}