Skip to content

Commit

Permalink
nydusify: add unit test for nydusify
Browse files Browse the repository at this point in the history
We had removed the tests files(e2e) in nydusify, we need add the unit tests
to improve test coverage.

Signed-off-by: Yadong Ding <[email protected]>
  • Loading branch information
Desiki-high committed Dec 24, 2023
1 parent b4354ce commit e7ecc55
Show file tree
Hide file tree
Showing 12 changed files with 1,111 additions and 49 deletions.
254 changes: 254 additions & 0 deletions contrib/nydusify/cmd/nydusify_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright 2023 Alibaba Cloud. All rights reserved.
// Copyright 2023 Nydus Developers. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0

package main

import (
"encoding/json"
"flag"
"os"
"testing"

"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)

func TestIsPossibleValue(t *testing.T) {
Expand All @@ -35,6 +38,12 @@ func TestAddReferenceSuffix(t *testing.T) {
_, err = addReferenceSuffix(source, suffix)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid source image reference")

source = "localhost:5000/nginx:latest@sha256:757574c5a2102627de54971a0083d4ecd24eb48fdf06b234d063f19f7bbc22fb"
suffix = "-suffix"
_, err = addReferenceSuffix(source, suffix)
require.Error(t, err)
require.Contains(t, err.Error(), "unsupported digested image reference")
}

func TestParseBackendConfig(t *testing.T) {
Expand Down Expand Up @@ -65,4 +74,249 @@ func TestParseBackendConfig(t *testing.T) {
// Failure situation
_, err = parseBackendConfig(configJSON, file.Name())
require.Error(t, err)

_, err = parseBackendConfig("", "non-existent.json")
require.Error(t, err)
}

func TestGetBackendConfig(t *testing.T) {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "prefixbackend-type",
Value: "",
},
&cli.StringFlag{
Name: "prefixbackend-config",
Value: "",
},
&cli.StringFlag{
Name: "prefixbackend-config-file",
Value: "",
},
},
}
ctx := cli.NewContext(app, nil, nil)

backendType, backendConfig, err := getBackendConfig(ctx, "prefix", false)
require.NoError(t, err)
require.Empty(t, backendType)
require.Empty(t, backendConfig)

backendType, backendConfig, err = getBackendConfig(ctx, "prefix", true)
require.Error(t, err)
require.Contains(t, err.Error(), "backend type is empty, please specify option")
require.Empty(t, backendType)
require.Empty(t, backendConfig)

flagSet := flag.NewFlagSet("test1", flag.PanicOnError)
flagSet.String("prefixbackend-type", "errType", "")
ctx = cli.NewContext(app, flagSet, nil)
backendType, backendConfig, err = getBackendConfig(ctx, "prefix", true)
require.Error(t, err)
require.Contains(t, err.Error(), "backend-type should be one of")
require.Empty(t, backendType)
require.Empty(t, backendConfig)

flagSet = flag.NewFlagSet("test2", flag.PanicOnError)
flagSet.String("prefixbackend-type", "oss", "")
ctx = cli.NewContext(app, flagSet, nil)
backendType, backendConfig, err = getBackendConfig(ctx, "prefix", true)
require.Error(t, err)
require.Contains(t, err.Error(), "backend configuration is empty, please specify option")
require.Empty(t, backendType)
require.Empty(t, backendConfig)

configJSON := `
{
"bucket_name": "test",
"endpoint": "region.oss.com",
"access_key_id": "testAK",
"access_key_secret": "testSK",
"meta_prefix": "meta",
"blob_prefix": "blob"
}`
require.True(t, json.Valid([]byte(configJSON)))

flagSet = flag.NewFlagSet("test3", flag.PanicOnError)
flagSet.String("prefixbackend-type", "oss", "")
flagSet.String("prefixbackend-config", configJSON, "")
ctx = cli.NewContext(app, flagSet, nil)
backendType, backendConfig, err = getBackendConfig(ctx, "prefix", true)
require.NoError(t, err)
require.Equal(t, "oss", backendType)
require.Equal(t, configJSON, backendConfig)

file, err := os.CreateTemp("", "nydusify-backend-config-test.json")
require.NoError(t, err)
defer os.RemoveAll(file.Name())

_, err = file.WriteString(configJSON)
require.NoError(t, err)
file.Sync()

flagSet = flag.NewFlagSet("test4", flag.PanicOnError)
flagSet.String("prefixbackend-type", "oss", "")
flagSet.String("prefixbackend-config-file", file.Name(), "")
ctx = cli.NewContext(app, flagSet, nil)
backendType, backendConfig, err = getBackendConfig(ctx, "prefix", true)
require.NoError(t, err)
require.Equal(t, "oss", backendType)
require.Equal(t, configJSON, backendConfig)

flagSet = flag.NewFlagSet("test5", flag.PanicOnError)
flagSet.String("prefixbackend-type", "oss", "")
flagSet.String("prefixbackend-config", configJSON, "")
flagSet.String("prefixbackend-config-file", file.Name(), "")
ctx = cli.NewContext(app, flagSet, nil)
backendType, backendConfig, err = getBackendConfig(ctx, "prefix", true)
require.Error(t, err)
require.Contains(t, err.Error(), "--backend-config conflicts with --backend-config-file")
require.Empty(t, backendType)
require.Empty(t, backendConfig)
}

