2014-11-14 02:42:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker-registry"
|
2014-11-20 02:52:09 +00:00
|
|
|
"github.com/docker/docker-registry/common/testutil"
|
|
|
|
"github.com/docker/docker-registry/digest"
|
2014-11-14 02:42:49 +00:00
|
|
|
)
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
type testBlob struct {
|
2014-11-20 02:52:09 +00:00
|
|
|
digest digest.Digest
|
2014-11-14 02:42:49 +00:00
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush(t *testing.T) {
|
|
|
|
name := "hello/world"
|
|
|
|
tag := "sometag"
|
2014-11-20 02:06:54 +00:00
|
|
|
testBlobs := []testBlob{
|
2014-11-14 02:42:49 +00:00
|
|
|
{
|
2014-11-20 02:06:54 +00:00
|
|
|
digest: "12345",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some contents"),
|
|
|
|
},
|
|
|
|
{
|
2014-11-20 02:06:54 +00:00
|
|
|
digest: "98765",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some other contents"),
|
|
|
|
},
|
|
|
|
}
|
2014-11-20 02:06:54 +00:00
|
|
|
uploadLocations := make([]string, len(testBlobs))
|
|
|
|
blobs := make([]registry.FSLayer, len(testBlobs))
|
|
|
|
history := make([]registry.ManifestHistory, len(testBlobs))
|
2014-11-14 02:42:49 +00:00
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
for i, blob := range testBlobs {
|
|
|
|
// TODO(bbland): this is returning the same location for all uploads,
|
|
|
|
// because we can't know which blob will get which location.
|
|
|
|
// It's sort of okay because we're using unique digests, but this needs
|
|
|
|
// to change at some point.
|
|
|
|
uploadLocations[i] = fmt.Sprintf("/v2/%s/blob/test-uuid", name)
|
|
|
|
blobs[i] = registry.FSLayer{BlobSum: blob.digest}
|
2014-11-20 02:52:09 +00:00
|
|
|
history[i] = registry.ManifestHistory{V1Compatibility: blob.digest.String()}
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
manifest := ®istry.ImageManifest{
|
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: "x86",
|
2014-11-20 02:06:54 +00:00
|
|
|
FSLayers: blobs,
|
2014-11-14 02:42:49 +00:00
|
|
|
History: history,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
}
|
|
|
|
manifestBytes, err := json.Marshal(manifest)
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
blobRequestResponseMappings := make([]testutil.RequestResponseMapping, 2*len(testBlobs))
|
2014-11-20 02:06:54 +00:00
|
|
|
for i, blob := range testBlobs {
|
2014-11-20 02:52:09 +00:00
|
|
|
blobRequestResponseMappings[2*i] = testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
2014-11-14 02:42:49 +00:00
|
|
|
Method: "POST",
|
2014-11-20 02:06:54 +00:00
|
|
|
Route: "/v2/" + name + "/blob/upload/",
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
2014-11-20 02:52:09 +00:00
|
|
|
Response: testutil.Response{
|
2014-11-20 02:06:54 +00:00
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Location": {uploadLocations[i]},
|
|
|
|
}),
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
|
|
|
}
|
2014-11-20 02:52:09 +00:00
|
|
|
blobRequestResponseMappings[2*i+1] = testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
2014-11-14 02:42:49 +00:00
|
|
|
Method: "PUT",
|
|
|
|
Route: uploadLocations[i],
|
2014-11-20 02:06:54 +00:00
|
|
|
QueryParams: map[string][]string{
|
|
|
|
"length": {fmt.Sprint(len(blob.contents))},
|
2014-11-20 02:52:09 +00:00
|
|
|
"digest": {blob.digest.String()},
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
2014-11-20 02:06:54 +00:00
|
|
|
Body: blob.contents,
|
|
|
|
},
|
2014-11-20 02:52:09 +00:00
|
|
|
Response: testutil.Response{
|
2014-11-20 02:06:54 +00:00
|
|
|
StatusCode: http.StatusCreated,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
handler := testutil.NewHandler(append(blobRequestResponseMappings, testutil.RequestResponseMap{
|
|
|
|
testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
2014-11-14 02:42:49 +00:00
|
|
|
Method: "PUT",
|
2014-11-20 02:06:54 +00:00
|
|
|
Route: "/v2/" + name + "/manifest/" + tag,
|
2014-11-14 02:42:49 +00:00
|
|
|
Body: manifestBytes,
|
|
|
|
},
|
2014-11-20 02:52:09 +00:00
|
|
|
Response: testutil.Response{
|
2014-11-20 02:06:54 +00:00
|
|
|
StatusCode: http.StatusOK,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}...))
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
client := New(server.URL)
|
|
|
|
objectStore := &memoryObjectStore{
|
|
|
|
mutex: new(sync.Mutex),
|
|
|
|
manifestStorage: make(map[string]*registry.ImageManifest),
|
2014-11-20 02:52:09 +00:00
|
|
|
layerStorage: make(map[digest.Digest]Layer),
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
for _, blob := range testBlobs {
|
|
|
|
l, err := objectStore.Layer(blob.digest)
|
2014-11-14 02:42:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer, err := l.Writer()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
writer.Write(blob.contents)
|
2014-11-14 02:42:49 +00:00
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
objectStore.WriteManifest(name, tag, manifest)
|
|
|
|
|
|
|
|
err = Push(client, objectStore, name, tag)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPull(t *testing.T) {
|
|
|
|
name := "hello/world"
|
|
|
|
tag := "sometag"
|
2014-11-20 02:06:54 +00:00
|
|
|
testBlobs := []testBlob{
|
2014-11-14 02:42:49 +00:00
|
|
|
{
|
2014-11-20 02:06:54 +00:00
|
|
|
digest: "12345",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some contents"),
|
|
|
|
},
|
|
|
|
{
|
2014-11-20 02:06:54 +00:00
|
|
|
digest: "98765",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some other contents"),
|
|
|
|
},
|
|
|
|
}
|
2014-11-20 02:06:54 +00:00
|
|
|
blobs := make([]registry.FSLayer, len(testBlobs))
|
|
|
|
history := make([]registry.ManifestHistory, len(testBlobs))
|
2014-11-14 02:42:49 +00:00
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
for i, blob := range testBlobs {
|
|
|
|
blobs[i] = registry.FSLayer{BlobSum: blob.digest}
|
2014-11-20 02:52:09 +00:00
|
|
|
history[i] = registry.ManifestHistory{V1Compatibility: blob.digest.String()}
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
manifest := ®istry.ImageManifest{
|
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: "x86",
|
2014-11-20 02:06:54 +00:00
|
|
|
FSLayers: blobs,
|
2014-11-14 02:42:49 +00:00
|
|
|
History: history,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
}
|
|
|
|
manifestBytes, err := json.Marshal(manifest)
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
blobRequestResponseMappings := make([]testutil.RequestResponseMapping, len(testBlobs))
|
2014-11-20 02:06:54 +00:00
|
|
|
for i, blob := range testBlobs {
|
2014-11-20 02:52:09 +00:00
|
|
|
blobRequestResponseMappings[i] = testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
2014-11-14 02:42:49 +00:00
|
|
|
Method: "GET",
|
2014-11-20 02:52:09 +00:00
|
|
|
Route: "/v2/" + name + "/blob/" + blob.digest.String(),
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
2014-11-20 02:52:09 +00:00
|
|
|
Response: testutil.Response{
|
2014-11-20 02:06:54 +00:00
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: blob.contents,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
handler := testutil.NewHandler(append(blobRequestResponseMappings, testutil.RequestResponseMap{
|
|
|
|
testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
2014-11-14 02:42:49 +00:00
|
|
|
Method: "GET",
|
2014-11-20 02:06:54 +00:00
|
|
|
Route: "/v2/" + name + "/manifest/" + tag,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
2014-11-20 02:52:09 +00:00
|
|
|
Response: testutil.Response{
|
2014-11-20 02:06:54 +00:00
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: manifestBytes,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}...))
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
client := New(server.URL)
|
|
|
|
objectStore := &memoryObjectStore{
|
|
|
|
mutex: new(sync.Mutex),
|
|
|
|
manifestStorage: make(map[string]*registry.ImageManifest),
|
2014-11-20 02:52:09 +00:00
|
|
|
layerStorage: make(map[digest.Digest]Layer),
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = Pull(client, objectStore, name, tag)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := objectStore.Manifest(name, tag)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mBytes, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(mBytes) != string(manifestBytes) {
|
|
|
|
t.Fatal("Incorrect manifest")
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
for _, blob := range testBlobs {
|
|
|
|
l, err := objectStore.Layer(blob.digest)
|
2014-11-14 02:42:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := l.Reader()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
blobBytes, err := ioutil.ReadAll(reader)
|
2014-11-14 02:42:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
if string(blobBytes) != string(blob.contents) {
|
|
|
|
t.Fatal("Incorrect blob")
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|