| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package ociserver_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"net/http" |
| 22 |
"net/http/httptest" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/go-quicktest/qt" |
| 26 |
"github.com/opencontainers/go-digest" |
| 27 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 |
|
| 29 |
"cuelabs.dev/go/oci/ociregistry" |
| 30 |
"cuelabs.dev/go/oci/ociregistry/ociclient" |
| 31 |
"cuelabs.dev/go/oci/ociregistry/ocimem" |
| 32 |
"cuelabs.dev/go/oci/ociregistry/ociserver" |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
var ( |
| 52 |
smallData = bytes.Repeat([]byte("x"), 10) |
| 53 |
largeData = bytes.Repeat([]byte("x"), 150*1024) |
| 54 |
) |
| 55 |
|
| 56 |
var proxyTests = []struct { |
| 57 |
name string |
| 58 |
clientDo func(context.Context, ociregistry.Interface) error |
| 59 |
|
| 60 |
proxyRequests []string |
| 61 |
backendRequests []string |
| 62 |
}{ |
| 63 |
{ |
| 64 |
name: "PushBlob_small", |
| 65 |
clientDo: func(ctx context.Context, client ociregistry.Interface) error { |
| 66 |
_, err := client.PushBlob(ctx, "foo/bar", ocispec.Descriptor{ |
| 67 |
Size: int64(len(smallData)), |
| 68 |
Digest: digest.FromBytes(smallData), |
| 69 |
}, bytes.NewReader(smallData)) |
| 70 |
return err |
| 71 |
}, |
| 72 |
proxyRequests: []string{ |
| 73 |
"POST len=0", |
| 74 |
"PUT len=10", |
| 75 |
}, |
| 76 |
backendRequests: []string{ |
| 77 |
"POST len=0", |
| 78 |
"PUT len=10", |
| 79 |
}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "PushBlob_large", |
| 83 |
clientDo: func(ctx context.Context, client ociregistry.Interface) error { |
| 84 |
_, err := client.PushBlob(ctx, "foo/bar", ocispec.Descriptor{ |
| 85 |
Size: int64(len(largeData)), |
| 86 |
Digest: digest.FromBytes(largeData), |
| 87 |
}, bytes.NewReader(largeData)) |
| 88 |
return err |
| 89 |
}, |
| 90 |
proxyRequests: []string{ |
| 91 |
"POST len=0", |
| 92 |
"PUT len=153600", |
| 93 |
}, |
| 94 |
backendRequests: []string{ |
| 95 |
"POST len=0", |
| 96 |
"PUT len=153600", |
| 97 |
}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "PushBlobChunked_large_oneWrite", |
| 101 |
clientDo: func(ctx context.Context, client ociregistry.Interface) error { |
| 102 |
bw, err := client.PushBlobChunked(ctx, "foo/bar", 0) |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
if _, err := bw.Write(largeData); err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
if _, err := bw.Commit(digest.FromBytes(largeData)); err != nil { |
| 110 |
return err |
| 111 |
} |
| 112 |
return nil |
| 113 |
}, |
| 114 |
proxyRequests: []string{ |
| 115 |
"POST len=0", |
| 116 |
"PATCH len=153600", |
| 117 |
"PUT len=0", |
| 118 |
}, |
| 119 |
backendRequests: []string{ |
| 120 |
"POST len=0", |
| 121 |
"PATCH len=153600", |
| 122 |
"PUT len=0", |
| 123 |
}, |
| 124 |
}, |
| 125 |
} |
| 126 |
|
| 127 |
func recordingServer(tb testing.TB, reqs *[]string, handler http.Handler) *httptest.Server { |
| 128 |
recHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 129 |
*reqs = append(*reqs, fmt.Sprintf("%s len=%d", r.Method, r.ContentLength)) |
| 130 |
handler.ServeHTTP(w, r) |
| 131 |
}) |
| 132 |
server := httptest.NewServer(recHandler) |
| 133 |
tb.Cleanup(server.Close) |
| 134 |
return server |
| 135 |
} |
| 136 |
|
| 137 |
func testClient(tb testing.TB, server *httptest.Server) ociregistry.Interface { |
| 138 |
client, err := ociclient.New(server.Listener.Addr().String(), &ociclient.Options{ |
| 139 |
Insecure: true, |
| 140 |
}) |
| 141 |
qt.Assert(tb, qt.IsNil(err)) |
| 142 |
return client |
| 143 |
} |
| 144 |
|
| 145 |
func TestProxyRequests(t *testing.T) { |
| 146 |
for _, test := range proxyTests { |
| 147 |
t.Run(test.name, func(t *testing.T) { |
| 148 |
|
| 149 |
var proxyReqs, backendReqs []string |
| 150 |
backendServer := recordingServer(t, &backendReqs, |
| 151 |
ociserver.New(ocimem.New(), nil)) |
| 152 |
|
| 153 |
|
| 154 |
proxyServer := recordingServer(t, &proxyReqs, |
| 155 |
ociserver.New(testClient(t, backendServer), nil)) |
| 156 |
|
| 157 |
|
| 158 |
inputClient := testClient(t, proxyServer) |
| 159 |
|
| 160 |
|
| 161 |
err := test.clientDo(context.TODO(), inputClient) |
| 162 |
qt.Assert(t, qt.IsNil(err)) |
| 163 |
|
| 164 |
qt.Check(t, qt.DeepEquals(proxyReqs, test.proxyRequests)) |
| 165 |
qt.Check(t, qt.DeepEquals(backendReqs, test.backendRequests)) |
| 166 |
}) |
| 167 |
} |
| 168 |
} |