Skip to content

Commit

Permalink
feat: add smoke test for nydusify commit
Browse files Browse the repository at this point in the history
Signed-off-by: YuQiang <y_q_email@163.com>
  • Loading branch information
PerseidMeteor committed Mar 5, 2024
1 parent 3f86600 commit b096c02
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions smoke/tests/commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package tests

import (
"fmt"
"io"
"os"
"strings"
"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
}

func (c *CommitTestSuite) TestCommitContainer() test.Generator {
scenarios := tool.DescartesIterator{}
scenarios.
Dimension(paramImage, []interface{}{"ubuntu:latest"}).
Dimension(paramFSVersion, []interface{}{"6"}).
Dimension(paramZran, []interface{}{false, true})

return func() (name string, testCase test.Case) {
if !scenarios.HasNext() {
return
}
scenario := scenarios.Next()
ctx := tool.DefaultContext(c.t)
ctx.Build.FSVersion = scenario.GetString(paramFSVersion)
ctx.Build.OCIRef = scenario.GetBool(paramZran)

image, committedImage := c.prepareImage(c.t, ctx, scenario.GetString(paramImage))
return scenario.Str(), func(t *testing.T) {
c.TestCommitAndCheck(*ctx, image, committedImage)
}
}
}

func (c *CommitTestSuite) TestCommitAndCheck(ctx tool.Context, image, commmitedImage string) {

// run nydus contaienr
containerName := uuid.NewString()
runContainerCmd := fmt.Sprintf("sudo nerdctl --snapshotter nydus run -d -t --insecure-registry --name=%s %s sh", containerName, image)
containerID := strings.Trim(tool.RunWithOutput(runContainerCmd), "\n")

// make some modifications in the read-write layer for commit action.
modifyCmd := fmt.Sprintf("echo \"This is Nydus commit\" > commit && sudo nerdctl cp commit %s:/root/", containerID)
tool.RunWithoutOutput(c.t, modifyCmd)
defer os.Remove("./commit")

// commit container
committedContainerName := fmt.Sprintf("%s-committed", containerName)

commitCmd := fmt.Sprintf("sudo %s commit --container %s --target %s", ctx.Binary.Nydusify, containerID, commmitedImage)
tool.RunWithoutOutput(c.t, commitCmd)
// run committed container
runCommittedContainerCmd := fmt.Sprintf("sudo nerdctl --snapshotter nydus run -d -t --insecure-registry --name=%s %s sh", committedContainerName, commmitedImage)
committedContainerID := strings.Trim(tool.RunWithOutput(runCommittedContainerCmd), "\n")

// check committed file content
checkCmd := fmt.Sprintf("sudo nerdctl cp %s:/root/commit container_commit", committedContainerID)
tool.RunWithoutOutput(c.t, checkCmd)

file, err := os.Open("./container_commit")
if err != nil {
c.t.Fatalf("open commit file failed: %s", err)
}
defer file.Close()
defer os.Remove(file.Name())

content, err := io.ReadAll(file)
if err != nil {
c.t.Fatalf("can not read commit file: %s", err)
}
fileContent := strings.Trim(string(content), "\n")
if fileContent != "This is Nydus commit" {
c.t.Fatalf("commit file content mismatched, expected \"This is Nydus commit\", found \"%s\"", fileContent)
}

// clear container
tool.ClearContainer(c.t, image, "nydus", containerName)
tool.ClearContainer(c.t, commmitedImage, "nydus", committedContainerName)
}

func (c *CommitTestSuite) prepareImage(t *testing.T, ctx *tool.Context, image string) (string, string) {

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"
}
// convert image
convertCmd := fmt.Sprintf("%s %s convert --source %s --target %s --nydus-image %s --work-dir %s %s %s",
ctx.Binary.Nydusify, logLevel, source, target, ctx.Binary.Builder, ctx.Env.WorkDir, fsVersion, enableOCIRef)
tool.RunWithoutOutput(t, convertCmd)

return target, fmt.Sprintf("%s-committed", target)
}

func TestCommit(t *testing.T) {
test.Run(t, &CommitTestSuite{t: t})
}

0 comments on commit b096c02

Please sign in to comment.