2015-11-10 23:42:07 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2015-11-10 23:42:07 +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 e2e_node
|
|
|
|
|
|
|
|
import (
|
2015-11-20 23:59:31 +00:00
|
|
|
"bytes"
|
2015-11-10 23:42:07 +00:00
|
|
|
"fmt"
|
2017-05-24 21:45:07 +00:00
|
|
|
"strings"
|
2015-11-20 23:59:31 +00:00
|
|
|
"time"
|
2015-11-10 23:42:07 +00:00
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-24 14:35:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/uuid"
|
2016-04-18 20:12:19 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2016-01-14 19:19:26 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2015-11-10 23:42:07 +00:00
|
|
|
)
|
|
|
|
|
2016-04-18 20:12:19 +00:00
|
|
|
var _ = framework.KubeDescribe("Kubelet", func() {
|
2016-07-08 05:56:46 +00:00
|
|
|
f := framework.NewDefaultFramework("kubelet-test")
|
|
|
|
var podClient *framework.PodClient
|
|
|
|
BeforeEach(func() {
|
|
|
|
podClient = f.PodClient()
|
|
|
|
})
|
2016-04-18 20:12:19 +00:00
|
|
|
Context("when scheduling a busybox command in a pod", func() {
|
2016-07-26 15:13:18 +00:00
|
|
|
podName := "busybox-scheduling-" + string(uuid.NewUUID())
|
2017-10-31 03:55:46 +00:00
|
|
|
framework.ConformanceIt("it should print the output to logs", func() {
|
2016-11-18 20:55:46 +00:00
|
|
|
podClient.CreateSync(&v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-04-18 20:12:19 +00:00
|
|
|
Name: podName,
|
|
|
|
},
|
2016-11-18 20:55:46 +00:00
|
|
|
Spec: v1.PodSpec{
|
2016-04-18 20:12:19 +00:00
|
|
|
// Don't restart the Pod since it is expected to exit
|
2016-11-18 20:55:46 +00:00
|
|
|
RestartPolicy: v1.RestartPolicyNever,
|
|
|
|
Containers: []v1.Container{
|
2016-04-18 20:12:19 +00:00
|
|
|
{
|
2017-08-29 08:32:08 +00:00
|
|
|
Image: busyboxImage,
|
2016-04-18 20:12:19 +00:00
|
|
|
Name: podName,
|
|
|
|
Command: []string{"sh", "-c", "echo 'Hello World' ; sleep 240"},
|
2015-11-20 23:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
2016-04-18 20:12:19 +00:00
|
|
|
},
|
2016-06-29 00:20:08 +00:00
|
|
|
})
|
2016-04-18 20:12:19 +00:00
|
|
|
Eventually(func() string {
|
2016-12-07 14:40:26 +00:00
|
|
|
sinceTime := metav1.NewTime(time.Now().Add(time.Duration(-1 * time.Hour)))
|
2016-11-18 20:55:46 +00:00
|
|
|
rc, err := podClient.GetLogs(podName, &v1.PodLogOptions{SinceTime: &sinceTime}).Stream()
|
2016-04-18 20:12:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
2015-11-10 23:42:07 +00:00
|
|
|
}
|
2016-04-18 20:12:19 +00:00
|
|
|
defer rc.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(rc)
|
|
|
|
return buf.String()
|
|
|
|
}, time.Minute, time.Second*4).Should(Equal("Hello World\n"))
|
2015-11-10 23:42:07 +00:00
|
|
|
})
|
2016-04-18 20:12:19 +00:00
|
|
|
})
|
2016-09-16 23:32:58 +00:00
|
|
|
Context("when scheduling a busybox command that always fails in a pod", func() {
|
|
|
|
var podName string
|
2016-02-11 22:31:26 +00:00
|
|
|
|
2016-09-16 23:32:58 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
podName = "bin-false" + string(uuid.NewUUID())
|
2016-11-18 20:55:46 +00:00
|
|
|
podClient.Create(&v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-16 23:32:58 +00:00
|
|
|
Name: podName,
|
|
|
|
},
|
2016-11-18 20:55:46 +00:00
|
|
|
Spec: v1.PodSpec{
|
2016-09-16 23:32:58 +00:00
|
|
|
// Don't restart the Pod since it is expected to exit
|
2016-11-18 20:55:46 +00:00
|
|
|
RestartPolicy: v1.RestartPolicyNever,
|
|
|
|
Containers: []v1.Container{
|
2016-09-16 23:32:58 +00:00
|
|
|
{
|
2017-08-29 08:32:08 +00:00
|
|
|
Image: busyboxImage,
|
2016-09-16 23:32:58 +00:00
|
|
|
Name: podName,
|
|
|
|
Command: []string{"/bin/false"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should have an error terminated reason", func() {
|
|
|
|
Eventually(func() error {
|
2016-12-07 14:40:26 +00:00
|
|
|
podData, err := podClient.Get(podName, metav1.GetOptions{})
|
2016-09-16 23:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(podData.Status.ContainerStatuses) != 1 {
|
|
|
|
return fmt.Errorf("expected only one container in the pod %q", podName)
|
|
|
|
}
|
|
|
|
contTerminatedState := podData.Status.ContainerStatuses[0].State.Terminated
|
|
|
|
if contTerminatedState == nil {
|
|
|
|
return fmt.Errorf("expected state to be terminated. Got pod status: %+v", podData.Status)
|
|
|
|
}
|
|
|
|
if contTerminatedState.Reason != "Error" {
|
|
|
|
return fmt.Errorf("expected terminated state reason to be error. Got %+v", contTerminatedState)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, time.Minute, time.Second*4).Should(BeNil())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should be possible to delete", func() {
|
2017-01-24 15:38:21 +00:00
|
|
|
err := podClient.Delete(podName, &metav1.DeleteOptions{})
|
2016-09-16 23:32:58 +00:00
|
|
|
Expect(err).To(BeNil(), fmt.Sprintf("Error deleting Pod %v", err))
|
|
|
|
})
|
|
|
|
})
|
2017-05-24 21:45:07 +00:00
|
|
|
Context("when scheduling a busybox Pod with hostAliases", func() {
|
|
|
|
podName := "busybox-host-aliases" + string(uuid.NewUUID())
|
|
|
|
|
|
|
|
It("it should write entries to /etc/hosts", func() {
|
|
|
|
podClient.CreateSync(&v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: podName,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
// Don't restart the Pod since it is expected to exit
|
|
|
|
RestartPolicy: v1.RestartPolicyNever,
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
2017-08-29 08:32:08 +00:00
|
|
|
Image: busyboxImage,
|
2017-05-24 21:45:07 +00:00
|
|
|
Name: podName,
|
|
|
|
Command: []string{"/bin/sh", "-c", "cat /etc/hosts; sleep 6000"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HostAliases: []v1.HostAlias{
|
|
|
|
{
|
|
|
|
IP: "123.45.67.89",
|
|
|
|
Hostnames: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
Eventually(func() error {
|
|
|
|
rc, err := podClient.GetLogs(podName, &v1.PodLogOptions{}).Stream()
|
|
|
|
defer rc.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(rc)
|
|
|
|
hostsFileContent := buf.String()
|
|
|
|
|
|
|
|
if !strings.Contains(hostsFileContent, "123.45.67.89\tfoo") || !strings.Contains(hostsFileContent, "123.45.67.89\tbar") {
|
|
|
|
return fmt.Errorf("expected hosts file to contain entries from HostAliases. Got:\n%+v", hostsFileContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, time.Minute, time.Second*4).Should(BeNil())
|
|
|
|
})
|
|
|
|
})
|
2016-04-18 20:12:19 +00:00
|
|
|
Context("when scheduling a read only busybox container", func() {
|
2016-07-26 15:13:18 +00:00
|
|
|
podName := "busybox-readonly-fs" + string(uuid.NewUUID())
|
2017-10-31 03:55:46 +00:00
|
|
|
framework.ConformanceIt("it should not write to root filesystem", func() {
|
2016-04-18 20:12:19 +00:00
|
|
|
isReadOnly := true
|
2016-11-18 20:55:46 +00:00
|
|
|
podClient.CreateSync(&v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-04-18 20:12:19 +00:00
|
|
|
Name: podName,
|
|
|
|
},
|
2016-11-18 20:55:46 +00:00
|
|
|
Spec: v1.PodSpec{
|
2016-04-18 20:12:19 +00:00
|
|
|
// Don't restart the Pod since it is expected to exit
|
2016-11-18 20:55:46 +00:00
|
|
|
RestartPolicy: v1.RestartPolicyNever,
|
|
|
|
Containers: []v1.Container{
|
2016-04-18 20:12:19 +00:00
|
|
|
{
|
2017-08-29 08:32:08 +00:00
|
|
|
Image: busyboxImage,
|
2016-04-18 20:12:19 +00:00
|
|
|
Name: podName,
|
2017-01-26 17:09:13 +00:00
|
|
|
Command: []string{"/bin/sh", "-c", "echo test > /file; sleep 240"},
|
2016-11-18 20:55:46 +00:00
|
|
|
SecurityContext: &v1.SecurityContext{
|
2016-04-18 20:12:19 +00:00
|
|
|
ReadOnlyRootFilesystem: &isReadOnly,
|
2016-02-29 22:35:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-04-18 20:12:19 +00:00
|
|
|
},
|
2016-06-29 00:20:08 +00:00
|
|
|
})
|
2016-04-18 20:12:19 +00:00
|
|
|
Eventually(func() string {
|
2016-11-18 20:55:46 +00:00
|
|
|
rc, err := podClient.GetLogs(podName, &v1.PodLogOptions{}).Stream()
|
2016-04-18 20:12:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
2016-02-29 22:35:54 +00:00
|
|
|
}
|
2016-04-18 20:12:19 +00:00
|
|
|
defer rc.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(rc)
|
|
|
|
return buf.String()
|
2017-01-26 17:09:13 +00:00
|
|
|
}, time.Minute, time.Second*4).Should(Equal("/bin/sh: can't create /file: Read-only file system\n"))
|
2016-02-29 22:35:54 +00:00
|
|
|
})
|
2015-11-10 23:42:07 +00:00
|
|
|
})
|
|
|
|
})
|