-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters