Skip to content

Commit

Permalink
fix: unit test (#5073)
Browse files Browse the repository at this point in the history
Signed-off-by: jiefenghuang <[email protected]>
  • Loading branch information
jiefenghuang authored Aug 9, 2024
1 parent 17a3cfa commit 28e4467
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 167 deletions.
56 changes: 19 additions & 37 deletions cmd/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,29 @@ import (
"fmt"
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)

func TestDebug(t *testing.T) {
mountTemp(t, nil, nil, nil)
defer umountTemp(t)
Convey("TestDebug", t, func() {
Convey("Mount point does not exist", func() {
mp := "/jfs/test/mp"
So(Main([]string{"", "debug", mp}), ShouldNotBeNil)
})

Convey("Directory is not a mount point", func() {
mp := "./"
So(Main([]string{"", "debug", mp}), ShouldNotBeNil)
})

})

Convey("TestDebug_OutDir", t, func() {
Convey("Specify a file as out dir", func() {
So(Main([]string{"", "debug", "--out-dir", "./debug_test.go", testMountPoint}), ShouldNotBeNil)
})
})

Convey("TestDebug_LogArg", t, func() {
cases := []struct {
arg string
val string
}{
{"--log /var/log/jfs.log", "/var/log/jfs.log"},
{"--log=/var/log/jfs.log", "/var/log/jfs.log"},
{"--log = /var/log/jfs.log", "/var/log/jfs.log"},
{"--log= /var/log/jfs.log", "/var/log/jfs.log"},
{"--log =/var/log/jfs.log", "/var/log/jfs.log"},
{"--log /var/log/jfs.log", "/var/log/jfs.log"},
}
for i, c := range cases {
Convey(fmt.Sprintf("valid log arg %d", i), func() {
So(logArg.FindStringSubmatch(c.arg)[2] == c.val, ShouldBeTrue)
})
}
})
require.NotNil(t, Main([]string{"", "debug", "/jfs/test/mp"}), "mount point does not exist")
require.NotNil(t, Main([]string{"", "debug", "./"}), "directory is not a mount point")
require.NotNil(t, Main([]string{"", "debug", "--out-dir", "./debug_test.go", testMountPoint}), "specify a file as out dir")

cases := []struct {
arg string
val string
}{
{"--log /var/log/jfs.log", "/var/log/jfs.log"},
{"--log=/var/log/jfs.log", "/var/log/jfs.log"},
{"--log = /var/log/jfs.log", "/var/log/jfs.log"},
{"--log= /var/log/jfs.log", "/var/log/jfs.log"},
{"--log =/var/log/jfs.log", "/var/log/jfs.log"},
{"--log /var/log/jfs.log", "/var/log/jfs.log"},
}
for i, c := range cases {
require.True(t, logArg.FindStringSubmatch(c.arg)[2] == c.val, fmt.Sprintf("valid log arg %d", i))
}
}
9 changes: 6 additions & 3 deletions cmd/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"strings"
"testing"
"time"

"github.com/juicedata/juicefs/pkg/utils"
"github.com/stretchr/testify/require"
)

func writeSmallBlocks(mountDir string) error {
Expand Down Expand Up @@ -88,6 +91,8 @@ func TestGc(t *testing.T) {
}
}

os.Setenv("JFS_GC_SKIPPEDTIME", "0")
defer os.Unsetenv("JFS_GC_SKIPPEDTIME")
t.Logf("JFS_GC_SKIPPEDTIME is %s", os.Getenv("JFS_GC_SKIPPEDTIME"))

leaked := filepath.Join(dataDir, "0", "0", "123456789_0_1048576")
Expand All @@ -98,9 +103,7 @@ func TestGc(t *testing.T) {
t.Fatalf("gc delete failed: %s", err)
}

if _, err := os.Stat(leaked); err == nil {
t.Fatalf("gc delete didn't delete the leaked data")
}
require.False(t, utils.Exists(leaked))

