Skip to content

Commit

Permalink
Add flock test (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored Aug 5, 2024
1 parent 06da333 commit 32e4786
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
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

0 comments on commit 32e4786

Please sign in to comment.