Skip to content

Commit 8252ca5

Browse files
committed
test(e2e): migrate nerdctl tests to Ginkgo framework
Signed-off-by: Irving Mondragón <mirvingr@gmail.com>
1 parent a0c47bc commit 8252ca5

6 files changed

Lines changed: 182 additions & 39 deletions

File tree

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ test_hypervisors:
265265
.PHONY: test_nerdctl
266266
test_nerdctl:
267267
@echo "Testing nerdctl"
268-
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -run TestNerdctl -v
268+
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -run TestE2E -v --ginkgo.v --ginkgo.focus="Nerdctl"
269269
@echo " "
270270

271271
## test_ctr Run all end-to-end tests with ctr
272272
.PHONY: test_ctr
273273
test_ctr:
274274
@echo "Testing ctr"
275-
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -run TestCtr -v --ginkgo.v
275+
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -run TestE2E -v --ginkgo.v --ginkgo.focus="Ctr"
276276
@echo " "
277277

278278
## test_crictl Run all end-to-end tests with crictl
@@ -293,14 +293,14 @@ test_docker:
293293
.PHONY: test_nerdctl_%
294294
test_nerdctl_%:
295295
@echo "Testing nerdctl"
296-
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -v -run "TestNerdctl/$*"
296+
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -v --ginkgo.v -run TestE2E --ginkgo.focus="Nerdctl.*$*"
297297
@echo " "
298298

299299
## test_ctr_[pattern] Run all end-to-end tests with ctr that match pattern
300300
.PHONY: test_ctr_%
301301
test_ctr_%:
302302
@echo "Testing ctr"
303-
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -v --ginkgo.v -run TestCtr --ginkgo.focus="$*"
303+
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./tests/e2e -v --ginkgo.v -run TestE2E --ginkgo.focus="Ctr.*$*"
304304
@echo " "
305305

306306
## test_crictl_[pattern] Run all end-to-end tests with crictl that match pattern

