diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index dd734ee0f..0e84eee79 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -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" ) @@ -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) + }) +} diff --git a/pkg/storage/storage_fs_test.go b/pkg/storage/storage_fs_test.go index f34973221..24c786e1d 100644 --- a/pkg/storage/storage_fs_test.go +++ b/pkg/storage/storage_fs_test.go @@ -6,6 +6,7 @@ import ( _ "crypto/sha256" "encoding/json" "fmt" + "io" "io/ioutil" "math/big" "os" @@ -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) + }) +}