Skip to content

Commit 6cf605b

Browse files
committed
Add idempotence tests
Signed-off-by: Sascha Grunert <[email protected]>
1 parent accffba commit 6cf605b

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

Diff for: pkg/validate/idempotence.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
})
63+
64+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L53-L54
65+
It("RemovePodSandbox should not return an error if not found", func() {
66+
By("remove not existing sandbox")
67+
Expect(rc.RemovePodSandbox(c, uuid.New().String())).NotTo(HaveOccurred())
68+
})
69+
70+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L67-L68
71+
Context("StopContainer", func() {
72+
It("should not return an error if not found", func() {
73+
By("test stop not existing container")
74+
Expect(rc.StopContainer(c, uuid.New().String(), 0)).NotTo(HaveOccurred())
75+
})
76+
77+
It("should not return an error if already stopped", func() {
78+
By("create sandbox")
79+
podID, podConfig := framework.CreatePodSandboxForContainer(rc)
80+
81+
By("create container")
82+
containerID := framework.CreatePauseContainer(rc, ic, podID, podConfig, "idempotence-container-")
83+
84+
By("start container")
85+
startContainer(rc, containerID)
86+
87+
By("stop container")
88+
testStopContainer(rc, containerID)
89+
90+
By("remove container")
91+
removeContainer(rc, containerID)
92+
93+
By("remove container again")
94+
removeContainer(rc, containerID)
95+
})
96+
})
97+
98+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L74-L75
99+
It("RemoveContainer should not return an error if not found", func() {
100+
By("remove not existing comtainer")
101+
Expect(rc.RemoveContainer(c, uuid.New().String())).NotTo(HaveOccurred())
102+
})
103+
104+
// https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L156-L157
105+
It("RemoveImage should not return an error if not found", func() {
106+
By("remove not existing image")
107+
const fakeImageID = "0000000000000000000000000000000000000000000000000000000000000000"
108+
Expect(ic.RemoveImage(c, &runtimeapi.ImageSpec{Image: fakeImageID})).NotTo(HaveOccurred())
109+
})
110+
})

0 commit comments

Comments
 (0)