2019-06-20 18:36:40 -05:00
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
2019-09-17 17:45:28 -05:00
|
|
|
"bytes"
|
|
|
|
_ "crypto/sha256"
|
|
|
|
"encoding/json"
|
2019-06-20 18:36:40 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-25 17:33:58 -05:00
|
|
|
"github.com/anuvu/zot/pkg/log"
|
2019-06-20 18:36:40 -05:00
|
|
|
"github.com/anuvu/zot/pkg/storage"
|
2019-09-17 17:45:28 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2019-06-20 18:36:40 -05:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
2019-09-17 17:45:28 -05:00
|
|
|
func TestAPIs(t *testing.T) {
|
2019-06-20 18:36:40 -05:00
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2019-11-25 17:33:58 -05:00
|
|
|
il := storage.NewImageStore(dir, log.Logger{Logger: zerolog.New(os.Stdout)})
|
2019-06-20 18:36:40 -05:00
|
|
|
|
|
|
|
Convey("Repo layout", t, func(c C) {
|
|
|
|
repoName := "test"
|
|
|
|
|
|
|
|
Convey("Validate repo without initialization", func() {
|
|
|
|
v, err := il.ValidateRepo(repoName)
|
|
|
|
So(v, ShouldEqual, false)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Initialize repo", func() {
|
|
|
|
err := il.InitRepo(repoName)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Validate repo", func() {
|
|
|
|
v, err := il.ValidateRepo(repoName)
|
2019-09-17 17:45:28 -05:00
|
|
|
So(err, ShouldBeNil)
|
2019-06-20 18:36:40 -05:00
|
|
|
So(v, ShouldEqual, true)
|
2019-09-17 17:45:28 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Get repos", func() {
|
|
|
|
v, err := il.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(v, ShouldNotBeEmpty)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Get image tags", func() {
|
|
|
|
v, err := il.GetImageTags("test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(v, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
|
2020-01-31 02:59:36 -05:00
|
|
|
Convey("Full blob upload", func() {
|
|
|
|
body := []byte("this is a blob")
|
|
|
|
buf := bytes.NewBuffer(body)
|
|
|
|
d := godigest.FromBytes(body)
|
|
|
|
u, n, err := il.FullBlobUpload("test", buf, d.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(n, ShouldEqual, len(body))
|
|
|
|
So(u, ShouldNotBeEmpty)
|
|
|
|
})
|
|
|
|
|
2019-09-17 17:45:28 -05:00
|
|
|
Convey("New blob upload", func() {
|
|
|
|
v, err := il.NewBlobUpload("test")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(v, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
Convey("Get blob upload", func() {
|
|
|
|
b, err := il.GetBlobUpload("test", "invalid")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(b, ShouldEqual, -1)
|
|
|
|
|
|
|
|
b, err = il.GetBlobUpload("test", v)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(b, ShouldBeGreaterThanOrEqualTo, 0)
|
|
|
|
|
|
|
|
b, err = il.BlobUploadInfo("test", v)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(b, ShouldBeGreaterThanOrEqualTo, 0)
|
|
|
|
|
|
|
|
content := []byte("test-data")
|
|
|
|
buf := bytes.NewBuffer(content)
|
|
|
|
l := buf.Len()
|
|
|
|
d := godigest.FromBytes(content)
|
|
|
|
b, err = il.PutBlobChunk("test", v, 0, int64(l), buf)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(b, ShouldEqual, l)
|
|
|
|
|
|
|
|
err = il.FinishBlobUpload("test", v, buf, d.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(b, ShouldEqual, l)
|
|
|
|
|
|
|
|
_, _, err = il.CheckBlob("test", d.String(), "application/vnd.oci.image.layer.v1.tar+gzip")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, _, err = il.GetBlob("test", d.String(), "application/vnd.oci.image.layer.v1.tar+gzip")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
m := ispec.Manifest{}
|
|
|
|
mb, _ := json.Marshal(m)
|
|
|
|
|
|
|
|
Convey("Bad image manifest", func() {
|
|
|
|
_, err = il.PutImageManifest("test", d.String(), ispec.MediaTypeImageManifest, mb)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, _, _, err = il.GetImageManifest("test", d.String())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Good image manifest", func() {
|
|
|
|
m := ispec.Manifest{Layers: []ispec.Descriptor{{Digest: d}}}
|
|
|
|
mb, _ = json.Marshal(m)
|
|
|
|
d := godigest.FromBytes(mb)
|
|
|
|
_, err = il.PutImageManifest("test", d.String(), ispec.MediaTypeImageManifest, mb)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, _, _, err = il.GetImageManifest("test", d.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2020-01-28 16:55:58 -05:00
|
|
|
err = il.DeleteImageManifest("test", "1.0")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2019-09-17 17:45:28 -05:00
|
|
|
err = il.DeleteImageManifest("test", d.String())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, _, _, err = il.GetImageManifest("test", d.String())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
err = il.DeleteBlobUpload("test", v)
|
2019-06-20 18:36:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|