2022-10-10 07:05:55 -05:00
|
|
|
//go:build sync && scrub && metrics && search
|
|
|
|
// +build sync,scrub,metrics,search
|
2021-11-10 09:31:03 -05:00
|
|
|
|
|
|
|
package test_test
|
|
|
|
|
|
|
|
import (
|
2023-02-10 00:04:52 -05:00
|
|
|
"context"
|
2022-05-10 03:28:26 -05:00
|
|
|
"encoding/json"
|
2023-01-09 15:37:44 -05:00
|
|
|
"fmt"
|
2021-11-10 09:31:03 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2022-10-19 11:26:46 -05:00
|
|
|
"time"
|
2021-11-10 09:31:03 -05:00
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2022-05-10 03:28:26 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2021-11-10 09:31:03 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2023-01-09 15:37:44 -05:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-08-16 03:57:09 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2022-01-19 14:54:17 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-11-10 09:31:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCopyFiles(t *testing.T) {
|
|
|
|
Convey("sourceDir does not exist", t, func() {
|
2022-01-19 14:54:17 -05:00
|
|
|
err := test.CopyFiles("/path/to/some/unexisting/directory", os.TempDir())
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("destDir is a file", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-11-10 09:31:03 -05:00
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../test/data", dir)
|
2021-11-10 09:31:03 -05:00
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
err := test.CopyFiles(dir, "/etc/passwd")
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("sourceDir does not have read permissions", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-11-10 09:31:03 -05:00
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
err := os.Chmod(dir, 0o300)
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
err = test.CopyFiles(dir, os.TempDir())
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("sourceDir has a subfolder that does not have read permissions", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-11-10 09:31:03 -05:00
|
|
|
|
|
|
|
sdir := "subdir"
|
2022-03-07 03:55:12 -05:00
|
|
|
err := os.Mkdir(path.Join(dir, sdir), 0o300)
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
err = test.CopyFiles(dir, os.TempDir())
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("sourceDir has a file that does not have read permissions", t, func() {
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-11-10 09:31:03 -05:00
|
|
|
|
|
|
|
filePath := path.Join(dir, "file.txt")
|
2022-09-02 07:56:02 -05:00
|
|
|
err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec
|
2021-11-10 09:31:03 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = os.Chmod(filePath, 0o300)
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
err = test.CopyFiles(dir, os.TempDir())
|
2021-11-10 09:31:03 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
2023-01-18 11:24:44 -05:00
|
|
|
Convey("sourceDir contains a folder starting with invalid characters", t, func() {
|
|
|
|
srcDir := t.TempDir()
|
|
|
|
dstDir := t.TempDir()
|
|
|
|
|
|
|
|
err := os.MkdirAll(path.Join(srcDir, "_trivy", "db"), 0o755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(path.Join(srcDir, "test-index"), 0o755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filePathTrivy := path.Join(srcDir, "_trivy", "db", "trivy.db")
|
|
|
|
err = os.WriteFile(filePathTrivy, []byte("some dummy file content"), 0o644) //nolint: gosec
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
content, err := json.Marshal(index)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(path.Join(srcDir, "test-index", "index.json"), content, 0o644) //nolint: gosec
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = test.CopyFiles(srcDir, dstDir)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = os.Stat(path.Join(dstDir, "_trivy", "db", "trivy.db"))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(os.IsNotExist(err), ShouldBeTrue)
|
|
|
|
|
|
|
|
_, err = os.Stat(path.Join(dstDir, "test-index", "index.json"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2023-01-19 11:54:05 -05:00
|
|
|
Convey("panic when sourceDir does not exist", t, func() {
|
|
|
|
So(func() { test.CopyTestFiles("/path/to/some/unexisting/directory", os.TempDir()) }, ShouldPanic)
|
|
|
|
})
|
2021-11-10 09:31:03 -05:00
|
|
|
}
|
2022-05-10 03:28:26 -05:00
|
|
|
|
|
|
|
func TestGetOciLayoutDigests(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
Convey("image path is wrong", t, func() {
|
|
|
|
So(func() { _, _, _ = test.GetOciLayoutDigests("inexistent-image") }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("no permissions when getting index", t, func() {
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "test-index"))
|
2022-05-10 03:28:26 -05:00
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
err := os.Chmod(path.Join(dir, "test-index", "index.json"), 0o000)
|
2022-05-10 03:28:26 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(func() { _, _, _ = test.GetOciLayoutDigests(path.Join(dir, "test-index")) }, ShouldPanic)
|
|
|
|
|
|
|
|
err = os.Chmod(path.Join(dir, "test-index", "index.json"), 0o755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("can't access manifest digest", t, func() {
|
2023-01-20 13:01:52 -05:00
|
|
|
test.CopyTestFiles("../../test/data/zot-test", path.Join(dir, "test-manifest"))
|
2022-05-10 03:28:26 -05:00
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
buf, err := os.ReadFile(path.Join(dir, "test-manifest", "index.json"))
|
2022-05-10 03:28:26 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
if err := json.Unmarshal(buf, &index); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(path.Join(dir, "test-manifest", "blobs/sha256", index.Manifests[0].Digest.Encoded()), 0o000)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(func() { _, _, _ = test.GetOciLayoutDigests(path.Join(dir, "test-manifest")) }, ShouldPanic)
|
|
|
|
|
|
|
|
err = os.Chmod(path.Join(dir, "test-manifest", "blobs/sha256", index.Manifests[0].Digest.Encoded()), 0o755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
func TestGetImageComponents(t *testing.T) {
|
|
|
|
Convey("Inject failures for unreachable lines", t, func() {
|
|
|
|
injected := test.InjectFailure(0)
|
|
|
|
if injected {
|
|
|
|
_, _, _, err := test.GetImageComponents(100)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("finishes successfully", t, func() {
|
|
|
|
_, _, _, err := test.GetImageComponents(100)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-19 11:26:46 -05:00
|
|
|
func TestWaitTillTrivyDBDownloadStarted(t *testing.T) {
|
|
|
|
Convey("finishes successfully", t, func() {
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
go func() {
|
|
|
|
test.WaitTillTrivyDBDownloadStarted(tempDir)
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(test.SleepTime)
|
|
|
|
|
|
|
|
_, err := os.Create(path.Join(tempDir, "trivy.db"))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-10 00:04:52 -05:00
|
|
|
func TestControllerManager(t *testing.T) {
|
|
|
|
Convey("Test StartServer Init() panic", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
|
|
|
|
// No storage configured
|
|
|
|
So(func() { ctlrManager.StartServer() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test RunServer panic", t, func() {
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
|
|
|
|
// Invalid port
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = "999999"
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := ctlr.Init(ctx)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(func() { ctlrManager.RunServer(ctx) }, ShouldPanic)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:53:28 -05:00
|
|
|
func TestUploadArtifact(t *testing.T) {
|
|
|
|
Convey("Put request results in an error", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
artifact := ispec.Artifact{}
|
|
|
|
|
|
|
|
err := test.UploadArtifact(baseURL, "test", &artifact)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadBlob(t *testing.T) {
|
|
|
|
Convey("Post request results in an error", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
err := test.UploadBlob(baseURL, "test", []byte("test"), "zot.com.test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Post request status differs from accepted", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
err := os.Chmod(tempDir, 0o400)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-11-23 13:53:28 -05:00
|
|
|
|
|
|
|
err = test.UploadBlob(baseURL, "test", []byte("test"), "zot.com.test")
|
|
|
|
So(err, ShouldEqual, test.ErrPostBlob)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Put request results in an error", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-11-23 13:53:28 -05:00
|
|
|
|
|
|
|
blob := new([]byte)
|
|
|
|
|
|
|
|
err := test.UploadBlob(baseURL, "test", *blob, "zot.com.test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Put request status differs from accepted", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-11-23 13:53:28 -05:00
|
|
|
|
|
|
|
blob := []byte("test")
|
|
|
|
blobDigest := godigest.FromBytes(blob)
|
|
|
|
layerPath := path.Join(tempDir, "test", "blobs", "sha256")
|
|
|
|
blobPath := path.Join(layerPath, blobDigest.String())
|
|
|
|
if _, err := os.Stat(layerPath); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(layerPath, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(blobPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(layerPath, 0o000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = os.Chmod(layerPath, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(file.Name())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := test.UploadBlob(baseURL, "test", blob, "zot.com.test")
|
|
|
|
So(err, ShouldEqual, test.ErrPutBlob)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Put request successful", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-11-23 13:53:28 -05:00
|
|
|
|
|
|
|
blob := []byte("test")
|
|
|
|
|
|
|
|
err := test.UploadBlob(baseURL, "test", blob, "zot.com.test")
|
|
|
|
So(err, ShouldEqual, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 03:57:09 -05:00
|
|
|
func TestUploadImage(t *testing.T) {
|
|
|
|
Convey("Post request results in an error", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = t.TempDir()
|
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: make([][]byte, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Post request status differs from accepted", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
err := os.Chmod(tempDir, 0o400)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: make([][]byte, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Put request results in an error", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = t.TempDir()
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: make([][]byte, 10), // invalid format that will result in an error
|
|
|
|
Config: ispec.Image{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Image uploaded successfully", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = t.TempDir()
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
layerBlob := []byte("test")
|
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: [][]byte{
|
|
|
|
layerBlob,
|
|
|
|
}, // invalid format that will result in an error
|
|
|
|
Config: ispec.Image{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
Convey("Upload image with authentification", t, func() {
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
user1 := "test"
|
|
|
|
password1 := "test"
|
|
|
|
testString1 := getCredString(user1, password1)
|
|
|
|
htpasswdPath := test.MakeHtpasswdFileFromString(testString1)
|
|
|
|
defer os.Remove(htpasswdPath)
|
|
|
|
conf.HTTP.Auth = &config.AuthConfig{
|
|
|
|
HTPasswd: config.AuthHTPasswd{
|
|
|
|
Path: htpasswdPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
conf.AccessControl = &config.AccessControlConfig{
|
|
|
|
Repositories: config.Repositories{
|
|
|
|
"repo": config.PolicyGroup{
|
|
|
|
Policies: []config.Policy{
|
|
|
|
{
|
|
|
|
Users: []string{user1},
|
|
|
|
Actions: []string{"read", "create"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultPolicy: []string{},
|
|
|
|
},
|
|
|
|
"inaccessibleRepo": config.PolicyGroup{
|
|
|
|
Policies: []config.Policy{
|
|
|
|
{
|
|
|
|
Users: []string{user1},
|
|
|
|
Actions: []string{"create"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultPolicy: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AdminPolicy: config.Policy{
|
|
|
|
Users: []string{},
|
|
|
|
Actions: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
|
|
|
ctlr.Config.Storage.RootDirectory = tempDir
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
Convey("Request fail while pushing layer", func() {
|
|
|
|
err := test.UploadImageWithBasicAuth(test.Image{Layers: [][]byte{{1, 2, 3}}}, "badURL", "", "", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Request status is not StatusOk while pushing layer", func() {
|
|
|
|
err := test.UploadImageWithBasicAuth(test.Image{Layers: [][]byte{{1, 2, 3}}}, baseURL, "repo", "", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Request fail while pushing config", func() {
|
|
|
|
err := test.UploadImageWithBasicAuth(test.Image{}, "badURL", "", "", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Request status is not StatusOk while pushing config", func() {
|
|
|
|
err := test.UploadImageWithBasicAuth(test.Image{}, baseURL, "repo", "", "")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-08-16 03:57:09 -05:00
|
|
|
Convey("Blob upload wrong response status code", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
layerBlob := []byte("test")
|
2022-10-22 15:46:13 -05:00
|
|
|
layerBlobDigest := godigest.FromBytes(layerBlob)
|
2022-08-16 03:57:09 -05:00
|
|
|
layerPath := path.Join(tempDir, "test", "blobs", "sha256")
|
|
|
|
|
|
|
|
if _, err := os.Stat(layerPath); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(layerPath, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
file, err := os.Create(path.Join(layerPath, layerBlobDigest.Encoded()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(layerPath, 0o000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = os.Chmod(layerPath, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(file.Name())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: [][]byte{
|
|
|
|
layerBlob,
|
|
|
|
}, // invalid format that will result in an error
|
|
|
|
Config: ispec.Image{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("CreateBlobUpload wrong response status code", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
layerBlob := []byte("test")
|
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: [][]byte{
|
|
|
|
layerBlob,
|
|
|
|
}, // invalid format that will result in an error
|
|
|
|
Config: ispec.Image{},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("CreateBlobUpload", func() {
|
|
|
|
injected := test.InjectFailure(2)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("UpdateBlobUpload", func() {
|
|
|
|
injected := test.InjectFailure(4)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func getCredString(username, password string) string {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usernameAndHash := fmt.Sprintf("%s:%s", username, string(hash))
|
|
|
|
|
|
|
|
return usernameAndHash
|
|
|
|
}
|
|
|
|
|
2022-08-16 03:57:09 -05:00
|
|
|
func TestInjectUploadImage(t *testing.T) {
|
|
|
|
Convey("Inject failures for unreachable lines", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
layerBlob := []byte("test")
|
|
|
|
layerPath := path.Join(tempDir, "test", ".uploads")
|
|
|
|
|
|
|
|
if _, err := os.Stat(layerPath); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(layerPath, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: [][]byte{
|
|
|
|
layerBlob,
|
|
|
|
}, // invalid format that will result in an error
|
|
|
|
Config: ispec.Image{},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("first marshal", func() {
|
|
|
|
injected := test.InjectFailure(0)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("CreateBlobUpload POST call", func() {
|
|
|
|
injected := test.InjectFailure(1)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("UpdateBlobUpload PUT call", func() {
|
|
|
|
injected := test.InjectFailure(3)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("second marshal", func() {
|
|
|
|
injected := test.InjectFailure(5)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImage(img, baseURL, "test")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:17:06 -05:00
|
|
|
func TestReadLogFileAndSearchString(t *testing.T) {
|
|
|
|
logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logPath := logFile.Name()
|
|
|
|
defer os.Remove(logPath)
|
|
|
|
|
|
|
|
Convey("Invalid path", t, func() {
|
|
|
|
_, err = test.ReadLogFileAndSearchString("invalidPath", "DB update completed, next update scheduled", 90*time.Second)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Time too short", t, func() {
|
|
|
|
ok, err := test.ReadLogFileAndSearchString(logPath, "invalid string", time.Microsecond)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(ok, ShouldBeFalse)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func TestInjectUploadImageWithBasicAuth(t *testing.T) {
|
|
|
|
Convey("Inject failures for unreachable lines", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
user := "user"
|
|
|
|
password := "password"
|
|
|
|
testString := getCredString(user, password)
|
|
|
|
htpasswdPath := test.MakeHtpasswdFileFromString(testString)
|
|
|
|
defer os.Remove(htpasswdPath)
|
|
|
|
conf.HTTP.Auth = &config.AuthConfig{
|
|
|
|
HTPasswd: config.AuthHTPasswd{
|
|
|
|
Path: htpasswdPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-01-20 13:01:52 -05:00
|
|
|
ctlrManager := test.NewControllerManager(ctlr)
|
|
|
|
ctlrManager.StartAndWait(port)
|
|
|
|
defer ctlrManager.StopServer()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
layerBlob := []byte("test")
|
|
|
|
layerPath := path.Join(tempDir, "test", ".uploads")
|
|
|
|
|
|
|
|
if _, err := os.Stat(layerPath); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(layerPath, 0o700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
img := test.Image{
|
|
|
|
Layers: [][]byte{
|
|
|
|
layerBlob,
|
|
|
|
}, // invalid format that will result in an error
|
|
|
|
Config: ispec.Image{},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("first marshal", func() {
|
|
|
|
injected := test.InjectFailure(0)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImageWithBasicAuth(img, baseURL, "test", "user", "password")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("CreateBlobUpload POST call", func() {
|
|
|
|
injected := test.InjectFailure(1)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImageWithBasicAuth(img, baseURL, "test", "user", "password")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("UpdateBlobUpload PUT call", func() {
|
|
|
|
injected := test.InjectFailure(3)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImageWithBasicAuth(img, baseURL, "test", "user", "password")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Convey("second marshal", func() {
|
|
|
|
injected := test.InjectFailure(5)
|
|
|
|
if injected {
|
|
|
|
err := test.UploadImageWithBasicAuth(img, baseURL, "test", "user", "password")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|