Skip to content

Commit

Permalink
Improve test coverage and fix warnings (#180)
Browse files Browse the repository at this point in the history
* add os accessor tests

* remove unused method

* remove unused method and fix warnings

* add retry timer test

* add TestCalculateConfigurationBackupPath

* add os accessor tests

* add restore test

* fix imports

* add state read test
  • Loading branch information
korotkov-aerospike authored Apr 3, 2024
1 parent 59d0e9a commit 6262cd7
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 66 deletions.
6 changes: 3 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type HTTPServer struct {
backupBackends service.BackendsHolder
}

// NewHTTPServer returns a new instance of HTTPServer.
//
// Annotations to generate OpenAPI description (https://github.com/swaggo/swag)
// @title Backup Service REST API Specification
// @version 0.3.0
Expand All @@ -95,8 +97,6 @@ type HTTPServer struct {
//
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
//
// NewHTTPServer returns a new instance of HTTPServer.
func NewHTTPServer(backends service.BackendsHolder, config *model.Config,
scheduler quartz.Scheduler) *HTTPServer {
serverConfig := config.ServiceConfig.HTTPServer
Expand Down Expand Up @@ -211,7 +211,7 @@ func (ws *HTTPServer) Start() {

ws.server.Handler = ws.rateLimiterMiddleware(mux)
err := ws.server.ListenAndServe()
if strings.Contains(err.Error(), "Server closed") {
if err != nil && strings.Contains(err.Error(), "Server closed") {
slog.Info(err.Error())
} else {
panic(err)
Expand Down
38 changes: 2 additions & 36 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

import (
"bytes"
"fmt"
"io"
"log/slog"
Expand All @@ -16,7 +15,7 @@ import (
// LogHandler returns the application log handler with the
// configured level.
func LogHandler(config *model.LoggerConfig) slog.Handler {
addSource := true
const addSource = true
writer := logWriter(config)
switch strings.ToUpper(config.GetFormatOrDefault()) {
case "PLAIN":
Expand Down Expand Up @@ -80,43 +79,10 @@ func logLevel(level string) slog.Level {
}
}

// Check panics if the error is not nil.
func Check(e error) {
if e != nil {
panic(e)
}
}

// Returns an exit value for the error.
// ToExitVal returns an exit value for the error.
func ToExitVal(err error) int {
if err != nil {
return 1
}
return 0
}

// CaptureStdout returns the stdout output written during the
// given function execution.
func CaptureStdout(f func()) string {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w

f() // run the function

outC := make(chan string)
// copy the output in a separate goroutine so printing
// can't block indefinitely
go func() {
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
Check(err)
outC <- buf.String()
}()

// back to normal state
_ = w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC
return out
}
2 changes: 1 addition & 1 deletion modules/aerospike-tools-backup
15 changes: 10 additions & 5 deletions pkg/model/aerospike_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestAerospikeCluster_GetPassword(t *testing.T) {
if test.expectedErr {
assert.Nil(t, password)
}
os.RemoveAll(testdataFolder)
_ = os.RemoveAll(testdataFolder)
})
}
}
Expand All @@ -87,7 +87,7 @@ func TestAerospikeCluster_GetPasswordCaching(t *testing.T) {
assert.Equal(t, ptr.String("password"), password)

// remove file to ensure second call will not read it
os.RemoveAll(testdataFolder)
_ = os.RemoveAll(testdataFolder)

// Make a second call to GetPassword and check if the returned passwords are same
passwordAfterCache := cluster.GetPassword()
Expand All @@ -108,8 +108,13 @@ func TestAerospikeCluster_GetPasswordFromCredentials(t *testing.T) {

func createValidFile() {
text := []byte("password")
os.MkdirAll(testdataFolder, 0744)
_ = os.MkdirAll(testdataFolder, 0744)
f, _ := os.OpenFile(passwordPath, os.O_WRONLY|os.O_CREATE, 0644)
defer f.Close()
f.Write(text)
defer func(f *os.File) {
err := f.Close()
if err != nil {
panic(err)
}
}(f)
_, _ = f.Write(text)
}
5 changes: 0 additions & 5 deletions pkg/model/time_bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ func NewTimeBoundsTo(toTime int64) (*TimeBounds, error) {
return NewTimeBounds(nil, &toTime)
}

// NewTimeBoundsFrom creates a new TimeBounds from the provided fromTime.
func NewTimeBoundsFrom(toTime int64) (*TimeBounds, error) {
return NewTimeBounds(nil, &toTime)
}

// NewTimeBoundsFromString creates a TimeBounds from the string representation of
// time boundaries.
func NewTimeBoundsFromString(from, to string) (*TimeBounds, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/backup_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (b *BackupBackend) FullBackupList(timebounds *model.TimeBounds) ([]model.Ba
func (b *BackupBackend) detailsFromPaths(timebounds *model.TimeBounds, useCache bool,
paths ...string) []model.BackupDetails {
// each path contains a backup of specific time
backupDetails := []model.BackupDetails{}
backupDetails := make([]model.BackupDetails, 0, len(paths))
for _, path := range paths {
namespaces, err := b.lsDir(filepath.Join(path, model.DataDirectory))
if err != nil {
Expand Down
Loading

0 comments on commit 6262cd7

Please sign in to comment.