2023-05-31 12:26:23 -05:00
|
|
|
//go:build sync
|
|
|
|
// +build sync
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2022-10-06 11:41:16 -05:00
|
|
|
"bytes"
|
2021-06-08 15:11:18 -05:00
|
|
|
"context"
|
2021-10-28 04:10:01 -05:00
|
|
|
"encoding/json"
|
2021-06-08 15:11:18 -05:00
|
|
|
"fmt"
|
2021-10-28 04:10:01 -05:00
|
|
|
"os"
|
2021-06-08 15:11:18 -05:00
|
|
|
"testing"
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
"github.com/containers/image/v5/oci/layout"
|
2021-06-08 15:11:18 -05:00
|
|
|
"github.com/containers/image/v5/types"
|
2021-10-28 04:10:01 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/rs/zerolog"
|
2021-06-08 15:11:18 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
2023-05-31 12:26:23 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/config"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/lint"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
2023-05-31 12:26:23 -05:00
|
|
|
client "zotregistry.io/zot/pkg/extensions/sync/httpclient"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2023-05-31 12:26:23 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/cache"
|
2023-05-26 13:08:19 -05:00
|
|
|
storageConstants "zotregistry.io/zot/pkg/storage/constants"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2021-12-17 11:34:22 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2023-05-26 13:08:19 -05:00
|
|
|
"zotregistry.io/zot/pkg/test/inject"
|
2022-10-06 11:41:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/test/mocks"
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testImage = "zot-test"
|
|
|
|
testImageTag = "0.0.1"
|
|
|
|
|
|
|
|
host = "127.0.0.1:45117"
|
|
|
|
)
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
var ErrTestError = fmt.Errorf("testError")
|
|
|
|
|
2022-02-18 06:36:50 -05:00
|
|
|
func TestInjectSyncUtils(t *testing.T) {
|
|
|
|
Convey("Inject errors in utils functions", t, func() {
|
|
|
|
repositoryReference := fmt.Sprintf("%s/%s", host, testImage)
|
|
|
|
ref, err := parseRepositoryReference(repositoryReference)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(ref.Name(), ShouldEqual, repositoryReference)
|
|
|
|
|
2023-05-26 13:08:19 -05:00
|
|
|
injected := inject.InjectFailure(0)
|
2022-02-18 06:36:50 -05:00
|
|
|
if injected {
|
2023-05-31 12:26:23 -05:00
|
|
|
_, err = getRepoTags(context.Background(), &types.SystemContext{}, host, testImage)
|
2022-02-18 06:36:50 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:08:19 -05:00
|
|
|
injected = inject.InjectFailure(0)
|
2023-05-31 12:26:23 -05:00
|
|
|
_, err = getPolicyContext(log.NewLogger("debug", ""))
|
2022-02-18 06:36:50 -05:00
|
|
|
if injected {
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
} else {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2023-05-26 13:08:19 -05:00
|
|
|
imageStore := local.NewImageStore(t.TempDir(), false, storageConstants.DefaultGCDelay,
|
2022-11-02 17:53:08 -05:00
|
|
|
false, false, log, metrics, nil, nil,
|
2022-06-24 08:08:47 -05:00
|
|
|
)
|
2023-05-26 13:08:19 -05:00
|
|
|
injected = inject.InjectFailure(0)
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
ols := NewOciLayoutStorage(storage.StoreController{DefaultStore: imageStore})
|
|
|
|
_, err = ols.GetImageReference(testImage, testImageTag)
|
2022-02-18 06:36:50 -05:00
|
|
|
if injected {
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
} else {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
func TestSyncInternal(t *testing.T) {
|
2021-10-28 04:10:01 -05:00
|
|
|
Convey("Verify parseRepositoryReference func", t, func() {
|
2021-06-08 15:11:18 -05:00
|
|
|
repositoryReference := fmt.Sprintf("%s/%s", host, testImage)
|
|
|
|
ref, err := parseRepositoryReference(repositoryReference)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(ref.Name(), ShouldEqual, repositoryReference)
|
|
|
|
|
|
|
|
repositoryReference = fmt.Sprintf("%s/%s:tagged", host, testImage)
|
|
|
|
_, err = parseRepositoryReference(repositoryReference)
|
|
|
|
So(err, ShouldEqual, errors.ErrInvalidRepositoryName)
|
|
|
|
|
|
|
|
repositoryReference = fmt.Sprintf("http://%s/%s", host, testImage)
|
|
|
|
_, err = parseRepositoryReference(repositoryReference)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
repositoryReference = fmt.Sprintf("docker://%s/%s", host, testImage)
|
|
|
|
_, err = parseRepositoryReference(repositoryReference)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err = getFileCredentials("/path/to/inexistent/file")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
tempFile, err := os.CreateTemp("", "sync-credentials-")
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content := []byte(`{`)
|
2022-09-02 07:56:02 -05:00
|
|
|
if err := os.WriteFile(tempFile.Name(), content, 0o600); err != nil {
|
2021-06-08 15:11:18 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = getFileCredentials(tempFile.Name())
|
2021-06-08 15:11:18 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
srcCtx := &types.SystemContext{}
|
2023-05-31 12:26:23 -05:00
|
|
|
_, err = getRepoTags(context.Background(), srcCtx, host, testImage)
|
2021-06-08 15:11:18 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
_, err = getRepoTags(context.Background(), srcCtx, host, testImage)
|
2021-06-08 15:11:18 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err = getFileCredentials("/invalid/path/to/file")
|
|
|
|
So(err, ShouldNotBeNil)
|
2021-12-29 10:14:56 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
ok := isSupportedMediaType("unknown")
|
|
|
|
So(ok, ShouldBeFalse)
|
2021-10-28 04:10:01 -05:00
|
|
|
})
|
2023-05-31 12:26:23 -05:00
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func TestRemoteRegistry(t *testing.T) {
|
|
|
|
Convey("test remote registry", t, func() {
|
|
|
|
logger := log.NewLogger("debug", "")
|
|
|
|
cfg := client.Config{
|
|
|
|
URL: "url",
|
|
|
|
TLSVerify: false,
|
2021-10-28 04:10:01 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
client, err := client.New(cfg, logger)
|
2022-12-22 13:19:42 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-01-27 07:45:46 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
remote := NewRemoteRegistry(client, logger)
|
|
|
|
imageRef, err := layout.NewReference("dir", "image")
|
2022-12-22 13:19:42 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
_, _, _, err = remote.GetManifestContent(imageRef)
|
2021-12-29 10:14:56 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
tags, err := remote.GetRepoTags("repo")
|
|
|
|
So(tags, ShouldBeEmpty)
|
2021-12-29 10:14:56 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2021-10-28 04:10:01 -05:00
|
|
|
})
|
2023-05-31 12:26:23 -05:00
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func TestLocalRegistry(t *testing.T) {
|
|
|
|
Convey("make StoreController", t, func() {
|
|
|
|
dir := t.TempDir()
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
log := log.NewLogger("debug", "")
|
2022-11-22 13:13:08 -05:00
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2023-05-31 12:26:23 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2022-11-22 13:13:08 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
syncImgStore := local.NewImageStore(dir, true, storageConstants.DefaultGCDelay,
|
|
|
|
true, true, log, metrics, nil, cacheDriver)
|
|
|
|
repoName := "repo"
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
registry := NewLocalRegistry(storage.StoreController{DefaultStore: syncImgStore}, nil, log)
|
|
|
|
imageReference, err := registry.GetImageReference(repoName, "1.0")
|
2021-12-17 11:34:22 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
So(imageReference, ShouldNotBeNil)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
imgStore := getImageStoreFromImageReference(imageReference, repoName, "1.0")
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// create a blob/layer
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
2021-12-17 11:34:22 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
So(upload, ShouldNotBeEmpty)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
content := []byte("this is a blob1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
blob, err := imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
2022-10-22 02:26:14 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
bdgst1 := digest
|
|
|
|
bsize1 := len(content)
|
2022-10-22 02:26:14 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, digest)
|
2023-02-13 13:43:52 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
So(blob, ShouldEqual, buflen)
|
2023-02-13 13:43:52 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// push index image
|
2023-02-13 13:43:52 -05:00
|
|
|
var index ispec.Index
|
2023-05-31 12:26:23 -05:00
|
|
|
index.SchemaVersion = 2
|
|
|
|
index.MediaType = ispec.MediaTypeImageIndex
|
2023-02-13 13:43:52 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
// upload image config blob
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
2023-02-13 13:43:52 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
buf := bytes.NewBuffer(cblob)
|
|
|
|
buflen := buf.Len()
|
|
|
|
blob, err := imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, cdigest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
2023-05-10 12:15:33 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// create a manifest
|
|
|
|
manifest := ispec.Manifest{
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
MediaType: ispec.MediaTypeImageConfig,
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2023-05-10 12:15:33 -05:00
|
|
|
},
|
2023-05-31 12:26:23 -05:00
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageLayer,
|
|
|
|
Digest: bdgst1,
|
|
|
|
Size: int64(bsize1),
|
2023-05-10 12:15:33 -05:00
|
|
|
},
|
|
|
|
},
|
2023-05-31 12:26:23 -05:00
|
|
|
}
|
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
_, _, err = imgStore.PutImageManifest(repoName, digest.String(), ispec.MediaTypeImageManifest, content)
|
|
|
|
So(err, ShouldBeNil)
|
2023-05-10 12:15:33 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
index.Manifests = append(index.Manifests, ispec.Descriptor{
|
|
|
|
Digest: digest,
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
})
|
2021-11-25 07:04:39 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// upload index image
|
|
|
|
indexContent, err := json.Marshal(index)
|
2022-10-22 02:26:14 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
indexDigest := godigest.FromBytes(indexContent)
|
|
|
|
So(indexDigest, ShouldNotBeNil)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
_, _, err = imgStore.PutImageManifest(repoName, "1.0", ispec.MediaTypeImageIndex, indexContent)
|
2021-10-28 04:10:01 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("sync index image", func() {
|
|
|
|
ok, err := registry.CanSkipImage(repoName, "1.0", indexDigest)
|
|
|
|
So(ok, ShouldBeFalse)
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
})
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger GetImageManifest error in CommitImage()", func() {
|
|
|
|
err = os.Chmod(imgStore.BlobPath(repoName, indexDigest), 0o000)
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
})
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger linter error in CommitImage()", func() {
|
|
|
|
defaultVal := true
|
|
|
|
linter := lint.NewLinter(&config.LintConfig{
|
|
|
|
BaseConfig: config.BaseConfig{
|
|
|
|
Enable: &defaultVal,
|
|
|
|
},
|
|
|
|
MandatoryAnnotations: []string{"annot1"},
|
|
|
|
}, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
syncImgStore := local.NewImageStore(dir, true, storageConstants.DefaultGCDelay,
|
|
|
|
true, true, log, metrics, linter, cacheDriver)
|
|
|
|
repoName := "repo"
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
registry := NewLocalRegistry(storage.StoreController{DefaultStore: syncImgStore}, nil, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
})
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger GetBlobContent on manifest error in CommitImage()", func() {
|
|
|
|
err = os.Chmod(imgStore.BlobPath(repoName, digest), 0o000)
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
})
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger copyBlob() error in CommitImage()", func() {
|
|
|
|
err = os.Chmod(imgStore.BlobPath(repoName, bdgst1), 0o000)
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger PutImageManifest error on index manifest in CommitImage()", func() {
|
|
|
|
err = os.MkdirAll(syncImgStore.BlobPath(repoName, indexDigest), storageConstants.DefaultDirPerms)
|
2023-03-09 13:41:48 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = os.Chmod(syncImgStore.BlobPath(repoName, indexDigest), 0o000)
|
2023-03-09 13:41:48 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2023-03-09 13:41:48 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger repoDB error on index manifest in CommitImage()", func() {
|
|
|
|
registry := NewLocalRegistry(storage.StoreController{DefaultStore: syncImgStore}, mocks.RepoDBMock{
|
|
|
|
SetRepoReferenceFn: func(repo, Reference string, manifestDigest godigest.Digest, mediaType string) error {
|
|
|
|
if Reference == "1.0" {
|
|
|
|
return errors.ErrRepoMetaNotFound
|
|
|
|
}
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, log)
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
})
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("trigger repoDB error on image manifest in CommitImage()", func() {
|
|
|
|
registry := NewLocalRegistry(storage.StoreController{DefaultStore: syncImgStore}, mocks.RepoDBMock{
|
|
|
|
SetRepoReferenceFn: func(repo, Reference string, manifestDigest godigest.Digest, mediaType string) error {
|
|
|
|
return errors.ErrRepoMetaNotFound
|
|
|
|
},
|
|
|
|
}, log)
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = registry.CommitImage(imageReference, repoName, "1.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-05-31 12:26:23 -05:00
|
|
|
})
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
Convey("push image", func() {
|
|
|
|
imageReference, err := registry.GetImageReference(repoName, "2.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(imageReference, ShouldNotBeNil)
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
imgStore := getImageStoreFromImageReference(imageReference, repoName, "2.0")
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// upload image
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// create a blob/layer
|
|
|
|
upload, err := imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
content := []byte("this is a blob1")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
buflen := buf.Len()
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
blob, err := imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
|
|
|
bdgst1 := digest
|
|
|
|
bsize1 := len(content)
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, digest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
2022-03-10 10:39:11 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// upload image config blob
|
|
|
|
upload, err = imgStore.NewBlobUpload(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(upload, ShouldNotBeEmpty)
|
2022-03-10 10:39:11 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
cblob, cdigest := test.GetRandomImageConfig()
|
|
|
|
buf = bytes.NewBuffer(cblob)
|
|
|
|
buflen = buf.Len()
|
|
|
|
blob, err = imgStore.PutBlobChunkStreamed(repoName, upload, buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
2022-03-10 10:39:11 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = imgStore.FinishBlobUpload(repoName, upload, buf, cdigest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(blob, ShouldEqual, buflen)
|
2022-04-14 14:07:44 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// create a manifest
|
|
|
|
manifest := ispec.Manifest{
|
2022-04-14 14:07:44 -05:00
|
|
|
Config: ispec.Descriptor{
|
2023-05-31 12:26:23 -05:00
|
|
|
MediaType: ispec.MediaTypeImageConfig,
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2022-04-14 14:07:44 -05:00
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
2023-05-31 12:26:23 -05:00
|
|
|
MediaType: ispec.MediaTypeImageLayer,
|
|
|
|
Digest: bdgst1,
|
|
|
|
Size: int64(bsize1),
|
2022-04-14 14:07:44 -05:00
|
|
|
},
|
|
|
|
},
|
2023-05-31 12:26:23 -05:00
|
|
|
}
|
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, err = json.Marshal(manifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
2022-04-14 14:07:44 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
_, _, err = imgStore.PutImageManifest(repoName, "2.0", ispec.MediaTypeImageManifest, content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("sync image", func() {
|
|
|
|
ok, err := registry.CanSkipImage(repoName, "2.0", digest)
|
|
|
|
So(ok, ShouldBeFalse)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = registry.CommitImage(imageReference, repoName, "2.0")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
2022-04-14 14:07:44 -05:00
|
|
|
})
|
|
|
|
}
|