Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flock test #1110

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/utils/flock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
93 changes: 93 additions & 0 deletions pkg/utils/flock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//go:build !windows
// +build !windows

package utils_test

import (
"os"
"path/filepath"
"testing"

"github.com/ansible/receptor/pkg/utils"
)

func TestTryFLock(t *testing.T) {
type args struct {
filename string
}
tests := []struct {
name string
args args
want *utils.FLock
wantErr bool
}{
{
name: "Positive",
args: args{
filename: filepath.Join(os.TempDir(), "good_flock_listener"),
},
want: &utils.FLock{0},
wantErr: false,
},
{
name: "Negative",
args: args{
filename: "",
},
want: &utils.FLock{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := utils.TryFLock(tt.args.filename)
if (err != nil) != tt.wantErr {
t.Errorf("%s: TryFLock(): error = %v, wantErr %v", tt.name, err, tt.wantErr)

return
}

if err == nil {
if got.Fd < 0 {
t.Errorf("%s: UnixSocketListen(): Invalid got Fd = %+v", tt.name, got)
}
}
})
}
}

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 := &utils.FLock{
Fd: tt.fields.Fd,
}
if err := lock.Unlock(); (err != nil) != tt.wantErr {
t.Errorf("%s: FLock.Unlock() error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/utils/unixsock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down