-
Notifications
You must be signed in to change notification settings - Fork 19
/
registry.go
124 lines (101 loc) · 2.74 KB
/
registry.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package testhelpers
import (
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"sync"
"github.com/onsi/gomega/ghttp"
)
type blobHandler struct {
httpHandler http.HandlerFunc
order int
}
type FakeRegistry struct {
ActualRegistryURL *url.URL
blobHandlers map[string]blobHandler
blobRequestsCounter map[string]int
blobRegexp *regexp.Regexp
failNextRequests int
revProxy *httputil.ReverseProxy
server *ghttp.Server
mutex *sync.RWMutex
}
func NewFakeRegistry(actualRegistryURL *url.URL) *FakeRegistry {
return &FakeRegistry{
ActualRegistryURL: actualRegistryURL,
blobHandlers: make(map[string]blobHandler),
blobRequestsCounter: make(map[string]int),
mutex: &sync.RWMutex{},
}
}
func (r *FakeRegistry) Start() {
r.revProxy = httputil.NewSingleHostReverseProxy(r.ActualRegistryURL)
// Dockerhub returns 503 if the host is set to localhost as it happens with
// the reverse proxy
oldDirector := r.revProxy.Director
r.revProxy.Director = func(req *http.Request) {
oldDirector(req)
req.Host = r.ActualRegistryURL.Host
}
r.server = ghttp.NewTLSServer()
r.blobRegexp = regexp.MustCompile(`\/v2\/.*\/blobs\/sha256:([a-f0-9]*)`)
r.server.RouteToHandler("GET", r.blobRegexp, r.serveBlob)
ourRegexp := regexp.MustCompile(`.*`)
r.server.RouteToHandler("GET", ourRegexp, r.revProxy.ServeHTTP)
}
func (r *FakeRegistry) FailNextRequests(n int) {
r.failNextRequests = n
}
func (r *FakeRegistry) serveBlob(rw http.ResponseWriter, req *http.Request) {
if r.failNextRequests > 0 {
r.failNextRequests--
rw.WriteHeader(http.StatusUnauthorized)
_, _ = rw.Write([]byte("nope"))
return
}
match := r.blobRegexp.FindStringSubmatch(req.URL.Path)
if match == nil {
r.revProxy.ServeHTTP(rw, req)
return
}
digest := match[1]
r.mutex.Lock()
r.blobRequestsCounter[digest]++
r.mutex.Unlock()
r.mutex.RLock()
defer r.mutex.RUnlock()
handler, ok := r.blobHandlers[digest]
if !ok {
r.revProxy.ServeHTTP(rw, req)
return
}
if handler.order == 0 || r.blobRequestsCounter[digest] == handler.order {
handler.httpHandler(rw, req)
} else {
r.revProxy.ServeHTTP(rw, req)
}
}
func (r *FakeRegistry) Stop() {
r.server.Close()
}
func (r *FakeRegistry) Addr() string {
return r.server.Addr()
}
func (r *FakeRegistry) WhenGettingBlob(digest string, order int, httpHandler http.HandlerFunc) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.blobHandlers[digest] = blobHandler{
httpHandler: httpHandler,
order: order,
}
}
func (r *FakeRegistry) RequestedBlobs() []string {
r.mutex.RLock()
defer r.mutex.RUnlock()
blobDigests := []string{}
for digest, _ := range r.blobRequestsCounter {
blobDigests = append(blobDigests, digest)
}
return blobDigests
}