-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tarfs: add unit test cases for tarfs
Add unit test cases for tarfs. Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
- Loading branch information
Showing
5 changed files
with
281 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
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,50 @@ | ||
/* | ||
* Copyright (c) 2023. Nydus Developers. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package daemon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/containerd/nydus-snapshotter/config" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func TestConfigOptions(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
opts := []NewDaemonOpt{ | ||
WithSocketDir("/tmp/socket"), | ||
WithRef(5), | ||
WithLogDir(tmpDir), | ||
WithLogToStdout(true), | ||
WithLogLevel("Warning"), | ||
WithLogRotationSize(1024), | ||
WithConfigDir(tmpDir), | ||
WithMountpoint("/tmp/mnt"), | ||
WithNydusdThreadNum(4), | ||
WithFsDriver("fscache"), | ||
WithDaemonMode("dedicated"), | ||
} | ||
|
||
daemon, err := NewDaemon(opts...) | ||
assert.Assert(t, err) | ||
assert.Equal(t, daemon.States.APISocket, "/tmp/socket/"+daemon.ID()+"/api.sock") | ||
assert.Equal(t, daemon.ref, int32(5)) | ||
assert.Equal(t, daemon.States.LogDir, tmpDir+"/"+daemon.ID()) | ||
assert.Equal(t, daemon.States.LogToStdout, true) | ||
assert.Equal(t, daemon.States.LogLevel, "Warning") | ||
assert.Equal(t, daemon.States.LogRotationSize, 1024) | ||
assert.Equal(t, daemon.States.ConfigDir, tmpDir+"/"+daemon.ID()) | ||
assert.Equal(t, daemon.States.Mountpoint, "/tmp/mnt") | ||
assert.Equal(t, daemon.States.ThreadNum, 4) | ||
assert.Equal(t, daemon.States.FsDriver, "fscache") | ||
assert.Equal(t, string(daemon.States.DaemonMode), "dedicated") | ||
|
||
} | ||
|
||
func String(daemonMode config.DaemonMode) { | ||
panic("unimplemented") | ||
} |
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,22 @@ | ||
/* | ||
* Copyright (c) 2023. Nydus Developers. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package daemon | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
func TestIdGenerate(t *testing.T) { | ||
id1 := newID() | ||
id2 := newID() | ||
|
||
assert.Assert(t, len(id1) > 0) | ||
assert.Assert(t, len(id2) > 0) | ||
assert.Assert(t, id1 != id2) | ||
} |
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,66 @@ | ||
/* | ||
* Copyright (c) 2022. Nydus Developers. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package rafs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/containerd/nydus-snapshotter/config" | ||
"github.com/containerd/nydus-snapshotter/internal/constant" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func TestRafsSetEmpty(t *testing.T) { | ||
cache := NewRafsCache() | ||
|
||
assert.Assert(t, cache.Get("rafs1") == nil) | ||
assert.Equal(t, cache.Len(), 0) | ||
assert.Assert(t, cache.Head() == nil) | ||
instances := cache.List() | ||
assert.Equal(t, len(instances), 0) | ||
} | ||
|
||
func TestRafs(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
snapshotterConfig := config.SnapshotterConfig{} | ||
snapshotterConfig.Root = tmpDir | ||
snapshotterConfig.DaemonMode = constant.DaemonModeDedicated | ||
assert.Assert(t, config.ProcessConfigurations(&snapshotterConfig)) | ||
|
||
rafs, err := NewRafs("snapshot1", "image1", "fscache") | ||
assert.Assert(t, err) | ||
assert.Equal(t, rafs, RafsGlobalCache.Get("snapshot1")) | ||
assert.Equal(t, RafsGlobalCache.Len(), 1) | ||
assert.Equal(t, rafs, RafsGlobalCache.Head()) | ||
instances := RafsGlobalCache.List() | ||
assert.Equal(t, len(instances), 1) | ||
assert.Equal(t, instances["snapshot1"].SnapshotID, "snapshot1") | ||
|
||
RafsGlobalCache.Lock() | ||
instances2 := RafsGlobalCache.ListLocked() | ||
RafsGlobalCache.Unlock() | ||
assert.Equal(t, len(instances2), 1) | ||
|
||
RafsGlobalCache.SetIntances(instances) | ||
assert.Equal(t, RafsGlobalCache.Len(), 1) | ||
assert.Equal(t, RafsGlobalCache.Head().SnapshotID, "snapshot1") | ||
|
||
assert.Equal(t, len(rafs.Annotations), 0) | ||
rafs.AddAnnotation("key", "value") | ||
assert.Equal(t, len(rafs.Annotations), 1) | ||
assert.Equal(t, rafs.GetSnapshotDir(), tmpDir+"/snapshots/snapshot1") | ||
assert.Equal(t, rafs.RelaMountpoint(), "/snapshot1") | ||
assert.Equal(t, rafs.FscacheWorkDir(), tmpDir+"/snapshots/snapshot1/fs") | ||
assert.Equal(t, rafs.GetFsDriver(), "fscache") | ||
rafs.SetMountpoint("/tmp/mnt") | ||
assert.Equal(t, rafs.GetMountpoint(), "/tmp/mnt") | ||
_, err = rafs.BootstrapFile() | ||
assert.Assert(t, err != nil) | ||
|
||
RafsGlobalCache.Remove("snapshot1") | ||
assert.Equal(t, RafsGlobalCache.Len(), 0) | ||
} |
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,127 @@ | ||
/* | ||
* Copyright (c) 2023. Nydus Developers. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package tarfs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/containerd/continuity/testutil" | ||
"github.com/containerd/nydus-snapshotter/pkg/label" | ||
"github.com/containerd/nydus-snapshotter/pkg/rafs" | ||
"github.com/opencontainers/go-digest" | ||
"gotest.tools/assert" | ||
) | ||
|
||
const ( | ||
BusyboxRef = "quay.io/quay/busybox@sha256:92f3298bf80a1ba949140d77987f5de081f010337880cd771f7e7fc928f8c74d" | ||
BusyboxManifestDigest = "sha256:92f3298bf80a1ba949140d77987f5de081f010337880cd771f7e7fc928f8c74d" | ||
BusyboxLayerDigest = "sha256:ee780d08a5b4de5192a526d422987f451d9a065e6da42aefe8c3b20023a250c7" | ||
NydusImagePath = "nydus-image" | ||
) | ||
|
||
// TODO: add unit test for MergeLayers, ExportBlockData, MountErofs, RemountErofs, UmountTarErofs, DetachLayer, | ||
// RecoverSnapshoInfo, RecoverRafsInstance, getImageBlobInfo | ||
|
||
func TestPrepareLayer(t *testing.T) { | ||
manager := NewManager(true, true, t.TempDir(), NydusImagePath, 4) | ||
manifestDigest, err := digest.Parse(BusyboxManifestDigest) | ||
assert.Assert(t, err) | ||
layerDigest, err := digest.Parse(BusyboxLayerDigest) | ||
assert.Assert(t, err) | ||
|
||
err = manager.PrepareLayer("snapshot1", BusyboxRef, manifestDigest, layerDigest, t.TempDir()) | ||
assert.Assert(t, err) | ||
|
||
snapshot, err := manager.waitLayerReady("snapshot1", true) | ||
// tarfs_test.go:36: assertion failed: error is not nil: generate tarfs from image layer blob: converting OCIv1 layer blob to tarfs: exec: "nydus-image": executable file not found in $PATH | ||
assert.Assert(t, err != nil) | ||
if err == nil { | ||
assert.Equal(t, snapshot.blobID, "ee780d08a5b4de5192a526d422987f451d9a065e6da42aefe8c3b20023a250c7") | ||
} | ||
|
||
err = manager.PrepareLayer("snapshot1", BusyboxRef, manifestDigest, layerDigest, t.TempDir()) | ||
assert.Assert(t, err != nil) | ||
} | ||
|
||
func TestBlobProcess(t *testing.T) { | ||
manager := NewManager(true, true, t.TempDir(), NydusImagePath, 4) | ||
manifestDigest, err := digest.Parse(BusyboxManifestDigest) | ||
assert.Assert(t, err) | ||
layerDigest, err := digest.Parse(BusyboxLayerDigest) | ||
assert.Assert(t, err) | ||
|
||
err = manager.blobProcess(context.Background(), "snapshot2", BusyboxRef, manifestDigest, layerDigest, t.TempDir(), true) | ||
assert.Assert(t, err != nil) | ||
} | ||
|
||
func TestCheckTarfsHintAnnotation(t *testing.T) { | ||
manager := NewManager(true, true, t.TempDir(), NydusImagePath, 4) | ||
ctx := context.Background() | ||
hint, err := manager.CheckTarfsHintAnnotation(ctx, BusyboxRef, BusyboxManifestDigest) | ||
assert.Assert(t, err) | ||
assert.Equal(t, hint, false) | ||
} | ||
|
||
func TestGetConcurrentLimiter(t *testing.T) { | ||
manager := NewManager(false, false, t.TempDir(), NydusImagePath, 4) | ||
limiter := manager.GetConcurrentLimiter("busybox") | ||
assert.Assert(t, limiter != nil) | ||
assert.Equal(t, manager.GetConcurrentLimiter("busybox"), limiter) | ||
|
||
} | ||
|
||
func TestCopyTarfsAnnotations(t *testing.T) { | ||
manager := NewManager(false, false, t.TempDir(), NydusImagePath, 4) | ||
rafs := &rafs.Rafs{ | ||
Annotations: make(map[string]string), | ||
} | ||
|
||
annotations := map[string]string{} | ||
annotations[label.CRIImageRef] = "cri_image_ref" | ||
annotations[label.CRILayerDigest] = "cri_layer_digest" | ||
annotations[label.CRIManifestDigest] = "cri_manigest_digest" | ||
annotations[label.NydusTarfsLayer] = "nydus_tarfs_layer" | ||
annotations[label.NydusImageBlockInfo] = "nydus_image_block_info" | ||
annotations[label.NydusLayerBlockInfo] = "nydus_layer_block_info" | ||
annotations["unsupported_key"] = "error" | ||
|
||
manager.copyTarfsAnnotations(annotations, rafs) | ||
assert.Equal(t, len(rafs.Annotations), 6) | ||
assert.Equal(t, rafs.Annotations[label.CRIImageRef], annotations[label.CRIImageRef]) | ||
assert.Equal(t, rafs.Annotations[label.CRILayerDigest], annotations[label.CRILayerDigest]) | ||
} | ||
|
||
func TestTarfsFilePath(t *testing.T) { | ||
manager := NewManager(false, false, "/tmp/tarfs", NydusImagePath, 4) | ||
|
||
assert.Equal(t, manager.layerTarFilePath("blob1"), "/tmp/tarfs/blob1") | ||
assert.Equal(t, manager.layerDiskFilePath("blob1"), "/tmp/tarfs/blob1.layer.disk") | ||
assert.Equal(t, manager.ImageDiskFilePath("blob1"), "/tmp/tarfs/blob1.image.disk") | ||
assert.Equal(t, manager.layerMetaFilePath("/tarfs/fs"), "/tarfs/fs/image/layer.boot") | ||
assert.Equal(t, manager.imageMetaFilePath("/tarfs/fs"), "/tarfs/fs/image/image.boot") | ||
} | ||
|
||
func TestTarfsStatusString(t *testing.T) { | ||
assert.Equal(t, tarfsStatusString(TarfsStatusReady), "Ready") | ||
assert.Equal(t, tarfsStatusString(TarfsStatusPrepare), "Prepare") | ||
assert.Equal(t, tarfsStatusString(TarfsStatusFailed), "Failed") | ||
assert.Equal(t, tarfsStatusString(4), "Unknown") | ||
} | ||
|
||
func TestAttachBlob(t *testing.T) { | ||
testutil.RequiresRoot(t) | ||
|
||
manager := NewManager(false, false, t.TempDir(), NydusImagePath, 4) | ||
blobFile := createTempFile(t) | ||
loopdev, err := manager.attachLoopdev(blobFile) | ||
assert.Assert(t, err) | ||
err = deleteLoop(loopdev) | ||
assert.Assert(t, err) | ||
err = deleteLoop(loopdev) | ||
assert.Assert(t, err != nil) | ||
} |