-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add smoke test for nydusify commit
Signed-off-by: YuQiang <y_q_email@163.com>
- Loading branch information
1 parent
f132530
commit 4455f1b
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"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 | ||
} | ||
|
||
func (c *CommitTestSuite) TestCommitContainer() test.Generator { | ||
scenarios := tool.DescartesIterator{} | ||
scenarios. | ||
Dimension(paramImage, []interface{}{"nginx:latest"}). | ||
Dimension(paramFSVersion, []interface{}{"5", "6"}) | ||
|
||
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() | ||
_ = tool.RunContainer(c.t, image, "nydus", containerName) | ||
|
||
// 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/", containerName) | ||
tool.RunWithoutOutput(c.t, modifyCmd) | ||
|
||
// commit container | ||
committedContainerName := fmt.Sprintf("%s-committed", containerName) | ||
commitCmd := fmt.Sprintf("%s commit --container %s --target %s", ctx.Binary.Nydusify, committedContainerName, commmitedImage) | ||
tool.RunWithoutOutput(c.t, commitCmd) | ||
|
||
// run committed container | ||
_ = tool.RunContainer(c.t, commmitedImage, "nydus", committedContainerName) | ||
|
||
// check committed file content | ||
checkCmd := fmt.Sprintf("nerdctl cp %s:/root/commit commit", committedContainerName) | ||
tool.RunWithoutOutput(c.t, checkCmd) | ||
|
||
file, err := os.Open("./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 := string(content) | ||
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}) | ||
} |