Skip to content

Commit c8065bc

Browse files
authored
Merge pull request #51 from zdyxry/fix-test
fix(test): only clean up containers created by image tests
2 parents de1c8d9 + eb77d71 commit c8065bc

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

internal/cluster/images.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const (
2626
ClusterImagesVolumePrefix = "cluster-images"
2727
ClusterImagesMountPath = "/var/lib/cluster-images"
2828
populatorContainerNamePrefix = "cluster-images-populator"
29-
3029
)
3130

3231
// PopulatorContainerName returns the populator container name scoped to a kubeadm version.
@@ -184,6 +183,9 @@ func (c *Cluster) populateImagesVolume(ctx context.Context, volumeName, nodeImag
184183
Image: config.DefaultClusterImage,
185184
Entrypoint: []string{"/bin/sh"},
186185
Command: []string{"-c", "sleep infinity"},
186+
Labels: map[string]string{
187+
config.LabelComponent: "cluster-images-populator",
188+
},
187189
ImageVolumes: []*specgen.ImageVolume{{
188190
Source: nodeImage,
189191
Destination: "/images",

test/integration/images_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/bootc-dev/bink/test/integration/helpers"
2121
)
2222

23+
const verifyContainerName = "test-images-verify"
24+
2325
var _ = Describe("Cluster Images Volume", Serial, func() {
2426
var (
2527
podmanClient *podman.Client
@@ -34,9 +36,18 @@ var _ = Describe("Cluster Images Volume", Serial, func() {
3436
podmanClient, err = podman.NewClient()
3537
Expect(err).ToNot(HaveOccurred())
3638

37-
containers, err := podmanClient.ContainerList(ctx, "")
39+
// Clean up populator containers from previous test runs.
40+
populators, err := podmanClient.ContainerList(ctx, config.LabelFilter(config.LabelComponent, "cluster-images-populator"))
3841
Expect(err).ToNot(HaveOccurred())
39-
for _, name := range containers {
42+
for _, name := range populators {
43+
_ = podmanClient.ContainerStop(ctx, name)
44+
_ = podmanClient.ContainerRemove(ctx, name, true)
45+
}
46+
47+
// Clean up verify containers from previous test runs.
48+
verifiers, err := podmanClient.ContainerList(ctx, config.LabelFilter(config.LabelComponent, "cluster-images-test-verify"))
49+
Expect(err).ToNot(HaveOccurred())
50+
for _, name := range verifiers {
4051
_ = podmanClient.ContainerStop(ctx, name)
4152
_ = podmanClient.ContainerRemove(ctx, name, true)
4253
}
@@ -60,7 +71,11 @@ var _ = Describe("Cluster Images Volume", Serial, func() {
6071
AfterEach(func() {
6172
ctx := context.Background()
6273

63-
_ = podmanClient.ContainerRemove(ctx, "test-images-verify", true)
74+
verifiers, err := podmanClient.ContainerList(ctx, config.LabelFilter(config.LabelComponent, "cluster-images-test-verify"))
75+
Expect(err).ToNot(HaveOccurred())
76+
for _, name := range verifiers {
77+
_ = podmanClient.ContainerRemove(ctx, name, true)
78+
}
6479
})
6580

6681
It("should populate a versioned cluster-images volume with Kubernetes and Calico images", func() {
@@ -99,23 +114,25 @@ var _ = Describe("Cluster Images Volume", Serial, func() {
99114
Expect(err).ToNot(HaveOccurred(), ".completed marker should exist in the volume")
100115

101116
By("Verifying Calico images are stored in the volume")
102-
verifyContainer := "test-images-verify"
103117
_, err = podmanClient.ContainerCreate(ctx, &podman.ContainerCreateOptions{
104-
Name: verifyContainer,
118+
Name: verifyContainerName,
105119
Image: config.DefaultClusterImage,
106120
Command: []string{"sleep", "infinity"},
121+
Labels: map[string]string{
122+
config.LabelComponent: "cluster-images-test-verify",
123+
},
107124
Volumes: []*specgen.NamedVolume{{
108125
Name: volumeName,
109126
Dest: "/var/lib/containers/storage",
110127
}},
111128
})
112129
Expect(err).ToNot(HaveOccurred())
113130

114-
err = podmanClient.ContainerExecQuiet(ctx, verifyContainer,
131+
err = podmanClient.ContainerExecQuiet(ctx, verifyContainerName,
115132
[]string{"podman", "image", "exists", config.CalicoImageBase + "/node:" + config.CalicoVersion})
116133
Expect(err).ToNot(HaveOccurred(), "calico/node image should exist in the volume")
117134

118-
err = podmanClient.ContainerExecQuiet(ctx, verifyContainer,
135+
err = podmanClient.ContainerExecQuiet(ctx, verifyContainerName,
119136
[]string{"podman", "image", "exists", config.CalicoImageBase + "/cni:" + config.CalicoVersion})
120137
Expect(err).ToNot(HaveOccurred(), "calico/cni image should exist in the volume")
121138
})

0 commit comments

Comments
 (0)