From 524892bfefd72a68aae6404000c680377cf466f8 Mon Sep 17 00:00:00 2001 From: Thom Carlin Date: Mon, 5 Aug 2024 13:28:48 -0400 Subject: [PATCH 1/2] Add flock test --- pkg/utils/flock.go | 2 +- pkg/utils/flock_test.go | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 pkg/utils/flock_test.go diff --git a/pkg/utils/flock.go b/pkg/utils/flock.go index 6e004f0e6..d8ba15808 100644 --- a/pkg/utils/flock.go +++ b/pkg/utils/flock.go @@ -18,7 +18,7 @@ type FLock struct { // TryFLock non-blockingly attempts to acquire a lock on the file. func TryFLock(filename string) (*FLock, error) { - fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY|syscall.O_CLOEXEC, 0o600) + fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY|syscall.O_CLOEXEC, syscall.S_IRUSR | syscall.S_IWUSR ) if err != nil { return nil, err } diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go new file mode 100644 index 000000000..9775ca20f --- /dev/null +++ b/pkg/utils/flock_test.go @@ -0,0 +1,86 @@ +//go:build !windows +// +build !windows + +package utils + +import ( + "reflect" + "testing" +) + +func TestTryFLock(t *testing.T) { + type args struct { + filename string + } + tests := []struct { + name string + args args + want *FLock + wantErr bool + }{ + { + name: "Positive", + args: args{ + filename: "", + }, + want: &FLock{}, + wantErr: false, + }, + { + name: "Negative", + args: args{ + filename: "", + }, + want: &FLock{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := TryFLock(tt.args.filename) + if (err != nil) != tt.wantErr { + t.Errorf("TryFLock() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("TryFLock() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestFLock_Unlock(t *testing.T) { + type fields struct { + fd int + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + { + name: "Positive", + fields: fields{ + fd: 1, + }, + wantErr: false, + }, + { + name: "Negative", + fields: fields{ + fd: -1, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + lock := &FLock{ + fd: tt.fields.fd, + } + if err := lock.Unlock(); (err != nil) != tt.wantErr { + t.Errorf("FLock.Unlock() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From b5f1613475d17252036fef3c1b9b4947d3c091fd Mon Sep 17 00:00:00 2001 From: Thom Carlin Date: Mon, 5 Aug 2024 13:43:16 -0400 Subject: [PATCH 2/2] Fix lint issues --- pkg/utils/flock.go | 2 +- pkg/utils/flock_test.go | 69 +++++++++++++++++++++----------------- pkg/utils/unixsock_test.go | 4 +-- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/pkg/utils/flock.go b/pkg/utils/flock.go index 6f38d80cd..0a328ba72 100644 --- a/pkg/utils/flock.go +++ b/pkg/utils/flock.go @@ -18,7 +18,7 @@ type FLock struct { // TryFLock non-blockingly attempts to acquire a lock on the file. func TryFLock(filename string) (*FLock, error) { - fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY|syscall.O_CLOEXEC, syscall.S_IRUSR | syscall.S_IWUSR ) + fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY|syscall.O_CLOEXEC, syscall.S_IRUSR|syscall.S_IWUSR) if err != nil { return nil, err } diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go index 9775ca20f..4755eead9 100644 --- a/pkg/utils/flock_test.go +++ b/pkg/utils/flock_test.go @@ -1,11 +1,14 @@ //go:build !windows // +build !windows -package utils +package utils_test import ( - "reflect" + "os" + "path/filepath" "testing" + + "github.com/ansible/receptor/pkg/utils" ) func TestTryFLock(t *testing.T) { @@ -15,35 +18,39 @@ func TestTryFLock(t *testing.T) { tests := []struct { name string args args - want *FLock + want *utils.FLock wantErr bool }{ { - name: "Positive", - args: args{ - filename: "", - }, - want: &FLock{}, - wantErr: false, + name: "Positive", + args: args{ + filename: filepath.Join(os.TempDir(), "good_flock_listener"), + }, + want: &utils.FLock{0}, + wantErr: false, }, { - name: "Negative", - args: args{ - filename: "", - }, - want: &FLock{}, - wantErr: true, + name: "Negative", + args: args{ + filename: "", + }, + want: &utils.FLock{}, + wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := TryFLock(tt.args.filename) + got, err := utils.TryFLock(tt.args.filename) if (err != nil) != tt.wantErr { - t.Errorf("TryFLock() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("%s: TryFLock(): error = %v, wantErr %v", tt.name, err, tt.wantErr) + return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("TryFLock() = %v, want %v", got, tt.want) + + if err == nil { + if got.Fd < 0 { + t.Errorf("%s: UnixSocketListen(): Invalid got Fd = %+v", tt.name, got) + } } }) } @@ -51,7 +58,7 @@ func TestTryFLock(t *testing.T) { func TestFLock_Unlock(t *testing.T) { type fields struct { - fd int + Fd int } tests := []struct { name string @@ -59,27 +66,27 @@ func TestFLock_Unlock(t *testing.T) { wantErr bool }{ { - name: "Positive", - fields: fields{ - fd: 1, + name: "Positive", + fields: fields{ + Fd: 1, }, - wantErr: false, + wantErr: false, }, { - name: "Negative", - fields: fields{ - fd: -1, + name: "Negative", + fields: fields{ + Fd: -1, }, - wantErr: true, + wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - lock := &FLock{ - fd: tt.fields.fd, + lock := &utils.FLock{ + Fd: tt.fields.Fd, } if err := lock.Unlock(); (err != nil) != tt.wantErr { - t.Errorf("FLock.Unlock() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("%s: FLock.Unlock() error = %v, wantErr %v", tt.name, err, tt.wantErr) } }) } diff --git a/pkg/utils/unixsock_test.go b/pkg/utils/unixsock_test.go index d5569fa89..0ffcdd770 100644 --- a/pkg/utils/unixsock_test.go +++ b/pkg/utils/unixsock_test.go @@ -32,7 +32,7 @@ func TestUnixSocketListen(t *testing.T) { { name: "Positive", args: args{ - filename: filepath.Join(os.TempDir(), "good_listener"), + filename: filepath.Join(os.TempDir(), "good_unixsock_listener"), permissions: 0x0400, }, wantErr: false, @@ -60,7 +60,7 @@ func TestUnixSocketListen(t *testing.T) { if err == nil { if got1.Fd < 0 { - t.Errorf("%s: UnixSocketListen(): Invalid got1 fd = %+v", tt.name, got1) + t.Errorf("%s: UnixSocketListen(): Invalid got1 Fd = %+v", tt.name, got1) } defer got1.Unlock()