Skip to content

Commit

Permalink
Improve test coverage (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Nov 21, 2023
1 parent a99bac1 commit 3403d66
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ignore:
- "internal/**/*"
- "cmd/**/*"
- "docs/*"
- "**/pkg/model/*"
- "**/pkg/shared/*"
- "**/pkg/stdio/*"
- "**/pkg/service/backup_*.go"
- "**/pkg/service/configuration_manager_*.go"
19 changes: 19 additions & 0 deletions pkg/service/configuration_service_routine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import (
"github.com/aws/smithy-go/ptr"
)

func TestRoutine_Add(t *testing.T) {
policy := "policy"
cluster := "cluster"
storage := "storage"
config := &model.Config{
BackupRoutines: make(map[string]*model.BackupRoutine),
BackupPolicies: map[string]*model.BackupPolicy{policy: {Name: &policy}},
AerospikeClusters: map[string]*model.AerospikeCluster{cluster: {Name: &cluster}},
Storage: map[string]*model.Storage{storage: {Name: &storage}},
}

routine := model.BackupRoutine{Storage: storage, SourceCluster: cluster}
err := AddRoutine(config, &routine)
if err != nil {
t.Errorf("AddRoutine failed, expected nil error, got %v", err)
}
}

func TestRoutine_AddErrors(t *testing.T) {
routine := "routine"
policy := "policy"
Expand All @@ -18,6 +36,7 @@ func TestRoutine_AddErrors(t *testing.T) {
routine model.BackupRoutine
}{
{name: "empty", routine: model.BackupRoutine{}},
{name: "existing", routine: model.BackupRoutine{Storage: storage, SourceCluster: cluster, Name: routine}},
{name: "no storage", routine: model.BackupRoutine{SourceCluster: cluster}},
{name: "no cluster", routine: model.BackupRoutine{Storage: storage}},
{name: "wrong storage", routine: model.BackupRoutine{Storage: wrong, SourceCluster: cluster}},
Expand Down
33 changes: 32 additions & 1 deletion pkg/service/configuration_service_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ func TestStorage_Add(t *testing.T) {
}
}

func TestStorage_AddFailure(t *testing.T) {
config := &model.Config{
Storage: map[string]*model.Storage{},
}

newStorage := &model.Storage{
Name: ptr.String("storage"),
}

err := AddStorage(config, newStorage)
if err == nil {
t.Errorf("Expected validation error")
}
}

func TestStorage_Update(t *testing.T) {
storage := "storage"
config := &model.Config{
Expand Down Expand Up @@ -61,6 +76,22 @@ func TestStorage_Update(t *testing.T) {
}
}

func TestStorage_UpdateFailure(t *testing.T) {
storage := "storage"
config := &model.Config{
Storage: map[string]*model.Storage{storage: {Name: &storage}},
}

newStorage := &model.Storage{
Name: ptr.String(storage),
}

err := UpdateStorage(config, newStorage)
if err == nil {
t.Errorf("Expected validation error")
}
}

func TestStorage_Delete(t *testing.T) {
storage := "storage"
storage2 := "storage2"
Expand All @@ -83,7 +114,7 @@ func TestStorage_Delete(t *testing.T) {
t.Errorf("Expected nil error, got %v", err)
}
if len(config.Storage) != 1 {
t.Errorf("Expected size = 1")
t.Errorf("Expected config storage size to be 1")
}

// Deleting a non-existent storage should result in an error
Expand Down
29 changes: 29 additions & 0 deletions pkg/service/restore_memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package service

import (
"testing"

"github.com/aerospike/backup/pkg/model"
"github.com/aerospike/backup/pkg/util"
)

func TestRestoreMemory(t *testing.T) {
restoreService := NewRestoreMemory()
restoreRequest := &model.RestoreRequest{
Host: util.Ptr("localhost"),
Port: util.Ptr(int32(3000)),
Directory: util.Ptr("./testout/backup"),
SetList: []string{"set1"},
}
jobID := restoreService.Restore(restoreRequest)

jobStatus := restoreService.JobStatus(jobID)
if jobStatus != jobStatusRunning {
t.Errorf("Expected jobStatus to be %s", jobStatusRunning)
}

wrongJobStatus := restoreService.JobStatus(1111)
if wrongJobStatus != jobStatusNA {
t.Errorf("Expected jobStatus to be %s", jobStatusNA)
}
}
40 changes: 40 additions & 0 deletions pkg/util/loading_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package util

import (
"context"
"errors"
"strconv"
"testing"
)

func TestLoadingCache(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cache := NewLoadingCache(ctx, func(s string) (any, error) {
return strconv.Atoi(s)
})
value, _ := cache.Get("1")
if value != 1 {
t.Error("The value is expected to be 1")
}
value, _ = cache.Get("1") // existing
if value != 1 {
t.Error("The value is expected to be 1")
}
go cache.startCleanup()
cancel()
cache.clean()
value, _ = cache.Get("2")
if value != 2 {
t.Error("The value is expected to be 2")
}
}

func TestLoadingCache_Error(t *testing.T) {
cache := NewLoadingCache(context.Background(), func(s string) (any, error) {
return nil, errors.New("error")
})
_, err := cache.Get("1")
if err == nil {
t.Error("Error must not be nil")
}
}
5 changes: 4 additions & 1 deletion pkg/util/log_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package util

import (
"strings"
"testing"
)

func Test_libLogRegex(t *testing.T) {
tests := []string{
"2023-01-01 00:00:00 UTC [INF] [74813] Starting backup for 4096 partitions from 0 to 4095",
"2023-11-08 09:44:48 GMT [ERR] [ 14] The max S3 object size is 5497558138880"}
"2023-11-08 09:44:48 GMT [ERR] [ 14] The max S3 object size is 5497558138880",
}

for _, e := range tests {
match := libLogRegex.FindStringSubmatch(e)
if len(match) != 5 {
t.Fatal("libLogRegex error")
}
}
LogCaptured(strings.Join(tests, "\n"))
}
6 changes: 4 additions & 2 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ func Ptr[T any](obj T) *T {
return &obj
}

func Find[T any](collection map[string]T, f func(T) bool) (T, bool) {
for _, item := range collection {
// Find returns the first element from the given map that satisfies
// the predicate f.
func Find[T any](items map[string]T, f func(T) bool) (T, bool) {
for _, item := range items {
if f(item) {
return item, true
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package util

import (
"testing"
)

func TestPtr(t *testing.T) {
var n int32
np := Ptr(n)
if n != *np {
t.Error("Expected to be equal")
}
}

func TestFind(t *testing.T) {
elements := map[string]int{"a": 1}
_, found := Find(elements, func(i int) bool { return i == 1 })
if !found {
t.Error("Expected to be found")
}
_, found = Find(elements, func(i int) bool { return i == 2 })
if found {
t.Error("Expected not to be found")
}
}

0 comments on commit 3403d66

Please sign in to comment.