Skip to content

Commit

Permalink
add tests for zkfs utilities
Browse files Browse the repository at this point in the history
Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com>
  • Loading branch information
EshaanAgg committed Jan 20, 2024
1 parent 9af8692 commit 985eb20
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go/cmd/zk/internal/zkfs/zkfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func IsFile(path string) bool {

// ParsePermMode parses the mode string as a perm mask.
func ParsePermMode(mode string) (mask int32) {
if len(mode) < 2 {
panic("invalid mode")
}

for _, c := range mode[2:] {
mask |= charPermMap[string(c)]
}
Expand Down
79 changes: 79 additions & 0 deletions go/cmd/zk/internal/zkfs/zkfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package zkfs

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/z-division/go-zookeeper/zk"
)

func TestFS_CopyContext(t *testing.T) {
// fs := &FS{}

// Create a temporary directories for testing
srcPath, err := os.MkdirTemp("", "source")
assert.NoError(t, err)
defer os.RemoveAll(srcPath)

dstPath, err := os.MkdirTemp("", "destination")
assert.NoError(t, err)
defer os.RemoveAll(dstPath)

}

func TestIsFile(t *testing.T) {
assert.True(t, IsFile("/zk/somepath"))
assert.False(t, IsFile("/nonzk/somepath"))
assert.False(t, IsFile("nonzkpath"))
}

func TestParsePermMode(t *testing.T) {
assert.Equal(t, int32(0), ParsePermMode("zk"))
assert.Equal(t, int32(zk.PermRead|zk.PermWrite), ParsePermMode("zkrw"))
assert.Equal(t, int32(zk.PermRead|zk.PermWrite|zk.PermAdmin), ParsePermMode("zkrwa"))
assert.PanicsWithError(t, "invalid mode", func() {
ParsePermMode("")
ParsePermMode("z")
})
}

func TestFormatACL(t *testing.T) {
testCases := []struct {
name string
acl zk.ACL
expected string
}{
{
name: "Full Permissions",
acl: zk.ACL{Perms: zk.PermAll},
expected: "rwdca",
},
{
name: "Read and Write Permissions",
acl: zk.ACL{Perms: zk.PermRead | zk.PermWrite},
expected: "rw---",
},
{
name: "No Permissions",
acl: zk.ACL{Perms: 0},
expected: "-----",
},
{
name: "Create and Admin Permissions",
acl: zk.ACL{Perms: zk.PermAdmin | zk.PermCreate},
expected: "---ca",
},
{
name: "Mixed Permissions",
acl: zk.ACL{Perms: zk.PermRead | zk.PermDelete | zk.PermAdmin},
expected: "r-d-a",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, FormatACL(tc.acl))
})
}
}

0 comments on commit 985eb20

Please sign in to comment.