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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
package hdfs
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"testing"
"github.com/colinmarc/hdfs/v2/hadoopconf"
krb "github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var cachedClients = make(map[string]*Client)
func getClient(t *testing.T) *Client {
return getClientForUser(t, "gohdfs1")
}
func getClientForSuperUser(t *testing.T) *Client {
u, err := user.Current()
if err != nil {
t.Fatal(err)
}
return getClientForUser(t, u.Username)
}
func getClientForUser(t *testing.T, username string) *Client {
if c, ok := cachedClients[username]; ok {
return c
}
conf, err := hadoopconf.LoadFromEnvironment()
if err != nil || conf == nil {
t.Fatal("Couldn't load ambient config", err)
}
options := ClientOptionsFromConf(conf)
if options.Addresses == nil {
t.Fatal("Missing namenode addresses in ambient config")
}
if options.KerberosClient != nil {
options.KerberosClient = getKerberosClient(t, username)
} else {
options.User = username
}
client, err := NewClient(options)
if err != nil {
t.Fatal(err)
}
cachedClients[username] = client
return client
}
// getKerberosClient expects a ccache file for each user mentioned in the tests
// to live at /tmp/krb5cc_gohdfs_<username>, and krb5.conf to live at
// /etc/krb5.conf
func getKerberosClient(t *testing.T, username string) *krb.Client {
cfg, err := config.Load("/etc/krb5.conf")
if err != nil {
t.Skip("Couldn't load krb config:", err)
}
ccache, err := credentials.LoadCCache(fmt.Sprintf("/tmp/krb5cc_gohdfs_%s", username))
if err != nil {
t.Skipf("Couldn't load keytab for user %s: %s", username, err)
}
client, err := krb.NewFromCCache(ccache, cfg)
if err != nil {
t.Fatal("Couldn't initialize krb client:", err)
}
return client
}
func touch(t *testing.T, path string) {
touchMask(t, path, 0)
}
func touchMask(t *testing.T, path string, mask os.FileMode) {
c := getClient(t)
err := c.RemoveAll(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
err = c.CreateEmptyFile(path)
if err != nil && !os.IsExist(err) {
t.Fatal(err)
}
if mask != 0 {
err = c.Chmod(path, mask)
if err != nil {
t.Fatal(err)
}
}
}
func mkdirp(t *testing.T, path string) {
mkdirpMask(t, path, 0755)
}
func mkdirpMask(t *testing.T, path string, mask os.FileMode) {
c := getClient(t)
err := c.RemoveAll(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
err = c.MkdirAll(path, mask)
if err != nil && !os.IsExist(err) {
t.Fatal(err)
}
}
func baleet(t *testing.T, path string) {
c := getClient(t)
err := c.RemoveAll(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) {
conf, err := hadoopconf.LoadFromEnvironment()
if err != nil {
t.Fatal("Couldn't load ambient config", err)
}
nns := conf.Namenodes()
nns = append([]string{"localhost:100"}, nns...)
_, err = NewClient(ClientOptions{Addresses: nns, User: "gohdfs1"})
assert.Nil(t, err)
}
func TestNewWithFailingNode(t *testing.T) {
_, err := New("localhost:100")
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("testdata/foo.txt", "/_test/copytoremote.txt")
ignoreErrReplicating(t, err)
bytes, err := client.ReadFile("/_test/copytoremote.txt")
require.NoError(t, err)
assert.EqualValues(t, "bar\n", string(bytes))
}
|