2019-06-21 02:36:40 +03:00
package storage_test
import (
2019-09-18 01:45:28 +03:00
"bytes"
_ "crypto/sha256"
"encoding/json"
2019-06-21 02:36:40 +03:00
"io/ioutil"
"os"
"testing"
2019-11-26 01:33:58 +03:00
"github.com/anuvu/zot/pkg/log"
2019-06-21 02:36:40 +03:00
"github.com/anuvu/zot/pkg/storage"
2019-09-18 01:45:28 +03:00
godigest "github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
2019-06-21 02:36:40 +03:00
"github.com/rs/zerolog"
. "github.com/smartystreets/goconvey/convey"
)
2019-09-18 01:45:28 +03:00
func TestAPIs ( t * testing . T ) {
2019-06-21 02:36:40 +03:00
dir , err := ioutil . TempDir ( "" , "oci-repo-test" )
if err != nil {
panic ( err )
}
2019-12-13 08:53:18 +03:00
2019-06-21 02:36:40 +03:00
defer os . RemoveAll ( dir )
2019-11-26 01:33:58 +03:00
il := storage . NewImageStore ( dir , log . Logger { Logger : zerolog . New ( os . Stdout ) } )
2019-06-21 02:36:40 +03: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-18 01:45:28 +03:00
So ( err , ShouldBeNil )
2019-06-21 02:36:40 +03:00
So ( v , ShouldEqual , true )
2019-09-18 01:45:28 +03: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 )
} )
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-29 00:55:58 +03:00
err = il . DeleteImageManifest ( "test" , "1.0" )
So ( err , ShouldNotBeNil )
2019-09-18 01:45:28 +03:00
err = il . DeleteImageManifest ( "test" , d . String ( ) )
So ( err , ShouldBeNil )
_ , _ , _ , err = il . GetImageManifest ( "test" , d . String ( ) )
So ( err , ShouldNotBeNil )
} )
err = il . DeleteBlob ( "test" , d . String ( ) )
So ( err , ShouldBeNil )
} )
err = il . DeleteBlobUpload ( "test" , v )
2019-06-21 02:36:40 +03:00
So ( err , ShouldBeNil )
} )
} )
}