From ba74ee14b2cd59da16dde04cad1e951dc5fed57a Mon Sep 17 00:00:00 2001 From: Colin Marc Date: Sat, 21 Jul 2018 00:34:40 +0200 Subject: [PATCH] Use gohdfs1 and gohdfs2 for tests, rather than the superuser In the process, this fixes some of those tests that were dependent on using the superuser. --- client_test.go | 38 ++++++++++++++++++++++++++++++++++---- content_summary_test.go | 9 +++++---- file_reader_test.go | 11 +++++------ file_writer_test.go | 4 ++-- mkdir_test.go | 8 ++++---- perms_test.go | 33 ++++++++++++++++----------------- readdir_test.go | 6 +++--- remove_test.go | 4 ++-- rename_test.go | 14 +++++++------- stat_test.go | 10 +++++----- 10 files changed, 83 insertions(+), 54 deletions(-) diff --git a/client_test.go b/client_test.go index 7ccb645d..17f28ac4 100644 --- a/client_test.go +++ b/client_test.go @@ -3,6 +3,7 @@ package hdfs import ( "io/ioutil" "os" + "os/user" "path/filepath" "testing" @@ -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 { @@ -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) } diff --git a/content_summary_test.go b/content_summary_test.go index 8867d663..920c5108 100644 --- a/content_summary_test.go +++ b/content_summary_test.go @@ -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") @@ -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) } diff --git a/file_reader_test.go b/file_reader_test.go index 2b778766..5a60f123 100644 --- a/file_reader_test.go +++ b/file_reader_test.go @@ -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)) @@ -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) { diff --git a/file_writer_test.go b/file_writer_test.go index 90751027..ea72f1fa 100644 --- a/file_writer_test.go +++ b/file_writer_test.go @@ -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") diff --git a/mkdir_test.go b/mkdir_test.go index c0681e94..00f5a796 100644 --- a/mkdir_test.go +++ b/mkdir_test.go @@ -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") @@ -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") diff --git a/perms_test.go b/perms_test.go index e2ca5b0c..5b4dccfa 100644 --- a/perms_test.go +++ b/perms_test.go @@ -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) diff --git a/readdir_test.go b/readdir_test.go index d6b67d6e..ddcb9404 100644 --- a/readdir_test.go +++ b/readdir_test.go @@ -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) diff --git a/remove_test.go b/remove_test.go index 442e6eae..55effa3a 100644 --- a/remove_test.go +++ b/remove_test.go @@ -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) } diff --git a/rename_test.go b/rename_test.go index f12746c5..0aa750f8 100644 --- a/rename_test.go +++ b/rename_test.go @@ -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) } diff --git a/stat_test.go b/stat_test.go index 7933e52a..7a73f7f8 100644 --- a/stat_test.go +++ b/stat_test.go @@ -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) }