-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs_test.go
94 lines (79 loc) · 1.95 KB
/
fs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package fs
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
testingContent = "Hello World!"
)
var (
namenode = flag.String("fs.namenode", "", "Network address of HDFS namenode. Usually localhost:9000")
webapi = flag.String("fs.webapi", "", "Network address of WebHDFS server. Usually localhost:50070")
)
func init() {
if e := HookupHDFS(*namenode, *webapi, ""); e != nil {
log.Panicf("Failed connect to HDFS: %v", e)
}
}
func testSuite(t *testing.T, protocol string) {
dir := path.Join(protocol, fmt.Sprintf("tmp/test/github.com/wangkuiyi/fs/%v", time.Now().UnixNano()))
file := path.Join(dir, "hello.txt")
content := "Hello World!\n"
assert := assert.New(t)
ls, e := ReadDir(dir) // ReadDir on not existing dir
assert.NotNil(e)
assert.True(os.IsNotExist(e))
assert.Equal(0, len(ls))
_, e = Stat(file) // Stat on not existing file
assert.NotNil(e)
assert.True(os.IsNotExist(e))
if assert.Nil(Mkdir(dir)) { // Mkdir
ls, e := ReadDir(dir) // ReadDir on existing but empty dir
assert.Nil(e)
assert.Equal(0, len(ls))
w, e := Create(file) // Create
if assert.Nil(e) {
fmt.Fprintf(w, content)
w.Close()
if protocol == "/webfs" {
time.Sleep(time.Second / 2) // NOTE: WebHDFS API reacts slowly.
}
ls, e = ReadDir(dir) // ReadDir on existing and non-empty dir
assert.Nil(e)
assert.Equal(1, len(ls))
_, e = Stat(file) // Stat on exisitng file
assert.Nil(e)
assert.False(os.IsNotExist(e))
r, e := Open(file) // Read existing file
if assert.Nil(e) {
b, e := ioutil.ReadAll(r)
assert.Nil(e)
assert.Equal(string(b), content)
r.Close()
}
}
}
}
func TestWebFS(t *testing.T) {
if len(*webapi) > 0 {
testSuite(t, "/webfs")
}
}
func TestHDFS(t *testing.T) {
if len(*namenode) > 0 {
testSuite(t, "/hdfs")
}
}
func TestInMemFS(t *testing.T) {
testSuite(t, "/inmem")
}
func TestLocalFS(t *testing.T) {
testSuite(t, "/")
}