Skip to content

Commit

Permalink
tests: add tests for vitess/go/cmd/zk/internal/zkfilepath (#14970)
Browse files Browse the repository at this point in the history
Signed-off-by: Eshaan Aggarwal <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Co-authored-by: Matt Lord <[email protected]>
  • Loading branch information
EshaanAgg and mattlord authored Jan 18, 2024
1 parent 9036de8 commit 2ec885d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 14 deletions.
97 changes: 97 additions & 0 deletions go/cmd/zk/internal/zkfilepath/zfilepath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package zkfilepath

import (
"testing"
"time"

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

func TestClean(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"/path/to/some/dir/", "/path/to/some/dir"},
{"/", "/"},
{"", "."},
{"/root", "/root"},
{"no/slash/at/the/end", "no/slash/at/the/end"},
}

for _, test := range tests {
result := Clean(test.input)
assert.Equal(t, test.expected, result)
}
}

func TestFormat(t *testing.T) {
testTime := time.Now()
stat := &zk.Stat{
DataLength: 100,
NumChildren: 1,
Mtime: testTime.UnixMilli(),
EphemeralOwner: 1,
}

tests := []struct {
stat *zk.Stat
zkPath string
showFullPath bool
longListing bool
expected string
}{
// Checking the effect of showFullPath without longListing
{stat, "/path/to/node", true, false, "/path/to/node\n"},
{stat, "/path/to/node", false, false, "node\n"},

// Checking the effect of showFullPath with longListing
{stat, "/path/to/node", true, true, "drwxrwxrwx zk zk 100 " + testTime.Format(TimeFmt) + " /path/to/node\n"},
{stat, "/path/to/node", false, true, "drwxrwxrwx zk zk 100 " + testTime.Format(TimeFmt) + " node\n"},
}

for _, test := range tests {
result := Format(test.stat, test.zkPath, test.showFullPath, test.longListing)
assert.Equal(t, test.expected, result)
}
}

func TestGetPermissions(t *testing.T) {
tests := []struct {
numChildren int32
dataLength int32
ephemeralOwner int64
expected string
}{
// Children, Data, Ephemeral, Expected
{0, 0, 0, "-rw-rw-rw-"},
{1, 1, 0, "drwxrwxrwx"},
{1, 0, 123, "drwxrwxrwx"},
{0, 1, 1, "erw-rw-rw-"},
{1, 1, 0, "drwxrwxrwx"},
{0, 0, 1, "erw-rw-rw-"},
{0, 0, 0, "-rw-rw-rw-"},
}

for _, test := range tests {
result := getPermissions(test.numChildren, test.dataLength, test.ephemeralOwner)
assert.Equal(t, test.expected, result)
}
}
34 changes: 20 additions & 14 deletions go/cmd/zk/internal/zkfilepath/zkfilepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Clean(zkPath string) string {

// Format returns a path formatted to a canonical string.
func Format(stat *zk.Stat, zkPath string, showFullPath bool, longListing bool) string {
var name, perms string
var name string

if !showFullPath {
name = path.Base(zkPath)
Expand All @@ -53,23 +53,29 @@ func Format(stat *zk.Stat, zkPath string, showFullPath bool, longListing bool) s
}

if longListing {
if stat.NumChildren > 0 {
// FIXME(msolomon) do permissions check?
perms = "drwxrwxrwx"
if stat.DataLength > 0 {
// give a visual indication that this node has data as well as children
perms = "nrw-rw-rw-"
}
} else if stat.EphemeralOwner != 0 {
perms = "erw-rw-rw-"
} else {
perms = "-rw-rw-rw-"
}
// always print the Local version of the time. zookeeper's
perms := getPermissions(stat.NumChildren, stat.DataLength, stat.EphemeralOwner)

// Always print the Local version of the time. zookeeper's
// go / C library would return a local time anyway, but
// might as well be sure.
return fmt.Sprintf("%v %v %v % 8v % 20v %v\n", perms, "zk", "zk", stat.DataLength, zk2topo.Time(stat.Mtime).Local().Format(TimeFmt), name)
} else {
return fmt.Sprintf("%v\n", name)
}
}

// Utility function to return the permissions for a node
func getPermissions(numChildren int32, dataLength int32, ephemeralOwner int64) string {
if numChildren > 0 {
// FIXME(msolomon) do permissions check?
if dataLength > 0 {
// give a visual indication that this node has data as well as children
return "drwxrwxrwx"
}
return "drwxrwxrwx"
} else if ephemeralOwner != 0 {
return "erw-rw-rw-"
} else {
return "-rw-rw-rw-"
}
}

0 comments on commit 2ec885d

Please sign in to comment.