Skip to content

Commit a78bdf2

Browse files
committed
tests: Refactors PodSandbox creation
Most of the tests creating and using Pod Sandboxes in the same way. We can create a common function that will do that for us, so we can have less duplicated code, and make it easier to add new tests in the future. Signed-off-by: Claudiu Belu <[email protected]>
1 parent ada96ec commit a78bdf2

17 files changed

Lines changed: 56 additions & 175 deletions

integration/addition_gids_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,8 @@ func TestAdditionalGids(t *testing.T) {
3636
defer os.RemoveAll(testPodLogDir)
3737

3838
t.Log("Create a sandbox with log directory")
39-
sbConfig := PodSandboxConfig("sandbox", "additional-gids",
39+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "additional-gids",
4040
WithPodLogDirectory(testPodLogDir))
41-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
42-
require.NoError(t, err)
43-
defer func() {
44-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
45-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
46-
}()
4741

4842
var (
4943
testImage = GetImage(BusyBox)

integration/container_log_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ func TestContainerLogWithoutTailingNewLine(t *testing.T) {
3636
defer os.RemoveAll(testPodLogDir)
3737

3838
t.Log("Create a sandbox with log directory")
39-
sbConfig := PodSandboxConfig("sandbox", "container-log-without-tailing-newline",
39+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "container-log-without-tailing-newline",
4040
WithPodLogDirectory(testPodLogDir),
4141
)
42-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
43-
require.NoError(t, err)
44-
defer func() {
45-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
46-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
47-
}()
4842

4943
var (
5044
testImage = GetImage(BusyBox)
@@ -92,15 +86,9 @@ func TestLongContainerLog(t *testing.T) {
9286
defer os.RemoveAll(testPodLogDir)
9387

9488
t.Log("Create a sandbox with log directory")
95-
sbConfig := PodSandboxConfig("sandbox", "long-container-log",
89+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "long-container-log",
9690
WithPodLogDirectory(testPodLogDir),
9791
)
98-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
99-
require.NoError(t, err)
100-
defer func() {
101-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
102-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
103-
}()
10492

10593
var (
10694
testImage = GetImage(BusyBox)

integration/container_restart_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ import (
2626
// Test to verify container can be restarted
2727
func TestContainerRestart(t *testing.T) {
2828
t.Logf("Create a pod config and run sandbox container")
29-
sbConfig := PodSandboxConfig("sandbox1", "restart")
30-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
31-
require.NoError(t, err)
32-
defer func() {
33-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
34-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
35-
}()
29+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox1", "restart")
3630

3731
EnsureImageExists(t, pauseImage)
3832

integration/container_stats_test.go

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ import (
3131
// Test to verify for a container ID
3232
func TestContainerStats(t *testing.T) {
3333
t.Logf("Create a pod config and run sandbox container")
34-
sbConfig := PodSandboxConfig("sandbox1", "stats")
35-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
36-
require.NoError(t, err)
37-
defer func() {
38-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
39-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
40-
}()
34+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox1", "stats")
4135

4236
EnsureImageExists(t, pauseImage)
4337

@@ -78,13 +72,7 @@ func TestContainerStats(t *testing.T) {
7872
// Test to verify if the consumed stats are correct.
7973
func TestContainerConsumedStats(t *testing.T) {
8074
t.Logf("Create a pod config and run sandbox container")
81-
sbConfig := PodSandboxConfig("sandbox1", "stats")
82-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
83-
require.NoError(t, err)
84-
defer func() {
85-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
86-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
87-
}()
75+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox1", "stats")
8876

8977
testImage := GetImage(ResourceConsumer)
9078
img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil, sbConfig)
@@ -154,14 +142,12 @@ func TestContainerConsumedStats(t *testing.T) {
154142

155143
// Test to verify filtering without any filter
156144
func TestContainerListStats(t *testing.T) {
145+
var (
146+
stats []*runtime.ContainerStats
147+
err error
148+
)
157149
t.Logf("Create a pod config and run sandbox container")
158-
sbConfig := PodSandboxConfig("running-pod", "statsls")
159-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
160-
require.NoError(t, err)
161-
defer func() {
162-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
163-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
164-
}()
150+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "running-pod", "statsls")
165151

166152
EnsureImageExists(t, pauseImage)
167153

@@ -188,7 +174,6 @@ func TestContainerListStats(t *testing.T) {
188174
}
189175

190176
t.Logf("Fetch all container stats")
191-
var stats []*runtime.ContainerStats
192177
require.NoError(t, Eventually(func() (bool, error) {
193178
stats, err = runtimeService.ListContainerStats(&runtime.ContainerStatsFilter{})
194179
if err != nil {
@@ -211,14 +196,12 @@ func TestContainerListStats(t *testing.T) {
211196
// Test to verify filtering given a specific container ID
212197
// TODO Convert the filter tests into table driven tests and unit tests
213198
func TestContainerListStatsWithIdFilter(t *testing.T) {
199+
var (
200+
stats []*runtime.ContainerStats
201+
err error
202+
)
214203
t.Logf("Create a pod config and run sandbox container")
215-
sbConfig := PodSandboxConfig("running-pod", "statsls")
216-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
217-
require.NoError(t, err)
218-
defer func() {
219-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
220-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
221-
}()
204+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "running-pod", "statsls")
222205

223206
EnsureImageExists(t, pauseImage)
224207

@@ -245,7 +228,6 @@ func TestContainerListStatsWithIdFilter(t *testing.T) {
245228
}
246229

247230
t.Logf("Fetch container stats for each container with Filter")
248-
var stats []*runtime.ContainerStats
249231
for id := range containerConfigMap {
250232
require.NoError(t, Eventually(func() (bool, error) {
251233
stats, err = runtimeService.ListContainerStats(
@@ -273,14 +255,12 @@ func TestContainerListStatsWithIdFilter(t *testing.T) {
273255
// Test to verify filtering given a specific Sandbox ID. Stats for
274256
// all the containers in a pod should be returned
275257
func TestContainerListStatsWithSandboxIdFilter(t *testing.T) {
258+
var (
259+
stats []*runtime.ContainerStats
260+
err error
261+
)
276262
t.Logf("Create a pod config and run sandbox container")
277-
sbConfig := PodSandboxConfig("running-pod", "statsls")
278-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
279-
require.NoError(t, err)
280-
defer func() {
281-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
282-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
283-
}()
263+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "running-pod", "statsls")
284264

285265
EnsureImageExists(t, pauseImage)
286266

@@ -307,7 +287,6 @@ func TestContainerListStatsWithSandboxIdFilter(t *testing.T) {
307287
}
308288

309289
t.Logf("Fetch container stats for each container with Filter")
310-
var stats []*runtime.ContainerStats
311290
require.NoError(t, Eventually(func() (bool, error) {
312291
stats, err = runtimeService.ListContainerStats(
313292
&runtime.ContainerStatsFilter{PodSandboxId: sb})
@@ -332,14 +311,12 @@ func TestContainerListStatsWithSandboxIdFilter(t *testing.T) {
332311
// Test to verify filtering given a specific container ID and
333312
// sandbox ID
334313
func TestContainerListStatsWithIdSandboxIdFilter(t *testing.T) {
314+
var (
315+
stats []*runtime.ContainerStats
316+
err error
317+
)
335318
t.Logf("Create a pod config and run sandbox container")
336-
sbConfig := PodSandboxConfig("running-pod", "statsls")
337-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
338-
require.NoError(t, err)
339-
defer func() {
340-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
341-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
342-
}()
319+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "running-pod", "statsls")
343320

344321
EnsureImageExists(t, pauseImage)
345322

@@ -365,7 +342,6 @@ func TestContainerListStatsWithIdSandboxIdFilter(t *testing.T) {
365342
}()
366343
}
367344
t.Logf("Fetch container stats for sandbox ID and container ID filter")
368-
var stats []*runtime.ContainerStats
369345
for id, config := range containerConfigMap {
370346
require.NoError(t, Eventually(func() (bool, error) {
371347
stats, err = runtimeService.ListContainerStats(

integration/container_stop_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ func TestContainerStopCancellation(t *testing.T) {
7676
t.Skip("Skipped on Windows.")
7777
}
7878
t.Log("Create a pod sandbox")
79-
sbConfig := PodSandboxConfig("sandbox", "cancel-container-stop")
80-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
81-
require.NoError(t, err)
82-
defer func() {
83-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
84-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
85-
}()
79+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "cancel-container-stop")
8680

8781
var (
8882
testImage = GetImage(BusyBox)

integration/container_update_resources_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ func checkMemoryLimit(t *testing.T, spec *runtimespec.Spec, memLimit int64) {
4141
func TestUpdateContainerResources(t *testing.T) {
4242
// TODO(claudiub): Make this test work once https://github.com/microsoft/hcsshim/pull/931 merges.
4343
t.Log("Create a sandbox")
44-
sbConfig := PodSandboxConfig("sandbox", "update-container-resources")
45-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
46-
require.NoError(t, err)
47-
defer func() {
48-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
49-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
50-
}()
44+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "update-container-resources")
5145

5246
EnsureImageExists(t, pauseImage)
5347

integration/container_volume_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,9 @@ func TestContainerSymlinkVolumes(t *testing.T) {
9999
require.NoError(t, err)
100100

101101
t.Log("Create test sandbox with log directory")
102-
sbConfig := PodSandboxConfig("sandbox", "test-symlink",
102+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "test-symlink",
103103
WithPodLogDirectory(testPodLogDir),
104104
)
105-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
106-
require.NoError(t, err)
107-
defer func() {
108-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
109-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
110-
}()
111105

112106
var (
113107
testImage = GetImage(BusyBox)

integration/container_without_image_ref_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ import (
2727
// Test container lifecycle can work without image references.
2828
func TestContainerLifecycleWithoutImageRef(t *testing.T) {
2929
t.Log("Create a sandbox")
30-
sbConfig := PodSandboxConfig("sandbox", "container-lifecycle-without-image-ref")
31-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
32-
require.NoError(t, err)
33-
defer func() {
34-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
35-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
36-
}()
30+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "container-lifecycle-without-image-ref")
3731

3832
var (
3933
testImage = GetImage(BusyBox)

integration/containerd_image_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,7 @@ func TestContainerdImage(t *testing.T) {
122122
assert.Equal(t, imgByID.Labels()["io.cri-containerd.image"], "managed")
123123

124124
t.Logf("should be able to start container with the image")
125-
sbConfig := PodSandboxConfig("sandbox", "containerd-image")
126-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
127-
require.NoError(t, err)
128-
defer func() {
129-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
130-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
131-
}()
125+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "containerd-image")
132126

133127
cnConfig := ContainerConfig(
134128
"test-container",
@@ -183,7 +177,7 @@ func TestContainerdImageInOtherNamespaces(t *testing.T) {
183177
}
184178
require.NoError(t, Consistently(checkImage, 100*time.Millisecond, time.Second))
185179

186-
PodSandboxConfig("sandbox", "test")
180+
PodSandboxConfigWithCleanup(t, "sandbox", "test")
187181
EnsureImageExists(t, testImage)
188182

189183
t.Logf("cri plugin should see the image now")

integration/duplicate_name_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,15 @@ package integration
1919
import (
2020
"testing"
2121

22-
"github.com/stretchr/testify/assert"
2322
"github.com/stretchr/testify/require"
2423
)
2524

2625
func TestDuplicateName(t *testing.T) {
2726
t.Logf("Create a sandbox")
28-
sbConfig := PodSandboxConfig("sandbox", "duplicate-name")
29-
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
30-
require.NoError(t, err)
31-
defer func() {
32-
assert.NoError(t, runtimeService.StopPodSandbox(sb))
33-
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
34-
}()
27+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "duplicate-name")
3528

3629
t.Logf("Create the sandbox again should fail")
37-
_, err = runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
30+
_, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
3831
require.Error(t, err)
3932

4033
EnsureImageExists(t, pauseImage)

0 commit comments

Comments
 (0)