From 6cf605b9f8b90efe974827e87aae3ed414ebcb1d Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Tue, 30 Jul 2024 11:07:00 +0200 Subject: [PATCH] Add idempotence tests Signed-off-by: Sascha Grunert --- pkg/validate/idempotence.go | 110 ++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 pkg/validate/idempotence.go diff --git a/pkg/validate/idempotence.go b/pkg/validate/idempotence.go new file mode 100644 index 0000000000..96b752f65d --- /dev/null +++ b/pkg/validate/idempotence.go @@ -0,0 +1,110 @@ +/* +Copyright 2024 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 validate + +import ( + "context" + + "github.com/google/uuid" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + internalapi "k8s.io/cri-api/pkg/apis" + runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" + + "sigs.k8s.io/cri-tools/pkg/framework" +) + +var _ = framework.KubeDescribe("Idempotence", func() { + f := framework.NewDefaultCRIFramework() + c := context.Background() + + var ( + rc internalapi.RuntimeService + ic internalapi.ImageManagerService + ) + + BeforeEach(func() { + rc = f.CRIClient.CRIRuntimeClient + ic = f.CRIClient.CRIImageClient + }) + + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L45-L46 + Context("StopPodSandbox", func() { + It("should not return an error if not found", func() { + By("stop not existing sandbox") + Expect(rc.StopPodSandbox(c, uuid.New().String())).NotTo(HaveOccurred()) + }) + + It("should not return an error if already stopped", func() { + By("run PodSandbox") + podID := framework.RunDefaultPodSandbox(rc, "idempotence-pod-") + + By("stop sandbox") + testStopPodSandbox(rc, podID) + + By("stop sandbox again") + testStopPodSandbox(rc, podID) + }) + }) + + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L53-L54 + It("RemovePodSandbox should not return an error if not found", func() { + By("remove not existing sandbox") + Expect(rc.RemovePodSandbox(c, uuid.New().String())).NotTo(HaveOccurred()) + }) + + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L67-L68 + Context("StopContainer", func() { + It("should not return an error if not found", func() { + By("test stop not existing container") + Expect(rc.StopContainer(c, uuid.New().String(), 0)).NotTo(HaveOccurred()) + }) + + It("should not return an error if already stopped", func() { + By("create sandbox") + podID, podConfig := framework.CreatePodSandboxForContainer(rc) + + By("create container") + containerID := framework.CreatePauseContainer(rc, ic, podID, podConfig, "idempotence-container-") + + By("start container") + startContainer(rc, containerID) + + By("stop container") + testStopContainer(rc, containerID) + + By("remove container") + removeContainer(rc, containerID) + + By("remove container again") + removeContainer(rc, containerID) + }) + }) + + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L74-L75 + It("RemoveContainer should not return an error if not found", func() { + By("remove not existing comtainer") + Expect(rc.RemoveContainer(c, uuid.New().String())).NotTo(HaveOccurred()) + }) + + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L156-L157 + It("RemoveImage should not return an error if not found", func() { + By("remove not existing image") + const fakeImageID = "0000000000000000000000000000000000000000000000000000000000000000" + Expect(ic.RemoveImage(c, &runtimeapi.ImageSpec{Image: fakeImageID})).NotTo(HaveOccurred()) + }) +})