diff --git a/smoke/tests/commit_test.go b/smoke/tests/commit_test.go new file mode 100644 index 00000000000..d3d1635b3c2 --- /dev/null +++ b/smoke/tests/commit_test.go @@ -0,0 +1,131 @@ +package tests + +import ( + "encoding/json" + "fmt" + "os" + "testing" + + "github.com/dragonflyoss/nydus/smoke/tests/tool" + "github.com/dragonflyoss/nydus/smoke/tests/tool/test" + "github.com/google/uuid" +) + +type CommitTestSuite struct { + t *testing.T + testImage string + testCommittedImage string + testContainerName string + testCommittedContainerName string + snapshotter string +} + +func (c *CommitTestSuite) TestCommitContainer() { + ctx := tool.DefaultContext(c.t) + c.snapshotter = os.Getenv("SNAPSHOTTER") + if c.snapshotter == "" { + c.snapshotter = "nydus" + } + + // choose test mode + mode := os.Getenv("COMMIT_MODE") + if mode == "" { + mode = "fs-version-6" + } + switch mode { + case "oci": + case "fs-version-5": + ctx.Build.FSVersion = "5" + case "fs-version-6": + ctx.Build.FSVersion = "6" + case "zran": + ctx.Build.OCIRef = true + default: + c.t.Fatalf("commit don't support %s mode", mode) + } + + // prepare commit image + image := os.Getenv("COMMIT_TEST_IMAGE") + if image == "" { + image = "wordpress:6.1.1" + } else { + if !tool.SupportContainerImage(tool.ImageRepo(c.t, image)) { + c.t.Fatalf("commit don't support image " + image) + } + } + _, _ = c.prepareImage(c.t, ctx, image) + + // run contaienr + c.testContainerName = uuid.NewString() + _ = tool.RunContainer(c.t, c.testImage, c.snapshotter, c.testContainerName) + + // make some modifications in the read-write layer for commit action. + modifyCmd := fmt.Sprintf("echo 'This is Nydus commit' > commit && nerdctl cp commit %s:/root/", c.testContainerName) + tool.RunWithoutOutput(c.t, modifyCmd) + + // commit container + commitCmd := fmt.Sprintf("%s commit --container %s --target %s", ctx.Binary.Nydusify, c.testCommittedContainerName, c.testCommittedImage) + tool.RunWithoutOutput(c.t, commitCmd) + + // run committed container + _ = tool.RunContainer(c.t, c.testCommittedImage, c.snapshotter, c.testCommittedContainerName) + + // clear container and image + tool.ClearContainer(c.t, c.testImage, c.snapshotter, c.testContainerName) + tool.ClearContainer(c.t, c.testCommittedImage, c.snapshotter, c.testCommittedContainerName) +} + +func (c *CommitTestSuite) prepareImage(t *testing.T, ctx *tool.Context, image string) (int64, int64) { + if c.testImage != "" { + return 0, 0 + } + + ctx.PrepareWorkDir(t) + defer ctx.Destroy(t) + source := tool.PrepareImage(t, image) + + // Prepare options + target := fmt.Sprintf("%s-nydus-%s", source, uuid.NewString()) + fsVersion := fmt.Sprintf("--fs-version %s", ctx.Build.FSVersion) + logLevel := "--log-level warn" + if ctx.Binary.NydusifyOnlySupportV5 { + fsVersion = "" + logLevel = "" + } + enableOCIRef := "" + if ctx.Build.OCIRef { + enableOCIRef = "--oci-ref" + } + + convertMetricFile := fmt.Sprintf("./%s.json", uuid.NewString()) + // Convert image + convertCmd := fmt.Sprintf("%s %s convert --source %s --target %s --nydus-image %s --work-dir %s %s %s --output-json %s", + ctx.Binary.Nydusify, logLevel, source, target, ctx.Binary.Builder, ctx.Env.WorkDir, fsVersion, enableOCIRef, convertMetricFile) + tool.RunWithoutOutput(t, convertCmd) + defer os.Remove(convertMetricFile) + + metricData, err := os.ReadFile(convertMetricFile) + if err != nil { + t.Fatalf("can't read convert metric file") + return 0, 0 + } + var convertMetirc map[string]int64 + err = json.Unmarshal(metricData, &convertMetirc) + if err != nil { + t.Fatalf("can't parsing convert metric file") + return 0, 0 + } + if c.snapshotter == "nydus" { + c.testImage = target + return convertMetirc["TargetImageSize"], convertMetirc["ConversionElapsed"] + } + c.testImage = source + return convertMetirc["SourceImageSize"], 0 +} + +func TestCommit(t *testing.T) { + if os.Getenv("COMMIT_TEST") == "" { + t.Skip("skipping commit test") + } + test.Run(t, &CommitTestSuite{t: t}) +}