2019-02-20 17:32:13 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
|
"github.com/eventials/go-tus"
|
|
|
|
"io/ioutil"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-02-21 10:33:21 +08:00
|
|
|
CONST_SMALL_FILE_NAME = "small.txt"
|
|
|
|
CONST_BIG_FILE_NAME = "big.txt"
|
|
|
|
CONST_DOWNLOAD_BIG_FILE_NAME = "big_dowload.txt"
|
|
|
|
CONST_DOWNLOAD_SMALL_FILE_NAME = "small_dowload.txt"
|
2019-02-20 17:32:13 +08:00
|
|
|
)
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
var testUtil = Common{}
|
2019-02-20 17:32:13 +08:00
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
var endPoint = "http://127.0.0.1:8080"
|
2019-02-21 15:20:14 +08:00
|
|
|
var endPoint2 = ""
|
2019-02-20 17:32:13 +08:00
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
var testCfg *GloablConfig
|
2019-02-20 17:32:13 +08:00
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
var testSmallFileMd5 = ""
|
|
|
|
var testBigFileMd5 = ""
|
|
|
|
|
2019-02-21 15:20:14 +08:00
|
|
|
func initFile(smallSize, bigSig int) {
|
2019-02-20 17:32:13 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2019-02-21 15:20:14 +08:00
|
|
|
smallBytes := make([]byte, smallSize)
|
2019-02-21 10:33:21 +08:00
|
|
|
for i := 0; i < len(smallBytes); i++ {
|
|
|
|
smallBytes[i] = 'a'
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
2019-02-21 15:20:14 +08:00
|
|
|
bigBytes := make([]byte, bigSig)
|
2019-02-21 10:33:21 +08:00
|
|
|
for i := 0; i < len(smallBytes); i++ {
|
|
|
|
bigBytes[i] = 'a'
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
ioutil.WriteFile(CONST_SMALL_FILE_NAME, smallBytes, 0664)
|
|
|
|
ioutil.WriteFile(CONST_BIG_FILE_NAME, bigBytes, 0664)
|
|
|
|
testSmallFileMd5, err = testUtil.GetFileSumByName(CONST_SMALL_FILE_NAME, "")
|
|
|
|
if err != nil {
|
|
|
|
// testing.T.Error(err)
|
|
|
|
fmt.Println(err)
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
testBigFileMd5, err = testUtil.GetFileSumByName(CONST_BIG_FILE_NAME, "")
|
|
|
|
if err != nil {
|
2019-02-20 17:32:13 +08:00
|
|
|
//testing.T.Error(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
fmt.Println(CONST_SMALL_FILE_NAME, testSmallFileMd5)
|
|
|
|
fmt.Println(CONST_BIG_FILE_NAME, testBigFileMd5)
|
2019-02-20 17:32:13 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
func uploadContinueBig(t *testing.T) {
|
2019-02-20 17:32:13 +08:00
|
|
|
f, err := os.Open(CONST_BIG_FILE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-02-21 10:33:21 +08:00
|
|
|
client, err := tus.NewClient(endPoint+"/big/upload/", nil)
|
|
|
|
if err != nil {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
upload, err := tus.NewUploadFromFile(f)
|
2019-02-21 10:33:21 +08:00
|
|
|
if err != nil {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error(err)
|
2019-02-21 15:20:14 +08:00
|
|
|
return
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
uploader, err := client.CreateUpload(upload)
|
2019-02-21 10:33:21 +08:00
|
|
|
if err != nil {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error(err)
|
2019-02-21 15:20:14 +08:00
|
|
|
return
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
url := uploader.Url()
|
|
|
|
err = uploader.Upload()
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
if err != nil {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error(err)
|
2019-02-21 15:20:14 +08:00
|
|
|
return
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
if err := httplib.Get(url).ToFile(CONST_DOWNLOAD_BIG_FILE_NAME); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
fmt.Println(url)
|
|
|
|
|
|
|
|
if md5sum, err := testUtil.GetFileSumByName(CONST_DOWNLOAD_BIG_FILE_NAME, ""); md5sum != testBigFileMd5 {
|
|
|
|
t.Error("uploadContinue bigfile download fail")
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:20:14 +08:00
|
|
|
func refreshConfig(t *testing.T) {
|
|
|
|
var (
|
|
|
|
cfg GloablConfig
|
|
|
|
err error
|
|
|
|
cfgStr string
|
|
|
|
result string
|
|
|
|
)
|
|
|
|
|
|
|
|
if testCfg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfgStr = testUtil.JsonEncodePretty(testCfg)
|
|
|
|
if cfg.Addr == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("refreshConfig")
|
|
|
|
req := httplib.Post(endPoint + "/reload?action=set")
|
|
|
|
req.Param("cfg", cfgStr)
|
|
|
|
result, err = req.String()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = httplib.Get(endPoint + "/reload?action=reload")
|
|
|
|
|
|
|
|
result, err = req.String()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
fmt.Println(result)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
func testConfig(t *testing.T) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg GloablConfig
|
|
|
|
err error
|
|
|
|
cfgStr string
|
|
|
|
result string
|
|
|
|
jsonResult JsonResult
|
|
|
|
)
|
|
|
|
|
|
|
|
req := httplib.Get(endPoint + "/reload?action=get")
|
|
|
|
req.SetTimeout(time.Second*2, time.Second*3)
|
|
|
|
err = req.ToJSON(&jsonResult)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
cfgStr = testUtil.JsonEncodePretty(cfg)
|
|
|
|
cfgStr = testUtil.JsonEncodePretty(jsonResult.Data.(map[string]interface{}))
|
|
|
|
fmt.Println("cfg:\n", cfgStr)
|
|
|
|
if err = json.Unmarshal([]byte(cfgStr), &cfg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
testCfg = &cfg
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:20:14 +08:00
|
|
|
if cfg.Peers != nil && len(cfg.Peers) > 0 && endPoint2 == "" {
|
|
|
|
endPoint2 = cfg.Peers[0]
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
if cfg.Group == "" || cfg.Addr == "" {
|
|
|
|
t.Error("fail config")
|
2019-02-20 20:14:21 +08:00
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.EnableMergeSmallFile = true
|
|
|
|
cfgStr = testUtil.JsonEncodePretty(cfg)
|
|
|
|
req = httplib.Post(endPoint + "/reload?action=set")
|
|
|
|
req.Param("cfg", cfgStr)
|
|
|
|
result, err = req.String()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = httplib.Get(endPoint + "/reload?action=reload")
|
|
|
|
|
|
|
|
result, err = req.String()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
fmt.Println(result)
|
2019-02-20 20:14:21 +08:00
|
|
|
|
2019-02-21 15:20:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCommon(t *testing.T) {
|
|
|
|
|
|
|
|
testUtil.RemoveEmptyDir("files")
|
|
|
|
|
|
|
|
if len(testUtil.GetUUID()) != 36 {
|
|
|
|
t.Error("testCommon fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCommonMap(t *testing.T) {
|
|
|
|
var (
|
|
|
|
commonMap *CommonMap
|
|
|
|
)
|
|
|
|
commonMap = NewCommonMap(1)
|
|
|
|
commonMap.AddUniq("1")
|
|
|
|
//if len(commonMap.Keys()) != 1 {
|
|
|
|
// t.Error("testCommonMap fail")
|
|
|
|
//}
|
|
|
|
commonMap.Clear()
|
|
|
|
if len(commonMap.Keys()) != 0 {
|
|
|
|
t.Error("testCommonMap fail")
|
|
|
|
}
|
|
|
|
commonMap.AddCount("count", 1)
|
|
|
|
commonMap.Add("count")
|
|
|
|
if v, ok := commonMap.GetValue("count"); ok {
|
|
|
|
if v.(int) != 2 {
|
|
|
|
t.Error("testCommonMap fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !commonMap.Contains("count") {
|
|
|
|
t.Error("testCommonMap fail")
|
|
|
|
}
|
|
|
|
commonMap.Zero()
|
|
|
|
if v, ok := commonMap.GetValue("count"); ok {
|
|
|
|
if v.(int) != 0 {
|
|
|
|
t.Error("testCommonMap fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
commonMap.Remove("count")
|
|
|
|
|
|
|
|
if _, ok := commonMap.GetValue("count"); ok {
|
|
|
|
t.Error("testCommonMap fail")
|
|
|
|
}
|
2019-02-20 20:14:21 +08:00
|
|
|
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
func testApis(t *testing.T) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
result string
|
|
|
|
)
|
|
|
|
|
2019-02-21 17:30:09 +08:00
|
|
|
apis := []string{"/index","/status", "/stat", "/repair?force=1", "/repair_stat",
|
|
|
|
"/sync?force=1&date=" + testUtil.GetToDay(),"/delete?md5="+testSmallFileMd5,
|
|
|
|
"/repair_fileinfo",""}
|
2019-02-21 10:33:21 +08:00
|
|
|
for _, v := range apis {
|
|
|
|
req := httplib.Get(endPoint + v)
|
|
|
|
req.SetTimeout(time.Second*2, time.Second*3)
|
|
|
|
result, err = req.String()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-02-21 15:20:14 +08:00
|
|
|
fmt.Println("#########apis#########",v)
|
2019-02-21 10:33:21 +08:00
|
|
|
fmt.Println(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadContinueSmall(t *testing.T) {
|
2019-02-20 20:14:21 +08:00
|
|
|
f, err := os.Open(CONST_SMALL_FILE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-02-21 10:33:21 +08:00
|
|
|
client, err := tus.NewClient(endPoint+"/big/upload/", nil)
|
|
|
|
if err != nil {
|
2019-02-20 20:14:21 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
upload, err := tus.NewUploadFromFile(f)
|
2019-02-21 10:33:21 +08:00
|
|
|
if err != nil {
|
2019-02-20 20:14:21 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
uploader, err := client.CreateUpload(upload)
|
2019-02-21 10:33:21 +08:00
|
|
|
if err != nil {
|
2019-02-20 20:14:21 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
url := uploader.Url()
|
|
|
|
err = uploader.Upload()
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
if err != nil {
|
2019-02-20 20:14:21 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
if err := httplib.Get(url).ToFile(CONST_DOWNLOAD_SMALL_FILE_NAME); err != nil {
|
2019-02-20 20:14:21 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
fmt.Println(url)
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
if md5sum, err := testUtil.GetFileSumByName(CONST_DOWNLOAD_SMALL_FILE_NAME, ""); md5sum != testSmallFileMd5 {
|
2019-02-20 20:14:21 +08:00
|
|
|
t.Error("uploadContinue smallfile download fail")
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
func uploadSmall(t *testing.T) {
|
2019-02-20 17:32:13 +08:00
|
|
|
var obj FileResult
|
2019-02-21 10:33:21 +08:00
|
|
|
req := httplib.Post(endPoint + "/upload")
|
|
|
|
req.PostFile("file", CONST_SMALL_FILE_NAME)
|
|
|
|
req.Param("output", "json")
|
|
|
|
req.Param("scene", "")
|
|
|
|
req.Param("path", "")
|
2019-02-20 17:32:13 +08:00
|
|
|
req.ToJSON(&obj)
|
2019-02-20 20:14:21 +08:00
|
|
|
fmt.Println(obj.Url)
|
2019-02-21 10:33:21 +08:00
|
|
|
if obj.Md5 != testSmallFileMd5 {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error("file not equal")
|
2019-02-21 10:33:21 +08:00
|
|
|
} else {
|
|
|
|
req = httplib.Get(obj.Url)
|
2019-02-20 17:32:13 +08:00
|
|
|
req.ToFile(CONST_DOWNLOAD_SMALL_FILE_NAME)
|
2019-02-21 10:33:21 +08:00
|
|
|
if md5sum, err := testUtil.GetFileSumByName(CONST_DOWNLOAD_SMALL_FILE_NAME, ""); md5sum != testSmallFileMd5 {
|
|
|
|
t.Error("small file not equal", err)
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
func uploadLarge(t *testing.T) {
|
2019-02-20 17:32:13 +08:00
|
|
|
var obj FileResult
|
2019-02-21 10:33:21 +08:00
|
|
|
req := httplib.Post(endPoint + "/upload")
|
|
|
|
req.PostFile("file", CONST_BIG_FILE_NAME)
|
|
|
|
req.Param("output", "json")
|
|
|
|
req.Param("scene", "")
|
|
|
|
req.Param("path", "")
|
2019-02-20 17:32:13 +08:00
|
|
|
req.ToJSON(&obj)
|
2019-02-20 20:14:21 +08:00
|
|
|
fmt.Println(obj.Url)
|
2019-02-21 10:33:21 +08:00
|
|
|
if obj.Md5 != testBigFileMd5 {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error("file not equal")
|
|
|
|
} else {
|
2019-02-21 10:33:21 +08:00
|
|
|
req = httplib.Get(obj.Url)
|
2019-02-20 17:32:13 +08:00
|
|
|
req.ToFile(CONST_DOWNLOAD_BIG_FILE_NAME)
|
2019-02-21 10:33:21 +08:00
|
|
|
if md5sum, err := testUtil.GetFileSumByName(CONST_DOWNLOAD_BIG_FILE_NAME, ""); md5sum != testBigFileMd5 {
|
2019-02-20 20:14:21 +08:00
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
t.Error("big file not equal", err)
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
func checkFileExist(t *testing.T) {
|
2019-02-20 17:32:13 +08:00
|
|
|
var obj FileInfo
|
2019-02-21 10:33:21 +08:00
|
|
|
req := httplib.Post(endPoint + "/check_file_exist")
|
|
|
|
req.Param("md5", testBigFileMd5)
|
2019-02-20 17:32:13 +08:00
|
|
|
req.ToJSON(&obj)
|
2019-02-21 10:33:21 +08:00
|
|
|
if obj.Md5 != testBigFileMd5 {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error("file not equal testBigFileMd5")
|
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
req = httplib.Get(endPoint + "/check_file_exist?md5=" + testSmallFileMd5)
|
2019-02-20 17:32:13 +08:00
|
|
|
req.ToJSON(&obj)
|
2019-02-21 10:33:21 +08:00
|
|
|
if obj.Md5 != testSmallFileMd5 {
|
2019-02-20 17:32:13 +08:00
|
|
|
t.Error("file not equal testSmallFileMd5")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_main(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
}{
|
2019-02-21 10:33:21 +08:00
|
|
|
{"main"},
|
2019-02-20 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-02-21 15:20:14 +08:00
|
|
|
|
|
|
|
testCommonMap(t)
|
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
go main()
|
2019-02-21 15:20:14 +08:00
|
|
|
|
2019-02-21 10:33:21 +08:00
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
testConfig(t)
|
2019-02-21 15:20:14 +08:00
|
|
|
|
|
|
|
initFile(1024*testUtil.RandInt(100, 512), 1024*1024*testUtil.RandInt(2, 20))
|
2019-02-21 10:33:21 +08:00
|
|
|
uploadContinueBig(t)
|
|
|
|
uploadContinueSmall(t)
|
2019-02-21 15:20:14 +08:00
|
|
|
initFile(1024*testUtil.RandInt(100, 512), 1024*1024*testUtil.RandInt(2, 20))
|
2019-02-21 10:33:21 +08:00
|
|
|
uploadSmall(t)
|
|
|
|
uploadLarge(t)
|
|
|
|
checkFileExist(t)
|
|
|
|
testApis(t)
|
2019-02-21 15:20:14 +08:00
|
|
|
if endPoint != endPoint2 && endPoint2!="" {
|
|
|
|
endPoint = endPoint2
|
|
|
|
fmt.Println("#######endPoint2######",endPoint2)
|
|
|
|
initFile(1024*testUtil.RandInt(100, 512), 1024*1024*testUtil.RandInt(2, 20))
|
|
|
|
uploadContinueBig(t)
|
|
|
|
uploadContinueSmall(t)
|
|
|
|
initFile(1024*testUtil.RandInt(100, 512), 1024*1024*testUtil.RandInt(2, 20))
|
|
|
|
uploadSmall(t)
|
|
|
|
uploadLarge(t)
|
|
|
|
checkFileExist(t)
|
|
|
|
testApis(t)
|
|
|
|
}
|
2019-02-21 10:33:21 +08:00
|
|
|
time.Sleep(time.Second * 2)
|
2019-02-21 15:20:14 +08:00
|
|
|
//testCommon(t)
|
2019-02-20 17:32:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|