k3s/test/e2e_node/mirror_pod_test.go

191 lines
6.3 KiB
Go
Raw Normal View History

2016-04-18 05:00:59 +00:00
/*
Copyright 2016 The Kubernetes Authors.
2016-04-18 05:00:59 +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 (
2016-07-14 21:47:14 +00:00
goerrors "errors"
2016-04-18 05:00:59 +00:00
"fmt"
"os"
"path/filepath"
"time"
2017-01-13 17:48:50 +00:00
"k8s.io/apimachinery/pkg/api/errors"
2017-01-11 14:09:48 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"
2016-11-18 20:55:46 +00:00
"k8s.io/kubernetes/pkg/api/v1"
2017-01-10 08:49:34 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
"k8s.io/kubernetes/test/e2e/framework"
2016-04-18 05:00:59 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = framework.KubeDescribe("MirrorPod", func() {
f := framework.NewDefaultFramework("mirror-pod")
2016-04-18 05:00:59 +00:00
Context("when create a mirror pod ", func() {
var ns, manifestPath, staticPodName, mirrorPodName string
2016-04-18 05:00:59 +00:00
BeforeEach(func() {
ns = f.Namespace.Name
2016-07-26 15:13:18 +00:00
staticPodName = "static-pod-" + string(uuid.NewUUID())
mirrorPodName = staticPodName + "-" + framework.TestContext.NodeName
2016-04-18 05:00:59 +00:00
manifestPath = framework.TestContext.KubeletConfig.PodManifestPath
2016-04-18 05:00:59 +00:00
By("create the static pod")
err := createStaticPod(manifestPath, staticPodName, ns,
2016-11-18 20:55:46 +00:00
"gcr.io/google_containers/nginx-slim:0.7", v1.RestartPolicyAlways)
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
By("wait for the mirror pod to be running")
Eventually(func() error {
2016-10-18 13:00:38 +00:00
return checkMirrorPodRunning(f.ClientSet, mirrorPodName, ns)
2016-04-18 05:00:59 +00:00
}, 2*time.Minute, time.Second*4).Should(BeNil())
})
2016-09-20 23:33:35 +00:00
It("should be updated when static pod updated [Conformance]", func() {
2016-04-18 05:00:59 +00:00
By("get mirror pod uid")
2016-12-07 14:40:26 +00:00
pod, err := f.ClientSet.Core().Pods(ns).Get(mirrorPodName, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
uid := pod.UID
By("update the static pod container image")
image := framework.GetPauseImageNameForHostArch()
2016-11-18 20:55:46 +00:00
err = createStaticPod(manifestPath, staticPodName, ns, image, v1.RestartPolicyAlways)
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
By("wait for the mirror pod to be updated")
Eventually(func() error {
2016-10-18 13:00:38 +00:00
return checkMirrorPodRecreatedAndRunnig(f.ClientSet, mirrorPodName, ns, uid)
2016-04-18 05:00:59 +00:00
}, 2*time.Minute, time.Second*4).Should(BeNil())
By("check the mirror pod container image is updated")
2016-12-07 14:40:26 +00:00
pod, err = f.ClientSet.Core().Pods(ns).Get(mirrorPodName, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
Expect(len(pod.Spec.Containers)).Should(Equal(1))
Expect(pod.Spec.Containers[0].Image).Should(Equal(image))
})
2016-09-20 23:33:35 +00:00
It("should be recreated when mirror pod gracefully deleted [Conformance]", func() {
2016-04-18 05:00:59 +00:00
By("get mirror pod uid")
2016-12-07 14:40:26 +00:00
pod, err := f.ClientSet.Core().Pods(ns).Get(mirrorPodName, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
uid := pod.UID
By("delete the mirror pod with grace period 30s")
err = f.ClientSet.Core().Pods(ns).Delete(mirrorPodName, metav1.NewDeleteOptions(30))
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
By("wait for the mirror pod to be recreated")
Eventually(func() error {
2016-10-18 13:00:38 +00:00
return checkMirrorPodRecreatedAndRunnig(f.ClientSet, mirrorPodName, ns, uid)
2016-04-18 05:00:59 +00:00
}, 2*time.Minute, time.Second*4).Should(BeNil())
})
2016-09-20 23:33:35 +00:00
It("should be recreated when mirror pod forcibly deleted [Conformance]", func() {
2016-04-18 05:00:59 +00:00
By("get mirror pod uid")
2016-12-07 14:40:26 +00:00
pod, err := f.ClientSet.Core().Pods(ns).Get(mirrorPodName, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
uid := pod.UID
By("delete the mirror pod with grace period 0s")
err = f.ClientSet.Core().Pods(ns).Delete(mirrorPodName, metav1.NewDeleteOptions(0))
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
By("wait for the mirror pod to be recreated")
Eventually(func() error {
2016-10-18 13:00:38 +00:00
return checkMirrorPodRecreatedAndRunnig(f.ClientSet, mirrorPodName, ns, uid)
2016-04-18 05:00:59 +00:00
}, 2*time.Minute, time.Second*4).Should(BeNil())
})
AfterEach(func() {
By("delete the static pod")
err := deleteStaticPod(manifestPath, staticPodName, ns)
2016-04-18 05:00:59 +00:00
Expect(err).ShouldNot(HaveOccurred())
By("wait for the mirror pod to disappear")
Eventually(func() error {
2016-10-18 13:00:38 +00:00
return checkMirrorPodDisappear(f.ClientSet, mirrorPodName, ns)
2016-04-18 05:00:59 +00:00
}, 2*time.Minute, time.Second*4).Should(BeNil())
})
})
})
func staticPodPath(dir, name, namespace string) string {
return filepath.Join(dir, namespace+"-"+name+".yaml")
}
2016-11-18 20:55:46 +00:00
func createStaticPod(dir, name, namespace, image string, restart v1.RestartPolicy) error {
2016-04-18 05:00:59 +00:00
template := `
apiVersion: v1
kind: Pod
metadata:
name: %s
namespace: %s
spec:
containers:
- name: test
image: %s
restartPolicy: %s
`
file := staticPodPath(dir, name, namespace)
podYaml := fmt.Sprintf(template, name, namespace, image, string(restart))
f, err := os.OpenFile(file, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(podYaml)
return err
}
func deleteStaticPod(dir, name, namespace string) error {
file := staticPodPath(dir, name, namespace)
return os.Remove(file)
}
2016-10-18 13:00:38 +00:00
func checkMirrorPodDisappear(cl clientset.Interface, name, namespace string) error {
2016-12-07 14:40:26 +00:00
_, err := cl.Core().Pods(namespace).Get(name, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
if errors.IsNotFound(err) {
return nil
}
2016-07-14 21:47:14 +00:00
return goerrors.New("pod not disappear")
2016-04-18 05:00:59 +00:00
}
2016-10-18 13:00:38 +00:00
func checkMirrorPodRunning(cl clientset.Interface, name, namespace string) error {
2016-12-07 14:40:26 +00:00
pod, err := cl.Core().Pods(namespace).Get(name, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
if err != nil {
return fmt.Errorf("expected the mirror pod %q to appear: %v", name, err)
}
2016-11-18 20:55:46 +00:00
if pod.Status.Phase != v1.PodRunning {
2016-04-18 05:00:59 +00:00
return fmt.Errorf("expected the mirror pod %q to be running, got %q", name, pod.Status.Phase)
}
return nil
}
2016-10-18 13:00:38 +00:00
func checkMirrorPodRecreatedAndRunnig(cl clientset.Interface, name, namespace string, oUID types.UID) error {
2016-12-07 14:40:26 +00:00
pod, err := cl.Core().Pods(namespace).Get(name, metav1.GetOptions{})
2016-04-18 05:00:59 +00:00
if err != nil {
return fmt.Errorf("expected the mirror pod %q to appear: %v", name, err)
}
if pod.UID == oUID {
return fmt.Errorf("expected the uid of mirror pod %q to be changed, got %q", name, pod.UID)
}
2016-11-18 20:55:46 +00:00
if pod.Status.Phase != v1.PodRunning {
2016-04-18 05:00:59 +00:00
return fmt.Errorf("expected the mirror pod %q to be running, got %q", name, pod.Status.Phase)
}
return nil
}