mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-08-19 04:16:27 +00:00
add basic store tests
This commit is contained in:
@@ -20,6 +20,7 @@ airgap-scp.sh
|
||||
|
||||
# test artifacts
|
||||
*.tar*
|
||||
*.out
|
||||
|
||||
# generated
|
||||
dist/
|
||||
|
||||
@@ -43,6 +43,7 @@ require (
|
||||
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b // indirect
|
||||
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.1 // indirect
|
||||
github.com/containerd/stargz-snapshotter/estargz v0.10.0 // indirect
|
||||
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/docker/cli v20.10.11+incompatible // indirect
|
||||
@@ -125,6 +126,7 @@ require (
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
github.com/ulikunitz/xz v0.5.8 // indirect
|
||||
github.com/vbatts/tar-split v0.11.2 // indirect
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
|
||||
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
|
||||
|
||||
+1
-1
@@ -201,7 +201,7 @@ type ociPusher struct {
|
||||
// by the descriptor.
|
||||
func (p *ociPusher) Push(ctx context.Context, d ocispec.Descriptor) (ccontent.Writer, error) {
|
||||
switch d.MediaType {
|
||||
case ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex:
|
||||
case ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex, consts.DockerManifestSchema2:
|
||||
// if the hash of the content matches that which was provided as the hash for the root, mark it
|
||||
if p.digest != "" && p.digest == d.Digest.String() {
|
||||
if err := p.oci.LoadIndex(); err != nil {
|
||||
|
||||
+3
-10
@@ -97,16 +97,9 @@ func (l *layout) commit(ctx context.Context, b *Store) (ocispec.Descriptor, erro
|
||||
if err := src.LoadIndex(); err != nil {
|
||||
return ocispec.Descriptor{}, err
|
||||
}
|
||||
ref := src.index.Manifests[0].Annotations[ocispec.AnnotationRefName]
|
||||
// refs := src.ListReferences()
|
||||
// if len(refs) > 1 {
|
||||
// return ocispec.Descriptor{}, errors.Errorf("expected 1 reference to commit, got %d", len(refs))
|
||||
// }
|
||||
//
|
||||
// var ref string
|
||||
// for r := range refs {
|
||||
// ref = r
|
||||
// }
|
||||
|
||||
m := src.index.Manifests[0]
|
||||
ref := m.Annotations[ocispec.AnnotationRefName]
|
||||
|
||||
desc, err := oras.Copy(ctx, src, ref, dst, "",
|
||||
oras.WithAdditionalCachedMediaTypes(consts.DockerManifestSchema2))
|
||||
|
||||
+65
-15
@@ -1,16 +1,20 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
gv1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/partial"
|
||||
"github.com/google/go-containerregistry/pkg/v1/random"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
||||
"github.com/rancherfederal/hauler/pkg/artifact"
|
||||
"github.com/rancherfederal/hauler/pkg/content/image"
|
||||
"github.com/rancherfederal/hauler/pkg/store"
|
||||
)
|
||||
|
||||
@@ -27,42 +31,88 @@ func TestStore_AddArtifact(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
img, _ := image.NewImage("ghcr.io/stefanprodan/podinfo:6.0.3")
|
||||
ref, _ := name.ParseReference("ghcr.io/stephanprodan/podinfo:6.0.3")
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
oci artifact.OCI
|
||||
reference name.Reference
|
||||
reference string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want v1.Descriptor
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "should add artifact",
|
||||
name: "should add artifact with a valid reference",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
oci: img,
|
||||
reference: ref,
|
||||
reference: "random:v1",
|
||||
},
|
||||
want: v1.Descriptor{},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ref, err := name.ParseReference(tt.args.reference)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := s.AddArtifact(tt.args.ctx, tt.args.oci, tt.args.reference)
|
||||
oci, want := genArtifact(t, ref.Name())
|
||||
|
||||
got, err := s.AddArtifact(tt.args.ctx, oci, ref)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("AddArtifact() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("AddArtifact() got = %v, want %v", got, tt.want)
|
||||
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("AddArtifact() got = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type testImage struct {
|
||||
gv1.Image
|
||||
}
|
||||
|
||||
func (i *testImage) MediaType() string {
|
||||
mt, err := i.Image.MediaType()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(mt)
|
||||
}
|
||||
|
||||
func (i *testImage) RawConfig() ([]byte, error) {
|
||||
return i.RawConfigFile()
|
||||
}
|
||||
|
||||
func genArtifact(t *testing.T, ref string) (artifact.OCI, ocispec.Descriptor) {
|
||||
img, err := random.Image(1024, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
desc, err := partial.Descriptor(img)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
desc.Annotations = make(map[string]string)
|
||||
desc.Annotations[ocispec.AnnotationRefName] = ref
|
||||
|
||||
// gm, err := img.Manifest()
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
//
|
||||
data, err := json.Marshal(desc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var m ocispec.Descriptor
|
||||
if err := json.NewDecoder(bytes.NewBuffer(data)).Decode(&m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &testImage{Image: img}, m
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user