func TestGetTargetReference(t *testing.T) {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "target",
Value: "",
},
&cli.StringFlag{
Name: "target-suffix",
Value: "",
},
&cli.StringFlag{
Name: "source",
Value: "",
},
},
}
ctx := cli.NewContext(app, nil, nil)

target, err := getTargetReference(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "--target or --target-suffix is required")
require.Empty(t, target)

flagSet := flag.NewFlagSet("test1", flag.PanicOnError)
flagSet.String("target", "testTarget", "")
flagSet.String("target-suffix", "testSuffix", "")
ctx = cli.NewContext(app, flagSet, nil)
target, err = getTargetReference(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "-target conflicts with --target-suffix")
require.Empty(t, target)

flagSet = flag.NewFlagSet("test2", flag.PanicOnError)
flagSet.String("target-suffix", "-nydus", "")
flagSet.String("source", "localhost:5000/nginx:latest", "")
ctx = cli.NewContext(app, flagSet, nil)
target, err = getTargetReference(ctx)
require.NoError(t, err)
require.Equal(t, "localhost:5000/nginx:latest-nydus", target)

flagSet = flag.NewFlagSet("test3", flag.PanicOnError)
flagSet.String("target-suffix", "-nydus", "")
flagSet.String("source", "localhost:5000\nginx:latest", "")
ctx = cli.NewContext(app, flagSet, nil)
target, err = getTargetReference(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid source image reference")
require.Empty(t, target)

flagSet = flag.NewFlagSet("test4", flag.PanicOnError)
flagSet.String("target", "testTarget", "")
ctx = cli.NewContext(app, flagSet, nil)
target, err = getTargetReference(ctx)
require.NoError(t, err)
require.Equal(t, "testTarget", target)
}

func TestGetCacheReferencet(t *testing.T) {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "build-cache",
Value: "",
},
&cli.StringFlag{
Name: "build-cache-tag",
Value: "",
},
},
}
ctx := cli.NewContext(app, nil, nil)

cache, err := getCacheReference(ctx, "")
require.NoError(t, err)
require.Empty(t, cache)

flagSet := flag.NewFlagSet("test1", flag.PanicOnError)
flagSet.String("build-cache", "cache", "")
flagSet.String("build-cache-tag", "cacheTag", "")
ctx = cli.NewContext(app, flagSet, nil)
cache, err = getCacheReference(ctx, "")
require.Error(t, err)
require.Contains(t, err.Error(), "--build-cache conflicts with --build-cache-tag")
require.Empty(t, cache)

flagSet = flag.NewFlagSet("test2", flag.PanicOnError)
flagSet.String("build-cache-tag", "cacheTag", "errTarget")
ctx = cli.NewContext(app, flagSet, nil)
cache, err = getCacheReference(ctx, "")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid target image reference: invalid reference format")
require.Empty(t, cache)

flagSet = flag.NewFlagSet("test2", flag.PanicOnError)
flagSet.String("build-cache-tag", "latest-cache", "")
ctx = cli.NewContext(app, flagSet, nil)
cache, err = getCacheReference(ctx, "localhost:5000/nginx:latest")
require.NoError(t, err)
require.Equal(t, "localhost:5000/nginx:latest-cache", cache)
}

