-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob_test.go
58 lines (50 loc) · 1.48 KB
/
blob_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package client
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/opencontainers/go-digest"
)
func TestBlobVerify(t *testing.T) {
tt := []struct {
name string
transport http.RoundTripper
handler http.HandlerFunc
valid bool
}{
{"golden", &DefaultTransport, testVerifyBlobHandler, true},
{"bad round trip", &badTransport{}, testVerifyBlobHandler, false},
{"blob not found", &DefaultTransport, testVerifyBlobNotFoundHandler, false},
}
for _, tc := range tt {
tf := func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(tc.handler))
defer srv.Close()
cfg := Config{
Host: srv.URL,
Transport: tc.transport,
Auth: &TokenAuth{"token"},
}
c, err := New(cfg)
if err != nil {
t.Fatal(err)
}
api := NewDistributionAPI(c)
err = api.VerifyBlob("foo/bar", digest.Digest("sha256:4dda4f89dd2bec1791d7b41c8d63e33e8f7f19092aa3057f2c2be127c6e4b2b9"))
if err == nil && !tc.valid {
t.Fatal("expected invalid test-case")
}
}
t.Run(tc.name, tf)
}
}
func testVerifyBlobHandler(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/v2/foo/bar/blobs/sha256:4dda4f89dd2bec1791d7b41c8d63e33e8f7f19092aa3057f2c2be127c6e4b2b9" {
w.WriteHeader(http.StatusOK)
}
}
func testVerifyBlobNotFoundHandler(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/v2/foo/bar/blobs/sha256:4dda4f89dd2bec1791d7b41c8d63e33e8f7f19092aa3057f2c2be127c6e4b2b9" {
w.WriteHeader(http.StatusNotFound)
}
}