Skip to content

Commit

Permalink
fix: remove goconvey and fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: jiefenghuang <[email protected]>
  • Loading branch information
jiefenghuang committed Aug 7, 2024
1 parent 9220347 commit 3439c29
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 130 deletions.
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)
}
104 changes: 52 additions & 52 deletions cmd/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package cmd
import (
"context"
"fmt"
"github.com/juicedata/juicefs/pkg/version"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/url"
Expand All @@ -31,12 +29,15 @@ import (
"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 +47,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 Down Expand Up @@ -125,6 +122,7 @@ func mountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraMoun
ResetHttp()

os.Setenv("JFS_SUPERVISOR", "test")
defer os.Unsetenv("JFS_SUPERVISOR")
mountArgs := []string{"", "mount", "--enable-xattr", testMeta, testMountPoint, "--attr-cache", "0", "--entry-cache", "0", "--dir-entry-cache", "0", "--no-usage-report"}
if extraMountOpts != nil {
mountArgs = append(mountArgs, extraMountOpts...)
Expand All @@ -140,7 +138,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 @@ -227,6 +225,8 @@ func tryMountTemp(t *testing.T, bucket *string, extraFormatOpts []string, extraM
mountArgs = append(mountArgs, extraMountOpts...)
}

os.Setenv("JFS_SUPERVISOR", "test")
defer os.Unsetenv("JFS_SUPERVISOR")
errChan := make(chan error, 1)
go func() {
errChan <- Main(mountArgs)
Expand All @@ -245,7 +245,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
62 changes: 28 additions & 34 deletions cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,35 @@ import (
"testing"

"github.com/agiledragon/gomonkey/v2"

. "github.com/smartystreets/goconvey/convey"
)

func TestStatus(t *testing.T) {
Convey("TestStatus", t, func() {
Convey("TestStatus", 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()

if err = Main([]string{"", "status", testMeta}); err != nil {
t.Fatalf("status failed: %s", err)
}
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("read file failed: %s", err)
}
s := sections{}
if err = json.Unmarshal(content, &s); err != nil {
t.Fatalf("json unmarshal failed: %s", err)
}
if s.Setting.Name != testVolume || s.Setting.Storage != "file" {
t.Fatalf("setting is not as expected: %+v", s.Setting)
}
})
})
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 = Main([]string{"", "status", testMeta}); err != nil {
t.Fatalf("status failed: %s", err)
}
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("read file failed: %s", err)
}
s := sections{}
if err = json.Unmarshal(content, &s); err != nil {
t.Fatalf("json unmarshal failed: %s", err)
}
if s.Setting.Name != testVolume || s.Setting.Storage != "file" {
t.Fatalf("setting is not as expected: %+v", s.Setting)
}
}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ require (
github.com/qiniu/go-sdk/v7 v7.15.0
github.com/redis/go-redis/v9 v9.0.2
github.com/sirupsen/logrus v1.9.3
github.com/smartystreets/goconvey v1.7.2
github.com/stretchr/testify v1.8.4
github.com/studio-b12/gowebdav v0.0.0-20230203202212-3282f94193f2
github.com/tencentyun/cos-go-sdk-v5 v0.7.45
Expand Down Expand Up @@ -90,6 +89,7 @@ require (
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/go-resty/resty/v2 v2.11.0 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
)
Expand Down Expand Up @@ -148,7 +148,6 @@ require (
github.com/google/readahead v0.0.0-20161222183148-eaceba169032 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.6
Expand All @@ -168,7 +167,6 @@ require (
github.com/jcmturner/goidentity/v6 v6.0.1 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid v1.3.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
Expand Down Expand Up @@ -217,7 +215,6 @@ require (
github.com/secure-io/sio-go v0.3.1 // indirect
github.com/shirou/gopsutil/v3 v3.23.11 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stathat/consistent v1.0.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
Expand Down

0 comments on commit 3439c29

Please sign in to comment.