Skip to content

Commit

Permalink
code coverage improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Lisca Ana-Roberta <[email protected]>
  • Loading branch information
aokirisaki authored and rchincha committed May 26, 2022
1 parent dbe23e5 commit e5a1467
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"testing"
"time"

godigest "github.com/opencontainers/go-digest"
"github.com/rs/zerolog"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/log"
. "zotregistry.io/zot/pkg/test"
)

Expand Down Expand Up @@ -305,3 +308,35 @@ func TestAuditLogMessages(t *testing.T) {
})
})
}

func TestLogErrors(t *testing.T) {
Convey("Get error with unknown log level", t, func() {
So(func() { _ = log.NewLogger("invalid", "test.out") }, ShouldPanic)
})

Convey("Get error when opening log file", t, func() {
dir := t.TempDir()
logPath := path.Join(dir, "logFile")
err := ioutil.WriteFile(logPath, []byte{}, 0o000)
So(err, ShouldBeNil)
So(func() {
_ = log.NewLogger(zerolog.DebugLevel.String(), logPath)
}, ShouldPanic)
})
}

func TestAuditLogErrors(t *testing.T) {
Convey("Get error with unknown log level", t, func() {
So(func() { _ = log.NewAuditLogger("invalid", "test.out") }, ShouldPanic)
})

Convey("Get error when opening log file", t, func() {
dir := t.TempDir()
auditLogPath := path.Join(dir, "auditLogFile")
err := ioutil.WriteFile(auditLogPath, []byte{}, 0o000)
So(err, ShouldBeNil)
So(func() {
_ = log.NewAuditLogger(zerolog.DebugLevel.String(), auditLogPath)
}, ShouldPanic)
})
}
74 changes: 74 additions & 0 deletions pkg/storage/storage_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
Expand Down Expand Up @@ -1166,3 +1167,76 @@ func randSeq(n int) string {

return string(buf)
}

func TestInitRepo(t *testing.T) {
Convey("Get error when creating BlobUploadDir subdir on initRepo", t, func() {
dir := t.TempDir()

log := log.Logger{Logger: zerolog.New(os.Stdout)}
metrics := monitoring.NewMetricsServer(false, log)
imgStore := storage.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics)

err := os.Mkdir(path.Join(dir, "test-dir"), 0o000)
So(err, ShouldBeNil)

err = imgStore.InitRepo("test-dir")
So(err, ShouldNotBeNil)
})
}

func TestValidateRepo(t *testing.T) {
Convey("Get error when unable to read directory", t, func() {
dir := t.TempDir()

log := log.Logger{Logger: zerolog.New(os.Stdout)}
metrics := monitoring.NewMetricsServer(false, log)
imgStore := storage.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics)

err := os.Mkdir(path.Join(dir, "test-dir"), 0o000)
So(err, ShouldBeNil)

_, err = imgStore.ValidateRepo("test-dir")
So(err, ShouldNotBeNil)
})
}

func TestGetRepositoriesError(t *testing.T) {
Convey("Get error when returning relative path", t, func() {
dir := t.TempDir()

log := log.Logger{Logger: zerolog.New(os.Stdout)}
metrics := monitoring.NewMetricsServer(false, log)
imgStore := storage.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics)

// create valid directory with permissions
err := os.Mkdir(path.Join(dir, "test-dir"), 0o755)
So(err, ShouldBeNil)

err = ioutil.WriteFile(path.Join(dir, "test-dir/test-file"), []byte("this is test file"), 0o000)
So(err, ShouldBeNil)

_, err = imgStore.GetRepositories()
So(err, ShouldBeNil)
})
}

func TestPutBlobChunkStreamed(t *testing.T) {
Convey("Get error on opening file", t, func() {
dir := t.TempDir()

log := log.Logger{Logger: zerolog.New(os.Stdout)}
metrics := monitoring.NewMetricsServer(false, log)
imgStore := storage.NewImageStore(dir, true, storage.DefaultGCDelay, true, true, log, metrics)

uuid, err := imgStore.NewBlobUpload("test")
So(err, ShouldBeNil)

var reader io.Reader
blobPath := imgStore.BlobUploadPath("test", uuid)
err = os.Chmod(blobPath, 0o000)
So(err, ShouldBeNil)

_, err = imgStore.PutBlobChunkStreamed("test", uuid, reader)
So(err, ShouldNotBeNil)
})
}

0 comments on commit e5a1467

Please sign in to comment.