File: stat_test.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (75 lines) | stat: -rw-r--r-- 1,980 bytes parent folder | download
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
package hdfs

import (
	"os"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

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

	resp, err := client.Stat("/_test/foo.txt")
	require.NoError(t, err)

	assert.EqualValues(t, "foo.txt", resp.Name())
	assert.False(t, resp.IsDir())
	assert.EqualValues(t, 4, resp.Size())
	assert.EqualValues(t, time.Now().Year(), resp.ModTime().Year())
	assert.EqualValues(t, time.Now().Month(), resp.ModTime().Month())
}

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

	touch(t, "/_test/emptyfile2")

	resp, err := client.Stat("/_test/emptyfile2")
	require.NoError(t, err)

	assert.EqualValues(t, "emptyfile2", resp.Name())
	assert.False(t, resp.IsDir())
	assert.EqualValues(t, 0, resp.Size())
	assert.EqualValues(t, time.Now().Year(), resp.ModTime().Year())
	assert.EqualValues(t, time.Now().Month(), resp.ModTime().Month())
}

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

	resp, err := client.Stat("/_test/nonexistent")
	assertPathError(t, err, "stat", "/_test/nonexistent", os.ErrNotExist)
	assert.Nil(t, resp)
}

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

	mkdirp(t, "/_test/dir")

	resp, err := client.Stat("/_test/dir")
	require.NoError(t, err)

	assert.EqualValues(t, "dir", resp.Name())
	assert.True(t, resp.IsDir())
	assert.EqualValues(t, 0, resp.Size(), 0)
	assert.EqualValues(t, time.Now().Year(), resp.ModTime().Year())
	assert.EqualValues(t, time.Now().Month(), resp.ModTime().Month())
}

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

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

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

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