From 8a334ba0ee96cd39a3b58bcd0ad7e7c17bb9b138 Mon Sep 17 00:00:00 2001 From: heartlock <21521209@zju.edu.cn> Date: Fri, 19 May 2017 17:10:42 +0800 Subject: [PATCH 1/2] switch to critest Signed-off-by: heartlock <21521209@zju.edu.cn> --- hack/test-e2e.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hack/test-e2e.sh b/hack/test-e2e.sh index f973f73b..cc26cecf 100755 --- a/hack/test-e2e.sh +++ b/hack/test-e2e.sh @@ -38,7 +38,7 @@ function start_frakti() { --listen="${FRAKTI_LISTEN_ADDR}" \ --hyper-endpoint="127.0.0.1:${HYPERD_PORT}" \ --logtostderr \ - --v=5 1>&2 & \ + --v=3 1>&2 & \ FRAKTI_PID=$! } @@ -129,7 +129,7 @@ function test_cri() { go get github.com/kubernetes-incubator/cri-tools/cmd/critest # run critest - sudo env PATH=$PATH:$GOPATH/bin GOPATH=$GOPATH critest -r=/var/run/frakti.sock --focus="image" validation + sudo env PATH=$PATH:$GOPATH/bin GOPATH=$GOPATH critest -r=/var/run/frakti.sock --focus="Conformance" --skip="container port" validation } FRAKTI_LISTEN_ADDR=${FRAKTI_LISTEN_ADDR:-/var/run/frakti.sock} From e9585b35207468553736ac469cbc68bc60f08304 Mon Sep 17 00:00:00 2001 From: heartlock <21521209@zju.edu.cn> Date: Fri, 26 May 2017 16:54:29 +0800 Subject: [PATCH 2/2] remove dup test case and remain e2e framwork for frakti-specific tests Signed-off-by: heartlock <21521209@zju.edu.cn> --- test/e2e/cni.go | 57 ------- test/e2e/containers.go | 170 --------------------- test/e2e/images.go | 213 -------------------------- test/e2e/pods.go | 146 ------------------ test/e2e/streaming.go | 152 ------------------- test/e2e/utils.go | 333 ----------------------------------------- 6 files changed, 1071 deletions(-) delete mode 100644 test/e2e/cni.go delete mode 100644 test/e2e/containers.go delete mode 100644 test/e2e/images.go delete mode 100644 test/e2e/pods.go delete mode 100644 test/e2e/streaming.go delete mode 100644 test/e2e/utils.go diff --git a/test/e2e/cni.go b/test/e2e/cni.go deleted file mode 100644 index 30aa303d..00000000 --- a/test/e2e/cni.go +++ /dev/null @@ -1,57 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -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 - -import ( - "k8s.io/frakti/test/e2e/framework" - internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" - kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = framework.KubeDescribe("Test CNI", func() { - f := framework.NewDefaultFramework("test") - - var c internalapi.RuntimeService - - BeforeEach(func() { - c = f.Client.FraktiRuntimeService - }) - - It("test create simple pods with cni", func() { - name := "create-simple-sandbox-" + framework.NewUUID() - By("create a podSandbox with name") - metadata := buildPodSandboxMetadata(name) - config := &kubeapi.PodSandboxConfig{ - Metadata: metadata, - } - podId, err := c.RunPodSandbox(config) - framework.ExpectNoError(err, "Failed to create podsandbox: %v", err) - framework.Logf("Created Podsanbox %s\n", podId) - defer func() { - By("delete pod sandbox") - c.RemovePodSandbox(podId) - }() - By("get podSandbox status") - status, err := c.PodSandboxStatus(podId) - framework.ExpectNoError(err, "Failed to get podsandbox %s status: %v", podId, err) - Expect(framework.CniWork(status.GetNetwork())).To(BeTrue(), "cni not work") - }) - -}) diff --git a/test/e2e/containers.go b/test/e2e/containers.go deleted file mode 100644 index d36714a8..00000000 --- a/test/e2e/containers.go +++ /dev/null @@ -1,170 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -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 - -import ( - "os" - "time" - - "k8s.io/frakti/test/e2e/framework" - internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" - runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = framework.KubeDescribe("Test Container", func() { - f := framework.NewDefaultFramework("frakti-container-test") - - var c internalapi.RuntimeService - var ic internalapi.ImageManagerService - - BeforeEach(func() { - c = f.Client.FraktiRuntimeService - ic = f.Client.FraktiImageService - }) - - Context("test basic operations on container", func() { - var podID string - var podConfig *runtimeapi.PodSandboxConfig - - BeforeEach(func() { - podID, podConfig = createPodSandboxForContainer(c) - pullImageList(ic, []string{defaultContainerImage}) - }) - - AfterEach(func() { - By("stop PodSandbox") - c.StopPodSandbox(podID) - By("delete PodSandbox") - c.RemovePodSandbox(podID) - By("remove default image") - removeImageList(ic, []string{defaultContainerImage}) - }) - - It("test create container", func() { - By("test create container") - containerID := testCreateContainer(c, podID, podConfig) - - By("test list container") - containers := listContainerForIDOrFail(c, containerID) - Expect(containerFound(containers, containerID)).To(BeTrue(), "container should be created") - }) - - It("test start container", func() { - By("create container") - containerID := createContainerOrFail(c, "container-for-create-test-", podID, podConfig) - - By("test start container") - testStartContainer(c, containerID) - }) - - It("test stop container", func() { - By("create container") - containerID := createContainerOrFail(c, "container-for-create-test-", podID, podConfig) - - By("start container") - startContainerOrFail(c, containerID) - - By("test stop container") - testStopContainer(c, containerID) - }) - - It("test remove container", func() { - By("create container") - containerID := createContainerOrFail(c, "container-for-create-test-", podID, podConfig) - - By("test remove container") - removeContainerOrFail(c, containerID) - containers := listContainerForIDOrFail(c, containerID) - Expect(containerFound(containers, containerID)).To(BeFalse(), "container should be removed") - }) - }) - - Context("test container with volume", func() { - var podID string - var podConfig *runtimeapi.PodSandboxConfig - - BeforeEach(func() { - podID, podConfig = createPodSandboxForContainer(c) - pullImageList(ic, []string{defaultContainerImage}) - }) - - AfterEach(func() { - By("stop PodSandbox") - c.StopPodSandbox(podID) - By("delete PodSandbox") - c.RemovePodSandbox(podID) - By("remove default image") - removeImageList(ic, []string{defaultContainerImage}) - }) - - It("test start container with volume", func() { - By("create host path and flag file") - hostPath := "/test" + podID - flagFile := "test.file" - err := os.Mkdir(hostPath, 0777) - framework.ExpectNoError(err, "failed to create host path %s: %v", hostPath, err) - _, err = os.Create(hostPath + "/" + flagFile) - framework.ExpectNoError(err, "failed to create volume file %s: %v", flagFile, err) - - By("create container with volume") - containerID := createVolContainerOrFail(c, "container-with-volume-test-", podID, podConfig, hostPath, flagFile) - - By("test start container with volume") - testStartContainer(c, containerID) - }) - }) - - Context("test log", func() { - var podID string - var podConfig *runtimeapi.PodSandboxConfig - - BeforeEach(func() { - podID, podConfig = createPodSandboxWithLogDirectory(c) - pullImageList(ic, []string{defaultContainerImage}) - }) - - AfterEach(func() { - By("stop PodSandbox") - c.StopPodSandbox(podID) - By("delete PodSandbox") - c.RemovePodSandbox(podID) - By("remove default image") - removeImageList(ic, []string{defaultContainerImage}) - }) - - It("test start container with log", func() { - By("create container with log") - logPath, containerID := createLogContainerOrFail(c, "container-with-log-test-", podID, podConfig) - - By("start container with log") - testStartContainer(c, containerID) - - // sleep one second here to wait container started. - time.Sleep(1 * time.Second) - - By("check the log context") - expectedLogMessage := &logMessage{ - log: []byte(defaultLog + "\n"), - stream: stdoutType, - } - verifyLogContents(podConfig, logPath, expectedLogMessage) - }) - }) -}) diff --git a/test/e2e/images.go b/test/e2e/images.go deleted file mode 100644 index 7fd1b674..00000000 --- a/test/e2e/images.go +++ /dev/null @@ -1,213 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -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 - -import ( - "fmt" - "strings" - - "k8s.io/frakti/test/e2e/framework" - internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" - kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var ( - // image name for test image api - testImageName string = "busybox" - - // name-tagged reference for latest test image - latestTestImageRef string = testImageName + ":latest" - - // Digested reference for latest test image - latestBusyboxDigestRef string = testImageName + "@sha256:a59906e33509d14c036c8678d687bd4eec81ed7c4b8ce907b888c607f6a1e0e6" -) - -func testPublicImage(client internalapi.ImageManagerService, image string) { - By(fmt.Sprintf("pull image %s", image)) - _, err := client.PullImage(&kubeapi.ImageSpec{ - Image: image, - }, nil) - framework.ExpectNoError(err, "Failed to pull image: %v", err) - - if !strings.Contains(image, ":") { - image = image + ":latest" - } - imageSpec := kubeapi.ImageSpec{ - Image: image, - } - By("get image list") - // List all images, then use filter to get expected ones. - // NOTE: We have to do this because ListImages(filter) only works - // for docker 1.13+, but frakti && this test should work for old - // docker versions. Remove this when docker got upgraded. - imageList, err := client.ListImages(nil) - framework.ExpectNoError(err, "Failed to get image list: %v", err) - - filteredImage := filterImageFromList(imageList, &kubeapi.ImageFilter{ - Image: &imageSpec, - }) - - Expect(len(filteredImage)).To(Equal(1), "should have one image in list") - - By("remove image") - err = client.RemoveImage(&imageSpec) - framework.ExpectNoError(err, "Failed to remove image: %v", err) - - By("check image list empty") - imageList, err = client.ListImages(&kubeapi.ImageFilter{ - Image: &imageSpec, - }) - framework.ExpectNoError(err, "Failed to get image list: %v", err) - Expect(len(imageList)).To(Equal(0), "should have none image in list") -} - -// filterImageFromList filter out specified image from image list -func filterImageFromList(images []*kubeapi.Image, filter *kubeapi.ImageFilter) []*kubeapi.Image { - var results []*kubeapi.Image - for _, img := range images { - if filter != nil && filter.GetImage() != nil && filter.GetImage().Image != "" { - image := filter.GetImage().Image - if !inList(image, img.RepoTags) && !inList(image, img.RepoDigests) { - continue - } - } - - results = append(results, img) - } - return results -} - -// inList checks if a string is in a list -func inList(in string, list []string) bool { - for _, str := range list { - if in == str { - return true - } - } - - return false -} - -// TODO: need some test case with private image - -func pullImageList(client internalapi.ImageManagerService, imageList []string) { - for _, imageRef := range imageList { - By(fmt.Sprintf("pull image %s", imageRef)) - _, err := client.PullImage(&kubeapi.ImageSpec{ - Image: imageRef, - }, nil) - framework.ExpectNoError(err, "Failed to pull image: %v", err) - } -} - -func removeImageList(client internalapi.ImageManagerService, imageList []string) { - for _, imageRef := range imageList { - By(fmt.Sprintf("remove image %s", imageRef)) - err := client.RemoveImage(&kubeapi.ImageSpec{ - Image: imageRef, - }) - framework.ExpectNoError(err, "Failed to remove image: %v", err) - } -} - -var _ = framework.KubeDescribe("Test image", func() { - f := framework.NewDefaultFramework("image") - - var c internalapi.ImageManagerService - - BeforeEach(func() { - c = f.Client.FraktiImageService - // clear all images before each test - framework.ClearAllImages(c) - }) - - It("public image with tag should be pulled and removed", func() { - imageName := latestTestImageRef - testPublicImage(c, imageName) - }) - - It("public image without tag should be pulled and removed", func() { - imageName := testImageName - testPublicImage(c, imageName) - }) - - It("public image with digest should be pulled and removed", func() { - imageName := latestBusyboxDigestRef - testPublicImage(c, imageName) - }) - - It("image status get image fileds should not be empty", func() { - imageName := latestTestImageRef - imageSpec := kubeapi.ImageSpec{ - Image: imageName, - } - - By(fmt.Sprintf("pull image %s", imageName)) - _, err := c.PullImage(&imageSpec, nil) - framework.ExpectNoError(err, "Failed to pull image: %v", err) - - defer c.RemoveImage(&imageSpec) - - By("get image status") - image, err := c.ImageStatus(&imageSpec) - framework.ExpectNoError(err, "Failed to get image status: %v", err) - Expect(image.Id).NotTo(BeNil(), "image Id should not be nil") - Expect(len(image.RepoTags)).NotTo(Equal(0), "should have repoTags in image") - Expect(image.Size_).NotTo(BeNil(), "image Size should not be nil") - }) - - It("ListImage should get exactly 3 image in the result list", func() { - // different tags refer to different images - testImageList := []string{ - "busybox:1", - "busybox:musl", - "busybox:glibc", - } - - pullImageList(c, testImageList) - - defer removeImageList(c, testImageList) - - By("get all image list") - imageList, err := c.ListImages(nil) - framework.ExpectNoError(err, "Failed to get image list: %v", err) - Expect(len(imageList)).To(Equal(3), "should have exactly three images in list") - }) - - It("ListImage should get exactly 3 repoTags in the result image", func() { - // different tags refer to the same image - testImageList := []string{ - "busybox:1", - "busybox:1-uclibc", - "busybox:uclibc", - } - - pullImageList(c, testImageList) - - defer removeImageList(c, testImageList) - - By("get all image list") - imageList, err := c.ListImages(nil) - framework.ExpectNoError(err, "Failed to get image list: %v", err) - - Expect(len(imageList)).To(Equal(1), "should have only one image in list") - Expect(len(imageList[0].RepoTags)).To(Equal(3), "should have three repoTags in image") - }) -}) diff --git a/test/e2e/pods.go b/test/e2e/pods.go deleted file mode 100644 index 80e9235a..00000000 --- a/test/e2e/pods.go +++ /dev/null @@ -1,146 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -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 - -import ( - "k8s.io/frakti/test/e2e/framework" - internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" - kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = framework.KubeDescribe("Test PodSandbox", func() { - f := framework.NewDefaultFramework("test") - - var c internalapi.RuntimeService - - BeforeEach(func() { - c = f.Client.FraktiRuntimeService - }) - - It("test create simple podsandbox", func() { - name := "create-simple-sandbox-" + framework.NewUUID() - By("create a podSandbox with name") - metadata := buildPodSandboxMetadata(name) - config := &kubeapi.PodSandboxConfig{ - Metadata: metadata, - } - podId, err := c.RunPodSandbox(config) - framework.ExpectNoError(err, "Failed to create podsandbox: %v", err) - framework.Logf("Created Podsanbox %s\n", podId) - defer func() { - By("delete pod sandbox") - c.RemovePodSandbox(podId) - }() - By("get podSandbox status") - status, err := c.PodSandboxStatus(podId) - framework.ExpectNoError(err, "Failed to get podsandbox %s status: %v", podId, err) - Expect(framework.PodReady(status)).To(BeTrue(), "pod state shoud be ready") - }) - - It("test stop PodSandbox", func() { - name := "create-simple-sandbox-for-stop" + framework.NewUUID() - - By("create a podSandbox with name") - metadata := buildPodSandboxMetadata(name) - config := &kubeapi.PodSandboxConfig{ - Metadata: metadata, - } - podId, err := c.RunPodSandbox(config) - framework.ExpectNoError(err, "Failed to create podsandbox: %v", err) - framework.Logf("Created Podsanbox %s\n", podId) - defer func() { - By("delete pod sandbox") - c.RemovePodSandbox(podId) - }() - - By("get podSandbox status") - status, err := c.PodSandboxStatus(podId) - framework.ExpectNoError(err, "Failed to get podsandbox %s status: %v", podId, err) - Expect(framework.PodReady(status)).To(BeTrue(), "pod state should be ready") - - By("stop podSandbox with poId") - err = c.StopPodSandbox(podId) - framework.ExpectNoError(err, "Failed to stop podsandbox: %v", err) - framework.Logf("Stoped Podsanbox %s\n", podId) - - By("get podSandbox status") - status, err = c.PodSandboxStatus(podId) - framework.ExpectNoError(err, "Failed to get podsandbox %s status: %v", podId, err) - Expect(framework.PodReady(status)).To(BeFalse(), "pod state should be not ready") - }) - - It("test remove podsandbox", func() { - name := "create-simple-sandbox-for-remove" + framework.NewUUID() - By("create a podSandbox with name") - metadata := buildPodSandboxMetadata(name) - config := &kubeapi.PodSandboxConfig{ - Metadata: metadata, - } - podId, err := c.RunPodSandbox(config) - framework.ExpectNoError(err, "Failed to create podsandbox: %v", err) - framework.Logf("Created Podsanbox %s\n", podId) - - By("get podSandbox status") - status, err := c.PodSandboxStatus(podId) - framework.ExpectNoError(err, "Failed to get podsandbox %s status: %v", podId, err) - Expect(framework.PodReady(status)).To(BeTrue(), "pod state should be ready") - - By("remove podSandbox with podId") - err = c.RemovePodSandbox(podId) - framework.ExpectNoError(err, "Failed to remove podsandbox: %v", err) - framework.Logf("Removed Podsanbox %s\n", podId) - - By("list podSandbox with podId") - filter := &kubeapi.PodSandboxFilter{ - Id: podId, - } - podsandboxs, err := c.ListPodSandbox(filter) - framework.ExpectNoError(err, "Failed to list podsandbox %s status: %v", podId, err) - Expect(framework.PodFound(podsandboxs, podId)).To(BeFalse(), "pod should be removed") - }) - - It("test list podsandbox", func() { - name := "create-simple-sandbox-for-list" + framework.NewUUID() - By("create a podSandbox with name") - metadata := buildPodSandboxMetadata(name) - config := &kubeapi.PodSandboxConfig{ - Metadata: metadata, - } - podId, err := c.RunPodSandbox(config) - framework.ExpectNoError(err, "Failed to create podsandbox: %v", err) - framework.Logf("Created Podsanbox %s\n", podId) - defer func() { - By("delete pod sandbox") - c.RemovePodSandbox(podId) - }() - By("get podSandbox status") - status, err := c.PodSandboxStatus(podId) - framework.ExpectNoError(err, "Failed to get podsandbox %s status: %v", podId, err) - Expect(framework.PodReady(status)).To(BeTrue(), "pod state should be ready") - - By("list podSandbox with podId") - filter := &kubeapi.PodSandboxFilter{ - Id: podId, - } - podsandboxs, err := c.ListPodSandbox(filter) - framework.ExpectNoError(err, "Failed to list podsandbox %s status: %v", podId, err) - Expect(framework.PodFound(podsandboxs, podId)).To(BeTrue(), "pod should be listed") - }) -}) diff --git a/test/e2e/streaming.go b/test/e2e/streaming.go deleted file mode 100644 index d8f441a6..00000000 --- a/test/e2e/streaming.go +++ /dev/null @@ -1,152 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -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 - -import ( - "time" - - "k8s.io/frakti/test/e2e/framework" - internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" - kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = framework.KubeDescribe("Test streaming in container", func() { - f := framework.NewDefaultFramework("test") - - var ( - runtimeClient internalapi.RuntimeService - imageClient internalapi.ImageManagerService - ) - - BeforeEach(func() { - runtimeClient = f.Client.FraktiRuntimeService - imageClient = f.Client.FraktiImageService - }) - - It("test exec a command in container synchronously and successfully", func() { - podID, cID := startLongRunningContainer(runtimeClient, imageClient) - defer func(podId string) { - By("delete pod sandbox") - runtimeClient.RemovePodSandbox(podID) - }(podID) - - By("exec command in container synchronously") - magicWords := "blablabla" - stdout, stderr, err := runtimeClient.ExecSync(cID, []string{"echo", magicWords}, 0) - framework.ExpectNoError(err, "Failed to exec cmd in container: %v", err) - framework.Logf("stdout: %q, stderr: %q", string(stdout), string(stderr)) - Expect(len(stderr)).To(Equal(0), "stderr should not have content") - Expect(string(stdout)).To(BeIdenticalTo(magicWords+"\n"), "stdout should be same as defined") - }) - - It("test exec a command in container synchronously and failed", func() { - podID, cID := startLongRunningContainer(runtimeClient, imageClient) - defer func(podID string) { - By("delete pod sandbox") - runtimeClient.RemovePodSandbox(podID) - }(podID) - - By("exec command in container synchronously") - magicCmd := "blablabla" - stdout, stderr, err := runtimeClient.ExecSync(cID, []string{magicCmd}, 0) - Expect(err).NotTo(Equal(nil), "Exec non-exist cmd should failed") - framework.Logf("stdout: %q, stderr: %q", string(stdout), string(stderr)) - Expect(len(stderr)).NotTo(Equal(0), "stderr should have content") - }) - - It("test get a exec url", func() { - podID, cID := startLongRunningContainer(runtimeClient, imageClient) - defer func(podID string) { - By("delete pod sandbox") - runtimeClient.RemovePodSandbox(podID) - }(podID) - - By("prepare exec command url in container") - magicCmd := []string{"blablabla"} - execReq := &kubeapi.ExecRequest{ - ContainerId: cID, - Cmd: magicCmd, - } - resp, err := runtimeClient.Exec(execReq) - framework.ExpectNoError(err, "Failed to get exec url in container: %v", err) - framework.Logf("ExecUrl: %q", resp.Url) - Expect(len(resp.Url)).NotTo(Equal(0), "exec url should not be null") - }) - - It("test get a attach url", func() { - podID, cID := startLongRunningContainer(runtimeClient, imageClient) - defer func(podID string) { - By("delete pod sandbox") - runtimeClient.RemovePodSandbox(podID) - }(podID) - - By("prepare attach command url in container") - stdin := true - attachReq := &kubeapi.AttachRequest{ - ContainerId: cID, - Stdin: stdin, - } - resp, err := runtimeClient.Attach(attachReq) - framework.ExpectNoError(err, "Failed to get attach url in container: %v", err) - framework.Logf("AttachUrl: %q", resp.Url) - Expect(len(resp.Url)).NotTo(Equal(0), "attach url should not be null") - }) -}) - -func startLongRunningContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService) (podId, containerId string) { - podName := "simple-sandbox-" + framework.NewUUID() - By("create a podSandbox") - podConfig := &kubeapi.PodSandboxConfig{ - Metadata: &kubeapi.PodSandboxMetadata{ - Name: podName, - }, - } - podId, err := rc.RunPodSandbox(podConfig) - framework.ExpectNoError(err, "Failed to create podsandbox: %v", err) - - By("pull necessary image") - imageSpec := &kubeapi.ImageSpec{ - Image: latestTestImageRef, - } - _, err = ic.PullImage(imageSpec, nil) - framework.ExpectNoError(err, "Failed to pull image: %v", err) - - By("create container in pod") - containerName := "simple-container-" + framework.NewUUID() - containerConfig := &kubeapi.ContainerConfig{ - Metadata: &kubeapi.ContainerMetadata{ - Name: containerName, - }, - Image: imageSpec, - Command: []string{"sh", "-c", "top"}, - } - containerId, err = rc.CreateContainer(podId, containerConfig, podConfig) - framework.ExpectNoError(err, "Failed to create container: %v", err) - - By("start container") - err = rc.StartContainer(containerId) - framework.ExpectNoError(err, "Failed to start container: %v", err) - - // sleep 2s to make sure container start is ready, workaround for random failed in travis - // TODO: remove this - time.Sleep(2 * time.Second) - - return podId, containerId -} diff --git a/test/e2e/utils.go b/test/e2e/utils.go deleted file mode 100644 index 19315b12..00000000 --- a/test/e2e/utils.go +++ /dev/null @@ -1,333 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -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 - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "os" - "time" - - "github.com/docker/docker/pkg/jsonlog" - "k8s.io/frakti/test/e2e/framework" - internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" - runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var ( - defaultUid string = "e2e-cri-uid" - defaultNamespace string = "e2e-cri-namespace" - defaultAttempt uint32 = 2 - defaultContainerImage string = "busybox:latest" - defaultStopContainerTimeout int64 = 60 - defaultLog string = "hello world" -) - -// streamType is the type of the stream. -type streamType string - -const ( - stderrType streamType = "stderr" - stdoutType streamType = "stdout" - - // timeFormat is the time format used in the log. - timeFormat = time.RFC3339Nano - // blockSize is the block size used in tail. - blockSize = 1024 -) - -var ( - // eol is the end-of-line sign in the log. - eol = []byte{'\n'} - // delimiter is the delimiter for timestamp and streamtype in log line. - delimiter = []byte{' '} -) - -// logMessage is the internal log type. -type logMessage struct { - timestamp time.Time - stream streamType - log []byte -} - -// buildPodSandboxMetadata builds default PodSandboxMetadata with podSandboxName. -func buildPodSandboxMetadata(podSandboxName string) *runtimeapi.PodSandboxMetadata { - return &runtimeapi.PodSandboxMetadata{ - Name: podSandboxName, - Uid: defaultUid, - Namespace: defaultNamespace, - Attempt: defaultAttempt, - } -} - -// buildContainerMetadata builds default PodSandboxMetadata with containerName. -func buildContainerMetadata(containerName string) *runtimeapi.ContainerMetadata { - return &runtimeapi.ContainerMetadata{ - Name: containerName, - Attempt: defaultAttempt, - } -} - -// createPodSandboxForContainer creates a PodSandbox for creating containers. -func createPodSandboxForContainer(c internalapi.RuntimeService) (string, *runtimeapi.PodSandboxConfig) { - By("create a PodSandbox for creating containers") - podName := "PodSandbox-for-create-container-" + framework.NewUUID() - podConfig := &runtimeapi.PodSandboxConfig{ - Metadata: buildPodSandboxMetadata(podName), - } - return createPodSandboxOrFail(c, podConfig), podConfig -} - -// -func createPodSandboxWithLogDirectory(c internalapi.RuntimeService) (string, *runtimeapi.PodSandboxConfig) { - By("create a PodSandbox with log directory") - podName := "PodSandbox-with-log-directory-" + framework.NewUUID() - dir := fmt.Sprintf("/var/log/pods/%s/", podName) - podConfig := &runtimeapi.PodSandboxConfig{ - Metadata: buildPodSandboxMetadata(podName), - LogDirectory: dir, - } - return createPodSandboxOrFail(c, podConfig), podConfig -} - -// createPodSandboxOrFail creates a PodSandbox and fails if it gets error. -func createPodSandboxOrFail(c internalapi.RuntimeService, podConfig *runtimeapi.PodSandboxConfig) string { - podID, err := c.RunPodSandbox(podConfig) - framework.ExpectNoError(err, "Failed to create PodSandbox: %v", err) - framework.Logf("Created PodSandbox %s\n", podID) - return podID -} - -// listPodSanboxforID lists PodSandbox for podID. -func listPodSanboxForID(c internalapi.RuntimeService, podID string) ([]*runtimeapi.PodSandbox, error) { - By("list PodSandbox for podID") - filter := &runtimeapi.PodSandboxFilter{ - Id: podID, - } - return c.ListPodSandbox(filter) -} - -// listContainerforID lists container for podID. -func listContainerForID(c internalapi.RuntimeService, containerID string) ([]*runtimeapi.Container, error) { - By("list containers for containerID") - filter := &runtimeapi.ContainerFilter{ - Id: containerID, - } - return c.ListContainers(filter) -} - -// listContainerforID lists container for podID and fails if it gets error. -func listContainerForIDOrFail(c internalapi.RuntimeService, containerID string) []*runtimeapi.Container { - containers, err := listContainerForID(c, containerID) - framework.ExpectNoError(err, "Failed to list containers %s status: %v", containerID, err) - return containers -} - -// createContainer creates a container with the prefix of containerName. -func createContainer(c internalapi.RuntimeService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, error) { - By("create a container with name") - containerName := prefix + framework.NewUUID() - containerConfig := &runtimeapi.ContainerConfig{ - Metadata: buildContainerMetadata(containerName), - Image: &runtimeapi.ImageSpec{Image: defaultContainerImage}, - Command: []string{"sh", "-c", "top"}, - } - return c.CreateContainer(podID, containerConfig, podConfig) -} - -// createVolContainer creates a container with volume and the prefix of containerName. -func createVolContainer(c internalapi.RuntimeService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig, volPath, flagFile string) (string, error) { - By("create a container with volume and name") - containerName := prefix + framework.NewUUID() - containerConfig := &runtimeapi.ContainerConfig{ - Metadata: buildContainerMetadata(containerName), - Image: &runtimeapi.ImageSpec{Image: defaultContainerImage}, - // mount host path to the same directory in container, and check if flag file exists - Command: []string{"sh", "-c", "while [ -f " + volPath + "/" + flagFile + " ]; do sleep 1; done;"}, - Mounts: []*runtimeapi.Mount{ - { - HostPath: volPath, - ContainerPath: volPath, - }, - }, - } - return c.CreateContainer(podID, containerConfig, podConfig) -} - -// createLogContainer creates a container with log and the prefix of containerName. -func createLogContainer(c internalapi.RuntimeService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, string, error) { - By("create a container with log and name") - containerName := prefix + framework.NewUUID() - path := fmt.Sprintf("%s.log", containerName) - containerConfig := &runtimeapi.ContainerConfig{ - Metadata: buildContainerMetadata(containerName), - Image: &runtimeapi.ImageSpec{Image: defaultContainerImage}, - Command: []string{"echo", defaultLog}, - LogPath: path, - } - containerID, err := c.CreateContainer(podID, containerConfig, podConfig) - return containerConfig.LogPath, containerID, err -} - -// createContainerOrFail creates a container with the prefix of containerName and fails if it gets error. -func createContainerOrFail(c internalapi.RuntimeService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) string { - containerID, err := createContainer(c, prefix, podID, podConfig) - framework.ExpectNoError(err, "Failed to create container: %v", err) - framework.Logf("Created container %s\n", containerID) - return containerID -} - -// createVolContainerOrFail creates a container with volume and the prefix of containerName and fails if it gets error. -func createVolContainerOrFail(c internalapi.RuntimeService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig, hostPath, flagFile string) string { - containerID, err := createVolContainer(c, prefix, podID, podConfig, hostPath, flagFile) - framework.ExpectNoError(err, "Failed to create container: %v", err) - framework.Logf("Created container %s\n", containerID) - return containerID -} - -// createLogContainerOrFail creates a container with log and the prefix of containerName and fails if it gets error. -func createLogContainerOrFail(c internalapi.RuntimeService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, string) { - logPath, containerID, err := createLogContainer(c, prefix, podID, podConfig) - framework.ExpectNoError(err, "Failed to create container: %v", err) - framework.Logf("Created container %s\n", containerID) - return logPath, containerID -} - -// testCreateContainer creates a container in the pod which ID is podID and make sure it be ready. -func testCreateContainer(c internalapi.RuntimeService, podID string, podConfig *runtimeapi.PodSandboxConfig) string { - containerID := createContainerOrFail(c, "container-for-create-test-", podID, podConfig) - verifyContainerStatus(c, containerID, runtimeapi.ContainerState_CONTAINER_CREATED, "created") - return containerID -} - -// startContainer start the container for containerID. -func startContainer(c internalapi.RuntimeService, containerID string) error { - By("start container") - return c.StartContainer(containerID) -} - -// startcontainerOrFail starts the container for containerID and fails if it gets error. -func startContainerOrFail(c internalapi.RuntimeService, containerID string) { - err := startContainer(c, containerID) - framework.ExpectNoError(err, "Failed to start container: %v", err) - framework.Logf("Start container %s\n", containerID) -} - -// testStartContainer starts the container for containerID and make sure it be running. -func testStartContainer(c internalapi.RuntimeService, containerID string) { - startContainerOrFail(c, containerID) - verifyContainerStatus(c, containerID, runtimeapi.ContainerState_CONTAINER_RUNNING, "running") -} - -// stopContainer stops the container for containerID. -func stopContainer(c internalapi.RuntimeService, containerID string, timeout int64) error { - By("stop container") - return c.StopContainer(containerID, timeout) -} - -// stopContainerOrFail stops the container for containerID and fails if it gets error. -func stopContainerOrFail(c internalapi.RuntimeService, containerID string, timeout int64) { - err := stopContainer(c, containerID, timeout) - framework.ExpectNoError(err, "Failed to stop container: %v", err) - framework.Logf("Stop container %s\n", containerID) -} - -// testStopContainer stops the container for containerID and make sure it be exited. -func testStopContainer(c internalapi.RuntimeService, containerID string) { - stopContainerOrFail(c, containerID, defaultStopContainerTimeout) - verifyContainerStatus(c, containerID, runtimeapi.ContainerState_CONTAINER_EXITED, "exited") -} - -// verifyContainerStatus verifies whether status for given containerID matches. -func verifyContainerStatus(c internalapi.RuntimeService, containerID string, expectedStatus runtimeapi.ContainerState, stateName string) { - status := getContainerStatusOrFail(c, containerID) - Expect(status.State).To(Equal(expectedStatus), "Container state should be %s", stateName) -} - -// getPodSandboxStatusOrFail gets ContainerState for containerID and fails if it gets error. -func getContainerStatusOrFail(c internalapi.RuntimeService, containerID string) *runtimeapi.ContainerStatus { - status, err := getContainerStatus(c, containerID) - framework.ExpectNoError(err, "Failed to get container %s status: %v", containerID, err) - return status -} - -// removePodSandbox removes the container for containerID. -func removeContainer(c internalapi.RuntimeService, containerID string) error { - By("remove container for containerID") - return c.RemoveContainer(containerID) -} - -// removeContainerOrFail removes the container for containerID and fails if it gets error. -func removeContainerOrFail(c internalapi.RuntimeService, containerID string) { - err := removeContainer(c, containerID) - framework.ExpectNoError(err, "Failed to remove container: %v", err) - framework.Logf("Removed container %s\n", containerID) -} - -// getContainerStatus gets ContainerState for containerID. -func getContainerStatus(c internalapi.RuntimeService, containerID string) (*runtimeapi.ContainerStatus, error) { - By("get container status") - return c.ContainerStatus(containerID) -} - -// containerFound returns whether containers is found. -func containerFound(containers []*runtimeapi.Container, containerID string) bool { - if len(containers) == 1 && containers[0].Id == containerID { - return true - - } - return false -} - -// parseDockerJSONLog parses logs in Docker JSON log format. Docker JSON log format -// example: -// {"log":"content 1","stream":"stdout","time":"2016-10-20T18:39:20.57606443Z"} -// {"log":"content 2","stream":"stderr","time":"2016-10-20T18:39:20.57606444Z"} -func parseDockerJSONLog(log []byte, msg *logMessage) { - var l jsonlog.JSONLog - - err := json.Unmarshal(log, &l) - framework.ExpectNoError(err, "failed with %v to unmarshal log %q", err, l) - - msg.timestamp = l.Created - msg.stream = streamType(l.Stream) - msg.log = []byte(l.Log) -} - -// verifyLogContents verifies the contents of container log. -func verifyLogContents(podConfig *runtimeapi.PodSandboxConfig, logPath string, expectedLogMessage *logMessage) { - path := podConfig.LogDirectory + logPath - f, err := os.Open(path) - framework.ExpectNoError(err, "Failed to open log file: %v", err) - framework.Logf("Open log file %s\n", path) - defer f.Close() - - log, err := ioutil.ReadAll(f) - framework.ExpectNoError(err, "Failed to read log file: %v", err) - framework.Logf("Log file context is %s\n", log) - - var msg logMessage - parseDockerJSONLog(log, &msg) - framework.Logf("Parse json log succeed") - - Expect(string(msg.log)).To(Equal(string(expectedLogMessage.log)), "Log should be %s", expectedLogMessage.log) - Expect(string(msg.stream)).To(Equal(string(expectedLogMessage.stream)), "Stream should be %s", string(expectedLogMessage.stream)) -}