|
| 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 | +}) |
0 commit comments