Skip to content

Commit

Permalink
Use gohdfs1 and gohdfs2 for tests, rather than the superuser
Browse files Browse the repository at this point in the history
In the process, this fixes some of those tests that were dependent on using
the superuser.
  • Loading branch information
colinmarc committed Jul 20, 2018
1 parent ca25fc2 commit ba74ee1
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 54 deletions.
38 changes: 34 additions & 4 deletions client_test.go
Expand Up @@ -3,6 +3,7 @@ package hdfs
import (
"io/ioutil"
"os"
"os/user"
"path/filepath"
"testing"

Expand All @@ -13,12 +14,16 @@ import (
var cachedClients = make(map[string]*Client)

func getClient(t *testing.T) *Client {
username, err := Username()
return getClientForUser(t, "gohdfs1")
}

func getClientForSuperUser(t *testing.T) *Client {
u, err := user.Current()
if err != nil {
t.Fatal(err)
}

return getClientForUser(t, username)
return getClientForUser(t, u.Username)
}

func getClientForUser(t *testing.T, user string) *Client {
Expand All @@ -41,18 +46,43 @@ func getClientForUser(t *testing.T, user string) *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.CreateEmptyFile(path)
err := c.Remove(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.MkdirAll(path, 0644)
err := c.Remove(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}

err = c.MkdirAll(path, mask)
if err != nil && !os.IsExist(err) {
t.Fatal(err)
}
Expand Down
9 changes: 5 additions & 4 deletions content_summary_test.go
Expand Up @@ -11,6 +11,7 @@ import (
func TestContentSummaryDir(t *testing.T) {
client := getClient(t)

baleet(t, "/_test/dirforcs")
mkdirp(t, "/_test/dirforcs/1")
mkdirp(t, "/_test/dirforcs/2")
touch(t, "/_test/dirforcs/foo")
Expand Down Expand Up @@ -44,11 +45,11 @@ func TestContentSummaryNonExistent(t *testing.T) {
}

func TestContentSummaryDirWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")
mkdirpMask(t, "/_test/accessdenied", 0700)
touchMask(t, "/_test/accessdenied/foo", 0600)

_, err := otherClient.GetContentSummary("/_test/accessdenied/foo")
_, err := client2.GetContentSummary("/_test/accessdenied/foo")
assertPathError(t, err, "content summary", "/_test/accessdenied/foo", os.ErrPermission)
}
11 changes: 5 additions & 6 deletions file_reader_test.go
Expand Up @@ -246,7 +246,6 @@ func TestFileReadDirMany(t *testing.T) {
total := maxReadDir*5 + maxReadDir/2 + 35
firstBatch := maxReadDir + 71

baleet(t, "/_test/fulldir5")
mkdirp(t, "/_test/fulldir5")
for i := 0; i < total; i++ {
touch(t, fmt.Sprintf("/_test/fulldir5/%04d", i))
Expand Down Expand Up @@ -275,14 +274,14 @@ func TestFileReadDirMany(t *testing.T) {
}

func TestOpenFileWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")
mkdirpMask(t, "/_test/accessdenied", 0700)
touchMask(t, "/_test/accessdenied/foo", 0700)

file, err := otherClient.Open("/_test/accessdenied/foo")
assert.Nil(t, file)
file, err := client2.Open("/_test/accessdenied/foo")
assertPathError(t, err, "open", "/_test/accessdenied/foo", os.ErrPermission)
assert.Nil(t, file)
}

func TestFileChecksum(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions file_writer_test.go
Expand Up @@ -229,12 +229,12 @@ func TestCreateEmptyFileWithoutParent(t *testing.T) {

func TestCreateEmptyFileWithoutPermission(t *testing.T) {
client := getClient(t)
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")
baleet(t, "/_test/accessdenied/emptyfile")

err := otherClient.CreateEmptyFile("/_test/accessdenied/emptyfile")
err := client2.CreateEmptyFile("/_test/accessdenied/emptyfile")
assertPathError(t, err, "create", "/_test/accessdenied/emptyfile", os.ErrPermission)

_, err = client.Stat("/_test/accessdenied/emptyfile")
Expand Down
8 changes: 4 additions & 4 deletions mkdir_test.go
Expand Up @@ -79,11 +79,11 @@ func TestMkdirAllExists(t *testing.T) {

func TestMkdirWIthoutPermission(t *testing.T) {
client := getClient(t)
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")

err := otherClient.Mkdir("/_test/accessdenied/dir", mode)
err := client2.Mkdir("/_test/accessdenied/dir", mode)
assertPathError(t, err, "mkdir", "/_test/accessdenied/dir", os.ErrPermission)

_, err = client.Stat("/_test/accessdenied/dir")
Expand All @@ -92,11 +92,11 @@ func TestMkdirWIthoutPermission(t *testing.T) {

func TestMkdirAllWIthoutPermission(t *testing.T) {
client := getClient(t)
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")

err := otherClient.Mkdir("/_test/accessdenied/dir2/foo", mode)
err := client2.Mkdir("/_test/accessdenied/dir2/foo", mode)
assertPathError(t, err, "mkdir", "/_test/accessdenied/dir2/foo", os.ErrPermission)

_, err = client.Stat("/_test/accessdenied/dir2/foo")
Expand Down
33 changes: 16 additions & 17 deletions perms_test.go
Expand Up @@ -45,64 +45,63 @@ func TestChmodNonexistent(t *testing.T) {
}

func TestChmodWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")

err := otherClient.Chmod("/_test/accessdenied", 0777)
err := client2.Chmod("/_test/accessdenied", 0777)
assertPathError(t, err, "chmod", "/_test/accessdenied", os.ErrPermission)
}

func TestChown(t *testing.T) {
client := getClient(t)
superClient := getClientForSuperUser(t)

baleet(t, "/_test/tochown")
touch(t, "/_test/tochown")

err := client.Chown("/_test/tochown", "other", "")
err := superClient.Chown("/_test/tochown", "foo", "bar")
require.NoError(t, err)

fi, err := client.Stat("/_test/tochown")
fi, err := superClient.Stat("/_test/tochown")
assert.NoError(t, err)
assert.EqualValues(t, fi.(*FileInfo).Owner(), "other")
assert.EqualValues(t, fi.(*FileInfo).Owner(), "foo")
assert.EqualValues(t, fi.(*FileInfo).OwnerGroup(), "bar")
}

func TestChownDir(t *testing.T) {
client := getClient(t)
superClient := getClientForSuperUser(t)

baleet(t, "/_test/tochowndir")
mkdirp(t, "/_test/tochowndir")

err := client.Chown("/_test/tochowndir", "other", "")
err := superClient.Chown("/_test/tochowndir", "foo", "bar")
require.NoError(t, err)

fi, err := client.Stat("/_test/tochowndir")
fi, err := superClient.Stat("/_test/tochowndir")
assert.NoError(t, err)
assert.EqualValues(t, fi.(*FileInfo).Owner(), "other")
assert.EqualValues(t, fi.(*FileInfo).Owner(), "foo")
assert.EqualValues(t, fi.(*FileInfo).OwnerGroup(), "bar")
}

func TestChownNonexistent(t *testing.T) {
client := getClient(t)
superClient := getClientForSuperUser(t)

baleet(t, "/_test/nonexistent")

err := client.Chown("/_test/nonexistent", "other", "")
err := superClient.Chown("/_test/nonexistent", "gohdfs2", "")
assertPathError(t, err, "chown", "/_test/nonexistent", os.ErrNotExist)
}

func TestChownWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")

err := otherClient.Chown("/_test/accessdenied", "owner", "")
err := client2.Chown("/_test/accessdenied", "owner", "")
assertPathError(t, err, "chown", "/_test/accessdenied", os.ErrPermission)
}

func TestChtimes(t *testing.T) {
client := getClient(t)

baleet(t, "/_test/tochtime")
touch(t, "/_test/tochtime")

birthday := time.Date(1990, 1, 22, 14, 33, 35, 0, time.UTC)
Expand Down
6 changes: 3 additions & 3 deletions readdir_test.go
Expand Up @@ -96,10 +96,10 @@ func TestReadDirNonexistent(t *testing.T) {
}

func TestReadDirWithoutPermission(t *testing.T) {
mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")
mkdirpMask(t, "/_test/accessdenied", 0700)
touchMask(t, "/_test/accessdenied/foo", 0600)

client := getClientForUser(t, "other")
client := getClientForUser(t, "gohdfs2")

res, err := client.ReadDir("/_test/accessdenied")
assertPathError(t, err, "readdir", "/_test/accessdenied", os.ErrPermission)
Expand Down
4 changes: 2 additions & 2 deletions remove_test.go
Expand Up @@ -32,11 +32,11 @@ func TestRemoveNotExistent(t *testing.T) {
}

func TestRemoveWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")

err := otherClient.Remove("/_test/accessdenied/foo")
err := client2.Remove("/_test/accessdenied/foo")
assertPathError(t, err, "remove", "/_test/accessdenied/foo", os.ErrPermission)
}
14 changes: 7 additions & 7 deletions rename_test.go
Expand Up @@ -46,23 +46,23 @@ func TestRenameDestExists(t *testing.T) {
}

func TestRenameWithoutPermissionForSrc(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")
mkdirpMask(t, "/_test/accessdenied", 0700)
touchMask(t, "/_test/accessdenied/foo", 0600)

err := otherClient.Rename("/_test/accessdenied/foo", "/_test/tomovedest3")
err := client2.Rename("/_test/accessdenied/foo", "/_test/tomovedest3")
assertPathError(t, err, "rename", "/_test/accessdenied/foo", os.ErrPermission)
}

func TestRenameWithoutPermissionForDest(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

baleet(t, "/_test/ownedbyother2")

err := otherClient.CreateEmptyFile("/_test/ownedbyother2")
err := client2.CreateEmptyFile("/_test/ownedbyother2")
require.NoError(t, err)

err = otherClient.Rename("/_test/ownedbyother2", "/_test/accessdenied/tomovedest4")
err = client2.Rename("/_test/ownedbyother2", "/_test/accessdenied/tomovedest4")
assertPathError(t, err, "rename", "/_test/accessdenied/tomovedest4", os.ErrPermission)
}
10 changes: 5 additions & 5 deletions stat_test.go
Expand Up @@ -61,15 +61,15 @@ func TestStatDir(t *testing.T) {
}

func TestStatDirWithoutPermission(t *testing.T) {
otherClient := getClientForUser(t, "other")
client2 := getClientForUser(t, "gohdfs2")

mkdirp(t, "/_test/accessdenied")
touch(t, "/_test/accessdenied/foo")
mkdirpMask(t, "/_test/accessdenied", 0700)
touchMask(t, "/_test/accessdenied/foo", 0600)

resp, err := otherClient.Stat("/_test/accessdenied")
resp, err := client2.Stat("/_test/accessdenied")
assert.NoError(t, err)
assert.NotEqual(t, "", resp.(*FileInfo).Owner())

_, err = otherClient.Stat("/_test/accessdenied/foo")
_, err = client2.Stat("/_test/accessdenied/foo")
assertPathError(t, err, "stat", "/_test/accessdenied/foo", os.ErrPermission)
}

0 comments on commit ba74ee1

Please sign in to comment.