func TestGetPrefetchPatterns(t *testing.T) {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "prefetch-dir",
Value: "",
},
&cli.BoolFlag{
Name: "prefetch-patterns",
Value: false,
},
},
}
ctx := cli.NewContext(app, nil, nil)

patterns, err := getPrefetchPatterns(ctx)
require.NoError(t, err)
require.Equal(t, "/", patterns)

flagSet := flag.NewFlagSet("test1", flag.PanicOnError)
flagSet.String("prefetch-dir", "/etc/passwd", "")
ctx = cli.NewContext(app, flagSet, nil)
patterns, err = getPrefetchPatterns(ctx)
require.NoError(t, err)
require.Equal(t, "/etc/passwd", patterns)

flagSet = flag.NewFlagSet("test2", flag.PanicOnError)
flagSet.String("prefetch-dir", "/etc/passwd", "")
flagSet.Bool("prefetch-patterns", true, "")
ctx = cli.NewContext(app, flagSet, nil)
patterns, err = getPrefetchPatterns(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "--prefetch-dir conflicts with --prefetch-patterns")
require.Empty(t, patterns)

flagSet = flag.NewFlagSet("test3", flag.PanicOnError)
flagSet.Bool("prefetch-patterns", true, "")
ctx = cli.NewContext(app, flagSet, nil)
patterns, err = getPrefetchPatterns(ctx)
require.NoError(t, err)
require.Equal(t, "/", patterns)
}
66 changes: 66 additions & 0 deletions contrib/nydusify/pkg/backend/backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2023 Nydus Developers. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0

package backend

import (
"encoding/json"
"testing"

"github.com/dragonflyoss/nydus/contrib/nydusify/pkg/provider"
"github.com/dragonflyoss/nydus/contrib/nydusify/pkg/utils"
"github.com/stretchr/testify/require"
)

func TestBlobDesc(t *testing.T) {
desc := blobDesc(123456, "205eed24cbec29ad9cb4593a73168ef1803402370a82f7d51ce25646fc2f943a")
require.Equal(t, int64(123456), desc.Size)
require.Equal(t, "sha256:205eed24cbec29ad9cb4593a73168ef1803402370a82f7d51ce25646fc2f943a", desc.Digest.String())
require.Equal(t, utils.MediaTypeNydusBlob, desc.MediaType)
require.Equal(t, map[string]string{
utils.LayerAnnotationUncompressed: "sha256:205eed24cbec29ad9cb4593a73168ef1803402370a82f7d51ce25646fc2f943a",
utils.LayerAnnotationNydusBlob: "true",
}, desc.Annotations)
}

func TestNewBackend(t *testing.T) {
ossConfigJSON := `
{
"bucket_name": "test",
"endpoint": "region.oss.com",
"access_key_id": "testAK",
"access_key_secret": "testSK",
"object_prefix": "blob"
}`
require.True(t, json.Valid([]byte(ossConfigJSON)))
backend, err := NewBackend("oss", []byte(ossConfigJSON), nil)
require.NoError(t, err)
require.Equal(t, OssBackend, backend.Type())

s3ConfigJSON := `
{
"bucket_name": "test",
"endpoint": "s3.amazonaws.com",
"access_key_id": "testAK",
"access_key_secret": "testSK",
"object_prefix": "blob",
"scheme": "https",
"region": "region1"
}`
require.True(t, json.Valid([]byte(s3ConfigJSON)))
backend, err = NewBackend("s3", []byte(s3ConfigJSON), nil)
require.NoError(t, err)
require.Equal(t, S3backend, backend.Type())

testRegistryRemote, err := provider.DefaultRemote("test", false)
require.NoError(t, err)
backend, err = NewBackend("registry", nil, testRegistryRemote)
require.NoError(t, err)
require.Equal(t, RegistryBackend, backend.Type())

backend, err = NewBackend("errBackend", nil, testRegistryRemote)
require.Error(t, err)
require.Contains(t, err.Error(), "unsupported backend type")
require.Nil(t, backend)
}
Loading

0 comments on commit e7ecc55

Please sign in to comment.