tests/e2e/ctr_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import (
2525
const testCtr = "TestCtr"
2626

2727
var _ = Describe("Ctr", Ordered, ContinueOnFailure, func() {
28-
var (
29-
tool *ctrInfo
30-
cwd string
31-
)
28+
var tool *ctrInfo
3229

3330
BeforeAll(func() {
3431
cases := ctrTestCases()
@@ -42,15 +39,7 @@ var _ = Describe("Ctr", Ordered, ContinueOnFailure, func() {
4239
})
4340

4441
BeforeEach(func() {
45-
var err error
46-
cwd, err = os.Getwd()
47-
Expect(err).NotTo(HaveOccurred())
48-
testDir := GinkgoT().TempDir()
49-
Expect(os.Chdir(testDir)).To(Succeed())
50-
51-
DeferCleanup(func() {
52-
Expect(os.Chdir(cwd)).To(Succeed())
53-
})
42+
setupTestDir()
5443
})
5544

5645
ReportAfterEach(func(report SpecReport) {

tests/e2e/e2e_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ import (
1818
"testing"
1919
)
2020

21-
func TestNerdctl(t *testing.T) {
22-
kvmGroup, err := getKVMGroupID()
23-
if err != nil {
24-
t.Errorf("Failed to get KVM grou id")
25-
}
26-
tests := nerdctlTestCases(kvmGroup)
27-
for _, tc := range tests {
28-
t.Run(tc.Name, func(t *testing.T) {
29-
nerdctlTool := newNerdctlTool(tc)
30-
runTest(nerdctlTool, t)
31-
})
32-
}
33-
}
34-
3521
func TestCrictl(t *testing.T) {
3622
kvmGroup, err := getKVMGroupID()
3723
if err != nil {

tests/e2e/nerdctl_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package urunce2etesting
16+
17+
import (
18+
"fmt"
19+
"os"
20+
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
const testNerdctl = "TestNerdctl"
26+
27+
var _ = Describe("Nerdctl", Ordered, ContinueOnFailure, func() {
28+
var (
29+
tool *nerdctlInfo
30+
kvmGroup int64
31+
)
32+
33+
BeforeAll(func() {
34+
var err error
35+
kvmGroup, err = getKVMGroupID()
36+
if err != nil {
37+
GinkgoLogr.Error(err, "Failed to get KVM group ID")
38+
}
39+
40+
cases := nerdctlTestCases(kvmGroup)
41+
images := getTestImages(cases)
42+
err = pullAllImages(testNerdctl, images)
43+
Expect(err).NotTo(HaveOccurred(), "Failed to pull nerdctl images")
44+
45+
DeferCleanup(func() {
46+
removeAllImages(testNerdctl, images)
47+
})
48+
})
49+
50+
BeforeEach(func() {
51+
setupTestDir()
52+
})
53+
54+
ReportAfterEach(func(report SpecReport) {
55+
if report.Failed() && tool != nil {
56+
AddReportEntry("test-args", tool.getTestArgs())
57+
}
58+
})
59+
60+
Context("foreground containers", func() {
61+
DescribeTable("unikernel containers",
62+
func(tc containerTestArgs) {
63+
for _, vol := range tc.Volumes {
64+
if _, err := os.Stat(vol.Source); err != nil {
65+
Skip(fmt.Sprintf("Could not find %s", vol.Source))
66+
}
67+
}
68+
69+
tool = newNerdctlTool(tc)
70+
tool.setContainerID(tc.Name)
71+
72+
DeferCleanup(func() {
73+
if tool != nil && tool.getContainerID() != "" {
74+
By("Cleaning up container")
75+
if err := testCleanup(tool); err != nil {
76+
GinkgoLogr.Error(err, "Container cleanup failed")
77+
}
78+
}
79+
})
80+
81+
By("Running container")
82+
output, err := tool.runContainer(false)
83+
Expect(err).NotTo(HaveOccurred(), "Failed to run container: %s", output)
84+
85+
By("Verifying container output")
86+
Expect(output).To(ContainSubstring(tc.ExpectOut))
87+
},
88+
toTableEntries(selectTestCases(nerdctlTestCases(), false)),
89+
)
90+
})
91+
92+
Context("detached containers", func() {
93+
DescribeTable("unikernel containers",
94+
func(tc containerTestArgs) {
95+
for _, vol := range tc.Volumes {
96+
if _, err := os.Stat(vol.Source); err != nil {
97+
Skip(fmt.Sprintf("Could not find %s", vol.Source))
98+
}
99+
}
100+
101+
// Inject actual kvmGroup (table entries built before BeforeAll with group=0)
102+
if len(tc.Groups) > 0 {
103+
tc.Groups = []int64{kvmGroup}
104+
}
105+
106+
tool = newNerdctlTool(tc)
107+
108+
By("Creating container")
109+
cID, err := tool.createContainer()
110+
Expect(err).NotTo(HaveOccurred(), "Failed to create container: %s", cID)
111+
tool.setContainerID(cID)
112+
113+
DeferCleanup(func() {
114+
if tool != nil && tool.getContainerID() != "" {
115+
By("Stopping container")
116+
if err := tool.stopContainer(); err != nil {
117+
GinkgoLogr.Error(err, "Failed to stop container")
118+
}
119+
By("Removing container")
120+
if err := tool.rmContainer(); err != nil {
121+
GinkgoLogr.Error(err, "Failed to remove container")
122+
}
123+
By("Verifying container removal")
124+
if err := testVerifyRm(tool); err != nil {
125+
GinkgoLogr.Error(err, "Failed to verify removal")
126+
}
127+
}
128+
})
129+
130+
By("Starting container")
131+
output, err := tool.startContainer(true)
132+
Expect(err).NotTo(HaveOccurred(), "Failed to start container: %s", output)
133+
134+
By("Running test function")
135+
Eventually(func() error {
136+
return tc.TestFunc(tool)
137+
}, defaultTimeout, defaultInterval).Should(Succeed())
138+
},
139+
toTableEntries(selectTestCases(nerdctlTestCases(), true)),
140+
)
141+
})
142+
})

tests/e2e/setup_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
const (
28-
testNerdctl = "TestNerdctl"
28+
testE2E = "TestE2E"
2929
testCrictl = "TestCrictl"
3030
testDocker = "TestDocker"
3131
maxPullRetries = 5
@@ -68,18 +68,15 @@ func parseTestPattern() (string, string) {
6868

6969
func getTestCases(testFunc string) []containerTestArgs {
7070
switch testFunc {
71-
case testNerdctl:
72-
return nerdctlTestCases()
73-
case testCtr:
74-
// Images managed by BeforeAll in ctr_test.go
71+
case testE2E:
72+
// Images managed by BeforeAll in Ginkgo specs
7573
return []containerTestArgs{}
7674
case testCrictl:
7775
return crictlTestCases()
7876
case testDocker:
7977
return dockerTestCases()
8078
default:
8179
var allCases []containerTestArgs
82-
allCases = append(allCases, nerdctlTestCases()...)
8380
allCases = append(allCases, crictlTestCases()...)
8481
allCases = append(allCases, dockerTestCases()...)
8582
return allCases

tests/e2e/suite_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,35 @@
1515
package urunce2etesting
1616

1717
import (
18+
"os"
1819
"testing"
20+
"time"
1921

2022
. "github.com/onsi/ginkgo/v2"
2123
. "github.com/onsi/gomega"
2224
"github.com/onsi/gomega/format"
2325
)
2426

25-
func TestCtr(t *testing.T) {
27+
const (
28+
eventuallyTimeout = 10 * time.Second
29+
eventuallyInterval = 1 * time.Second
30+
)
31+
32+
func TestE2E(t *testing.T) {
2633
format.MaxLength = 0 // Do not truncate failure output
2734
RegisterFailHandler(Fail)
28-
RunSpecs(t, "Ctr E2E Suite")
35+
RunSpecs(t, "E2E Suite")
36+
}
37+
38+
// setupTestDir switches to a temp directory, restored via DeferCleanup.
39+
func setupTestDir() {
40+
cwd, err := os.Getwd()
41+
Expect(err).NotTo(HaveOccurred())
42+
testDir := GinkgoT().TempDir()
43+
Expect(os.Chdir(testDir)).To(Succeed())
44+
DeferCleanup(func() {
45+
Expect(os.Chdir(cwd)).To(Succeed())
46+
})
2947
}
3048

3149
// toTableEntries converts a slice of containerTestArgs into Ginkgo TableEntry
@@ -37,3 +55,14 @@ func toTableEntries(cases []containerTestArgs) []TableEntry {
3755
}
3856
return entries
3957
}
58+
59+
// selectTestCases returns cases with or without a TestFunc.
60+
func selectTestCases(cases []containerTestArgs, hasTestFunc bool) []containerTestArgs {
61+
var out []containerTestArgs
62+
for _, tc := range cases {
63+
if (tc.TestFunc != nil) == hasTestFunc {
64+
out = append(out, tc)
65+
}
66+
}
67+
return out
68+
}

0 commit comments

Comments
 (0)