From 6f2ce21f55b9d2021e63fcd0c89b19e23084b885 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Wed, 16 Oct 2024 09:45:19 -0700 Subject: [PATCH] Move CheckLock function into testing only Signed-off-by: Derek Nola --- pkg/flock/flock_unix.go | 14 -------------- pkg/flock/flock_unix_test.go | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pkg/flock/flock_unix.go b/pkg/flock/flock_unix.go index db1a1d79d473..eb4253aa2621 100644 --- a/pkg/flock/flock_unix.go +++ b/pkg/flock/flock_unix.go @@ -19,9 +19,6 @@ limitations under the License. package flock import ( - "os/exec" - "strings" - "golang.org/x/sys/unix" ) @@ -49,14 +46,3 @@ func AcquireShared(path string) (int, error) { func Release(lock int) error { return unix.Flock(lock, unix.LOCK_UN) } - -// CheckLock checks whether any process is using the lock -func CheckLock(path string) bool { - lockByte, _ := exec.Command("lsof", "-w", "-F", "lfn", path).Output() - locks := string(lockByte) - if locks == "" { - return false - } - readWriteLock := strings.Split(locks, "\n")[2] - return readWriteLock == "lR" || readWriteLock == "lW" -} diff --git a/pkg/flock/flock_unix_test.go b/pkg/flock/flock_unix_test.go index a47015fa17a1..7eb8794aea85 100644 --- a/pkg/flock/flock_unix_test.go +++ b/pkg/flock/flock_unix_test.go @@ -19,9 +19,22 @@ limitations under the License. package flock import ( + "os/exec" + "strings" "testing" ) +// checkLock checks whether any process is using the lock +func checkLock(path string) bool { + lockByte, _ := exec.Command("lsof", "-w", "-F", "lfn", path).Output() + locks := string(lockByte) + if locks == "" { + return false + } + readWriteLock := strings.Split(locks, "\n")[2] + return readWriteLock == "lR" || readWriteLock == "lW" +} + func Test_UnitFlock(t *testing.T) { tests := []struct { name string @@ -45,7 +58,7 @@ func Test_UnitFlock(t *testing.T) { return } - if got := CheckLock(tt.path); got != tt.wantCheck { + if got := checkLock(tt.path); got != tt.wantCheck { t.Errorf("CheckLock() = %+v\nWant = %+v", got, tt.wantCheck) } @@ -53,7 +66,7 @@ func Test_UnitFlock(t *testing.T) { t.Errorf("Release() error = %v, wantErr %v", err, tt.wantErr) } - if got := CheckLock(tt.path); got == tt.wantCheck { + if got := checkLock(tt.path); got == tt.wantCheck { t.Errorf("CheckLock() = %+v\nWant = %+v", got, !tt.wantCheck) } })