Mocked registry: Added X-Docker-Size when fetching the layer

This commit is contained in:
Sam Alba 2013-07-25 12:57:09 -07:00 committed by shin-
parent 3ca4529fbe
commit 34fc4b8407

View file

@ -6,9 +6,11 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"strconv"
"testing" "testing"
"time" "time"
) )
@ -88,7 +90,15 @@ func init() {
r.HandleFunc("/v1/repositories/{repository:.+}{action:/images|/}", handlerImages).Methods("GET", "PUT", "DELETE") r.HandleFunc("/v1/repositories/{repository:.+}{action:/images|/}", handlerImages).Methods("GET", "PUT", "DELETE")
r.HandleFunc("/v1/repositories/{repository:.+}/auth", handlerAuth).Methods("PUT") r.HandleFunc("/v1/repositories/{repository:.+}/auth", handlerAuth).Methods("PUT")
r.HandleFunc("/v1/search", handlerSearch).Methods("GET") r.HandleFunc("/v1/search", handlerSearch).Methods("GET")
testHttpServer = httptest.NewServer(r) testHttpServer = httptest.NewServer(handlerAccessLog(r))
}
func handlerAccessLog(handler http.Handler) http.Handler {
logHandler := func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s \"%s %s\"", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
}
return http.HandlerFunc(logHandler)
} }
func makeURL(req string) string { func makeURL(req string) string {
@ -104,8 +114,6 @@ func writeHeaders(w http.ResponseWriter) {
h.Add("Cache-Control", "no-cache") h.Add("Cache-Control", "no-cache")
h.Add("X-Docker-Registry-Version", "0.0.0") h.Add("X-Docker-Registry-Version", "0.0.0")
h.Add("X-Docker-Registry-Config", "mock") h.Add("X-Docker-Registry-Config", "mock")
u, _ := url.Parse(testHttpServer.URL)
h.Add("X-Docker-Endpoints", u.Host)
} }
func writeResponse(w http.ResponseWriter, message interface{}, code int) { func writeResponse(w http.ResponseWriter, message interface{}, code int) {
@ -181,6 +189,8 @@ func handlerGetImage(w http.ResponseWriter, r *http.Request) {
return return
} }
writeHeaders(w) writeHeaders(w)
layer_size := len(layer["layer"])
w.Header().Add("X-Docker-Size", strconv.Itoa(layer_size))
io.WriteString(w, layer[vars["action"]]) io.WriteString(w, layer[vars["action"]])
} }
@ -279,6 +289,8 @@ func handlerUsers(w http.ResponseWriter, r *http.Request) {
} }
func handlerImages(w http.ResponseWriter, r *http.Request) { func handlerImages(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(testHttpServer.URL)
w.Header().Add("X-Docker-Endpoints", u.Host)
if r.Method == "PUT" { if r.Method == "PUT" {
writeResponse(w, "", 200) writeResponse(w, "", 200)
return return
@ -292,6 +304,7 @@ func handlerImages(w http.ResponseWriter, r *http.Request) {
image := make(map[string]string) image := make(map[string]string)
image["id"] = image_id image["id"] = image_id
image["checksum"] = layer["checksum_tarsum"] image["checksum"] = layer["checksum_tarsum"]
image["Tag"] = "latest"
images = append(images, image) images = append(images, image)
} }
writeResponse(w, images, 200) writeResponse(w, images, 200)
@ -317,11 +330,11 @@ func TestPing(t *testing.T) {
/* Uncomment this to test Mocked Registry locally with curl /* Uncomment this to test Mocked Registry locally with curl
* WARNING: Don't push on the repos uncommented, it'll block the tests * WARNING: Don't push on the repos uncommented, it'll block the tests
* */
func TestWait(t *testing.T) { func TestWait(t *testing.T) {
fmt.Println("Test HTTP server ready and waiting...") log.Println("Test HTTP server ready and waiting:", testHttpServer.URL)
fmt.Println(testHttpServer.URL)
c := make(chan int) c := make(chan int)
<-c <-c
} }
//*/ //*/