registry: lint

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-10-06 22:34:39 +03:00
parent d1a85078b5
commit ae3b59c171
5 changed files with 41 additions and 38 deletions

View file

@ -224,12 +224,11 @@ func Login(authConfig *AuthConfig, factory *utils.HTTPRequestFactory) (string, e
return "", fmt.Errorf("Login: Account is not Active. Please check your e-mail for a confirmation link.")
}
return "", fmt.Errorf("Login: Account is not Active. Please see the documentation of the registry %s for instructions how to activate it.", serverAddress)
} else {
return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body, resp.StatusCode, resp.Header)
}
} else {
return "", fmt.Errorf("Registration: %s", reqBody)
return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body, resp.StatusCode, resp.Header)
}
return "", fmt.Errorf("Registration: %s", reqBody)
} else if reqStatusCode == 401 {
// This case would happen with private registries where /v1/users is
// protected, so people can use `docker login` as an auth check.

View file

@ -13,7 +13,7 @@ import (
)
// scans string for api version in the URL path. returns the trimmed hostname, if version found, string and API version.
func scanForApiVersion(hostname string) (string, APIVersion) {
func scanForAPIVersion(hostname string) (string, APIVersion) {
var (
chunks []string
apiVersionStr string
@ -43,7 +43,7 @@ func NewEndpoint(hostname string) (*Endpoint, error) {
if !strings.HasPrefix(hostname, "http") {
hostname = "https://" + hostname
}
trimmedHostname, endpoint.Version = scanForApiVersion(hostname)
trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
endpoint.URL, err = url.Parse(trimmedHostname)
if err != nil {
return nil, err

View file

@ -19,7 +19,7 @@ import (
)
var (
testHttpServer *httptest.Server
testHTTPServer *httptest.Server
testLayers = map[string]map[string]string{
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20": {
"json": `{"id":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
@ -99,7 +99,7 @@ func init() {
// /v2/
r.HandleFunc("/v2/version", handlerGetPing).Methods("GET")
testHttpServer = httptest.NewServer(handlerAccessLog(r))
testHTTPServer = httptest.NewServer(handlerAccessLog(r))
}
func handlerAccessLog(handler http.Handler) http.Handler {
@ -111,7 +111,7 @@ func handlerAccessLog(handler http.Handler) http.Handler {
}
func makeURL(req string) string {
return testHttpServer.URL + req
return testHTTPServer.URL + req
}
func writeHeaders(w http.ResponseWriter) {
@ -198,8 +198,8 @@ func handlerGetImage(w http.ResponseWriter, r *http.Request) {
return
}
writeHeaders(w)
layer_size := len(layer["layer"])
w.Header().Add("X-Docker-Size", strconv.Itoa(layer_size))
layerSize := len(layer["layer"])
w.Header().Add("X-Docker-Size", strconv.Itoa(layerSize))
io.WriteString(w, layer[vars["action"]])
}
@ -208,16 +208,16 @@ func handlerPutImage(w http.ResponseWriter, r *http.Request) {
return
}
vars := mux.Vars(r)
image_id := vars["image_id"]
imageID := vars["image_id"]
action := vars["action"]
layer, exists := testLayers[image_id]
layer, exists := testLayers[imageID]
if !exists {
if action != "json" {
http.NotFound(w, r)
return
}
layer = make(map[string]string)
testLayers[image_id] = layer
testLayers[imageID] = layer
}
if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
if checksum != layer["checksum_simple"] && checksum != layer["checksum_tarsum"] {
@ -301,7 +301,7 @@ func handlerUsers(w http.ResponseWriter, r *http.Request) {
}
func handlerImages(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(testHttpServer.URL)
u, _ := url.Parse(testHTTPServer.URL)
w.Header().Add("X-Docker-Endpoints", fmt.Sprintf("%s , %s ", u.Host, "test.example.com"))
w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
if r.Method == "PUT" {
@ -317,9 +317,9 @@ func handlerImages(w http.ResponseWriter, r *http.Request) {
return
}
images := []map[string]string{}
for image_id, layer := range testLayers {
for imageID, layer := range testLayers {
image := make(map[string]string)
image["id"] = image_id
image["id"] = imageID
image["checksum"] = layer["checksum_tarsum"]
image["Tag"] = "latest"
images = append(images, image)

View file

@ -11,9 +11,12 @@ import (
)
var (
IMAGE_ID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
TOKEN = []string{"fake-token"}
REPO = "foo42/bar"
token = []string{"fake-token"}
)
const (
imageID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
REPO = "foo42/bar"
)
func spawnTestRegistrySession(t *testing.T) *Session {
@ -43,27 +46,27 @@ func TestPingRegistryEndpoint(t *testing.T) {
func TestGetRemoteHistory(t *testing.T) {
r := spawnTestRegistrySession(t)
hist, err := r.GetRemoteHistory(IMAGE_ID, makeURL("/v1/"), TOKEN)
hist, err := r.GetRemoteHistory(imageID, makeURL("/v1/"), token)
if err != nil {
t.Fatal(err)
}
assertEqual(t, len(hist), 2, "Expected 2 images in history")
assertEqual(t, hist[0], IMAGE_ID, "Expected "+IMAGE_ID+"as first ancestry")
assertEqual(t, hist[0], imageID, "Expected "+imageID+"as first ancestry")
assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
"Unexpected second ancestry")
}
func TestLookupRemoteImage(t *testing.T) {
r := spawnTestRegistrySession(t)
found := r.LookupRemoteImage(IMAGE_ID, makeURL("/v1/"), TOKEN)
found := r.LookupRemoteImage(imageID, makeURL("/v1/"), token)
assertEqual(t, found, true, "Expected remote lookup to succeed")
found = r.LookupRemoteImage("abcdef", makeURL("/v1/"), TOKEN)
found = r.LookupRemoteImage("abcdef", makeURL("/v1/"), token)
assertEqual(t, found, false, "Expected remote lookup to fail")
}
func TestGetRemoteImageJSON(t *testing.T) {
r := spawnTestRegistrySession(t)
json, size, err := r.GetRemoteImageJSON(IMAGE_ID, makeURL("/v1/"), TOKEN)
json, size, err := r.GetRemoteImageJSON(imageID, makeURL("/v1/"), token)
if err != nil {
t.Fatal(err)
}
@ -72,7 +75,7 @@ func TestGetRemoteImageJSON(t *testing.T) {
t.Fatal("Expected non-empty json")
}
_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), TOKEN)
_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), token)
if err == nil {
t.Fatal("Expected image not found error")
}
@ -80,7 +83,7 @@ func TestGetRemoteImageJSON(t *testing.T) {
func TestGetRemoteImageLayer(t *testing.T) {
r := spawnTestRegistrySession(t)
data, err := r.GetRemoteImageLayer(IMAGE_ID, makeURL("/v1/"), TOKEN, 0)
data, err := r.GetRemoteImageLayer(imageID, makeURL("/v1/"), token, 0)
if err != nil {
t.Fatal(err)
}
@ -88,7 +91,7 @@ func TestGetRemoteImageLayer(t *testing.T) {
t.Fatal("Expected non-nil data result")
}
_, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), TOKEN, 0)
_, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), token, 0)
if err == nil {
t.Fatal("Expected image not found error")
}
@ -96,14 +99,14 @@ func TestGetRemoteImageLayer(t *testing.T) {
func TestGetRemoteTags(t *testing.T) {
r := spawnTestRegistrySession(t)
tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, REPO, TOKEN)
tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, REPO, token)
if err != nil {
t.Fatal(err)
}
assertEqual(t, len(tags), 1, "Expected one tag")
assertEqual(t, tags["latest"], IMAGE_ID, "Expected tag latest to map to "+IMAGE_ID)
assertEqual(t, tags["latest"], imageID, "Expected tag latest to map to "+imageID)
_, err = r.GetRemoteTags([]string{makeURL("/v1/")}, "foo42/baz", TOKEN)
_, err = r.GetRemoteTags([]string{makeURL("/v1/")}, "foo42/baz", token)
if err == nil {
t.Fatal("Expected error when fetching tags for bogus repo")
}
@ -111,11 +114,11 @@ func TestGetRemoteTags(t *testing.T) {
func TestGetRepositoryData(t *testing.T) {
r := spawnTestRegistrySession(t)
parsedUrl, err := url.Parse(makeURL("/v1/"))
parsedURL, err := url.Parse(makeURL("/v1/"))
if err != nil {
t.Fatal(err)
}
host := "http://" + parsedUrl.Host + "/v1/"
host := "http://" + parsedURL.Host + "/v1/"
data, err := r.GetRepositoryData("foo42/bar")
if err != nil {
t.Fatal(err)
@ -137,7 +140,7 @@ func TestPushImageJSONRegistry(t *testing.T) {
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
}
err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"), TOKEN)
err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"), token)
if err != nil {
t.Fatal(err)
}
@ -146,7 +149,7 @@ func TestPushImageJSONRegistry(t *testing.T) {
func TestPushImageLayerRegistry(t *testing.T) {
r := spawnTestRegistrySession(t)
layer := strings.NewReader("")
_, _, err := r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN, []byte{})
_, _, err := r.PushImageLayerRegistry(imageID, layer, makeURL("/v1/"), token, []byte{})
if err != nil {
t.Fatal(err)
}
@ -180,7 +183,7 @@ func TestResolveRepositoryName(t *testing.T) {
func TestPushRegistryTag(t *testing.T) {
r := spawnTestRegistrySession(t)
err := r.PushRegistryTag("foo42/bar", IMAGE_ID, "stable", makeURL("/v1/"), TOKEN)
err := r.PushRegistryTag("foo42/bar", imageID, "stable", makeURL("/v1/"), token)
if err != nil {
t.Fatal(err)
}

View file

@ -3,6 +3,7 @@ package registry
import (
"bytes"
"crypto/sha256"
// this is required for some certificates
_ "crypto/sha512"
"encoding/hex"
"encoding/json"
@ -243,11 +244,11 @@ func (r *Session) GetRemoteTags(registries []string, repository string, token []
func buildEndpointsList(headers []string, indexEp string) ([]string, error) {
var endpoints []string
parsedUrl, err := url.Parse(indexEp)
parsedURL, err := url.Parse(indexEp)
if err != nil {
return nil, err
}
var urlScheme = parsedUrl.Scheme
var urlScheme = parsedURL.Scheme
// The Registry's URL scheme has to match the Index'
for _, ep := range headers {
epList := strings.Split(ep, ",")