Skip to content

Commit

Permalink
tests: use NilError
Browse files Browse the repository at this point in the history
  • Loading branch information
sgotti committed Dec 15, 2023
1 parent 798d135 commit d0386d9
Show file tree
Hide file tree
Showing 22 changed files with 1,250 additions and 2,235 deletions.
6 changes: 3 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/sorintlab/errors"
"k8s.io/apimachinery/pkg/api/resource"

"agola.io/agola/internal/testutil"
"agola.io/agola/internal/util"
"agola.io/agola/services/types"
)
Expand Down Expand Up @@ -604,9 +605,8 @@ func TestParseOutput(t *testing.T) {
t.Parallel()

out, err := ParseConfig([]byte(tt.in), ConfigFormatJSON, &ConfigContext{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
testutil.NilError(t, err)

if diff := cmp.Diff(tt.out, out, cmp.Comparer(func(x, y *resource.Quantity) bool {
if x == nil && y == nil {
return true
Expand Down
63 changes: 27 additions & 36 deletions internal/objectstorage/objectstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"reflect"
"strings"
"testing"

"agola.io/agola/internal/testutil"
)

func setupPosix(t *testing.T, dir string) (*PosixStorage, error) {
Expand All @@ -49,17 +51,13 @@ func TestList(t *testing.T) {
dir := t.TempDir()

ps, err := setupPosix(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

pfs, err := setupPosixFlat(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

s3s, err := setupS3(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

type listop struct {
prefix string
Expand Down Expand Up @@ -300,17 +298,13 @@ func TestWriteObject(t *testing.T) {
dir := t.TempDir()

ps, err := setupPosix(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

pfs, err := setupPosixFlat(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

s3s, err := setupS3(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

newBuf := func(n int64) *bytes.Buffer {
testBytes := make([]byte, n)
Expand All @@ -335,27 +329,25 @@ func TestWriteObject(t *testing.T) {
// Test write without size. Should write whole buffer.
buf := newBuf(n)
objName := "obj01"
if err := os.WriteObject(objName, buf, -1, false); err != nil {
t.Fatalf("unexpected err: %v", err)
}
err := os.WriteObject(objName, buf, -1, false)
testutil.NilError(t, err)

oi, err := os.Stat(objName)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

if oi.Size != n {
t.Fatalf("expected object size: %d, got %d", n, oi.Size)
}

// Test write with object size equal to buf size.
buf = newBuf(n)
objName = "obj02"
if err := os.WriteObject(objName, buf, n, false); err != nil {
t.Fatalf("unexpected err: %v", err)
}
err := os.WriteObject(objName, buf, n, false)
testutil.NilError(t, err)

oi, err = os.Stat(objName)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

if oi.Size != n {
t.Fatalf("expected object size: %d, got %d", n, oi.Size)
}
Expand All @@ -364,13 +356,12 @@ func TestWriteObject(t *testing.T) {
buf = newBuf(n)
objName = "obj03"
size := int64(800)
if err := os.WriteObject(objName, buf, int64(size), false); err != nil {
t.Fatalf("unexpected err: %v", err)
}
err := os.WriteObject(objName, buf, int64(size), false)
testutil.NilError(t, err)

oi, err = os.Stat(objName)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

if oi.Size != size {
t.Fatalf("expected object size: %d, got %d", size, oi.Size)
}
Expand Down
28 changes: 13 additions & 15 deletions internal/objectstorage/posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"path/filepath"
"testing"

"agola.io/agola/internal/testutil"
)

func TestPosixDeleteObject(t *testing.T) {
Expand All @@ -27,28 +29,24 @@ func TestPosixDeleteObject(t *testing.T) {
dir := t.TempDir()

ls, err := NewPosix(dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

for _, obj := range objects {
if err := ls.WriteObject(obj, bytes.NewReader([]byte{}), 0, true); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := ls.DeleteObject(obj); err != nil {
t.Fatalf("unexpected err: %v", err)
}
err := ls.WriteObject(obj, bytes.NewReader([]byte{}), 0, true)
testutil.NilError(t, err)

err := ls.DeleteObject(obj)
testutil.NilError(t, err)

}

// no files and directories should be left
bd, err := os.Open(filepath.Join(dir, dataDirName))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

files, err := bd.Readdirnames(0)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

if len(files) > 0 {
t.Fatalf("expected 0 files got %d files", len(files))
}
Expand Down
27 changes: 12 additions & 15 deletions internal/objectstorage/posixflat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"
"testing"

"agola.io/agola/internal/testutil"
"github.com/sorintlab/errors"
)

Expand Down Expand Up @@ -84,28 +85,24 @@ func TestPosixFlatDeleteObject(t *testing.T) {
dir := t.TempDir()

ls, err := NewPosixFlat(dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

for _, obj := range objects {
if err := ls.WriteObject(obj, bytes.NewReader([]byte{}), 0, true); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := ls.DeleteObject(obj); err != nil {
t.Fatalf("unexpected err: %v", err)
}
err := ls.WriteObject(obj, bytes.NewReader([]byte{}), 0, true)
testutil.NilError(t, err)

err := ls.DeleteObject(obj)
testutil.NilError(t, err)

}

// no files and directories should be left
bd, err := os.Open(filepath.Join(dir, dataDirName))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

files, err := bd.Readdirnames(0)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

if len(files) > 0 {
t.Fatalf("expected 0 files got %d files", len(files))
}
Expand Down
6 changes: 3 additions & 3 deletions internal/services/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"agola.io/agola/internal/testutil"
"github.com/google/go-cmp/cmp"
"github.com/sorintlab/errors"
)
Expand Down Expand Up @@ -823,9 +824,8 @@ gitserver:

content := []byte(tt.in)
err := os.WriteFile(path.Join(dir, "config.yml"), content, 0644)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
testutil.NilError(t, err)

c, err := Parse(path.Join(dir, "config.yml"), tt.services)
if err != nil {
if tt.err == nil {
Expand Down
Loading

0 comments on commit d0386d9

Please sign in to comment.