refactor: Cleanup/simplify testcases in /pkg/extensions (#1116)

Signed-off-by: Nicol Draghici <idraghic@cisco.com>

fix: Increase coverage when copying files

Signed-off-by: Nicol Draghici <idraghic@cisco.com>
This commit is contained in:
Nicol 2023-01-19 18:54:05 +02:00 committed by GitHub
parent b997176363
commit 70a60b4660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 224 additions and 719 deletions

View File

@ -4,7 +4,6 @@
package extensions_test
import (
"context"
"os"
"testing"
@ -21,7 +20,6 @@ func TestEnableExtension(t *testing.T) {
Convey("Verify log if sync disabled in config", t, func() {
globalDir := t.TempDir()
port := test.GetFreePort()
baseURL := test.GetBaseURL(port)
conf := config.New()
falseValue := false
@ -42,21 +40,13 @@ func TestEnableExtension(t *testing.T) {
defer os.Remove(logFile.Name()) // cleanup
ctlr := api.NewController(conf)
ctlrManager := test.NewControllerManager(ctlr)
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctlrManager.StopServer()
ctlr.Config.Storage.RootDirectory = globalDir
go func() {
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
test.WaitTillServerReady(baseURL)
ctlrManager.StartAndWait(port)
data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)
@ -71,7 +61,6 @@ func TestMetricsExtension(t *testing.T) {
conf := config.New()
port := test.GetFreePort()
conf.HTTP.Port = port
baseURL := test.GetBaseURL(port)
logFile, err := os.CreateTemp(globalDir, "zot-log*.txt")
So(err, ShouldBeNil)
@ -87,6 +76,7 @@ func TestMetricsExtension(t *testing.T) {
defer os.Remove(logFile.Name()) // cleanup
ctlr := api.NewController(conf)
ctlrManager := test.NewControllerManager(ctlr)
subPaths := make(map[string]config.StorageConfig)
subPaths["/a"] = config.StorageConfig{}
@ -94,12 +84,7 @@ func TestMetricsExtension(t *testing.T) {
ctlr.Config.Storage.RootDirectory = globalDir
ctlr.Config.Storage.SubPaths = subPaths
go func() {
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
test.WaitTillServerReady(baseURL)
ctlrManager.StartAndWait(port)
data, _ := os.ReadFile(logFile.Name())

View File

@ -4,7 +4,6 @@
package lint_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
@ -56,16 +55,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -104,16 +100,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -152,16 +145,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -206,16 +196,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -295,16 +282,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -383,16 +367,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -435,16 +416,13 @@ func TestVerifyMandatoryAnnotations(t *testing.T) {
ctlr := api.NewController(conf)
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
ctlr.Config.Storage.RootDirectory = dir
cm := test.NewControllerManager(ctlr)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm.StartAndWait(port)
defer cm.StopServer()
resp, err := resty.R().SetBasicAuth(username, passphrase).
Get(baseURL + "/v2/zot-test/manifests/0.0.1")
@ -486,10 +464,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
@ -519,10 +494,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
@ -552,10 +524,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json"))
@ -616,10 +585,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json"))
@ -679,10 +645,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json"))
@ -744,10 +707,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json"))
@ -818,10 +778,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
dir := t.TempDir()
err := test.CopyFiles("../../../test/data", dir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", dir)
var index ispec.Index
buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json"))
@ -907,16 +864,3 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) {
}
})
}
func startServer(c *api.Controller) {
// this blocks
ctx := context.Background()
if err := c.Run(ctx); err != nil {
return
}
}
func stopServer(c *api.Controller) {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}

View File

@ -4,7 +4,6 @@
package monitoring_test
import (
"context"
"net/http"
"path"
"testing"
@ -40,9 +39,9 @@ func TestExtensionMetrics(t *testing.T) {
ctlr := api.NewController(conf)
So(ctlr, ShouldNotBeNil)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
// improve code coverage
ctlr.Metrics.SendMetric(baseURL)
@ -56,10 +55,7 @@ func TestExtensionMetrics(t *testing.T) {
monitoring.IncDownloadCounter(ctlr.Metrics, "alpine")
monitoring.IncUploadCounter(ctlr.Metrics, "alpine")
err := test.CopyFiles("../../../test/data/zot-test", path.Join(rootDir, "alpine"))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(rootDir, "alpine"))
monitoring.SetStorageUsage(ctlr.Metrics, rootDir, "alpine")
monitoring.ObserveStorageLockLatency(ctlr.Metrics, time.Millisecond, rootDir, "RWLock")
@ -91,9 +87,9 @@ func TestExtensionMetrics(t *testing.T) {
ctlr := api.NewController(conf)
So(ctlr, ShouldNotBeNil)
go startServer(ctlr)
defer stopServer(ctlr)
test.WaitTillServerReady(baseURL)
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
defer cm.StopServer()
So(ctlr.Metrics.IsEnabled(), ShouldBeFalse)
@ -103,15 +99,3 @@ func TestExtensionMetrics(t *testing.T) {
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
})
}
func startServer(c *api.Controller) {
// this blocks
if err := c.Run(context.Background()); err != nil {
return
}
}
func stopServer(c *api.Controller) {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}

View File

@ -4,7 +4,6 @@
package scrub_test
import (
"context"
"fmt"
"os"
"path"
@ -13,7 +12,6 @@ import (
godigest "github.com/opencontainers/go-digest"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
@ -34,7 +32,6 @@ const (
func TestScrubExtension(t *testing.T) {
Convey("Blobs integrity not affected", t, func(c C) {
port := test.GetFreePort()
url := test.GetBaseURL(port)
logFile, err := os.CreateTemp("", "zot-log*.txt")
So(err, ShouldBeNil)
@ -62,33 +59,13 @@ func TestScrubExtension(t *testing.T) {
ctlr := api.NewController(conf)
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
go func(controller *api.Controller) {
// this blocks
if err := controller.Run(context.Background()); err != nil {
return
}
}(ctlr)
// wait till ready
for {
_, err := resty.R().Get(url)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
time.Sleep(6 * time.Second)
defer func(controller *api.Controller) {
ctx := context.Background()
_ = controller.Server.Shutdown(ctx)
}(ctlr)
defer cm.StopServer()
data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)
@ -97,7 +74,6 @@ func TestScrubExtension(t *testing.T) {
Convey("Blobs integrity affected", t, func(c C) {
port := test.GetFreePort()
url := test.GetBaseURL(port)
logFile, err := os.CreateTemp("", "zot-log*.txt")
So(err, ShouldBeNil)
@ -122,10 +98,7 @@ func TestScrubExtension(t *testing.T) {
ctlr := api.NewController(conf)
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
var manifestDigest godigest.Digest
manifestDigest, _, _ = test.GetOciLayoutDigests("../../../test/data/zot-test")
@ -134,28 +107,11 @@ func TestScrubExtension(t *testing.T) {
panic(err)
}
go func(controller *api.Controller) {
// this blocks
if err := controller.Run(context.Background()); err != nil {
return
}
}(ctlr)
// wait till ready
for {
_, err := resty.R().Get(url)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
time.Sleep(6 * time.Second)
defer func(controller *api.Controller) {
ctx := context.Background()
_ = controller.Server.Shutdown(ctx)
}(ctlr)
defer cm.StopServer()
data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)
@ -164,7 +120,6 @@ func TestScrubExtension(t *testing.T) {
Convey("Generator error - not enough permissions to access root directory", t, func(c C) {
port := test.GetFreePort()
url := test.GetBaseURL(port)
logFile, err := os.CreateTemp("", "zot-log*.txt")
So(err, ShouldBeNil)
@ -189,35 +144,15 @@ func TestScrubExtension(t *testing.T) {
ctlr := api.NewController(conf)
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
So(os.Chmod(path.Join(dir, repoName), 0o000), ShouldBeNil)
go func(controller *api.Controller) {
// this blocks
if err := controller.Run(context.Background()); err != nil {
return
}
}(ctlr)
// wait till ready
for {
_, err := resty.R().Get(url)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(port)
time.Sleep(6 * time.Second)
defer func(controller *api.Controller) {
ctx := context.Background()
_ = controller.Server.Shutdown(ctx)
}(ctlr)
defer cm.StopServer()
data, err := os.ReadFile(logFile.Name())
So(err, ShouldBeNil)
@ -249,10 +184,7 @@ func TestRunScrubRepo(t *testing.T) {
imgStore := local.NewImageStore(dir, true, 1*time.Second, true,
true, log, metrics, nil, cacheDriver)
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
err = scrub.RunScrubRepo(imgStore, repoName, log)
So(err, ShouldBeNil)
@ -284,10 +216,7 @@ func TestRunScrubRepo(t *testing.T) {
imgStore := local.NewImageStore(dir, true, 1*time.Second, true,
true, log, metrics, nil, cacheDriver)
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
var manifestDigest godigest.Digest
manifestDigest, _, _ = test.GetOciLayoutDigests("../../../test/data/zot-test")
@ -326,10 +255,7 @@ func TestRunScrubRepo(t *testing.T) {
true, true, log, metrics, nil, cacheDriver,
)
err = test.CopyFiles("../../../test/data/zot-test", path.Join(dir, repoName))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data/zot-test", path.Join(dir, repoName))
So(os.Chmod(path.Join(dir, repoName), 0o000), ShouldBeNil)

View File

@ -156,10 +156,7 @@ func testSetup(t *testing.T, subpath string) error { //nolint:unparam
subRootDir = subDir
err := CopyFiles("../../../../test/data", rootDir)
if err != nil {
return err
}
CopyTestFiles("../../../../test/data", rootDir)
return CopyFiles("../../../../test/data", path.Join(subDir, subpath))
}
@ -324,9 +321,9 @@ func TestRepoListWithNewestImage(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
resp, err := resty.R().Get(baseURL + "/v2/")
So(resp, ShouldNotBeNil)
@ -618,9 +615,9 @@ func TestRepoListWithNewestImage(t *testing.T) {
ctlr := api.NewController(conf)
ctlr.Log.Logger = ctlr.Log.Output(writers)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
substring := "{\"Search\":{\"Enable\":true,\"CVE\":{\"UpdateInterval\":3600000000000,\"Trivy\":{\"DBRepository\":\"ghcr.io/project-zot/trivy-db\"}}}" //nolint: lll
found, err := readFileAndSearchString(logPath, substring, 2*time.Minute)
@ -717,11 +714,9 @@ func TestGetReferrersGQL(t *testing.T) {
conf.Extensions.Search.CVE = nil
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
// =======================
@ -893,9 +888,9 @@ func TestExpandedRepoInfo(t *testing.T) {
err = os.WriteFile(indexPath, buf, 0o600)
So(err, ShouldBeNil)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
query := "{ExpandedRepoInfo(repo:\"test1\"){Summary%20{Name%20LastUpdated%20Size%20Platforms%20{Os%20Arch}%20Vendors%20Score}%20Images%20{Digest%20IsSigned%20Tag%20Layers%20{Size%20Digest}}}}" //nolint: lll
@ -934,10 +929,9 @@ func TestExpandedRepoInfo(t *testing.T) {
conf.Extensions.Search.CVE = nil
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
log := log.NewLogger("debug", "")
metrics := monitoring.NewMetricsServer(false, log)
@ -1232,30 +1226,10 @@ func TestDerivedImageList(t *testing.T) {
conf.Extensions.Search.CVE = nil
ctlr := api.NewController(conf)
ctlrManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
// shut down server
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("Test dependency list for image working", t, func() {
// create test images
@ -1583,9 +1557,9 @@ func TestDerivedImageListNoRepos(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
query := `
{
@ -1659,30 +1633,10 @@ func TestBaseImageList(t *testing.T) {
conf.Extensions.Search.CVE = nil
ctlr := api.NewController(conf)
ctlrManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
// shut down server
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("Test base image list for image working", t, func() {
// create test images
@ -2110,9 +2064,9 @@ func TestBaseImageListNoRepos(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
query := `
{
@ -2179,10 +2133,10 @@ func TestGlobalSearchImageAuthor(t *testing.T) {
conf.Extensions.Search.CVE = nil
ctlr := api.NewController(conf)
ctlrManager := NewControllerManager(ctlr)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("Test global search with author in manifest's annotations", t, func() {
cfg, layers, manifest, err := GetImageComponents(10000)
@ -2342,9 +2296,9 @@ func TestGlobalSearch(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
// push test images to repo 1 image 1
config1, layers1, manifest1, err := GetImageComponents(100)
@ -2662,9 +2616,9 @@ func TestGlobalSearch(t *testing.T) {
ctlr := api.NewController(conf)
ctlr.Log.Logger = ctlr.Log.Output(writers)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
// Wait for trivy db to download
substring := "{\"Search\":{\"Enable\":true,\"CVE\":{\"UpdateInterval\":3600000000000,\"Trivy\":{\"DBRepository\":\"ghcr.io/project-zot/trivy-db\"}}}" //nolint: lll
@ -2934,9 +2888,9 @@ func TestCleaningFilteringParamsGlobalSearch(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
config, layers, manifest, err := GetImageWithConfig(ispec.Image{
Platform: ispec.Platform{
@ -3016,9 +2970,9 @@ func TestGlobalSearchFiltering(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
config, layers, manifest, err := GetRandomImageComponents(100)
So(err, ShouldBeNil)
@ -3093,9 +3047,9 @@ func TestGlobalSearchWithInvalidInput(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
longString := RandomString(1000)
@ -3191,9 +3145,9 @@ func TestImageList(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
imageStore := ctlr.StoreController.DefaultStore
@ -3263,9 +3217,9 @@ func TestImageList(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
config := ispec.Image{
Platform: ispec.Platform{
@ -3374,9 +3328,9 @@ func TestGlobalSearchPagination(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
for i := 0; i < 3; i++ {
config, layers, manifest, err := GetImageComponents(10)
@ -3569,9 +3523,9 @@ func TestBuildImageInfo(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
olu := &common.BaseOciLayoutUtils{
StoreController: ctlr.StoreController,
@ -3683,9 +3637,9 @@ func TestRepoDBWhenSigningImages(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
// push test images to repo 1 image 1
config1, layers1, manifest1, err := GetImageComponents(100)
@ -3868,9 +3822,9 @@ func TestRepoDBWhenPushingImages(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("SetManifestMeta fails", func() {
ctlr.RepoDB = mocks.RepoDBMock{
@ -3960,9 +3914,9 @@ func TestRepoDBWhenReadingImages(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
config1, layers1, manifest1, err := GetImageComponents(100)
So(err, ShouldBeNil)
@ -4053,9 +4007,9 @@ func TestRepoDBWhenDeletingImages(t *testing.T) {
ctlr := api.NewController(conf)
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
// push test images to repo 1 image 1
config1, layers1, manifest1, err := GetImageComponents(100)
@ -4634,9 +4588,9 @@ func TestSearchSize(t *testing.T) {
dir := t.TempDir()
ctlr.Config.Storage.RootDirectory = dir
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
repoName := "testrepo"
config, layers, manifest, err := GetImageComponents(10000)
@ -4855,9 +4809,9 @@ func TestImageSummary(t *testing.T) {
configBlob, errConfig := json.Marshal(config)
configDigest := godigest.FromBytes(configBlob)
So(errConfig, ShouldBeNil) // marshall success, config is valid JSON
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
manifestBlob, errMarsal := json.Marshal(manifest)
So(errMarsal, ShouldBeNil)
@ -5037,9 +4991,9 @@ func TestImageSummary(t *testing.T) {
configBlob, errConfig := json.Marshal(config)
configDigest := godigest.FromBytes(configBlob)
So(errConfig, ShouldBeNil) // marshall success, config is valid JSON
go startServer(ctlr)
defer stopServer(ctlr)
WaitTillServerReady(baseURL)
ctlrManager := NewControllerManager(ctlr)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
manifestBlob, errMarsal := json.Marshal(manifest)
So(errMarsal, ShouldBeNil)
@ -5102,16 +5056,3 @@ func TestImageSummary(t *testing.T) {
So(imgSummary.Vulnerabilities.MaxSeverity, ShouldEqual, "NONE")
})
}
func startServer(c *api.Controller) {
// this blocks
ctx := context.Background()
if err := c.Run(ctx); err != nil {
return
}
}
func stopServer(c *api.Controller) {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}

View File

@ -5,7 +5,6 @@
package cveinfo_test
import (
"context"
"encoding/json"
"fmt"
"io"
@ -410,22 +409,9 @@ func TestCVESearchDisabled(t *testing.T) {
ctlr := api.NewController(conf)
ctlr.Log.Logger = ctlr.Log.Output(writers)
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
// Wait for trivy db to download
_, err = ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second)
@ -433,10 +419,7 @@ func TestCVESearchDisabled(t *testing.T) {
panic(err)
}
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
resp, _ := resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.FullSearchPrefix + "?query={CVEListForImage(image:\"zot-test\"){Tag%20CVEList{Id%20Description%20Severity%20PackageList{Name%20InstalledVersion%20FixedVersion}}}}")
So(string(resp.Body()), ShouldContainSubstring, "search: CVE search is disabled")
@ -502,22 +485,9 @@ func TestCVESearch(t *testing.T) {
ctlr := api.NewController(conf)
ctlr.Log.Logger = ctlr.Log.Output(writers)
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
// Wait for trivy db to download
_, err = ReadLogFileAndSearchString(logPath, "DB update completed, next update scheduled", 90*time.Second)
@ -525,10 +495,7 @@ func TestCVESearch(t *testing.T) {
panic(err)
}
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
// without creds, should get access error
resp, err := resty.R().Get(baseURL + "/v2/")
@ -740,10 +707,7 @@ func TestCVEConfig(t *testing.T) {
secondDir := t.TempDir()
err := CopyFiles("../../../../test/data", path.Join(secondDir, "a"))
if err != nil {
panic(err)
}
CopyTestFiles("../../../../test/data", path.Join(secondDir, "a"))
ctlr.Config.Storage.RootDirectory = firstDir
subPaths := make(map[string]config.StorageConfig)
@ -752,22 +716,9 @@ func TestCVEConfig(t *testing.T) {
}
ctlr.Config.Storage.SubPaths = subPaths
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
resp, _ := resty.R().SetBasicAuth(username, passphrase).Get(baseURL + constants.RoutePrefix + "/")
So(resp, ShouldNotBeNil)
@ -785,10 +736,7 @@ func TestCVEConfig(t *testing.T) {
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, 404)
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
})
}
@ -813,10 +761,7 @@ func TestHTTPOptionsResponse(t *testing.T) {
defer os.RemoveAll(firstDir)
defer os.RemoveAll(secondDir)
err = CopyFiles("../../../../test/data", path.Join(secondDir, "a"))
if err != nil {
panic(err)
}
CopyTestFiles("../../../../test/data", path.Join(secondDir, "a"))
ctlr.Config.Storage.RootDirectory = firstDir
subPaths := make(map[string]config.StorageConfig)
@ -825,31 +770,15 @@ func TestHTTPOptionsResponse(t *testing.T) {
}
ctlr.Config.Storage.SubPaths = subPaths
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
resp, _ := resty.R().Options(baseURL + constants.RoutePrefix + constants.ExtCatalogPrefix)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusNoContent)
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
})
}

View File

@ -5,12 +5,10 @@
package digestinfo_test
import (
"context"
"encoding/json"
"net/url"
"os"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
@ -72,15 +70,9 @@ func testSetup(t *testing.T) (string, string, *digestinfo.DigestInfo) {
panic(err)
}
err = CopyFiles("../../../../test/data", rootDir)
if err != nil {
panic(err)
}
CopyTestFiles("../../../../test/data", rootDir)
err = CopyFiles("../../../../test/data", subDir+"/a/")
if err != nil {
panic(err)
}
CopyTestFiles("../../../../test/data", subDir+"/a/")
log := log.NewLogger("debug", "")
metrics := monitoring.NewMetricsServer(false, log)
@ -143,28 +135,12 @@ func TestDigestSearchHTTP(t *testing.T) {
}
ctlr := api.NewController(conf)
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
// shut down server
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
resp, err := resty.R().Get(baseURL + "/v2/")
So(resp, ShouldNotBeNil)
@ -310,28 +286,12 @@ func TestDigestSearchHTTPSubPaths(t *testing.T) {
subPathMap["/a"] = config.StorageConfig{RootDirectory: subRootDir}
ctlr.Config.Storage.SubPaths = subPathMap
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
// shut down server
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
resp, err := resty.R().Get(baseURL + "/v2/")
So(resp, ShouldNotBeNil)
@ -372,28 +332,12 @@ func TestDigestSearchDisabled(t *testing.T) {
}
ctlr := api.NewController(conf)
ctrlManager := NewControllerManager(ctlr)
go func() {
// this blocks
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(baseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
ctrlManager.StartAndWait(port)
// shut down server
defer func() {
ctx := context.Background()
_ = ctlr.Server.Shutdown(ctx)
}()
defer ctrlManager.StopServer()
resp, err := resty.R().Get(baseURL + "/v2/")
So(resp, ShouldNotBeNil)

View File

@ -4,7 +4,6 @@
package sync_test
import (
"context"
"os"
"testing"
@ -42,17 +41,10 @@ func TestSyncExtension(t *testing.T) {
conf.Log.Output = logFile.Name()
ctlr := api.NewController(conf)
ctlrManager := test.NewControllerManager(ctlr)
go func() {
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
defer func() {
_ = ctlr.Server.Shutdown(context.Background())
}()
test.WaitTillServerReady(baseURL)
ctlrManager.StartAndWait(port)
defer ctlrManager.StopServer()
Convey("verify sync is skipped when binary doesn't include it", func() {
resp, err := resty.R().

View File

@ -390,10 +390,7 @@ func TestSyncInternal(t *testing.T) {
Convey("Test canSkipImage()", t, func() {
storageDir := t.TempDir()
err := test.CopyFiles("../../../test/data", storageDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", storageDir)
log := log.Logger{Logger: zerolog.New(os.Stdout)}
metrics := monitoring.NewMetricsServer(false, log)
@ -407,7 +404,7 @@ func TestSyncInternal(t *testing.T) {
},
}}
err = os.Chmod(path.Join(imageStore.RootDir(), testImage, "index.json"), 0o000)
err := os.Chmod(path.Join(imageStore.RootDir(), testImage, "index.json"), 0o000)
So(err, ShouldBeNil)
canBeSkipped, err := canSkipImage(testImage, testImageTag, "fakeDigest", imageStore, log)
@ -520,10 +517,7 @@ func TestSyncInternal(t *testing.T) {
panic(err)
}
err = test.CopyFiles("../../../test/data", testRootDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", testRootDir)
testImageStore := local.NewImageStore(testRootDir, false,
storage.DefaultGCDelay, false, false, log, metrics, nil, nil)

View File

@ -154,31 +154,14 @@ func startUpstreamServer(
srcDir := t.TempDir()
err := test.CopyFiles("../../../test/data", srcDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", srcDir)
srcConfig.Storage.RootDirectory = srcDir
sctlr := api.NewController(srcConfig)
go func() {
// this blocks
if err := sctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := client.R().Get(srcBaseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
scm := test.NewControllerManager(sctlr)
scm.StartAndWait(srcPort)
return sctlr, srcBaseURL, srcDir, htpasswdPath, client
}
@ -236,22 +219,8 @@ func startDownstreamServer(
dctlr := api.NewController(destConfig)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
// wait till ready
for {
_, err := client.R().Get(destBaseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
dcm := test.NewControllerManager(dctlr)
dcm.StartAndWait(destPort)
return dctlr, destBaseURL, destDir, client
}
@ -710,10 +679,9 @@ func TestOnDemandPermsDenied(t *testing.T) {
destConfig.Extensions.Sync = syncConfig
dctlr := api.NewController(destConfig)
dcm := test.NewControllerManager(dctlr)
defer func() {
dctlr.Shutdown()
}()
defer dcm.StopServer()
syncSubDir := path.Join(destDir, testImage, sync.SyncBlobUploadDir)
@ -723,14 +691,7 @@ func TestOnDemandPermsDenied(t *testing.T) {
err = os.Chmod(syncSubDir, 0o000)
So(err, ShouldBeNil)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
test.WaitTillServerReady(destBaseURL)
dcm.StartAndWait(destPort)
resp, err := resty.R().Get(destBaseURL + "/v2/" + testImage + "/manifests/" + testImageTag)
So(err, ShouldBeNil)
@ -802,10 +763,9 @@ func TestConfigReloader(t *testing.T) {
destConfig.Log.Output = logFile.Name()
dctlr := api.NewController(destConfig)
dcm := test.NewControllerManager(dctlr)
defer func() {
dctlr.Shutdown()
}()
defer dcm.StopServer()
content := fmt.Sprintf(`{"distSpecVersion": "0.1.0-dev", "storage": {"rootDirectory": "%s"},
"http": {"address": "127.0.0.1", "port": "%s"},
@ -926,27 +886,11 @@ func TestMandatoryAnnotations(t *testing.T) {
destConfig.Extensions.Lint.MandatoryAnnotations = []string{"annot1", "annot2", "annot3"}
dctlr := api.NewController(destConfig)
dcm := test.NewControllerManager(dctlr)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
dcm.StartAndWait(destPort)
// wait till ready
for {
_, err := destClient.R().Get(destBaseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
defer func() {
dctlr.Shutdown()
}()
defer dcm.StopServer()
// give it time to set up sync
time.Sleep(5 * time.Second)
@ -1260,19 +1204,9 @@ func TestBasicAuth(t *testing.T) {
}
dctlr := api.NewController(destConfig)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
defer func() {
dctlr.Shutdown()
}()
test.WaitTillServerReady(destBaseURL)
dcm := test.NewControllerManager(dctlr)
dcm.StartAndWait(destPort)
defer dcm.StopServer()
time.Sleep(3 * time.Second)
@ -1977,27 +1911,15 @@ func TestSubPaths(t *testing.T) {
subpath := "/subpath"
err := test.CopyFiles("../../../test/data", path.Join(srcDir, subpath))
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", path.Join(srcDir, subpath))
srcConfig.Storage.RootDirectory = srcDir
sctlr := api.NewController(srcConfig)
go func() {
// this blocks
if err := sctlr.Run(context.Background()); err != nil {
return
}
}()
test.WaitTillServerReady(srcBaseURL)
defer func() {
sctlr.Shutdown()
}()
scm := test.NewControllerManager(sctlr)
scm.StartAndWait(srcPort)
defer scm.StopServer()
regex := ".*"
var semver bool
@ -2053,18 +1975,9 @@ func TestSubPaths(t *testing.T) {
dctlr := api.NewController(destConfig)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
test.WaitTillServerReady(destBaseURL)
defer func() {
dctlr.Shutdown()
}()
dcm := test.NewControllerManager(dctlr)
dcm.StartAndWait(destPort)
defer dcm.StopServer()
var destTagsList TagsList
@ -3096,14 +3009,12 @@ func TestOnDemandRetryGoroutine(t *testing.T) {
srcDir := t.TempDir()
err := test.CopyFiles("../../../test/data", srcDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", srcDir)
srcConfig.Storage.RootDirectory = srcDir
sctlr := api.NewController(srcConfig)
scm := test.NewControllerManager(sctlr)
regex := ".*"
semver := true
@ -3147,17 +3058,9 @@ func TestOnDemandRetryGoroutine(t *testing.T) {
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 404)
// start upstream server
go func() {
// this blocks
if err := sctlr.Run(context.Background()); err != nil {
return
}
}()
scm.StartServer()
defer func() {
sctlr.Shutdown()
}()
defer scm.StopServer()
// in the meantime ondemand should retry syncing
time.Sleep(15 * time.Second)
@ -3287,14 +3190,12 @@ func TestOnDemandMultipleRetries(t *testing.T) {
srcDir := t.TempDir()
err := test.CopyFiles("../../../test/data", srcDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", srcDir)
srcConfig.Storage.RootDirectory = srcDir
sctlr := api.NewController(srcConfig)
scm := test.NewControllerManager(sctlr)
var tlsVerify bool
@ -3356,18 +3257,9 @@ func TestOnDemandMultipleRetries(t *testing.T) {
}()
// start upstream server
go func() {
// this blocks
if err := sctlr.Run(context.Background()); err != nil {
return
}
}()
scm.StartAndWait(srcPort)
test.WaitTillServerReady(srcBaseURL)
defer func() {
sctlr.Shutdown()
}()
defer scm.StopServer()
// wait sync
for {
@ -3840,10 +3732,7 @@ func TestSyncOnlyDiff(t *testing.T) {
destDir := t.TempDir()
// copy images so we have them before syncing, sync should not pull them again
err := test.CopyFiles("../../../test/data", destDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", destDir)
destConfig.Storage.RootDirectory = destDir
destConfig.Storage.Dedupe = false
@ -3855,19 +3744,11 @@ func TestSyncOnlyDiff(t *testing.T) {
destConfig.Log.Output = path.Join(destDir, "sync.log")
dctlr := api.NewController(destConfig)
dcm := test.NewControllerManager(dctlr)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
dcm.StartAndWait(destPort)
test.WaitTillServerReady(destBaseURL)
defer func() {
dctlr.Shutdown()
}()
defer dcm.StopServer()
resp, err := resty.R().Get(destBaseURL + "/v2/" + testImage + "/manifests/" + testImageTag)
So(err, ShouldBeNil)
@ -3923,10 +3804,7 @@ func TestSyncWithDiffDigest(t *testing.T) {
destDir := t.TempDir()
// copy images so we have them before syncing, sync should not pull them again
err := test.CopyFiles("../../../test/data", destDir)
if err != nil {
panic(err)
}
test.CopyTestFiles("../../../test/data", destDir)
destConfig.Storage.RootDirectory = destDir
destConfig.Storage.Dedupe = false
@ -3937,6 +3815,7 @@ func TestSyncWithDiffDigest(t *testing.T) {
destConfig.Extensions.Sync = syncConfig
dctlr := api.NewController(destConfig)
dcm := test.NewControllerManager(dctlr)
// before starting downstream server, let's modify an image manifest so that sync should pull it
// change digest of the manifest so that sync should happen
@ -3991,12 +3870,7 @@ func TestSyncWithDiffDigest(t *testing.T) {
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, 201)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
dcm.StartServer()
// watch .sync subdir, should be populated
done := make(chan bool)
@ -4016,9 +3890,7 @@ func TestSyncWithDiffDigest(t *testing.T) {
}
}()
defer func() {
dctlr.Shutdown()
}()
defer dcm.StopServer()
test.WaitTillServerReady(destBaseURL)
@ -4815,31 +4687,15 @@ func TestSyncOCIArtifactsWithTag(t *testing.T) {
destConfig.Extensions.Sync = syncConfig
dctlr := api.NewController(destConfig)
dcm := test.NewControllerManager(dctlr)
manifestPath := path.Join(destDir, repoName, "blobs", "sha256", artifactDigest.Encoded())
So(os.MkdirAll(manifestPath, 0o755), ShouldBeNil)
So(os.Chmod(manifestPath, 0o000), ShouldBeNil)
go func() {
// this blocks
if err := dctlr.Run(context.Background()); err != nil {
return
}
}()
dcm.StartAndWait(destConfig.HTTP.Port)
// wait till ready
for {
_, err := resty.R().Get(destBaseURL)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
defer func() {
dctlr.Shutdown()
}()
defer dcm.StopServer()
defer func() {
err := os.Chmod(manifestPath, 0o755)

View File

@ -179,6 +179,13 @@ func CopyFiles(sourceDir, destDir string) error {
return nil
}
func CopyTestFiles(sourceDir, destDir string) {
err := CopyFiles(sourceDir, destDir)
if err != nil {
panic(err)
}
}
type Controller interface {
Run(ctx context.Context) error
Shutdown()

View File

@ -113,6 +113,9 @@ func TestCopyFiles(t *testing.T) {
_, err = os.Stat(path.Join(dstDir, "test-index", "index.json"))
So(err, ShouldBeNil)
})
Convey("panic when sourceDir does not exist", t, func() {
So(func() { test.CopyTestFiles("/path/to/some/unexisting/directory", os.TempDir()) }, ShouldPanic)
})
}
func TestGetOciLayoutDigests(t *testing.T) {