Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
avtakkar committed Mar 26, 2024
1 parent 11ddb88 commit 8d69ec9
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 10 deletions.
2 changes: 2 additions & 0 deletions build/package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ FROM mcr.microsoft.com/cbl-mariner/base/core:2.0 as runtime

ARG USER_ID=6191

ENV GIN_MODE=release

# Install useful packages
RUN tdnf update -y && \
tdnf install ca-certificates-microsoft -y && \
Expand Down
107 changes: 107 additions & 0 deletions internal/handlers/v2/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package handlers

import (
"context"
"net/http"
"net/http/httptest"
"testing"

p2pcontext "github.com/azure/peerd/internal/context"
"github.com/azure/peerd/internal/oci/distribution"
storetests "github.com/azure/peerd/internal/oci/store/tests"
"github.com/azure/peerd/internal/routing/tests"
"github.com/gin-gonic/gin"
)

func TestNew(t *testing.T) {
mr := tests.NewMockRouter(nil)
ms := storetests.NewMockContainerdStore(nil)

h, err := New(context.Background(), mr, ms)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if h == nil {
t.Fatalf("unexpected nil handler")
}
}

func TestFillDefault(t *testing.T) {
mr := tests.NewMockRouter(nil)
ms := storetests.NewMockContainerdStore(nil)

h, err := New(context.Background(), mr, ms)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

recorder := httptest.NewRecorder()
mc, _ := gin.CreateTestContext(recorder)

req, err := http.NewRequest("GET", "http://127.0.0.1:5000/v2/library/alpine/manifests/3.18.0", nil)
if err != nil {
t.Fatal(err)
}
mc.Request = req

err = h.fill(mc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

gotNs := mc.GetString(p2pcontext.NamespaceCtxKey)
if gotNs != "docker.io" {
t.Fatalf("expected docker.io, got %s", gotNs)
}

if mc.GetString(p2pcontext.ReferenceCtxKey) != "docker.io/library/alpine:3.18.0" {
t.Fatalf("expected library/alpine, got %s", mc.GetString(p2pcontext.ReferenceCtxKey))
}

if mc.GetString(p2pcontext.DigestCtxKey) != "" {
t.Fatalf("expected empty string, got %s", mc.GetString(p2pcontext.DigestCtxKey))
}

gotRefType, ok := mc.Get(p2pcontext.RefTypeCtxKey)
if !ok {
t.Fatalf("expected reference type, got nil")
}

if gotRefType.(distribution.ReferenceType) != distribution.ReferenceTypeManifest {
t.Fatalf("expected Manifest, got %v", gotRefType)
}

mc2, _ := gin.CreateTestContext(recorder)
req2, err := http.NewRequest("GET", "http://127.0.0.1:5000/v2/library/alpine/manifests/sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30?ns=k8s.io", nil)
if err != nil {
t.Fatal(err)
}
mc2.Request = req2

err = h.fill(mc2)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if mc2.GetString(p2pcontext.NamespaceCtxKey) != "k8s.io" {
t.Fatalf("expected k8s.io, got %s", mc2.GetString(p2pcontext.NamespaceCtxKey))
}

if mc2.GetString(p2pcontext.ReferenceCtxKey) != "" {
t.Fatalf("expected empty string, got %s", mc2.GetString(p2pcontext.ReferenceCtxKey))
}

if mc2.GetString(p2pcontext.DigestCtxKey) != "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30" {
t.Fatalf("expected sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30, got %s", mc2.GetString(p2pcontext.DigestCtxKey))
}

gotRefType, ok = mc2.Get(p2pcontext.RefTypeCtxKey)
if !ok {
t.Fatalf("expected reference type, got nil")
}

if gotRefType.(distribution.ReferenceType) != distribution.ReferenceTypeManifest {
t.Fatalf("expected Manifest, got %v", gotRefType)
}
}
59 changes: 50 additions & 9 deletions internal/oci/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http/httptest"
"testing"

p2pcontext "github.com/azure/peerd/internal/context"
"github.com/azure/peerd/internal/oci/distribution"
"github.com/azure/peerd/internal/oci/store/tests"
"github.com/azure/peerd/pkg/containerd"
"github.com/gin-gonic/gin"
Expand All @@ -22,7 +24,7 @@ func TestNewRegistry(t *testing.T) {
}

func TestHandleManifest(t *testing.T) {
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:abcdef")
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
if err != nil {
t.Fatal(err)
}
Expand All @@ -42,7 +44,7 @@ func TestHandleManifest(t *testing.T) {

mc.Request = req

r.handleManifest(mc, "sha256:abcdef")
r.handleManifest(mc, "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")

if mr.Code != 200 {
t.Fatalf("expected 200, got %d", mr.Code)
Expand All @@ -62,7 +64,7 @@ func TestHandleManifest(t *testing.T) {
}

func TestHandleBlob(t *testing.T) {
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:abcdef")
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:blob")
if err != nil {
t.Fatal(err)
}
Expand All @@ -75,14 +77,14 @@ func TestHandleBlob(t *testing.T) {
mr := httptest.NewRecorder()
mc, _ := gin.CreateTestContext(mr)

req, err := http.NewRequest("GET", "http://127.0.0.1:5000/v2/library/alpine/blobs/sha256:abcdef", nil)
req, err := http.NewRequest("GET", "http://127.0.0.1:5000/v2/library/alpine/blobs/sha256:blob", nil)
if err != nil {
t.Fatal(err)
}

mc.Request = req

r.handleBlob(mc, "sha256:abcdef")
r.handleBlob(mc, "sha256:blob")

if mr.Code != 200 {
t.Fatalf("expected 200, got %d", mr.Code)
Expand All @@ -96,11 +98,50 @@ func TestHandleBlob(t *testing.T) {
t.Fatalf("expected 4, got %s", mr.Header().Get(contentLengthHeader))
}

if mr.Header().Get(dockerContentDigestHeader) != "sha256:abcdef" {
t.Fatalf("expected sha256:abcdef, got %s", mr.Header().Get(dockerContentDigestHeader))
if mr.Header().Get(dockerContentDigestHeader) != "sha256:blob" {
t.Fatalf("expected sha256:blob, got %s", mr.Header().Get(dockerContentDigestHeader))
}
}

func TestHandle(t *testing.T) {
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
if err != nil {
t.Fatal(err)
}
refs := []containerd.Reference{img}

ms := tests.NewMockContainerdStore(refs)

r := NewRegistry(ms)

mr := httptest.NewRecorder()
mc, _ := gin.CreateTestContext(mr)

req, err := http.NewRequest("GET", "http://127.0.0.1:5000/v2/library/alpine/manifests/3.18.0", nil)
if err != nil {
t.Fatal(err)
}

if mr.Header().Get(contentTypeHeader) != "application/octet-stream" {
t.Fatalf("expected application/octet-stream, got %s", mr.Header().Get(contentTypeHeader))
mc.Request = req
mc.Set(p2pcontext.DigestCtxKey, "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
mc.Set(p2pcontext.ReferenceCtxKey, "library/alpine:3.18.0")
mc.Set(p2pcontext.RefTypeCtxKey, distribution.ReferenceType(distribution.ReferenceTypeManifest))

r.Handle(mc)

if mr.Code != 200 {
t.Fatalf("expected 200, got %d", mr.Code)
}

if mr.Body.String() != "test" {
t.Fatalf("expected test, got %s", mr.Body.String())
}

if mr.Header().Get(contentTypeHeader) != "application/vnd.oci.image.manifest.v1+json" {
t.Fatalf("expected application/vnd.oci.image.manifest.v1+json, got %s", mr.Header().Get(contentTypeHeader))
}

if mr.Header().Get(contentLengthHeader) != "4" {
t.Fatalf("expected 4, got %s", mr.Header().Get(contentLengthHeader))
}
}
4 changes: 3 additions & 1 deletion internal/oci/store/tests/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func (m *MockContainerdStore) Size(ctx context.Context, dgst digest.Digest) (int
}

func (m *MockContainerdStore) Write(ctx context.Context, dst io.Writer, dgst digest.Digest) error {
return nil
val := []byte("test")
_, err := dst.Write(val)
return err
}

func (m *MockContainerdStore) Bytes(ctx context.Context, dgst digest.Digest) ([]byte, string, error) {
Expand Down

0 comments on commit 8d69ec9

Please sign in to comment.