mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
feat(graphql): add an api to return referrers (#1009)
UI can now make use of OCI artifacts and references using `Referrers` gQL query. It returns a list of descriptors that refer on their `subject` field to another digest. Signed-off-by: Alex Stan <alexandrustan96@yahoo.ro>
This commit is contained in:
@@ -446,6 +446,55 @@ func UploadImage(img Image, baseURL, repo string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func UploadArtifact(baseURL, repo string, artifactManifest *imagespec.Artifact) error {
|
||||
// put manifest
|
||||
artifactManifestBlob, err := json.Marshal(artifactManifest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
artifactManifestDigest := godigest.FromBytes(artifactManifestBlob)
|
||||
|
||||
_, err = resty.R().
|
||||
SetHeader("Content-type", imagespec.MediaTypeArtifactManifest).
|
||||
SetBody(artifactManifestBlob).
|
||||
Put(baseURL + "/v2/" + repo + "/manifests/" + artifactManifestDigest.String())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func UploadBlob(baseURL, repo string, blob []byte, artifactBlobMediaType string) error {
|
||||
resp, err := resty.R().Post(baseURL + "/v2/" + repo + "/blobs/uploads/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode() != http.StatusAccepted {
|
||||
return ErrPostBlob
|
||||
}
|
||||
|
||||
loc := resp.Header().Get("Location")
|
||||
|
||||
blobDigest := godigest.FromBytes(blob).String()
|
||||
|
||||
resp, err = resty.R().
|
||||
SetHeader("Content-Length", fmt.Sprintf("%d", len(blob))).
|
||||
SetHeader("Content-Type", artifactBlobMediaType).
|
||||
SetQueryParam("digest", blobDigest).
|
||||
SetBody(blob).
|
||||
Put(baseURL + loc)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode() != http.StatusCreated {
|
||||
return ErrPutBlob
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadLogFileAndSearchString(logPath string, stringToMatch string, timeout time.Duration) (bool, error) {
|
||||
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancelFunc()
|
||||
|
||||
@@ -156,6 +156,142 @@ func TestWaitTillTrivyDBDownloadStarted(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
go startServer(ctlr)
|
||||
defer stopServer(ctlr)
|
||||
|
||||
test.WaitTillServerReady(baseURL)
|
||||
|
||||
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)
|
||||
go startServer(ctlr)
|
||||
defer stopServer(ctlr)
|
||||
|
||||
test.WaitTillServerReady(baseURL)
|
||||
|
||||
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)
|
||||
go startServer(ctlr)
|
||||
defer stopServer(ctlr)
|
||||
|
||||
test.WaitTillServerReady(baseURL)
|
||||
|
||||
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)
|
||||
go startServer(ctlr)
|
||||
defer stopServer(ctlr)
|
||||
|
||||
test.WaitTillServerReady(baseURL)
|
||||
|
||||
blob := []byte("test")
|
||||
|
||||
err := test.UploadBlob(baseURL, "test", blob, "zot.com.test")
|
||||
So(err, ShouldEqual, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUploadImage(t *testing.T) {
|
||||
Convey("Post request results in an error", t, func() {
|
||||
port := test.GetFreePort()
|
||||
|
||||
Reference in New Issue
Block a user