Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add testcase #144

Merged
merged 3 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .tools/gen_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#/bin/bash
mkdir -p ~/.paddle
Copy link
Collaborator

@helinwang helinwang Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of generating this file with a script (unit test breaks without running this script), you can check in this file into the code base, maybe under testdata, like:https://github.com/golang/go/blob/master/src/net/testdata/hosts . And add argument to override the default location ~/.paddle to the test file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config是一个全局变量,这个参数不太好覆盖。如果不用这个全局变量,需要改动的地方太多。这个能否后续再想想如何更简单的解决这问题?

cat > ~/.paddle/config << EOF
datacenters:
- name: datacenter1
username: [email protected]
password: T123
endpoint: http://127.0.0.1:8080
current-datacenter: datacenter1
EOF
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ matrix:
include:
- language: go
go: 1.8.x
script: cd go && go test ./...
script: bash .tools/gen_config.sh && cd go && go test ./...
- language: python
python: 2.7
sudo: required
Expand Down
62 changes: 62 additions & 0 deletions go/filemanager/pfsmodules/chunkmeta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pfsmodules

import (
"crypto/md5"
"fmt"
"testing"
)

func TestGetDiffMeta(t *testing.T) {
var src []ChunkMeta
var dst []ChunkMeta

var data []ChunkMeta

for i := 0; i < 4; i++ {
data = append(data, ChunkMeta{
Offset: int64(i),
Checksum: fmt.Sprintf("%x", md5.Sum([]byte("1"))),
Len: 1})
}

src = data[0:3]
// check when len(dst) is 0
diff, _ := GetDiffChunkMeta(src, dst)
if len(diff) != 3 {
t.Error(len(diff))
}

for i, _ := range diff {
if diff[i] != src[i] {
t.Error(i)
}
}

// check when dst is same as src
dst = src
diff, _ = GetDiffChunkMeta(src, dst)
if len(diff) != 0 {
t.Error(len(diff))
}

// check when dst is small than src
dst = dst[:0]
dst = append(dst, data[0])
dst = append(dst, data[2])

diff, _ = GetDiffChunkMeta(src, dst)
if len(diff) != 1 {
t.Error(len(diff))
}

if diff[0] != data[1] {
t.Error(0)
}

// check when dst is large then src
dst = data
diff, _ = GetDiffChunkMeta(src, dst)
if len(diff) != 0 {
t.Error(len(diff))
}
}
1 change: 1 addition & 0 deletions go/filemanager/pfsmodules/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Command interface {
}

// CheckUser checks if a user has authority to access a path.
// path example:/pfs/$datacenter/home/$user
func checkUser(path string, user string) error {
a := strings.Split(path, "/")
if len(a) < 3 {
Expand Down
47 changes: 47 additions & 0 deletions go/filemanager/pfsmodules/cp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pfsmodules

import (
"flag"
"strings"
"testing"
)

func TestNewCpCmdFromFlag(t *testing.T) {
cmdLine := "cp -v /pfs/datacenter/home/user1/1.txt /pfs/datacenter/home/user1/2.txt ./user1/"
a := strings.Split(cmdLine, " ")

flag := flag.NewFlagSet("cp", flag.ExitOnError)
flag.Bool("v", false, "")

if err := flag.Parse(a[1:]); err != nil {
t.Error(err.Error())
}

d, err := newCpCmdFromFlag(flag)
if err != nil {
t.Error(err.Error())
}

if d.Method != "cp" {
t.Error(d.Method)
}

if !d.V {
t.Error(d.V)
}

if d.Dst != "./user1/" {
t.Error(d.Dst)
}

if len(d.Src) != 2 {
t.Error(len(d.Src))
}

for _, s := range d.Src {
if s != "/pfs/datacenter/home/user1/1.txt" &&
s != "/pfs/datacenter/home/user1/2.txt" {
t.Error(s)
}
}
}
73 changes: 73 additions & 0 deletions go/filemanager/pfsmodules/ls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package pfsmodules

import (
"flag"
"reflect"
"strings"
"testing"
)

func TestNewLsCmdFromURLParam(t *testing.T) {
s := LsCmd{
Method: "ls",
R: false,
Args: []string{"/pfs/test1/", "/pfs/test2/"},
}

path := "arg=%2Fpfs%2Ftest1%2F&arg=%2Fpfs%2Ftest2%2F&method=ls&r=false"

d, err := NewLsCmdFromURLParam(path)
if err != nil {
t.Error(err.Error())
}

if s.Method != d.Method {
t.Error(d.Method)
}

if s.R != d.R {
t.Error(d.R)
}

eq := reflect.DeepEqual(s.Args, d.Args)
if !eq {
t.Error(s.Args)
t.Error(d.Args)
}
}

func TestNewLsCmdFromFlag(t *testing.T) {
cmdLine := "ls -r /pfs/datacenter/home/user1/1.txt /pfs/datacenter/home/user1/"
a := strings.Split(cmdLine, " ")

flag := flag.NewFlagSet("ls", flag.ExitOnError)
flag.Bool("r", false, "")

if err := flag.Parse(a[1:]); err != nil {
t.Error(err.Error())
}

d, err := newLsCmdFromFlag(flag)
if err != nil {
t.Error(err.Error())
}

if d.Method != "ls" {
t.Error(d.Method)
}

if !d.R {
t.Error(d.R)
}

if len(d.Args) != 2 {
t.Error(len(d.Args))
}

for _, s := range d.Args {
if s != "/pfs/datacenter/home/user1/1.txt" &&
s != "/pfs/datacenter/home/user1/" {
t.Error(s)
}
}
}