2014-11-14 02:42:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/common/testutil"
|
|
|
|
"github.com/docker/distribution/digest"
|
2015-01-02 21:21:29 +00:00
|
|
|
"github.com/docker/distribution/manifest"
|
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-12-13 03:09:26 +00:00
|
|
|
digest: "tarsum.v2+sha256:12345",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some contents"),
|
|
|
|
},
|
|
|
|
{
|
2014-12-13 03:09:26 +00:00
|
|
|
digest: "tarsum.v2+sha256: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))
|
2015-01-02 21:21:29 +00:00
|
|
|
blobs := make([]manifest.FSLayer, len(testBlobs))
|
|
|
|
history := make([]manifest.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.
|
2014-12-11 06:29:58 +00:00
|
|
|
uploadLocations[i] = fmt.Sprintf("/v2/%s/blobs/test-uuid", name)
|
2015-01-02 21:21:29 +00:00
|
|
|
blobs[i] = manifest.FSLayer{BlobSum: blob.digest}
|
|
|
|
history[i] = manifest.ManifestHistory{V1Compatibility: blob.digest.String()}
|
2014-11-22 03:29:08 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
m := &manifest.SignedManifest{
|
|
|
|
Manifest: manifest.Manifest{
|
2014-11-22 03:29:08 +00:00
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: "x86",
|
|
|
|
FSLayers: blobs,
|
|
|
|
History: history,
|
2015-01-02 21:21:29 +00:00
|
|
|
Versioned: manifest.Versioned{
|
2014-11-22 03:29:08 +00:00
|
|
|
SchemaVersion: 1,
|
|
|
|
},
|
|
|
|
},
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
2014-12-10 05:25:54 +00:00
|
|
|
var err error
|
2015-01-02 21:21:29 +00:00
|
|
|
m.Raw, err = json.Marshal(m)
|
2014-11-14 02:42:49 +00:00
|
|
|
|
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-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/blobs/uploads/",
|
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{
|
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-12-10 19:09:14 +00:00
|
|
|
handler := testutil.NewHandler(append(blobRequestResponseMappings, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PUT",
|
2014-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/manifests/" + tag,
|
2015-01-02 21:21:29 +00:00
|
|
|
Body: m.Raw,
|
2014-12-10 19:09:14 +00:00
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
2014-12-10 19:09:14 +00:00
|
|
|
}))
|
2014-12-10 05:25:54 +00:00
|
|
|
var server *httptest.Server
|
|
|
|
|
|
|
|
// HACK(stevvooe): Super hack to follow: the request response map approach
|
|
|
|
// above does not let us correctly format the location header to the
|
|
|
|
// server url. This handler intercepts and re-writes the location header
|
|
|
|
// to the server url.
|
|
|
|
|
|
|
|
hack := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w = &headerInterceptingResponseWriter{ResponseWriter: w, serverURL: server.URL}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
server = httptest.NewServer(hack)
|
2014-12-13 03:09:26 +00:00
|
|
|
client, err := New(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating client: %v", err)
|
|
|
|
}
|
2014-11-14 02:42:49 +00:00
|
|
|
objectStore := &memoryObjectStore{
|
|
|
|
mutex: new(sync.Mutex),
|
2015-01-02 21:21:29 +00:00
|
|
|
manifestStorage: make(map[string]*manifest.SignedManifest),
|
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-18 23:44:39 +00:00
|
|
|
writer.SetSize(len(blob.contents))
|
2014-11-20 02:06:54 +00:00
|
|
|
writer.Write(blob.contents)
|
2014-11-14 02:42:49 +00:00
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
objectStore.WriteManifest(name, tag, m)
|
2014-11-14 02:42:49 +00:00
|
|
|
|
|
|
|
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-12-13 03:09:26 +00:00
|
|
|
digest: "tarsum.v2+sha256:12345",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some contents"),
|
|
|
|
},
|
|
|
|
{
|
2014-12-13 03:09:26 +00:00
|
|
|
digest: "tarsum.v2+sha256:98765",
|
2014-11-14 02:42:49 +00:00
|
|
|
contents: []byte("some other contents"),
|
|
|
|
},
|
|
|
|
}
|
2015-01-02 21:21:29 +00:00
|
|
|
blobs := make([]manifest.FSLayer, len(testBlobs))
|
|
|
|
history := make([]manifest.ManifestHistory, len(testBlobs))
|
2014-11-14 02:42:49 +00:00
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
for i, blob := range testBlobs {
|
2015-01-02 21:21:29 +00:00
|
|
|
blobs[i] = manifest.FSLayer{BlobSum: blob.digest}
|
|
|
|
history[i] = manifest.ManifestHistory{V1Compatibility: blob.digest.String()}
|
2014-11-22 03:29:08 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
m := &manifest.SignedManifest{
|
|
|
|
Manifest: manifest.Manifest{
|
2014-11-22 03:29:08 +00:00
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: "x86",
|
|
|
|
FSLayers: blobs,
|
|
|
|
History: history,
|
2015-01-02 21:21:29 +00:00
|
|
|
Versioned: manifest.Versioned{
|
2014-11-22 03:29:08 +00:00
|
|
|
SchemaVersion: 1,
|
|
|
|
},
|
|
|
|
},
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
2015-01-02 21:21:29 +00:00
|
|
|
manifestBytes, err := json.Marshal(m)
|
2014-11-14 02:42:49 +00:00
|
|
|
|
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-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/blobs/" + 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-12-10 19:09:14 +00:00
|
|
|
handler := testutil.NewHandler(append(blobRequestResponseMappings, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
2014-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/manifests/" + tag,
|
2014-11-14 02:42:49 +00:00
|
|
|
},
|
2014-12-10 19:09:14 +00:00
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: manifestBytes,
|
|
|
|
},
|
|
|
|
}))
|
2014-11-14 02:42:49 +00:00
|
|
|
server := httptest.NewServer(handler)
|
2014-12-13 03:09:26 +00:00
|
|
|
client, err := New(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating client: %v", err)
|
|
|
|
}
|
2014-11-14 02:42:49 +00:00
|
|
|
objectStore := &memoryObjectStore{
|
|
|
|
mutex: new(sync.Mutex),
|
2015-01-02 21:21:29 +00:00
|
|
|
manifestStorage: make(map[string]*manifest.SignedManifest),
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
m, err = objectStore.Manifest(name, tag)
|
2014-11-14 02:42:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 23:44:39 +00:00
|
|
|
|
|
|
|
func TestPullResume(t *testing.T) {
|
|
|
|
name := "hello/world"
|
|
|
|
tag := "sometag"
|
|
|
|
testBlobs := []testBlob{
|
|
|
|
{
|
2014-12-13 03:09:26 +00:00
|
|
|
digest: "tarsum.v2+sha256:12345",
|
2014-11-18 23:44:39 +00:00
|
|
|
contents: []byte("some contents"),
|
|
|
|
},
|
|
|
|
{
|
2014-12-13 03:09:26 +00:00
|
|
|
digest: "tarsum.v2+sha256:98765",
|
2014-11-18 23:44:39 +00:00
|
|
|
contents: []byte("some other contents"),
|
|
|
|
},
|
|
|
|
}
|
2015-01-02 21:21:29 +00:00
|
|
|
layers := make([]manifest.FSLayer, len(testBlobs))
|
|
|
|
history := make([]manifest.ManifestHistory, len(testBlobs))
|
2014-11-18 23:44:39 +00:00
|
|
|
|
|
|
|
for i, layer := range testBlobs {
|
2015-01-02 21:21:29 +00:00
|
|
|
layers[i] = manifest.FSLayer{BlobSum: layer.digest}
|
|
|
|
history[i] = manifest.ManifestHistory{V1Compatibility: layer.digest.String()}
|
2014-11-18 23:44:39 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
m := &manifest.Manifest{
|
2014-11-25 00:38:33 +00:00
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: "x86",
|
|
|
|
FSLayers: layers,
|
|
|
|
History: history,
|
2015-01-02 21:21:29 +00:00
|
|
|
Versioned: manifest.Versioned{
|
2014-11-25 00:38:33 +00:00
|
|
|
SchemaVersion: 1,
|
|
|
|
},
|
2014-11-18 23:44:39 +00:00
|
|
|
}
|
2015-01-02 21:21:29 +00:00
|
|
|
manifestBytes, err := json.Marshal(m)
|
2014-11-18 23:44:39 +00:00
|
|
|
|
|
|
|
layerRequestResponseMappings := make([]testutil.RequestResponseMapping, 2*len(testBlobs))
|
|
|
|
for i, blob := range testBlobs {
|
|
|
|
layerRequestResponseMappings[2*i] = testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
2014-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/blobs/" + blob.digest.String(),
|
2014-11-18 23:44:39 +00:00
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: blob.contents[:len(blob.contents)/2],
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Content-Length": {fmt.Sprint(len(blob.contents))},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
layerRequestResponseMappings[2*i+1] = testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
2014-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/blobs/" + blob.digest.String(),
|
2014-11-18 23:44:39 +00:00
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: blob.contents[len(blob.contents)/2:],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
2014-12-10 19:09:14 +00:00
|
|
|
layerRequestResponseMappings = append(layerRequestResponseMappings, testutil.RequestResponseMapping{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
2014-12-11 06:29:58 +00:00
|
|
|
Route: "/v2/" + name + "/manifests/" + tag,
|
2014-12-10 19:09:14 +00:00
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: manifestBytes,
|
2014-11-18 23:44:39 +00:00
|
|
|
},
|
2014-12-10 19:09:14 +00:00
|
|
|
})
|
2014-11-18 23:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handler := testutil.NewHandler(layerRequestResponseMappings)
|
|
|
|
server := httptest.NewServer(handler)
|
2014-12-13 03:09:26 +00:00
|
|
|
client, err := New(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating client: %v", err)
|
|
|
|
}
|
2014-11-18 23:44:39 +00:00
|
|
|
objectStore := &memoryObjectStore{
|
|
|
|
mutex: new(sync.Mutex),
|
2015-01-02 21:21:29 +00:00
|
|
|
manifestStorage: make(map[string]*manifest.SignedManifest),
|
2014-11-18 23:44:39 +00:00
|
|
|
layerStorage: make(map[digest.Digest]Layer),
|
|
|
|
}
|
|
|
|
|
|
|
|
for attempts := 0; attempts < 3; attempts++ {
|
|
|
|
err = Pull(client, objectStore, name, tag)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
sm, err := objectStore.Manifest(name, tag)
|
2014-11-18 23:44:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-02 21:21:29 +00:00
|
|
|
mBytes, err := json.Marshal(sm)
|
2014-11-18 23:44:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(mBytes) != string(manifestBytes) {
|
|
|
|
t.Fatal("Incorrect manifest")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blob := range testBlobs {
|
|
|
|
l, err := objectStore.Layer(blob.digest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := l.Reader()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
layerBytes, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(layerBytes) != string(blob.contents) {
|
|
|
|
t.Fatal("Incorrect blob")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 05:25:54 +00:00
|
|
|
|
|
|
|
// headerInterceptingResponseWriter is a hacky workaround to re-write the
|
|
|
|
// location header to have the server url.
|
|
|
|
type headerInterceptingResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
serverURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hirw *headerInterceptingResponseWriter) WriteHeader(status int) {
|
|
|
|
location := hirw.Header().Get("Location")
|
|
|
|
if location != "" {
|
|
|
|
hirw.Header().Set("Location", hirw.serverURL+location)
|
|
|
|
}
|
|
|
|
|
|
|
|
hirw.ResponseWriter.WriteHeader(status)
|
|
|
|
}
|