mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
refactor(common): add unit test
Signed-off-by: JasonHu520 <hastyjsaon500@gmail.com>
This commit is contained in:
parent
6b615bccfc
commit
178afe1a52
@ -193,7 +193,7 @@ func TestAccessLimiterBase(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(time.Second)
|
||||
cancel()
|
||||
close(closeCh)
|
||||
wg.Wait()
|
||||
|
||||
@ -19,8 +19,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/util/errors"
|
||||
|
||||
"github.com/desertbit/grumble"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/api/clustermgr"
|
||||
@ -28,6 +26,7 @@ import (
|
||||
"github.com/cubefs/cubefs/blobstore/cli/config"
|
||||
"github.com/cubefs/cubefs/blobstore/common/rpc"
|
||||
"github.com/cubefs/cubefs/blobstore/common/rpc/auth"
|
||||
"github.com/cubefs/cubefs/blobstore/util/errors"
|
||||
)
|
||||
|
||||
func NewCMClient(secret string, clusterID string, hosts []string) (*clustermgr.Client, error) {
|
||||
|
||||
@ -20,12 +20,11 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/common/kvstore"
|
||||
|
||||
"github.com/desertbit/grumble"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/clustermgr/persistence/normaldb"
|
||||
"github.com/cubefs/cubefs/blobstore/clustermgr/persistence/volumedb"
|
||||
"github.com/cubefs/cubefs/blobstore/common/kvstore"
|
||||
)
|
||||
|
||||
type volInfo struct {
|
||||
|
||||
@ -540,7 +540,7 @@ func (s *Service) loop() {
|
||||
for {
|
||||
select {
|
||||
case <-reportTicker.C:
|
||||
if !s.raftNode.IsLeader() {
|
||||
if !s.raftNode.IsLeader() || s.ConsulAgentAddr == "" {
|
||||
continue
|
||||
}
|
||||
clusterInfo := clustermgr.ClusterInfo{
|
||||
|
||||
@ -2,25 +2,29 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/util/log"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestReload(t *testing.T) {
|
||||
// test load
|
||||
var config interface{}
|
||||
err := LoadFile(&config, srcconf)
|
||||
if err != nil {
|
||||
t.Logf("Load config error: %s", err)
|
||||
}
|
||||
HotReload(context.Background(), srcconf)
|
||||
require.NoError(t, err)
|
||||
|
||||
var reload []byte
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
HotReload(ctx, srcconf)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
// test reload success
|
||||
Register(func(conf []byte) error {
|
||||
log.Infof("run reload func,config:%s", conf)
|
||||
reload = append(reload, conf...)
|
||||
return nil
|
||||
})
|
||||
pid := syscall.Getpid()
|
||||
@ -31,4 +35,17 @@ func TestReload(t *testing.T) {
|
||||
err = p.Signal(syscall.SIGUSR1)
|
||||
require.NoError(t, err)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
require.Greater(t, len(reload), 0)
|
||||
|
||||
// test reload failed
|
||||
reload = make([]byte, 0, 5)
|
||||
Register(func(conf []byte) error {
|
||||
return errors.New("some error occurred")
|
||||
})
|
||||
err = p.Signal(syscall.SIGUSR1)
|
||||
require.NoError(t, err)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
require.Equal(t, len(reload), 0)
|
||||
|
||||
cancel()
|
||||
}
|
||||
|
||||
@ -113,6 +113,9 @@ func TestLrcEncoder(t *testing.T) {
|
||||
encoder, err := NewEncoder(cfg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = encoder.Split([]byte{})
|
||||
assert.Error(t, err)
|
||||
|
||||
// source data split
|
||||
shards, err := encoder.Split(srcData)
|
||||
assert.NoError(t, err)
|
||||
@ -141,6 +144,12 @@ func TestLrcEncoder(t *testing.T) {
|
||||
for i := range dataShards[0] {
|
||||
dataShards[0][i] = 222
|
||||
}
|
||||
|
||||
// test verify failed
|
||||
ok, err := encoder.Verify(shards)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, ok)
|
||||
|
||||
// reconstruct data and check
|
||||
err = encoder.ReconstructData(shards, []int{0})
|
||||
assert.NoError(t, err)
|
||||
@ -168,10 +177,22 @@ func TestLrcEncoder(t *testing.T) {
|
||||
assert.True(t, ok)
|
||||
}
|
||||
|
||||
badIdxs := make([]int, 0)
|
||||
|
||||
// add a local broken
|
||||
for j := range shards[cfg.CodeMode.N+cfg.CodeMode.M+1] {
|
||||
shards[cfg.CodeMode.N+cfg.CodeMode.M+1][j] = 222
|
||||
}
|
||||
badIdxs = append(badIdxs, cfg.CodeMode.N+cfg.CodeMode.M+1)
|
||||
|
||||
// test local verify failed
|
||||
ok, err = encoder.Verify(shards)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, ok)
|
||||
|
||||
// global reconstruct shard and check
|
||||
dataShards = encoder.GetDataShards(shards)
|
||||
parityShards := encoder.GetParityShards(shards)
|
||||
badIdxs := make([]int, 0)
|
||||
for i := 0; i < cfg.CodeMode.M; i++ {
|
||||
if i%2 == 0 {
|
||||
badIdxs = append(badIdxs, i)
|
||||
@ -189,14 +210,15 @@ func TestLrcEncoder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// add a local broken
|
||||
for j := range shards[cfg.CodeMode.N+cfg.CodeMode.M+1] {
|
||||
shards[cfg.CodeMode.N+cfg.CodeMode.M+1][j] = 222
|
||||
}
|
||||
badIdxs = append(badIdxs, cfg.CodeMode.N+cfg.CodeMode.M+1)
|
||||
|
||||
// test verify failed
|
||||
ok, err = encoder.Verify(shards)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, ok)
|
||||
|
||||
err = encoder.Reconstruct(shards, badIdxs)
|
||||
assert.NoError(t, err)
|
||||
ok, err := encoder.Verify(shards)
|
||||
ok, err = encoder.Verify(shards)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, ok)
|
||||
wbuff = bytes.NewBuffer(make([]byte, 0))
|
||||
@ -208,6 +230,20 @@ func TestLrcEncoder(t *testing.T) {
|
||||
assert.Equal(t, cfg.CodeMode.L, len(ls))
|
||||
si := encoder.GetShardsInIdc(shards, 0)
|
||||
assert.Equal(t, (cfg.CodeMode.N+cfg.CodeMode.M+cfg.CodeMode.L)/cfg.CodeMode.AZCount, len(si))
|
||||
|
||||
// test data len
|
||||
shards[badIdxs[0]] = shards[badIdxs[0]][:0]
|
||||
ok, err = encoder.Verify(shards)
|
||||
assert.Error(t, err)
|
||||
assert.False(t, ok)
|
||||
|
||||
err = encoder.Reconstruct(shards, badIdxs)
|
||||
assert.NoError(t, err)
|
||||
|
||||
shards[badIdxs[len(badIdxs)-1]] = shards[len(badIdxs)-1][:0]
|
||||
ok, err = encoder.Verify(shards)
|
||||
assert.Error(t, err)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestLrcReconstruct(t *testing.T) {
|
||||
|
||||
@ -40,6 +40,11 @@ func TestDb(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
ins, ok := db.(*instance)
|
||||
assert.Equal(t, true, ok)
|
||||
assert.Equal(t, path, ins.Name())
|
||||
assert.NotNil(t, ins.GetDB())
|
||||
|
||||
err = db.Put(KV{Key: k1, Value: v1})
|
||||
assert.NoError(t, err)
|
||||
|
||||
@ -88,6 +93,18 @@ func TestTable(t *testing.T) {
|
||||
t1 := db.Table(tableName1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
tab, ok := t1.(*table)
|
||||
assert.Equal(t, true, ok)
|
||||
assert.NotNil(t, tab)
|
||||
assert.Equal(t, tableName1, tab.Name())
|
||||
assert.NoError(t, tab.Flush())
|
||||
|
||||
assert.NotNil(t, tab.GetDB())
|
||||
assert.NotNil(t, tab.GetCf())
|
||||
writeBatch := tab.NewWriteBatch()
|
||||
assert.NotNil(t, writeBatch)
|
||||
assert.NoError(t, tab.DoBatch(writeBatch))
|
||||
|
||||
// not found
|
||||
_, err = t1.Get(k1)
|
||||
assert.Equal(t, ErrNotFound, err)
|
||||
@ -102,6 +119,10 @@ func TestTable(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, v1, v)
|
||||
|
||||
snapshot := tab.NewSnapshot()
|
||||
assert.NotNil(t, snapshot)
|
||||
tab.ReleaseSnapshot(snapshot)
|
||||
|
||||
db.Close()
|
||||
}
|
||||
|
||||
@ -126,14 +147,10 @@ func TestTable(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, v2, v)
|
||||
|
||||
// test snapshot
|
||||
// s := t1.NewSnapshot()
|
||||
// assert.NotNil(t, s)
|
||||
i := t1.NewIterator(nil)
|
||||
assert.NotNil(t, i)
|
||||
|
||||
kvs := make([]KV, 0)
|
||||
// i.Seek(nil)
|
||||
for i.SeekToFirst(); i.Valid(); i.Next() {
|
||||
assert.NoError(t, i.Err())
|
||||
ik := i.Key().Data()
|
||||
@ -161,6 +178,40 @@ func TestTable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTableBatchOP(t *testing.T) {
|
||||
path, err := ioutil.TempDir("", "testrocksdbkvdb12345")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
tableName1 := "table1"
|
||||
tableName2 := "table2"
|
||||
k1 := []byte("t1k1")
|
||||
v1 := []byte("t1v1")
|
||||
|
||||
err = os.MkdirAll(path, 0o755)
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := OpenDBWithCF(path, false, []string{tableName1, tableName2})
|
||||
assert.NoError(t, err)
|
||||
|
||||
t1 := db.Table(tableName1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
kvs := []KV{
|
||||
{Key: k1, Value: v1},
|
||||
}
|
||||
|
||||
err = t1.WriteBatch(kvs, false)
|
||||
assert.NoError(t, err)
|
||||
|
||||
v, err := t1.Get(k1)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, v1, v)
|
||||
|
||||
err = t1.DeleteBatch([][]byte{k1}, true)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDbBatchOP(t *testing.T) {
|
||||
path, err := ioutil.TempDir("", "testrocksdbkvdb121212323")
|
||||
require.NoError(t, err)
|
||||
@ -174,31 +225,56 @@ func TestDbBatchOP(t *testing.T) {
|
||||
defer db.Close()
|
||||
|
||||
keyPrefix := "testkey"
|
||||
var kvs []KV
|
||||
for i := 0; i < 10; i++ {
|
||||
var value [8]byte
|
||||
|
||||
key := []byte(fmt.Sprintf("%s/%d", keyPrefix, i))
|
||||
binary.BigEndian.PutUint64(value[:], uint64(i))
|
||||
{
|
||||
var kvs []KV
|
||||
for i := 0; i < 10; i++ {
|
||||
var value [8]byte
|
||||
|
||||
kv := KV{
|
||||
Key: key,
|
||||
Value: value[:],
|
||||
key := []byte(fmt.Sprintf("%s/%d", keyPrefix, i))
|
||||
binary.BigEndian.PutUint64(value[:], uint64(i))
|
||||
|
||||
kv := KV{
|
||||
Key: key,
|
||||
Value: value[:],
|
||||
}
|
||||
kvs = append(kvs, kv)
|
||||
}
|
||||
kvs = append(kvs, kv)
|
||||
}
|
||||
|
||||
err = db.WriteBatch(kvs, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
// valid
|
||||
for i := 0; i < 10; i++ {
|
||||
key := []byte(fmt.Sprintf("%s/%d", keyPrefix, i))
|
||||
expValue := i
|
||||
valueBytes, err := db.Get([]byte(key))
|
||||
err = db.WriteBatch(kvs, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
value := binary.BigEndian.Uint64(valueBytes)
|
||||
require.Equal(t, int64(expValue), int64(value))
|
||||
// valid
|
||||
for i := 0; i < 10; i++ {
|
||||
key := []byte(fmt.Sprintf("%s/%d", keyPrefix, i))
|
||||
expValue := i
|
||||
valueBytes, err := db.Get([]byte(key))
|
||||
require.NoError(t, err)
|
||||
|
||||
value := binary.BigEndian.Uint64(valueBytes)
|
||||
require.Equal(t, int64(expValue), int64(value))
|
||||
}
|
||||
}
|
||||
|
||||
{ // delete batch
|
||||
var ks [][]byte
|
||||
for i := 0; i < 10; i++ {
|
||||
key := []byte(fmt.Sprintf("%s/%d", keyPrefix, i))
|
||||
ks = append(ks, key)
|
||||
}
|
||||
err = db.DeleteBatch(ks, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
// valid
|
||||
for i := 0; i < 10; i++ {
|
||||
key := []byte(fmt.Sprintf("%s/%d", keyPrefix, i))
|
||||
_, err = db.Get(key)
|
||||
require.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
{ // test Delete
|
||||
err = db.Delete([]byte(fmt.Sprintf("%s/%d", keyPrefix, 1)))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,12 @@ import (
|
||||
func TestNewMemCache(t *testing.T) {
|
||||
_, err := memcache.NewMemCache(context.TODO(), 1024)
|
||||
require.Nil(t, err)
|
||||
|
||||
// test 0 or -1
|
||||
_, err = memcache.NewMemCache(context.TODO(), -1)
|
||||
require.Error(t, err)
|
||||
_, err = memcache.NewMemCache(context.TODO(), 0)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMemCacheGetSet(t *testing.T) {
|
||||
@ -56,6 +62,10 @@ func TestMemCacheDel(t *testing.T) {
|
||||
mc.Set(key, nil)
|
||||
v = mc.Get(key)
|
||||
require.Nil(t, v)
|
||||
|
||||
// test no key
|
||||
test := mc.Get("test-no-key")
|
||||
require.Nil(t, test)
|
||||
}
|
||||
|
||||
func BenchmarkNewMemCache(b *testing.B) {
|
||||
|
||||
@ -75,6 +75,13 @@ func TestProfileBase(t *testing.T) {
|
||||
require.Equal(t, 200, resp.StatusCode)
|
||||
resp.Body.Close()
|
||||
|
||||
// test /debug/pprof/:key, ignore profile
|
||||
for _, m := range []string{"cmdline", "symbol", "trace", " "} {
|
||||
resp, err := http.Get(profileAddr + "/debug/pprof/" + m)
|
||||
require.NoError(t, err)
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
// get one expvar
|
||||
resp, err = http.Get(profileAddr + "/debug/var/")
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -49,7 +49,7 @@ func (s *testSnapshot) Close() {
|
||||
}
|
||||
|
||||
func TestSnapshotter(t *testing.T) {
|
||||
shotter := newSnapshotter(10, time.Second)
|
||||
shotter := newSnapshotter(10, time.Millisecond)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
snap := &snapshot{
|
||||
@ -106,8 +106,13 @@ func TestSnapshotter(t *testing.T) {
|
||||
|
||||
buffer := &bytes.Buffer{}
|
||||
st := newApplySnapshot(buffer)
|
||||
defer st.Close()
|
||||
|
||||
name := st.Name()
|
||||
_, err := st.Read()
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "", name)
|
||||
assert.Equal(t, uint64(0), st.Index())
|
||||
|
||||
b := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(b, 10)
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
package recordlog
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
@ -85,7 +84,8 @@ func TestLogger(t *testing.T) {
|
||||
wg.Wait()
|
||||
|
||||
// test read
|
||||
dec := json.NewDecoder(enc.(io.Reader))
|
||||
dec, err := NewDecoder(conf)
|
||||
require.NoError(t, err)
|
||||
count := 0
|
||||
var m Doc
|
||||
for {
|
||||
|
||||
@ -40,12 +40,20 @@ func TestOpen(t *testing.T) {
|
||||
tracer := trace.NewTracer(moduleName)
|
||||
trace.SetGlobalTracer(tracer)
|
||||
|
||||
ah, lc, err := Open(moduleName, &Config{})
|
||||
assert.Nil(t, ah)
|
||||
assert.Nil(t, lc)
|
||||
assert.Nil(t, err)
|
||||
|
||||
tmpDir := os.TempDir() + "/testauditog" + strconv.FormatInt(time.Now().Unix(), 10) + strconv.Itoa(rand.Intn(100000))
|
||||
err := os.Mkdir(tmpDir, 0o755)
|
||||
err = os.Mkdir(tmpDir, 0o755)
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
ah, lc, err := Open(moduleName, &Config{LogDir: tmpDir, ChunkBits: 29})
|
||||
ah, lc, err = Open(moduleName, &Config{
|
||||
LogDir: tmpDir, ChunkBits: 29,
|
||||
KeywordsFilter: []string{"Get"},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, ah)
|
||||
assert.NotNil(t, lc)
|
||||
@ -70,12 +78,32 @@ func TestOpen(t *testing.T) {
|
||||
url := server.URL
|
||||
client := http.DefaultClient
|
||||
|
||||
for _, method := range []string{http.MethodPost, http.MethodGet, http.MethodDelete, http.MethodPut} {
|
||||
// test keywords filter
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
assert.NoError(t, err)
|
||||
resp, err := client.Do(req)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
respData := &testRespData{}
|
||||
err = json.Unmarshal(b, respData)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success", respData.Result)
|
||||
resp.Body.Close()
|
||||
|
||||
open, err := os.Open(tmpDir)
|
||||
assert.NoError(t, err)
|
||||
dirEntries, err := open.ReadDir(-1)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(dirEntries))
|
||||
assert.NoError(t, open.Close())
|
||||
|
||||
for _, method := range []string{http.MethodPost, http.MethodDelete, http.MethodPut} {
|
||||
req, err := http.NewRequest(method, url, nil)
|
||||
assert.NoError(t, err)
|
||||
resp, err := client.Do(req)
|
||||
assert.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
@ -83,5 +111,12 @@ func TestOpen(t *testing.T) {
|
||||
err = json.Unmarshal(b, respData)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success", respData.Result)
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
open, err = os.Open(tmpDir)
|
||||
assert.NoError(t, err)
|
||||
dirEntries, err = open.ReadDir(-1)
|
||||
assert.NoError(t, err)
|
||||
assert.Greater(t, len(dirEntries), 0)
|
||||
}
|
||||
|
||||
41
blobstore/common/rpc/auditlog/decoder_test.go
Normal file
41
blobstore/common/rpc/auditlog/decoder_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2022 The CubeFS Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
// implied. See the License for the specific language governing
|
||||
// permissions and limitations under the License.
|
||||
|
||||
package auditlog
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/common/rpc"
|
||||
)
|
||||
|
||||
func TestDefaultDecoder_DecodeReq(t *testing.T) {
|
||||
request, err := http.NewRequest(http.MethodPost, "/test", strings.NewReader("test"))
|
||||
assert.NoError(t, err)
|
||||
|
||||
decoder := defaultDecoder{}
|
||||
|
||||
request.Header.Set("Content-Type", rpc.MIMEJSON)
|
||||
request.Header.Set("Content-MD5", "1")
|
||||
decodeReq := decoder.DecodeReq(request)
|
||||
assert.NotNil(t, decodeReq)
|
||||
|
||||
request.Header.Set("Content-Type", rpc.MIMEPOSTForm)
|
||||
decodeReq = decoder.DecodeReq(request)
|
||||
assert.NotNil(t, decodeReq)
|
||||
}
|
||||
@ -74,4 +74,20 @@ func TestAuth(t *testing.T) {
|
||||
err = json.NewDecoder(response.Body).Decode(result)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testName, result.Name)
|
||||
|
||||
// test empty token
|
||||
c := http.Client{}
|
||||
req, err = http.NewRequest("POST", testServer.URL+"/get/name?id="+strconv.Itoa(101), nil)
|
||||
assert.NoError(t, err)
|
||||
response, err = c.Do(req)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusForbidden, response.StatusCode)
|
||||
response.Body.Close()
|
||||
|
||||
// test token failed
|
||||
req.Header.Set(TokenHeaderKey, "#$@%DF#$@#$")
|
||||
response, err = c.Do(req)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusForbidden, response.StatusCode)
|
||||
response.Body.Close()
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ func (self *AuthHandler) Handler(w http.ResponseWriter, req *http.Request, f fun
|
||||
}
|
||||
info, err := decodeAuthInfo(token)
|
||||
if err != nil {
|
||||
f(w, req)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
info.others = genEncodeStr(req)
|
||||
|
||||
@ -40,9 +40,6 @@ func NewAuthTransport(tr http.RoundTripper, cfg *Config) http.RoundTripper {
|
||||
// a simple auth token
|
||||
func (self *AuthTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||
now := time.Now().Unix()
|
||||
if err != nil {
|
||||
return self.Tr.RoundTrip(req)
|
||||
}
|
||||
|
||||
info := &authInfo{timestamp: now, others: genEncodeStr(req)}
|
||||
|
||||
|
||||
@ -141,12 +141,12 @@ func TestLbClient_Put(t *testing.T) {
|
||||
func TestLbClient_GetWith(t *testing.T) {
|
||||
now := time.Now().UnixNano() / 1e6
|
||||
log.SetOutputLevel(log.Ldebug)
|
||||
cfg := newCfg([]string{testServer.URL, "http://127.0.0.1:8898", "http://127.0.0.1:8888"},
|
||||
cfg := newCfg([]string{testServer.URL, "http://127.0.0.1:8898", "http://127.0.0.1:12345"},
|
||||
[]string{testServer.URL})
|
||||
cfg.FailRetryIntervalS = 5
|
||||
client := NewLbClient(cfg, nil)
|
||||
wg := sync.WaitGroup{}
|
||||
var number int = 100
|
||||
var number int = 50
|
||||
wg.Add(number)
|
||||
for i := 0; i < number; i++ {
|
||||
go func() {
|
||||
@ -155,7 +155,6 @@ func TestLbClient_GetWith(t *testing.T) {
|
||||
result := &ret{}
|
||||
err := client.GetWith(ctx, "/get/name?id="+strconv.Itoa(122), result)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, "Test_GetWith", result.Name)
|
||||
}()
|
||||
}
|
||||
|
||||
55
blobstore/common/rpc/selector_test.go
Normal file
55
blobstore/common/rpc/selector_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
// Copyright 2022 The CubeFS Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
// implied. See the License for the specific language governing
|
||||
// permissions and limitations under the License.
|
||||
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSelector_Base(t *testing.T) {
|
||||
cfg := newCfg([]string{"http://127.0.0.1:8888", "http://127.0.0.1:8888"},
|
||||
[]string{"http://127.0.0.1:8988", "http://127.0.0.1:8998"})
|
||||
cfg.HostTryTimes = 3
|
||||
cfg.FailRetryIntervalS = 1
|
||||
cfg.MaxFailsPeriodS = 1
|
||||
s := newSelector(cfg)
|
||||
|
||||
sel, ok := s.(*selector)
|
||||
assert.Equal(t, true, ok)
|
||||
assert.NotNil(t, sel)
|
||||
assert.Equal(t, 4, len(s.GetAvailableHosts()))
|
||||
|
||||
// test set fail
|
||||
s.SetFail("http://127.0.0.1:8988")
|
||||
s.SetFail("http://127.0.0.1:8988")
|
||||
s.SetFail("http://127.0.0.1:8988")
|
||||
assert.Equal(t, 3, len(s.GetAvailableHosts()))
|
||||
|
||||
// test enable host
|
||||
for key := range sel.crackHosts {
|
||||
sel.enableHost(key)
|
||||
}
|
||||
assert.Equal(t, 4, len(s.GetAvailableHosts()))
|
||||
|
||||
// test detect
|
||||
s.SetFail("http://127.0.0.1:8888")
|
||||
s.SetFail("http://127.0.0.1:8888")
|
||||
s.SetFail("http://127.0.0.1:8888")
|
||||
time.Sleep(time.Second * 2)
|
||||
assert.Equal(t, 4, len(s.GetAvailableHosts()))
|
||||
}
|
||||
@ -25,6 +25,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cubefs/cubefs/blobstore/common/crc32block"
|
||||
"github.com/cubefs/cubefs/blobstore/common/rpc/auth"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -40,8 +41,7 @@ func (s *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
case "/retry":
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil || int64(len(b)) != r.ContentLength || first {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte("test retry"))
|
||||
ReplyErr(w, 500, "test retry")
|
||||
first = false
|
||||
}
|
||||
case "/crc":
|
||||
@ -54,8 +54,7 @@ func (s *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
case "/timeout":
|
||||
timeout(w, r)
|
||||
case "/notfound":
|
||||
w.WriteHeader(404)
|
||||
w.Write([]byte("404 page not found"))
|
||||
ReplyWith(w, 404, "", []byte("404 page not found"))
|
||||
default:
|
||||
{
|
||||
marshal, err := json.Marshal(ret{Name: "Test_GetWith"})
|
||||
@ -114,6 +113,10 @@ var simpleCfg = &Config{
|
||||
MaxIdleConnsPerHost: 10,
|
||||
IdleConnTimeoutMs: 60000,
|
||||
DisableCompression: true,
|
||||
Auth: auth.Config{
|
||||
EnableAuth: true,
|
||||
Secret: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
var simpleClient = NewClient(simpleCfg)
|
||||
@ -196,12 +199,22 @@ func TestClient_PostWithReadResponseTimeout(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
cache := make([]byte, 128)
|
||||
_, err = resp.Body.Read(cache)
|
||||
for err == nil {
|
||||
if _, e := resp.Body.Read(cache); e != nil {
|
||||
assert.Equal(t, "read body timeout", e.Error())
|
||||
return
|
||||
}
|
||||
_, err = resp.Body.Read(cache)
|
||||
}
|
||||
assert.Equal(t, "read body timeout", err.Error())
|
||||
|
||||
simpleCfg.BodyBaseTimeoutMs = 1
|
||||
simpleCfg.BodyBandwidthMBPs = 1
|
||||
simpleCli = NewClient(simpleCfg)
|
||||
resp, err = simpleCli.Post(ctx, testServer.URL+"/timeout", &ret{Name: "TestClient_Post"})
|
||||
assert.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
cache = make([]byte, 2*1024*1024*1024)
|
||||
_, err = resp.Body.Read(cache)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "read body timeout", err.Error())
|
||||
}
|
||||
|
||||
func TestClient_Put(t *testing.T) {
|
||||
|
||||
@ -37,15 +37,23 @@ func TestNewVolumeMgr(t *testing.T) {
|
||||
defer ctrl.Finish()
|
||||
|
||||
cmcli := mock.ProxyMockClusterMgrCli(ctrl)
|
||||
_, err := NewVolumeMgr(ctx, BlobConfig{}, VolConfig{InitVolumeNum: 4}, cmcli)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
vm, err := NewVolumeMgr(ctx, BlobConfig{}, VolConfig{
|
||||
InitVolumeNum: 4,
|
||||
MetricReportIntervalS: 1, RetainIntervalS: 1,
|
||||
}, cmcli)
|
||||
time.Sleep(2 * time.Second)
|
||||
require.NoError(t, err)
|
||||
vm.Close()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestGetAllocList(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
cmcli := mock.ProxyMockClusterMgrCli(ctrl)
|
||||
bid, err := NewBidMgr(context.Background(), BlobConfig{BidAllocNums: 10000}, cmcli)
|
||||
require.NoError(t, err)
|
||||
|
||||
expireTime := time.Now().UnixNano() + 300*int64(math.Pow(10, 9))
|
||||
volInfo1 := cm.AllocVolumeInfo{
|
||||
VolumeInfo: cm.VolumeInfo{
|
||||
@ -122,6 +130,7 @@ func TestGetAllocList(t *testing.T) {
|
||||
BlobConfig: BlobConfig{
|
||||
BidAllocNums: 1000,
|
||||
},
|
||||
BidMgr: bid,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
@ -159,6 +168,12 @@ func TestGetAllocList(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
require.Equal(t, 0, int(vid))
|
||||
}
|
||||
|
||||
{
|
||||
alloc, err := vm.Alloc(context.Background(), &proxy.AllocVolsArgs{Fsize: 100, CodeMode: 2, BidCount: 1})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, alloc)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVolumeMgr_Alloc(b *testing.B) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user