mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #8423 from unclejack/lint_changes
lint changes part 1
This commit is contained in:
commit
9df3e45ba9
11 changed files with 107 additions and 85 deletions
|
@ -92,7 +92,7 @@ func httpError(w http.ResponseWriter, err error) {
|
||||||
// FIXME: this is brittle and should not be necessary.
|
// FIXME: this is brittle and should not be necessary.
|
||||||
// If we need to differentiate between different possible error types, we should
|
// If we need to differentiate between different possible error types, we should
|
||||||
// create appropriate error types with clearly defined meaning.
|
// create appropriate error types with clearly defined meaning.
|
||||||
if strings.Contains(err.Error(), "No such") {
|
if strings.Contains(err.Error(), "no such") {
|
||||||
statusCode = http.StatusNotFound
|
statusCode = http.StatusNotFound
|
||||||
} else if strings.Contains(err.Error(), "Bad parameter") {
|
} else if strings.Contains(err.Error(), "Bad parameter") {
|
||||||
statusCode = http.StatusBadRequest
|
statusCode = http.StatusBadRequest
|
||||||
|
|
|
@ -131,8 +131,8 @@ func (db *Database) Set(fullPath, id string) (*Entity, error) {
|
||||||
if _, err := db.conn.Exec("BEGIN EXCLUSIVE"); err != nil {
|
if _, err := db.conn.Exec("BEGIN EXCLUSIVE"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var entityId string
|
var entityID string
|
||||||
if err := db.conn.QueryRow("SELECT id FROM entity WHERE id = ?;", id).Scan(&entityId); err != nil {
|
if err := db.conn.QueryRow("SELECT id FROM entity WHERE id = ?;", id).Scan(&entityID); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
if _, err := db.conn.Exec("INSERT INTO entity (id) VALUES(?);", id); err != nil {
|
if _, err := db.conn.Exec("INSERT INTO entity (id) VALUES(?);", id); err != nil {
|
||||||
rollback()
|
rollback()
|
||||||
|
@ -320,14 +320,14 @@ func (db *Database) RefPaths(id string) Edges {
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var name string
|
var name string
|
||||||
var parentId string
|
var parentID string
|
||||||
if err := rows.Scan(&name, &parentId); err != nil {
|
if err := rows.Scan(&name, &parentID); err != nil {
|
||||||
return refs
|
return refs
|
||||||
}
|
}
|
||||||
refs = append(refs, &Edge{
|
refs = append(refs, &Edge{
|
||||||
EntityID: id,
|
EntityID: id,
|
||||||
Name: name,
|
Name: name,
|
||||||
ParentID: parentId,
|
ParentID: parentID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return refs
|
return refs
|
||||||
|
@ -443,11 +443,11 @@ func (db *Database) children(e *Entity, name string, depth int, entities []WalkM
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var entityId, entityName string
|
var entityID, entityName string
|
||||||
if err := rows.Scan(&entityId, &entityName); err != nil {
|
if err := rows.Scan(&entityID, &entityName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
child := &Entity{entityId}
|
child := &Entity{entityID}
|
||||||
edge := &Edge{
|
edge := &Edge{
|
||||||
ParentID: e.id,
|
ParentID: e.id,
|
||||||
Name: entityName,
|
Name: entityName,
|
||||||
|
@ -490,11 +490,11 @@ func (db *Database) parents(e *Entity) (parents []string, err error) {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var parentId string
|
var parentID string
|
||||||
if err := rows.Scan(&parentId); err != nil {
|
if err := rows.Scan(&parentID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
parents = append(parents, parentId)
|
parents = append(parents, parentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return parents, nil
|
return parents, nil
|
||||||
|
|
|
@ -6,18 +6,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Define our own version of RFC339Nano because we want one
|
// RFC3339NanoFixed is our own version of RFC339Nano because we want one
|
||||||
// that pads the nano seconds part with zeros to ensure
|
// that pads the nano seconds part with zeros to ensure
|
||||||
// the timestamps are aligned in the logs.
|
// the timestamps are aligned in the logs.
|
||||||
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
|
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
|
||||||
JSONFormat = `"` + time.RFC3339Nano + `"`
|
// JSONFormat is the format used by FastMarshalJSON
|
||||||
|
JSONFormat = `"` + time.RFC3339Nano + `"`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FastMarshalJSON avoids one of the extra allocations that
|
||||||
|
// time.MarshalJSON is making.
|
||||||
func FastMarshalJSON(t time.Time) (string, error) {
|
func FastMarshalJSON(t time.Time) (string, error) {
|
||||||
if y := t.Year(); y < 0 || y >= 10000 {
|
if y := t.Year(); y < 0 || y >= 10000 {
|
||||||
// RFC 3339 is clear that years are 4 digits exactly.
|
// RFC 3339 is clear that years are 4 digits exactly.
|
||||||
// See golang.org/issue/4556#c15 for more discussion.
|
// See golang.org/issue/4556#c15 for more discussion.
|
||||||
return "", errors.New("Time.MarshalJSON: year outside of range [0,9999]")
|
return "", errors.New("time.MarshalJSON: year outside of range [0,9999]")
|
||||||
}
|
}
|
||||||
return t.Format(JSONFormat), nil
|
return t.Format(JSONFormat), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNoID = errors.New("prefix can't be empty")
|
// ErrNoID is thrown when attempting to use empty prefixes
|
||||||
|
ErrNoID = errors.New("prefix can't be empty")
|
||||||
|
errDuplicateID = errors.New("multiple IDs were found")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -27,56 +29,62 @@ type TruncIndex struct {
|
||||||
ids map[string]struct{}
|
ids map[string]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTruncIndex creates a new TruncIndex and initializes with a list of IDs
|
||||||
func NewTruncIndex(ids []string) (idx *TruncIndex) {
|
func NewTruncIndex(ids []string) (idx *TruncIndex) {
|
||||||
idx = &TruncIndex{
|
idx = &TruncIndex{
|
||||||
ids: make(map[string]struct{}),
|
ids: make(map[string]struct{}),
|
||||||
trie: patricia.NewTrie(),
|
trie: patricia.NewTrie(),
|
||||||
}
|
}
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
idx.addId(id)
|
idx.addID(id)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (idx *TruncIndex) addId(id string) error {
|
func (idx *TruncIndex) addID(id string) error {
|
||||||
if strings.Contains(id, " ") {
|
if strings.Contains(id, " ") {
|
||||||
return fmt.Errorf("Illegal character: ' '")
|
return fmt.Errorf("illegal character: ' '")
|
||||||
}
|
}
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return ErrNoID
|
return ErrNoID
|
||||||
}
|
}
|
||||||
if _, exists := idx.ids[id]; exists {
|
if _, exists := idx.ids[id]; exists {
|
||||||
return fmt.Errorf("Id already exists: '%s'", id)
|
return fmt.Errorf("id already exists: '%s'", id)
|
||||||
}
|
}
|
||||||
idx.ids[id] = struct{}{}
|
idx.ids[id] = struct{}{}
|
||||||
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
|
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
|
||||||
return fmt.Errorf("Failed to insert id: %s", id)
|
return fmt.Errorf("failed to insert id: %s", id)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add adds a new ID to the TruncIndex
|
||||||
func (idx *TruncIndex) Add(id string) error {
|
func (idx *TruncIndex) Add(id string) error {
|
||||||
idx.Lock()
|
idx.Lock()
|
||||||
defer idx.Unlock()
|
defer idx.Unlock()
|
||||||
if err := idx.addId(id); err != nil {
|
if err := idx.addID(id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete removes an ID from the TruncIndex. If there are multiple IDs
|
||||||
|
// with the given prefix, an error is thrown.
|
||||||
func (idx *TruncIndex) Delete(id string) error {
|
func (idx *TruncIndex) Delete(id string) error {
|
||||||
idx.Lock()
|
idx.Lock()
|
||||||
defer idx.Unlock()
|
defer idx.Unlock()
|
||||||
if _, exists := idx.ids[id]; !exists || id == "" {
|
if _, exists := idx.ids[id]; !exists || id == "" {
|
||||||
return fmt.Errorf("No such id: '%s'", id)
|
return fmt.Errorf("no such id: '%s'", id)
|
||||||
}
|
}
|
||||||
delete(idx.ids, id)
|
delete(idx.ids, id)
|
||||||
if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
|
if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
|
||||||
return fmt.Errorf("No such id: '%s'", id)
|
return fmt.Errorf("no such id: '%s'", id)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get retrieves an ID from the TruncIndex. If there are multiple IDs
|
||||||
|
// with the given prefix, an error is thrown.
|
||||||
func (idx *TruncIndex) Get(s string) (string, error) {
|
func (idx *TruncIndex) Get(s string) (string, error) {
|
||||||
idx.RLock()
|
idx.RLock()
|
||||||
defer idx.RUnlock()
|
defer idx.RUnlock()
|
||||||
|
@ -90,17 +98,17 @@ func (idx *TruncIndex) Get(s string) (string, error) {
|
||||||
if id != "" {
|
if id != "" {
|
||||||
// we haven't found the ID if there are two or more IDs
|
// we haven't found the ID if there are two or more IDs
|
||||||
id = ""
|
id = ""
|
||||||
return fmt.Errorf("we've found two entries")
|
return errDuplicateID
|
||||||
}
|
}
|
||||||
id = string(prefix)
|
id = string(prefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
|
if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
|
||||||
return "", fmt.Errorf("No such id: %s", s)
|
return "", fmt.Errorf("no such id: %s", s)
|
||||||
}
|
}
|
||||||
if id != "" {
|
if id != "" {
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("No such id: %s", s)
|
return "", fmt.Errorf("no such id: %s", s)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
// See: http://en.wikipedia.org/wiki/Binary_prefix
|
// See: http://en.wikipedia.org/wiki/Binary_prefix
|
||||||
const (
|
const (
|
||||||
// Decimal
|
// Decimal
|
||||||
|
|
||||||
KB = 1000
|
KB = 1000
|
||||||
MB = 1000 * KB
|
MB = 1000 * KB
|
||||||
GB = 1000 * MB
|
GB = 1000 * MB
|
||||||
|
@ -17,6 +18,7 @@ const (
|
||||||
PB = 1000 * TB
|
PB = 1000 * TB
|
||||||
|
|
||||||
// Binary
|
// Binary
|
||||||
|
|
||||||
KiB = 1024
|
KiB = 1024
|
||||||
MiB = 1024 * KiB
|
MiB = 1024 * KiB
|
||||||
GiB = 1024 * MiB
|
GiB = 1024 * MiB
|
||||||
|
@ -60,7 +62,7 @@ func FromHumanSize(size string) (int64, error) {
|
||||||
return parseSize(size, decimalMap)
|
return parseSize(size, decimalMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses a human-readable string representing an amount of RAM
|
// RAMInBytes parses a human-readable string representing an amount of RAM
|
||||||
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
|
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
|
||||||
// returns the number of bytes, or -1 if the string is unparseable.
|
// returns the number of bytes, or -1 if the string is unparseable.
|
||||||
// Units are case-insensitive, and the 'b' suffix is optional.
|
// Units are case-insensitive, and the 'b' suffix is optional.
|
||||||
|
@ -72,7 +74,7 @@ func RAMInBytes(size string) (int64, error) {
|
||||||
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
||||||
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
||||||
if len(matches) != 3 {
|
if len(matches) != 3 {
|
||||||
return -1, fmt.Errorf("Invalid size: '%s'", sizeStr)
|
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
size, err := strconv.ParseInt(matches[1], 10, 0)
|
size, err := strconv.ParseInt(matches[1], 10, 0)
|
||||||
|
|
|
@ -5,53 +5,59 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Version provides utility methods for comparing versions.
|
||||||
type Version string
|
type Version string
|
||||||
|
|
||||||
func (me Version) compareTo(other Version) int {
|
func (v Version) compareTo(other Version) int {
|
||||||
var (
|
var (
|
||||||
meTab = strings.Split(string(me), ".")
|
currTab = strings.Split(string(v), ".")
|
||||||
otherTab = strings.Split(string(other), ".")
|
otherTab = strings.Split(string(other), ".")
|
||||||
)
|
)
|
||||||
|
|
||||||
max := len(meTab)
|
max := len(currTab)
|
||||||
if len(otherTab) > max {
|
if len(otherTab) > max {
|
||||||
max = len(otherTab)
|
max = len(otherTab)
|
||||||
}
|
}
|
||||||
for i := 0; i < max; i++ {
|
for i := 0; i < max; i++ {
|
||||||
var meInt, otherInt int
|
var currInt, otherInt int
|
||||||
|
|
||||||
if len(meTab) > i {
|
if len(currTab) > i {
|
||||||
meInt, _ = strconv.Atoi(meTab[i])
|
currInt, _ = strconv.Atoi(currTab[i])
|
||||||
}
|
}
|
||||||
if len(otherTab) > i {
|
if len(otherTab) > i {
|
||||||
otherInt, _ = strconv.Atoi(otherTab[i])
|
otherInt, _ = strconv.Atoi(otherTab[i])
|
||||||
}
|
}
|
||||||
if meInt > otherInt {
|
if currInt > otherInt {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
if otherInt > meInt {
|
if otherInt > currInt {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) LessThan(other Version) bool {
|
// LessThan checks if a version is less than another version
|
||||||
return me.compareTo(other) == -1
|
func (v Version) LessThan(other Version) bool {
|
||||||
|
return v.compareTo(other) == -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) LessThanOrEqualTo(other Version) bool {
|
// LessThanOrEqualTo checks if a version is less than or equal to another
|
||||||
return me.compareTo(other) <= 0
|
func (v Version) LessThanOrEqualTo(other Version) bool {
|
||||||
|
return v.compareTo(other) <= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) GreaterThan(other Version) bool {
|
// GreaterThan checks if a version is greater than another one
|
||||||
return me.compareTo(other) == 1
|
func (v Version) GreaterThan(other Version) bool {
|
||||||
|
return v.compareTo(other) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) GreaterThanOrEqualTo(other Version) bool {
|
// GreaterThanOrEqualTo checks ia version is greater than or equal to another
|
||||||
return me.compareTo(other) >= 0
|
func (v Version) GreaterThanOrEqualTo(other Version) bool {
|
||||||
|
return v.compareTo(other) >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) Equal(other Version) bool {
|
// Equal checks if a version is equal to another
|
||||||
return me.compareTo(other) == 0
|
func (v Version) Equal(other Version) bool {
|
||||||
|
return v.compareTo(other) == 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -227,12 +227,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 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)
|
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("Login: %s (Code: %d; Headers: %s)", body, resp.StatusCode, resp.Header)
|
||||||
return "", fmt.Errorf("Registration: %s", reqBody)
|
|
||||||
}
|
}
|
||||||
|
return "", fmt.Errorf("Registration: %s", reqBody)
|
||||||
|
|
||||||
} else if reqStatusCode == 401 {
|
} else if reqStatusCode == 401 {
|
||||||
// This case would happen with private registries where /v1/users is
|
// This case would happen with private registries where /v1/users is
|
||||||
// protected, so people can use `docker login` as an auth check.
|
// protected, so people can use `docker login` as an auth check.
|
||||||
|
|
|
@ -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.
|
// 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 (
|
var (
|
||||||
chunks []string
|
chunks []string
|
||||||
apiVersionStr string
|
apiVersionStr string
|
||||||
|
@ -43,7 +43,7 @@ func NewEndpoint(hostname string) (*Endpoint, error) {
|
||||||
if !strings.HasPrefix(hostname, "http") {
|
if !strings.HasPrefix(hostname, "http") {
|
||||||
hostname = "https://" + hostname
|
hostname = "https://" + hostname
|
||||||
}
|
}
|
||||||
trimmedHostname, endpoint.Version = scanForApiVersion(hostname)
|
trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
|
||||||
endpoint.URL, err = url.Parse(trimmedHostname)
|
endpoint.URL, err = url.Parse(trimmedHostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -19,7 +19,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testHttpServer *httptest.Server
|
testHTTPServer *httptest.Server
|
||||||
testLayers = map[string]map[string]string{
|
testLayers = map[string]map[string]string{
|
||||||
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20": {
|
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20": {
|
||||||
"json": `{"id":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
"json": `{"id":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
||||||
|
@ -99,7 +99,7 @@ func init() {
|
||||||
// /v2/
|
// /v2/
|
||||||
r.HandleFunc("/v2/version", handlerGetPing).Methods("GET")
|
r.HandleFunc("/v2/version", handlerGetPing).Methods("GET")
|
||||||
|
|
||||||
testHttpServer = httptest.NewServer(handlerAccessLog(r))
|
testHTTPServer = httptest.NewServer(handlerAccessLog(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerAccessLog(handler http.Handler) http.Handler {
|
func handlerAccessLog(handler http.Handler) http.Handler {
|
||||||
|
@ -111,7 +111,7 @@ func handlerAccessLog(handler http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeURL(req string) string {
|
func makeURL(req string) string {
|
||||||
return testHttpServer.URL + req
|
return testHTTPServer.URL + req
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeHeaders(w http.ResponseWriter) {
|
func writeHeaders(w http.ResponseWriter) {
|
||||||
|
@ -198,8 +198,8 @@ func handlerGetImage(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeHeaders(w)
|
writeHeaders(w)
|
||||||
layer_size := len(layer["layer"])
|
layerSize := len(layer["layer"])
|
||||||
w.Header().Add("X-Docker-Size", strconv.Itoa(layer_size))
|
w.Header().Add("X-Docker-Size", strconv.Itoa(layerSize))
|
||||||
io.WriteString(w, layer[vars["action"]])
|
io.WriteString(w, layer[vars["action"]])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,16 +208,16 @@ func handlerPutImage(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
image_id := vars["image_id"]
|
imageID := vars["image_id"]
|
||||||
action := vars["action"]
|
action := vars["action"]
|
||||||
layer, exists := testLayers[image_id]
|
layer, exists := testLayers[imageID]
|
||||||
if !exists {
|
if !exists {
|
||||||
if action != "json" {
|
if action != "json" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
layer = make(map[string]string)
|
layer = make(map[string]string)
|
||||||
testLayers[image_id] = layer
|
testLayers[imageID] = layer
|
||||||
}
|
}
|
||||||
if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
|
if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
|
||||||
if checksum != layer["checksum_simple"] && checksum != layer["checksum_tarsum"] {
|
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) {
|
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-Endpoints", fmt.Sprintf("%s , %s ", u.Host, "test.example.com"))
|
||||||
w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
|
w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
|
||||||
if r.Method == "PUT" {
|
if r.Method == "PUT" {
|
||||||
|
@ -317,9 +317,9 @@ func handlerImages(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
images := []map[string]string{}
|
images := []map[string]string{}
|
||||||
for image_id, layer := range testLayers {
|
for imageID, layer := range testLayers {
|
||||||
image := make(map[string]string)
|
image := make(map[string]string)
|
||||||
image["id"] = image_id
|
image["id"] = imageID
|
||||||
image["checksum"] = layer["checksum_tarsum"]
|
image["checksum"] = layer["checksum_tarsum"]
|
||||||
image["Tag"] = "latest"
|
image["Tag"] = "latest"
|
||||||
images = append(images, image)
|
images = append(images, image)
|
||||||
|
|
|
@ -11,9 +11,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
IMAGE_ID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
|
token = []string{"fake-token"}
|
||||||
TOKEN = []string{"fake-token"}
|
)
|
||||||
REPO = "foo42/bar"
|
|
||||||
|
const (
|
||||||
|
imageID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
|
||||||
|
REPO = "foo42/bar"
|
||||||
)
|
)
|
||||||
|
|
||||||
func spawnTestRegistrySession(t *testing.T) *Session {
|
func spawnTestRegistrySession(t *testing.T) *Session {
|
||||||
|
@ -43,27 +46,27 @@ func TestPingRegistryEndpoint(t *testing.T) {
|
||||||
|
|
||||||
func TestGetRemoteHistory(t *testing.T) {
|
func TestGetRemoteHistory(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(t)
|
r := spawnTestRegistrySession(t)
|
||||||
hist, err := r.GetRemoteHistory(IMAGE_ID, makeURL("/v1/"), TOKEN)
|
hist, err := r.GetRemoteHistory(imageID, makeURL("/v1/"), token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assertEqual(t, len(hist), 2, "Expected 2 images in history")
|
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",
|
assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
||||||
"Unexpected second ancestry")
|
"Unexpected second ancestry")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLookupRemoteImage(t *testing.T) {
|
func TestLookupRemoteImage(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(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")
|
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")
|
assertEqual(t, found, false, "Expected remote lookup to fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetRemoteImageJSON(t *testing.T) {
|
func TestGetRemoteImageJSON(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -72,7 +75,7 @@ func TestGetRemoteImageJSON(t *testing.T) {
|
||||||
t.Fatal("Expected non-empty json")
|
t.Fatal("Expected non-empty json")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), TOKEN)
|
_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), token)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected image not found error")
|
t.Fatal("Expected image not found error")
|
||||||
}
|
}
|
||||||
|
@ -80,7 +83,7 @@ func TestGetRemoteImageJSON(t *testing.T) {
|
||||||
|
|
||||||
func TestGetRemoteImageLayer(t *testing.T) {
|
func TestGetRemoteImageLayer(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -88,7 +91,7 @@ func TestGetRemoteImageLayer(t *testing.T) {
|
||||||
t.Fatal("Expected non-nil data result")
|
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 {
|
if err == nil {
|
||||||
t.Fatal("Expected image not found error")
|
t.Fatal("Expected image not found error")
|
||||||
}
|
}
|
||||||
|
@ -96,14 +99,14 @@ func TestGetRemoteImageLayer(t *testing.T) {
|
||||||
|
|
||||||
func TestGetRemoteTags(t *testing.T) {
|
func TestGetRemoteTags(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assertEqual(t, len(tags), 1, "Expected one tag")
|
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 {
|
if err == nil {
|
||||||
t.Fatal("Expected error when fetching tags for bogus repo")
|
t.Fatal("Expected error when fetching tags for bogus repo")
|
||||||
}
|
}
|
||||||
|
@ -111,11 +114,11 @@ func TestGetRemoteTags(t *testing.T) {
|
||||||
|
|
||||||
func TestGetRepositoryData(t *testing.T) {
|
func TestGetRepositoryData(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(t)
|
r := spawnTestRegistrySession(t)
|
||||||
parsedUrl, err := url.Parse(makeURL("/v1/"))
|
parsedURL, err := url.Parse(makeURL("/v1/"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
host := "http://" + parsedUrl.Host + "/v1/"
|
host := "http://" + parsedURL.Host + "/v1/"
|
||||||
data, err := r.GetRepositoryData("foo42/bar")
|
data, err := r.GetRepositoryData("foo42/bar")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -137,7 +140,7 @@ func TestPushImageJSONRegistry(t *testing.T) {
|
||||||
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -146,7 +149,7 @@ func TestPushImageJSONRegistry(t *testing.T) {
|
||||||
func TestPushImageLayerRegistry(t *testing.T) {
|
func TestPushImageLayerRegistry(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(t)
|
r := spawnTestRegistrySession(t)
|
||||||
layer := strings.NewReader("")
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -180,7 +183,7 @@ func TestResolveRepositoryName(t *testing.T) {
|
||||||
|
|
||||||
func TestPushRegistryTag(t *testing.T) {
|
func TestPushRegistryTag(t *testing.T) {
|
||||||
r := spawnTestRegistrySession(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package registry
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
// this is required for some certificates
|
||||||
_ "crypto/sha512"
|
_ "crypto/sha512"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -243,11 +244,11 @@ func (r *Session) GetRemoteTags(registries []string, repository string, token []
|
||||||
|
|
||||||
func buildEndpointsList(headers []string, indexEp string) ([]string, error) {
|
func buildEndpointsList(headers []string, indexEp string) ([]string, error) {
|
||||||
var endpoints []string
|
var endpoints []string
|
||||||
parsedUrl, err := url.Parse(indexEp)
|
parsedURL, err := url.Parse(indexEp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var urlScheme = parsedUrl.Scheme
|
var urlScheme = parsedURL.Scheme
|
||||||
// The Registry's URL scheme has to match the Index'
|
// The Registry's URL scheme has to match the Index'
|
||||||
for _, ep := range headers {
|
for _, ep := range headers {
|
||||||
epList := strings.Split(ep, ",")
|
epList := strings.Split(ep, ",")
|
||||||
|
|
Loading…
Reference in a new issue