Skip to content

Commit

Permalink
Merge pull request #144 from gongweibao/addtestcase
Browse files Browse the repository at this point in the history
add testcase
  • Loading branch information
gongweibao authored Jun 23, 2017
2 parents 191fc1d + 5b6059e commit 6ca58a3
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 1 deletion.
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
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)
}
}
}

0 comments on commit 6ca58a3

Please sign in to comment.