From 2ec885d348f4c55bd70e9fbb8b76f9d1206f2ee8 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:15:26 +0530 Subject: [PATCH] tests: add tests for `vitess/go/cmd/zk/internal/zkfilepath` (#14970) Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Signed-off-by: Matt Lord Co-authored-by: Matt Lord --- .../zk/internal/zkfilepath/zfilepath_test.go | 97 +++++++++++++++++++ go/cmd/zk/internal/zkfilepath/zkfilepath.go | 34 ++++--- 2 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 go/cmd/zk/internal/zkfilepath/zfilepath_test.go diff --git a/go/cmd/zk/internal/zkfilepath/zfilepath_test.go b/go/cmd/zk/internal/zkfilepath/zfilepath_test.go new file mode 100644 index 00000000000..2ab649fb3a4 --- /dev/null +++ b/go/cmd/zk/internal/zkfilepath/zfilepath_test.go @@ -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) + } +} diff --git a/go/cmd/zk/internal/zkfilepath/zkfilepath.go b/go/cmd/zk/internal/zkfilepath/zkfilepath.go index 7d5b76cc664..2e9edb1fdf8 100644 --- a/go/cmd/zk/internal/zkfilepath/zkfilepath.go +++ b/go/cmd/zk/internal/zkfilepath/zkfilepath.go @@ -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) @@ -53,19 +53,9 @@ 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) @@ -73,3 +63,19 @@ func Format(stat *zk.Stat, zkPath string, showFullPath bool, longListing bool) s 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-" + } +}