forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
128 lines (99 loc) · 2.52 KB
/
client_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package hdfs
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var cachedClients = make(map[string]*Client)
func getClient(t *testing.T) *Client {
username, err := Username()
if err != nil {
t.Fatal(err)
}
return getClientForUser(t, username)
}
func getClientForUser(t *testing.T, user string) *Client {
if c, ok := cachedClients[user]; ok {
return c
}
nn := os.Getenv("HADOOP_NAMENODE")
if nn == "" {
t.Fatal("HADOOP_NAMENODE not set")
}
client, err := NewForUser(nn, user)
if err != nil {
t.Fatal(err)
}
cachedClients[user] = client
return client
}
func touch(t *testing.T, path string) {
c := getClient(t)
err := c.CreateEmptyFile(path)
if err != nil && !os.IsExist(err) {
t.Fatal(err)
}
}
func mkdirp(t *testing.T, path string) {
c := getClient(t)
err := c.MkdirAll(path, 0644)
if err != nil && !os.IsExist(err) {
t.Fatal(err)
}
}
func baleet(t *testing.T, path string) {
c := getClient(t)
err := c.Remove(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
}
func assertPathError(t *testing.T, err error, op, path string, wrappedErr error) {
require.NotNil(t, err)
expected := &os.PathError{op, path, wrappedErr}
require.Equal(t, expected.Error(), err.Error())
require.Equal(t, expected, err)
}
func TestNewWithMultipleNodes(t *testing.T) {
nn := os.Getenv("HADOOP_NAMENODE")
if nn == "" {
t.Fatal("HADOOP_NAMENODE not set")
}
_, err := NewClient(ClientOptions{
Addresses: []string{"localhost:199", nn},
})
assert.Nil(t, err)
}
func TestNewWithFailingNode(t *testing.T) {
_, err := New("localhost:199")
assert.NotNil(t, err)
}
func TestReadFile(t *testing.T) {
client := getClient(t)
bytes, err := client.ReadFile("/_test/foo.txt")
assert.NoError(t, err)
assert.EqualValues(t, "bar\n", string(bytes))
}
func TestCopyToLocal(t *testing.T) {
client := getClient(t)
dir, _ := ioutil.TempDir("", "hdfs-test")
tmpfile := filepath.Join(dir, "foo.txt")
err := client.CopyToLocal("/_test/foo.txt", tmpfile)
require.NoError(t, err)
f, err := os.Open(tmpfile)
require.NoError(t, err)
bytes, _ := ioutil.ReadAll(f)
assert.EqualValues(t, "bar\n", string(bytes))
}
func TestCopyToRemote(t *testing.T) {
client := getClient(t)
baleet(t, "/_test/copytoremote.txt")
err := client.CopyToRemote("test/foo.txt", "/_test/copytoremote.txt")
require.NoError(t, err)
bytes, err := client.ReadFile("/_test/copytoremote.txt")
require.NoError(t, err)
assert.EqualValues(t, "bar\n", string(bytes))
}