Skip to content

Commit 2bb3732

Browse files
committed
Add idempotence tests
Signed-off-by: Sascha Grunert <[email protected]>
1 parent 32c9832 commit 2bb3732

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

Diff for: pkg/validate/idempotence.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package validate
18+
19+
import (
20+
"context"
21+
22+
"github.com/google/uuid"
23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
25+
internalapi "k8s.io/cri-api/pkg/apis"
26+
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
27+
28+
"sigs.k8s.io/cri-tools/pkg/framework"
29+
)
30+
31+
var _ = framework.KubeDescribe("Idempotence", func() {
32+
f := framework.NewDefaultCRIFramework()
33+
c := context.Background()
34+
35+
var (
36+
rc internalapi.RuntimeService
37+
ic internalapi.ImageManagerService
38+
)
39+
40+
BeforeEach(func() {
41+
rc = f.CRIClient.CRIRuntimeClient
42+
ic = f.CRIClient.CRIImageClient
43+
})
44+
45+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L45-L46
46+
Context("StopPodSandbox", func() {
47+
It("should not return an error if not found", func() {
48+
By("stop not existing sandbox")
49+
Expect(rc.StopPodSandbox(c, uuid.New().String())).NotTo(HaveOccurred())
50+
})
51+
52+
It("should not return an error if already stopped", func() {
53+
By("run PodSandbox")
54+
podID := framework.RunDefaultPodSandbox(rc, "idempotence-pod-")
55+
56+
By("stop sandbox")
57+
testStopPodSandbox(rc, podID)
58+
59+
By("stop sandbox again")
60+
testStopPodSandbox(rc, podID)
61+
62+
By("delete sandbox as cleanup")
63+
Expect(rc.RemovePodSandbox(c, podID)).NotTo(HaveOccurred())
64+
})
65+
})
66+
67+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L53-L54
68+
Context("RemovePodSandbox", func() {
69+
It("should not return an error if not found", func() {
70+
By("remove not existing sandbox")
71+
Expect(rc.RemovePodSandbox(c, uuid.New().String())).NotTo(HaveOccurred())
72+
})
73+
})
74+
75+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L67-L68
76+
Context("StopContainer", func() {
77+
It("should not return an error if not found", func() {
78+
By("test stop not existing container")
79+
Expect(rc.StopContainer(c, uuid.New().String(), 0)).NotTo(HaveOccurred())
80+
})
81+
82+
It("should not return an error if already stopped", func() {
83+
By("create sandbox")
84+
podID, podConfig := framework.CreatePodSandboxForContainer(rc)
85+
86+
By("create container")
87+
containerID := framework.CreatePauseContainer(rc, ic, podID, podConfig, "idempotence-container-")
88+
89+
By("start container")
90+
startContainer(rc, containerID)
91+
92+
By("stop container")
93+
testStopContainer(rc, containerID)
94+
95+
By("remove container")
96+
removeContainer(rc, containerID)
97+
98+
By("remove container again")
99+
removeContainer(rc, containerID)
100+
101+
By("stop sandbox as cleanup")
102+
Expect(rc.StopPodSandbox(c, podID)).NotTo(HaveOccurred())
103+
104+
By("delete sandbox as cleanup")
105+
Expect(rc.RemovePodSandbox(c, podID)).NotTo(HaveOccurred())
106+
})
107+
})
108+
109+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L74-L75
110+
Context("RemoveContainer", func() {
111+
It("should not return an error if not found", func() {
112+
By("remove not existing comtainer")
113+
Expect(rc.RemoveContainer(c, uuid.New().String())).NotTo(HaveOccurred())
114+
})
115+
})
116+
117+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L156-L157
118+
Context("RemoveImage", func() {
119+
It("should not return an error if not found", func() {
120+
By("remove not existing image")
121+
const fakeImageID = "0000000000000000000000000000000000000000000000000000000000000000"
122+
Expect(ic.RemoveImage(c, &runtimeapi.ImageSpec{Image: fakeImageID})).NotTo(HaveOccurred())
123+
124+
By("remove the image again")
125+
Expect(ic.RemoveImage(c, &runtimeapi.ImageSpec{Image: fakeImageID})).NotTo(HaveOccurred())
126+
})
127+
})
128+
})

0 commit comments

Comments
 (0)