2015-04-17 20:32:51 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2015-07-17 18:42:47 +00:00
|
|
|
"io"
|
2015-04-17 20:32:51 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-07-13 20:08:13 +00:00
|
|
|
"strconv"
|
2015-05-09 00:40:30 +00:00
|
|
|
"strings"
|
2015-04-17 20:32:51 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-05-08 23:33:27 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-04-17 20:32:51 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
2015-08-21 04:24:30 +00:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2015-05-15 01:21:39 +00:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-04-17 20:32:51 +00:00
|
|
|
"github.com/docker/distribution/testutil"
|
2015-09-15 04:12:33 +00:00
|
|
|
"github.com/docker/distribution/uuid"
|
|
|
|
"github.com/docker/libtrust"
|
2015-04-17 20:32:51 +00:00
|
|
|
)
|
|
|
|
|
2015-05-08 23:29:23 +00:00
|
|
|
func testServer(rrm testutil.RequestResponseMap) (string, func()) {
|
2015-04-17 20:32:51 +00:00
|
|
|
h := testutil.NewHandler(rrm)
|
|
|
|
s := httptest.NewServer(h)
|
2015-05-08 23:29:23 +00:00
|
|
|
return s.URL, s.Close
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRandomBlob(size int) (digest.Digest, []byte) {
|
|
|
|
b := make([]byte, size)
|
|
|
|
if n, err := rand.Read(b); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if n != size {
|
|
|
|
panic("unable to read enough bytes")
|
|
|
|
}
|
|
|
|
|
2015-12-14 22:30:51 +00:00
|
|
|
return digest.FromBytes(b), b
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addTestFetch(repo string, dgst digest.Digest, content []byte, m *testutil.RequestResponseMap) {
|
2015-07-14 23:25:37 +00:00
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
*m = append(*m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: content,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
2015-07-14 23:25:37 +00:00
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
*m = append(*m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "HEAD",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
func addTestCatalog(route string, content []byte, link string, m *testutil.RequestResponseMap) {
|
|
|
|
headers := map[string][]string{
|
|
|
|
"Content-Length": {strconv.Itoa(len(content))},
|
|
|
|
"Content-Type": {"application/json; charset=utf-8"},
|
|
|
|
}
|
|
|
|
if link != "" {
|
|
|
|
headers["Link"] = append(headers["Link"], link)
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
*m = append(*m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
2015-07-17 18:42:47 +00:00
|
|
|
Route: route,
|
2015-07-13 20:08:13 +00:00
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: content,
|
2015-07-17 18:42:47 +00:00
|
|
|
Headers: http.Header(headers),
|
2015-07-13 20:08:13 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
func TestBlobDelete(t *testing.T) {
|
|
|
|
dgst, _ := newRandomBlob(1024)
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
repo := "test.example.com/repo1"
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "DELETE",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
r, err := NewRepository(ctx, repo, e, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l := r.Blobs(ctx)
|
|
|
|
err = l.Delete(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error deleting blob: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func TestBlobFetch(t *testing.T) {
|
2015-04-17 20:32:51 +00:00
|
|
|
d1, b1 := newRandomBlob(1024)
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
addTestFetch("test.example.com/repo1", d1, b1, &m)
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
r, err := NewRepository(ctx, "test.example.com/repo1", e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
l := r.Blobs(ctx)
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
b, err := l.Get(ctx, d1)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if bytes.Compare(b, b1) != 0 {
|
|
|
|
t.Fatalf("Wrong bytes values fetched: [%d]byte != [%d]byte", len(b), len(b1))
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:05:44 +00:00
|
|
|
// TODO(dmcgowan): Test for unknown blob case
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 18:17:49 +00:00
|
|
|
func TestBlobExistsNoContentLength(t *testing.T) {
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
|
|
|
|
repo := "biff"
|
|
|
|
dgst, content := newRandomBlob(1024)
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: content,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
// "Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "HEAD",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
// "Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
r, err := NewRepository(ctx, repo, e, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l := r.Blobs(ctx)
|
|
|
|
|
|
|
|
_, err = l.Stat(ctx, dgst)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "missing content-length heade") {
|
|
|
|
t.Fatalf("Expected missing content-length error message")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func TestBlobExists(t *testing.T) {
|
2015-04-17 20:32:51 +00:00
|
|
|
d1, b1 := newRandomBlob(1024)
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
addTestFetch("test.example.com/repo1", d1, b1, &m)
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
r, err := NewRepository(ctx, "test.example.com/repo1", e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
l := r.Blobs(ctx)
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
stat, err := l.Stat(ctx, d1)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
|
|
|
|
if stat.Digest != d1 {
|
|
|
|
t.Fatalf("Unexpected digest: %s, expected %s", stat.Digest, d1)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 00:07:11 +00:00
|
|
|
if stat.Size != int64(len(b1)) {
|
|
|
|
t.Fatalf("Unexpected length: %d, expected %d", stat.Size, len(b1))
|
2015-05-15 20:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dmcgowan): Test error cases and ErrBlobUnknown case
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func TestBlobUploadChunked(t *testing.T) {
|
2015-04-17 20:32:51 +00:00
|
|
|
dgst, b1 := newRandomBlob(1024)
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
chunks := [][]byte{
|
|
|
|
b1[0:256],
|
|
|
|
b1[256:512],
|
|
|
|
b1[512:513],
|
|
|
|
b1[513:1024],
|
|
|
|
}
|
|
|
|
repo := "test.example.com/uploadrepo"
|
2015-05-22 22:55:54 +00:00
|
|
|
uuids := []string{uuid.Generate().String()}
|
2015-04-17 20:32:51 +00:00
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "POST",
|
|
|
|
Route: "/v2/" + repo + "/blobs/uploads/",
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Location": {"/v2/" + repo + "/blobs/uploads/" + uuids[0]},
|
|
|
|
"Docker-Upload-UUID": {uuids[0]},
|
|
|
|
"Range": {"0-0"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
offset := 0
|
|
|
|
for i, chunk := range chunks {
|
2015-05-22 22:55:54 +00:00
|
|
|
uuids = append(uuids, uuid.Generate().String())
|
2015-04-17 20:32:51 +00:00
|
|
|
newOffset := offset + len(chunk)
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: "/v2/" + repo + "/blobs/uploads/" + uuids[i],
|
|
|
|
Body: chunk,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Location": {"/v2/" + repo + "/blobs/uploads/" + uuids[i+1]},
|
|
|
|
"Docker-Upload-UUID": {uuids[i+1]},
|
|
|
|
"Range": {fmt.Sprintf("%d-%d", offset, newOffset-1)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
offset = newOffset
|
|
|
|
}
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PUT",
|
|
|
|
Route: "/v2/" + repo + "/blobs/uploads/" + uuids[len(uuids)-1],
|
|
|
|
QueryParams: map[string][]string{
|
|
|
|
"digest": {dgst.String()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusCreated,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Docker-Content-Digest": {dgst.String()},
|
|
|
|
"Content-Range": {fmt.Sprintf("0-%d", offset-1)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "HEAD",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(offset)},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
r, err := NewRepository(ctx, repo, e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
l := r.Blobs(ctx)
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
upload, err := l.Create(ctx)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
if upload.ID() != uuids[0] {
|
|
|
|
log.Fatalf("Unexpected UUID %s; expected %s", upload.ID(), uuids[0])
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
n, err := upload.Write(chunk)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n != len(chunk) {
|
|
|
|
t.Fatalf("Unexpected length returned from write: %d; expected: %d", n, len(chunk))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
blob, err := upload.Commit(ctx, distribution.Descriptor{
|
|
|
|
Digest: dgst,
|
2015-07-18 00:07:11 +00:00
|
|
|
Size: int64(len(b1)),
|
2015-05-15 20:29:44 +00:00
|
|
|
})
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-18 00:07:11 +00:00
|
|
|
if blob.Size != int64(len(b1)) {
|
|
|
|
t.Fatalf("Unexpected blob size: %d; expected: %d", blob.Size, len(b1))
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func TestBlobUploadMonolithic(t *testing.T) {
|
2015-04-17 20:32:51 +00:00
|
|
|
dgst, b1 := newRandomBlob(1024)
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
repo := "test.example.com/uploadrepo"
|
2015-05-22 22:55:54 +00:00
|
|
|
uploadID := uuid.Generate().String()
|
2015-04-17 20:32:51 +00:00
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "POST",
|
|
|
|
Route: "/v2/" + repo + "/blobs/uploads/",
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Location": {"/v2/" + repo + "/blobs/uploads/" + uploadID},
|
|
|
|
"Docker-Upload-UUID": {uploadID},
|
|
|
|
"Range": {"0-0"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: "/v2/" + repo + "/blobs/uploads/" + uploadID,
|
|
|
|
Body: b1,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Location": {"/v2/" + repo + "/blobs/uploads/" + uploadID},
|
|
|
|
"Docker-Upload-UUID": {uploadID},
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Docker-Content-Digest": {dgst.String()},
|
|
|
|
"Range": {fmt.Sprintf("0-%d", len(b1)-1)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PUT",
|
|
|
|
Route: "/v2/" + repo + "/blobs/uploads/" + uploadID,
|
|
|
|
QueryParams: map[string][]string{
|
|
|
|
"digest": {dgst.String()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusCreated,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Docker-Content-Digest": {dgst.String()},
|
|
|
|
"Content-Range": {fmt.Sprintf("0-%d", len(b1)-1)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "HEAD",
|
|
|
|
Route: "/v2/" + repo + "/blobs/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(b1))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
r, err := NewRepository(ctx, repo, e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
l := r.Blobs(ctx)
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
upload, err := l.Create(ctx)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
if upload.ID() != uploadID {
|
|
|
|
log.Fatalf("Unexpected UUID %s; expected %s", upload.ID(), uploadID)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := upload.ReadFrom(bytes.NewReader(b1))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n != int64(len(b1)) {
|
|
|
|
t.Fatalf("Unexpected ReadFrom length: %d; expected: %d", n, len(b1))
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
blob, err := upload.Commit(ctx, distribution.Descriptor{
|
|
|
|
Digest: dgst,
|
2015-07-18 00:07:11 +00:00
|
|
|
Size: int64(len(b1)),
|
2015-05-15 20:29:44 +00:00
|
|
|
})
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-18 00:07:11 +00:00
|
|
|
if blob.Size != int64(len(b1)) {
|
|
|
|
t.Fatalf("Unexpected blob size: %d; expected: %d", blob.Size, len(b1))
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:12:33 +00:00
|
|
|
func newRandomSchemaV1Manifest(name, tag string, blobCount int) (*schema1.SignedManifest, digest.Digest, []byte) {
|
2015-08-21 04:24:30 +00:00
|
|
|
blobs := make([]schema1.FSLayer, blobCount)
|
|
|
|
history := make([]schema1.History, blobCount)
|
2015-04-17 20:32:51 +00:00
|
|
|
|
|
|
|
for i := 0; i < blobCount; i++ {
|
|
|
|
dgst, blob := newRandomBlob((i % 5) * 16)
|
|
|
|
|
2015-08-21 04:24:30 +00:00
|
|
|
blobs[i] = schema1.FSLayer{BlobSum: dgst}
|
|
|
|
history[i] = schema1.History{V1Compatibility: fmt.Sprintf("{\"Hex\": \"%x\"}", blob)}
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:12:33 +00:00
|
|
|
m := schema1.Manifest{
|
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: "x86",
|
|
|
|
FSLayers: blobs,
|
|
|
|
History: history,
|
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 1,
|
2015-04-17 20:32:51 +00:00
|
|
|
},
|
|
|
|
}
|
2015-09-15 04:12:33 +00:00
|
|
|
|
|
|
|
pk, err := libtrust.GenerateECP256PrivateKey()
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-09-15 04:12:33 +00:00
|
|
|
|
|
|
|
sm, err := schema1.Sign(&m, pk)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:12:33 +00:00
|
|
|
p, err := sm.Payload()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-12-14 22:30:51 +00:00
|
|
|
return sm, digest.FromBytes(p), p
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 23:25:37 +00:00
|
|
|
func addTestManifestWithEtag(repo, reference string, content []byte, m *testutil.RequestResponseMap, dgst string) {
|
2015-12-14 22:30:51 +00:00
|
|
|
actualDigest := digest.FromBytes(content)
|
2015-07-14 23:25:37 +00:00
|
|
|
getReqWithEtag := testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/" + repo + "/manifests/" + reference,
|
|
|
|
Headers: http.Header(map[string][]string{
|
2015-07-29 18:12:01 +00:00
|
|
|
"If-None-Match": {fmt.Sprintf(`"%s"`, dgst)},
|
2015-07-14 23:25:37 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
var getRespWithEtag testutil.Response
|
|
|
|
if actualDigest.String() == dgst {
|
|
|
|
getRespWithEtag = testutil.Response{
|
|
|
|
StatusCode: http.StatusNotModified,
|
|
|
|
Body: []byte{},
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
getRespWithEtag = testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: content,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*m = append(*m, testutil.RequestResponseMapping{Request: getReqWithEtag, Response: getRespWithEtag})
|
|
|
|
}
|
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
func addTestManifest(repo, reference string, content []byte, m *testutil.RequestResponseMap) {
|
|
|
|
*m = append(*m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/" + repo + "/manifests/" + reference,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: content,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
*m = append(*m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "HEAD",
|
|
|
|
Route: "/v2/" + repo + "/manifests/" + reference,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(content))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:24:30 +00:00
|
|
|
func checkEqualManifest(m1, m2 *schema1.SignedManifest) error {
|
2015-04-17 20:32:51 +00:00
|
|
|
if m1.Name != m2.Name {
|
|
|
|
return fmt.Errorf("name does not match %q != %q", m1.Name, m2.Name)
|
|
|
|
}
|
|
|
|
if m1.Tag != m2.Tag {
|
|
|
|
return fmt.Errorf("tag does not match %q != %q", m1.Tag, m2.Tag)
|
|
|
|
}
|
|
|
|
if len(m1.FSLayers) != len(m2.FSLayers) {
|
2015-05-15 20:29:44 +00:00
|
|
|
return fmt.Errorf("fs blob length does not match %d != %d", len(m1.FSLayers), len(m2.FSLayers))
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
for i := range m1.FSLayers {
|
|
|
|
if m1.FSLayers[i].BlobSum != m2.FSLayers[i].BlobSum {
|
|
|
|
return fmt.Errorf("blobsum does not match %q != %q", m1.FSLayers[i].BlobSum, m2.FSLayers[i].BlobSum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m1.History) != len(m2.History) {
|
|
|
|
return fmt.Errorf("history length does not match %d != %d", len(m1.History), len(m2.History))
|
|
|
|
}
|
|
|
|
for i := range m1.History {
|
|
|
|
if m1.History[i].V1Compatibility != m2.History[i].V1Compatibility {
|
|
|
|
return fmt.Errorf("blobsum does not match %q != %q", m1.History[i].V1Compatibility, m2.History[i].V1Compatibility)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestFetch(t *testing.T) {
|
2015-06-15 17:39:34 +00:00
|
|
|
ctx := context.Background()
|
2015-04-17 20:32:51 +00:00
|
|
|
repo := "test.example.com/repo"
|
2015-09-15 04:12:33 +00:00
|
|
|
m1, dgst, _ := newRandomSchemaV1Manifest(repo, "latest", 6)
|
2015-04-17 20:32:51 +00:00
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
addTestManifest(repo, dgst.String(), m1.Raw, &m)
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
r, err := NewRepository(context.Background(), repo, e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
ms, err := r.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
|
|
|
ok, err := ms.Exists(dgst)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Manifest does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest, err := ms.Get(dgst)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := checkEqualManifest(manifest, m1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 23:25:37 +00:00
|
|
|
func TestManifestFetchWithEtag(t *testing.T) {
|
2015-04-17 20:32:51 +00:00
|
|
|
repo := "test.example.com/repo/by/tag"
|
2015-09-15 04:12:33 +00:00
|
|
|
_, d1, p1 := newRandomSchemaV1Manifest(repo, "latest", 6)
|
2015-04-17 20:32:51 +00:00
|
|
|
var m testutil.RequestResponseMap
|
2015-09-15 04:12:33 +00:00
|
|
|
addTestManifestWithEtag(repo, "latest", p1, &m, d1.String())
|
2015-04-17 20:32:51 +00:00
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
r, err := NewRepository(context.Background(), repo, e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ms, err := r.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-09-18 18:00:44 +00:00
|
|
|
_, err = ms.GetByTag("latest", AddEtagToTag("latest", d1.String()))
|
|
|
|
if err != distribution.ErrManifestNotModified {
|
2015-04-17 20:32:51 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestDelete(t *testing.T) {
|
|
|
|
repo := "test.example.com/repo/delete"
|
2015-09-15 04:12:33 +00:00
|
|
|
_, dgst1, _ := newRandomSchemaV1Manifest(repo, "latest", 6)
|
|
|
|
_, dgst2, _ := newRandomSchemaV1Manifest(repo, "latest", 6)
|
2015-04-17 20:32:51 +00:00
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "DELETE",
|
|
|
|
Route: "/v2/" + repo + "/manifests/" + dgst1.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
2015-05-27 17:52:22 +00:00
|
|
|
StatusCode: http.StatusAccepted,
|
2015-04-17 20:32:51 +00:00
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
r, err := NewRepository(context.Background(), repo, e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ms, err := r.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
|
|
|
if err := ms.Delete(dgst1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ms.Delete(dgst2); err == nil {
|
|
|
|
t.Fatal("Expected error deleting unknown manifest")
|
|
|
|
}
|
|
|
|
// TODO(dmcgowan): Check for specific unknown error
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestPut(t *testing.T) {
|
|
|
|
repo := "test.example.com/repo/delete"
|
2015-09-15 04:12:33 +00:00
|
|
|
m1, dgst, _ := newRandomSchemaV1Manifest(repo, "other", 6)
|
2015-04-17 20:32:51 +00:00
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PUT",
|
|
|
|
Route: "/v2/" + repo + "/manifests/other",
|
|
|
|
Body: m1.Raw,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
"Docker-Content-Digest": {dgst.String()},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
r, err := NewRepository(context.Background(), repo, e, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ms, err := r.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
|
|
|
if err := ms.Put(m1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:05:44 +00:00
|
|
|
// TODO(dmcgowan): Check for invalid input error
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-05-09 00:40:30 +00:00
|
|
|
|
|
|
|
func TestManifestTags(t *testing.T) {
|
|
|
|
repo := "test.example.com/repo/tags/list"
|
|
|
|
tagsList := []byte(strings.TrimSpace(`
|
|
|
|
{
|
|
|
|
"name": "test.example.com/repo/tags/list",
|
|
|
|
"tags": [
|
|
|
|
"tag1",
|
|
|
|
"tag2",
|
|
|
|
"funtag"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/" + repo + "/tags/list",
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: tagsList,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(tagsList))},
|
|
|
|
"Last-Modified": {time.Now().Add(-1 * time.Second).Format(time.ANSIC)},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
r, err := NewRepository(context.Background(), repo, e, nil)
|
2015-05-09 00:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ms, err := r.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 00:40:30 +00:00
|
|
|
|
|
|
|
tags, err := ms.Tags()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tags) != 3 {
|
|
|
|
t.Fatalf("Wrong number of tags returned: %d, expected 3", len(tags))
|
|
|
|
}
|
|
|
|
// TODO(dmcgowan): Check array
|
|
|
|
|
|
|
|
// TODO(dmcgowan): Check for error cases
|
|
|
|
}
|
2015-05-20 17:05:44 +00:00
|
|
|
|
|
|
|
func TestManifestUnauthorized(t *testing.T) {
|
|
|
|
repo := "test.example.com/repo"
|
2015-09-15 04:12:33 +00:00
|
|
|
_, dgst, _ := newRandomSchemaV1Manifest(repo, "latest", 6)
|
2015-05-20 17:05:44 +00:00
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
|
|
|
|
m = append(m, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/" + repo + "/manifests/" + dgst.String(),
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
Body: []byte("<html>garbage</html>"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
|
|
|
r, err := NewRepository(context.Background(), repo, e, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ms, err := r.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-20 17:05:44 +00:00
|
|
|
|
|
|
|
_, err = ms.Get(dgst)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error fetching manifest")
|
|
|
|
}
|
2015-06-18 00:39:27 +00:00
|
|
|
v2Err, ok := err.(errcode.Error)
|
2015-05-20 17:05:44 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected error type: %#v", err)
|
|
|
|
}
|
2015-08-06 23:25:08 +00:00
|
|
|
if v2Err.Code != errcode.ErrorCodeUnauthorized {
|
2015-05-20 17:05:44 +00:00
|
|
|
t.Fatalf("Unexpected error code: %s", v2Err.Code.String())
|
|
|
|
}
|
2015-08-06 23:25:08 +00:00
|
|
|
if expected := errcode.ErrorCodeUnauthorized.Message(); v2Err.Message != expected {
|
2015-06-18 00:39:27 +00:00
|
|
|
t.Fatalf("Unexpected message value: %q, expected %q", v2Err.Message, expected)
|
2015-05-20 17:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
|
|
|
func TestCatalog(t *testing.T) {
|
|
|
|
var m testutil.RequestResponseMap
|
2015-07-17 18:42:47 +00:00
|
|
|
addTestCatalog(
|
|
|
|
"/v2/_catalog?n=5",
|
|
|
|
[]byte("{\"repositories\":[\"foo\", \"bar\", \"baz\"]}"), "", &m)
|
2015-07-13 20:08:13 +00:00
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
entries := make([]string, 5)
|
|
|
|
|
2015-07-22 22:18:03 +00:00
|
|
|
r, err := NewRegistry(context.Background(), e, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
ctx := context.Background()
|
2015-07-22 22:18:03 +00:00
|
|
|
numFilled, err := r.Repositories(ctx, entries, "")
|
2015-07-17 18:42:47 +00:00
|
|
|
if err != io.EOF {
|
2015-07-13 20:08:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
if numFilled != 3 {
|
|
|
|
t.Fatalf("Got wrong number of repos")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalogInParts(t *testing.T) {
|
|
|
|
var m testutil.RequestResponseMap
|
|
|
|
addTestCatalog(
|
|
|
|
"/v2/_catalog?n=2",
|
|
|
|
[]byte("{\"repositories\":[\"bar\", \"baz\"]}"),
|
|
|
|
"</v2/_catalog?last=baz&n=2>", &m)
|
|
|
|
addTestCatalog(
|
|
|
|
"/v2/_catalog?last=baz&n=2",
|
|
|
|
[]byte("{\"repositories\":[\"foo\"]}"),
|
|
|
|
"", &m)
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
|
|
|
entries := make([]string, 2)
|
|
|
|
|
2015-07-22 22:18:03 +00:00
|
|
|
r, err := NewRegistry(context.Background(), e, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
ctx := context.Background()
|
2015-07-22 22:18:03 +00:00
|
|
|
numFilled, err := r.Repositories(ctx, entries, "")
|
2015-07-13 20:08:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
if numFilled != 2 {
|
|
|
|
t.Fatalf("Got wrong number of repos")
|
|
|
|
}
|
|
|
|
|
2015-07-22 22:18:03 +00:00
|
|
|
numFilled, err = r.Repositories(ctx, entries, "baz")
|
2015-07-17 18:42:47 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if numFilled != 1 {
|
2015-07-13 20:08:13 +00:00
|
|
|
t.Fatalf("Got wrong number of repos")
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 02:00:28 +00:00
|
|
|
|
|
|
|
func TestSanitizeLocation(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
description string
|
|
|
|
location string
|
|
|
|
source string
|
|
|
|
expected string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "ensure relative location correctly resolved",
|
|
|
|
location: "/v2/foo/baasdf",
|
|
|
|
source: "http://blahalaja.com/v1",
|
|
|
|
expected: "http://blahalaja.com/v2/foo/baasdf",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "ensure parameters are preserved",
|
|
|
|
location: "/v2/foo/baasdf?_state=asdfasfdasdfasdf&digest=foo",
|
|
|
|
source: "http://blahalaja.com/v1",
|
|
|
|
expected: "http://blahalaja.com/v2/foo/baasdf?_state=asdfasfdasdfasdf&digest=foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "ensure new hostname overidden",
|
|
|
|
location: "https://mwhahaha.com/v2/foo/baasdf?_state=asdfasfdasdfasdf",
|
|
|
|
source: "http://blahalaja.com/v1",
|
|
|
|
expected: "https://mwhahaha.com/v2/foo/baasdf?_state=asdfasfdasdfasdf",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
fatalf := func(format string, args ...interface{}) {
|
|
|
|
t.Fatalf(testcase.description+": "+format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := sanitizeLocation(testcase.location, testcase.source)
|
|
|
|
if err != testcase.err {
|
|
|
|
if testcase.err != nil {
|
|
|
|
fatalf("expected error: %v != %v", err, testcase)
|
|
|
|
} else {
|
|
|
|
fatalf("unexpected error sanitizing: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s != testcase.expected {
|
|
|
|
fatalf("bad sanitize: %q != %q", s, testcase.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|