if err := Main([]string{"", "gc", testMeta}); err != nil {
t.Fatalf("gc failed: %s", err)
Expand Down
70 changes: 33 additions & 37 deletions cmd/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,42 @@ import (
"testing"

"github.com/agiledragon/gomonkey/v2"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)

func TestInfo(t *testing.T) {
Convey("TestInfo", t, func() {
Convey("TestInfo", func() {
tmpFile, err := os.CreateTemp("/tmp", "")
if err != nil {
t.Fatalf("create temporary file: %s", err)
}
defer tmpFile.Close()
defer os.Remove(tmpFile.Name())
mountTemp(t, nil, nil, nil)
defer umountTemp(t)
// mock os.Stdout
patches := gomonkey.ApplyGlobalVar(os.Stdout, *tmpFile)
defer patches.Reset()
tmpFile, err := os.CreateTemp("/tmp", "")
if err != nil {
t.Fatalf("create temporary file: %s", err)
}
defer tmpFile.Close()
defer os.Remove(tmpFile.Name())
mountTemp(t, nil, nil, nil)
defer umountTemp(t)
// mock os.Stdout
patches := gomonkey.ApplyGlobalVar(os.Stdout, *tmpFile)
defer patches.Reset()

if err = os.MkdirAll(fmt.Sprintf("%s/dir1", testMountPoint), 0777); err != nil {
t.Fatalf("mkdirAll failed: %s", err)
}
for i := 0; i < 10; i++ {
filename := fmt.Sprintf("%s/dir1/f%d.txt", testMountPoint, i)
if err = os.WriteFile(filename, []byte("test"), 0644); err != nil {
t.Fatalf("write file failed: %s", err)
}
}
if err = os.MkdirAll(fmt.Sprintf("%s/dir1", testMountPoint), 0777); err != nil {
t.Fatalf("mkdirAll failed: %s", err)
}
for i := 0; i < 10; i++ {
filename := fmt.Sprintf("%s/dir1/f%d.txt", testMountPoint, i)
if err = os.WriteFile(filename, []byte("test"), 0644); err != nil {
t.Fatalf("write file failed: %s", err)
}
}

if err = Main([]string{"", "info", fmt.Sprintf("%s/dir1", testMountPoint), "--strict"}); err != nil {
t.Fatalf("info failed: %s", err)
}
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("read file failed: %s", err)
}
replacer := strings.NewReplacer("\n", "", " ", "")
res := replacer.Replace(string(content))
answer := fmt.Sprintf("%s/dir1: inode: 2 files: 10 dirs: 1 length: 40 Bytes size: 44.00 KiB (45056 Bytes) path: /dir1", testMountPoint)
answer = replacer.Replace(answer)
So(res, ShouldEqual, answer)
})
})
if err = Main([]string{"", "info", fmt.Sprintf("%s/dir1", testMountPoint), "--strict"}); err != nil {
t.Fatalf("info failed: %s", err)
}
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("read file failed: %s", err)
}
replacer := strings.NewReplacer("\n", "", " ", "")
res := replacer.Replace(string(content))
answer := fmt.Sprintf("%s/dir1: inode: 2 files: 10 dirs: 1 length: 40 Bytes size: 44.00 KiB (45056 Bytes) path: /dir1", testMountPoint)
answer = replacer.Replace(answer)
require.Equal(t, answer, res)
}
117 changes: 65 additions & 52 deletions cmd/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ package cmd
import (
"context"
"fmt"
"github.com/juicedata/juicefs/pkg/version"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/url"
"os"
"reflect"
"runtime"
"strings"
"sync"
"testing"
"time"

"github.com/juicedata/juicefs/pkg/version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/agiledragon/gomonkey/v2"
"github.com/juicedata/juicefs/pkg/meta"
"github.com/juicedata/juicefs/pkg/utils"
"github.com/juicedata/juicefs/pkg/vfs"
"github.com/redis/go-redis/v9"
. "github.com/smartystreets/goconvey/convey"
"github.com/urfave/cli/v2"
)

Expand All @@ -46,54 +48,50 @@ const testVolume = "test"

// gomonkey may encounter the problem of insufficient permissions under mac, please solve it by viewing this link https://github.com/agiledragon/gomonkey/issues/70
func Test_exposeMetrics(t *testing.T) {
Convey("Test_exposeMetrics", t, func() {
Convey("Test_exposeMetrics", func() {
addr := "redis://127.0.0.1:6379/12"
client := meta.NewClient(addr, nil)
format := &meta.Format{
Name: "test",
BlockSize: 4096,
Capacity: 1 << 30,
DirStats: true,
}
_ = client.Init(format, true)
var appCtx *cli.Context
stringPatches := gomonkey.ApplyMethod(reflect.TypeOf(appCtx), "String", func(_ *cli.Context, arg string) string {
switch arg {
case "metrics":
return "127.0.0.1:9567"
case "consul":
return "127.0.0.1:8500"
case "custom-labels":
return "key1:value1"
default:
return ""
}
})
isSetPatches := gomonkey.ApplyMethod(reflect.TypeOf(appCtx), "IsSet", func(_ *cli.Context, arg string) bool {
switch arg {
case "custom-labels":
return true
default:
return false
}
})
defer stringPatches.Reset()
defer isSetPatches.Reset()
ResetHttp()
registerer, registry := wrapRegister(appCtx, "test", "test")
metricsAddr := exposeMetrics(appCtx, registerer, registry)
client.InitMetrics(registerer)
vfs.InitMetrics(registerer)
u := url.URL{Scheme: "http", Host: metricsAddr, Path: "/metrics"}
resp, err := http.Get(u.String())
So(err, ShouldBeNil)
all, err := io.ReadAll(resp.Body)
So(err, ShouldBeNil)
So(string(all), ShouldNotBeBlank)
So(string(all), ShouldContainSubstring, `key1="value1"`)
})
addr := "redis://127.0.0.1:6379/12"
client := meta.NewClient(addr, nil)
format := &meta.Format{
Name: "test",
BlockSize: 4096,
Capacity: 1 << 30,
DirStats: true,
}
_ = client.Init(format, true)
var appCtx *cli.Context
stringPatches := gomonkey.ApplyMethod(reflect.TypeOf(appCtx), "String", func(_ *cli.Context, arg string) string {
switch arg {
case "metrics":
return "127.0.0.1:9567"
case "consul":
return "127.0.0.1:8500"
case "custom-labels":
return "key1:value1"
default:
return ""
}
})
isSetPatches := gomonkey.ApplyMethod(reflect.TypeOf(appCtx), "IsSet", func(_ *cli.Context, arg string) bool {
switch arg {
case "custom-labels":
return true
default:
return false
}
})
defer stringPatches.Reset()
defer isSetPatches.Reset()
ResetHttp()
registerer, registry := wrapRegister(appCtx, "test", "test")
metricsAddr := exposeMetrics(appCtx, registerer, registry)
client.InitMetrics(registerer)
vfs.InitMetrics(registerer)
u := url.URL{Scheme: "http", Host: metricsAddr, Path: "/metrics"}
resp, err := http.Get(u.String())
require.Nil(t, err)
all, err := io.ReadAll(resp.Body)
require.Nil(t, err)
require.NotEmpty(t, all)
require.Contains(t, string(all), `key1="value1"`)
}

func ResetHttp() {
Expand All @@ -107,7 +105,14 @@ func resetTestMeta() *redis.Client { // using Redis
return rdb
}

var mountLock sync.Mutex

func mountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraMountOpts []string) {
// wait for last mount exit
for !mountLock.TryLock() {
time.Sleep(100 * time.Millisecond)
}

_ = resetTestMeta()
testDir := t.TempDir()
if bucket != nil {
Expand All @@ -130,6 +135,7 @@ func mountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraMoun
mountArgs = append(mountArgs, extraMountOpts...)
}
go func() {
defer mountLock.Unlock()
if err := Main(mountArgs); err != nil {
t.Errorf("mount failed: %s", err)
}
Expand All @@ -140,7 +146,7 @@ func mountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraMoun
t.Fatalf("get file inode failed: %s", err)
}
if inode != 1 {
t.Fatalf("mount failed: inode of %s is not 1", testMountPoint)
t.Fatalf("mount failed: inode of %s got %d, expect 1", testMountPoint, inode)
} else {
t.Logf("mount %s success", testMountPoint)
}
Expand Down Expand Up @@ -206,6 +212,11 @@ func TestUmount(t *testing.T) {
}

func tryMountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraMountOpts []string) error {
// wait for last mount exit
for !mountLock.TryLock() {
time.Sleep(100 * time.Millisecond)
}

_ = resetTestMeta()
testDir := t.TempDir()
if bucket != nil {
Expand All @@ -227,8 +238,10 @@ func tryMountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraM
mountArgs = append(mountArgs, extraMountOpts...)
}

os.Setenv("JFS_SUPERVISOR", "test")
errChan := make(chan error, 1)
go func() {
defer mountLock.Unlock()
errChan <- Main(mountArgs)
}()

Expand All @@ -245,7 +258,7 @@ func tryMountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraM
return fmt.Errorf("get file inode failed: %w", err)
}
if inode != 1 {
return fmt.Errorf("mount failed: inode of %s is not 1", testMountPoint)
return fmt.Errorf("mount failed: inode of %s is %d, expect 1", testMountPoint, inode)
}
t.Logf("mount %s success", testMountPoint)
return nil
Expand Down
Loading

0 comments on commit 28e4467

Please sign in to comment.