Merge pull request #1628 from dotcloud/bump_0.6.0

Bump to 0.6.0
This commit is contained in:
Michael Crosby 2013-08-23 10:45:14 -07:00
commit 037e1bb1a3
4 changed files with 646 additions and 274 deletions

View file

@ -1,3 +1,3 @@
Sam Alba <sam@dotcloud.com> Sam Alba <sam@dotcloud.com> (@samalba)
Joffrey Fuhrer <joffrey@dotcloud.com> Joffrey Fuhrer <joffrey@dotcloud.com> (@shin-)
Ken Cochrane <ken@dotcloud.com> Ken Cochrane <ken@dotcloud.com> (@kencochrane)

View file

@ -9,16 +9,20 @@ import (
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"io" "io"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url" "net/url"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
var ErrAlreadyExists = errors.New("Image already exists") var (
var ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")") ErrAlreadyExists = errors.New("Image already exists")
ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
)
func pingRegistryEndpoint(endpoint string) error { func pingRegistryEndpoint(endpoint string) error {
if endpoint == auth.IndexServerAddress() { if endpoint == auth.IndexServerAddress() {
@ -26,7 +30,19 @@ func pingRegistryEndpoint(endpoint string) error {
// (and we never want to fallback to http in case of error) // (and we never want to fallback to http in case of error)
return nil return nil
} }
resp, err := http.Get(endpoint + "_ping") httpDial := func(proto string, addr string) (net.Conn, error) {
// Set the connect timeout to 5 seconds
conn, err := net.DialTimeout(proto, addr, time.Duration(5)*time.Second)
if err != nil {
return nil, err
}
// Set the recv timeout to 10 seconds
conn.SetDeadline(time.Now().Add(time.Duration(10) * time.Second))
return conn, nil
}
httpTransport := &http.Transport{Dial: httpDial}
client := &http.Client{Transport: httpTransport}
resp, err := client.Get(endpoint + "_ping")
if err != nil { if err != nil {
return err return err
} }
@ -67,7 +83,8 @@ func ResolveRepositoryName(reposName string) (string, string, error) {
return "", "", ErrInvalidRepositoryName return "", "", ErrInvalidRepositoryName
} }
nameParts := strings.SplitN(reposName, "/", 2) nameParts := strings.SplitN(reposName, "/", 2)
if !strings.Contains(nameParts[0], ".") && !strings.Contains(nameParts[0], ":") { if !strings.Contains(nameParts[0], ".") && !strings.Contains(nameParts[0], ":") &&
nameParts[0] != "localhost" {
// This is a Docker Index repos (ex: samalba/hipache or ubuntu) // This is a Docker Index repos (ex: samalba/hipache or ubuntu)
err := validateRepositoryName(reposName) err := validateRepositoryName(reposName)
return auth.IndexServerAddress(), reposName, err return auth.IndexServerAddress(), reposName, err
@ -98,13 +115,6 @@ func ResolveRepositoryName(reposName string) (string, string, error) {
return endpoint, reposName, err return endpoint, reposName, err
} }
// VersionInfo is used to model entities which has a version.
// It is basically a tupple with name and version.
type VersionInfo interface {
Name() string
Version() string
}
func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) { func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) {
for _, cookie := range c.Jar.Cookies(req.URL) { for _, cookie := range c.Jar.Cookies(req.URL) {
req.AddCookie(cookie) req.AddCookie(cookie)
@ -119,33 +129,18 @@ func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) {
return res, err return res, err
} }
// Set the user agent field in the header based on the versions provided
// in NewRegistry() and extra.
func (r *Registry) setUserAgent(req *http.Request, extra ...VersionInfo) {
if len(r.baseVersions)+len(extra) == 0 {
return
}
if len(extra) == 0 {
req.Header.Set("User-Agent", r.baseVersionsStr)
} else {
req.Header.Set("User-Agent", appendVersions(r.baseVersionsStr, extra...))
}
return
}
// Retrieve the history of a given image from the Registry. // Retrieve the history of a given image from the Registry.
// Return a list of the parent's json (requested image included) // Return a list of the parent's json (requested image included)
func (r *Registry) GetRemoteHistory(imgID, registry string, token []string) ([]string, error) { func (r *Registry) GetRemoteHistory(imgID, registry string, token []string) ([]string, error) {
req, err := http.NewRequest("GET", registry+"images/"+imgID+"/ancestry", nil) req, err := r.reqFactory.NewRequest("GET", registry+"images/"+imgID+"/ancestry", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil || res.StatusCode != 200 { if err != nil || res.StatusCode != 200 {
if res != nil { if res != nil {
return nil, fmt.Errorf("Internal server error: %d trying to fetch remote history for %s", res.StatusCode, imgID) return nil, utils.NewHTTPRequestError(fmt.Sprintf("Internal server error: %d trying to fetch remote history for %s", res.StatusCode, imgID), res)
} }
return nil, err return nil, err
} }
@ -166,13 +161,13 @@ func (r *Registry) GetRemoteHistory(imgID, registry string, token []string) ([]s
// Check if an image exists in the Registry // Check if an image exists in the Registry
func (r *Registry) LookupRemoteImage(imgID, registry string, token []string) bool { func (r *Registry) LookupRemoteImage(imgID, registry string, token []string) bool {
rt := &http.Transport{Proxy: http.ProxyFromEnvironment}
req, err := http.NewRequest("GET", registry+"images/"+imgID+"/json", nil) req, err := r.reqFactory.NewRequest("GET", registry+"images/"+imgID+"/json", nil)
if err != nil { if err != nil {
return false return false
} }
res, err := rt.RoundTrip(req) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return false return false
} }
@ -183,19 +178,18 @@ func (r *Registry) LookupRemoteImage(imgID, registry string, token []string) boo
// Retrieve an image from the Registry. // Retrieve an image from the Registry.
func (r *Registry) GetRemoteImageJSON(imgID, registry string, token []string) ([]byte, int, error) { func (r *Registry) GetRemoteImageJSON(imgID, registry string, token []string) ([]byte, int, error) {
// Get the JSON // Get the JSON
req, err := http.NewRequest("GET", registry+"images/"+imgID+"/json", nil) req, err := r.reqFactory.NewRequest("GET", registry+"images/"+imgID+"/json", nil)
if err != nil { if err != nil {
return nil, -1, fmt.Errorf("Failed to download json: %s", err) return nil, -1, fmt.Errorf("Failed to download json: %s", err)
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return nil, -1, fmt.Errorf("Failed to download json: %s", err) return nil, -1, fmt.Errorf("Failed to download json: %s", err)
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
return nil, -1, fmt.Errorf("HTTP code %d", res.StatusCode) return nil, -1, utils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d", res.StatusCode), res)
} }
imageSize, err := strconv.Atoi(res.Header.Get("X-Docker-Size")) imageSize, err := strconv.Atoi(res.Header.Get("X-Docker-Size"))
@ -211,16 +205,19 @@ func (r *Registry) GetRemoteImageJSON(imgID, registry string, token []string) ([
} }
func (r *Registry) GetRemoteImageLayer(imgID, registry string, token []string) (io.ReadCloser, error) { func (r *Registry) GetRemoteImageLayer(imgID, registry string, token []string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", registry+"images/"+imgID+"/layer", nil) req, err := r.reqFactory.NewRequest("GET", registry+"images/"+imgID+"/layer", nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error while getting from the server: %s\n", err) return nil, fmt.Errorf("Error while getting from the server: %s\n", err)
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if res.StatusCode != 200 {
return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
res.StatusCode, imgID)
}
return res.Body, nil return res.Body, nil
} }
@ -232,12 +229,12 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
} }
for _, host := range registries { for _, host := range registries {
endpoint := fmt.Sprintf("%srepositories/%s/tags", host, repository) endpoint := fmt.Sprintf("%srepositories/%s/tags", host, repository)
req, err := r.opaqueRequest("GET", endpoint, nil) req, err := r.reqFactory.NewRequest("GET", endpoint, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return nil, err return nil, err
@ -268,7 +265,9 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
func (r *Registry) GetRepositoryData(indexEp, remote string) (*RepositoryData, error) { func (r *Registry) GetRepositoryData(indexEp, remote string) (*RepositoryData, error) {
repositoryTarget := fmt.Sprintf("%srepositories/%s/images", indexEp, remote) repositoryTarget := fmt.Sprintf("%srepositories/%s/images", indexEp, remote)
req, err := r.opaqueRequest("GET", repositoryTarget, nil) utils.Debugf("[registry] Calling GET %s", repositoryTarget)
req, err := r.reqFactory.NewRequest("GET", repositoryTarget, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -276,7 +275,6 @@ func (r *Registry) GetRepositoryData(indexEp, remote string) (*RepositoryData, e
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
} }
req.Header.Set("X-Docker-Token", "true") req.Header.Set("X-Docker-Token", "true")
r.setUserAgent(req)
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
@ -284,12 +282,12 @@ func (r *Registry) GetRepositoryData(indexEp, remote string) (*RepositoryData, e
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode == 401 { if res.StatusCode == 401 {
return nil, fmt.Errorf("Please login first (HTTP code %d)", res.StatusCode) return nil, utils.NewHTTPRequestError(fmt.Sprintf("Please login first (HTTP code %d)", res.StatusCode), res)
} }
// TODO: Right now we're ignoring checksums in the response body. // TODO: Right now we're ignoring checksums in the response body.
// In the future, we need to use them to check image validity. // In the future, we need to use them to check image validity.
if res.StatusCode != 200 { if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP code: %d", res.StatusCode) return nil, utils.NewHTTPRequestError(fmt.Sprintf("HTTP code: %d", res.StatusCode), res)
} }
var tokens []string var tokens []string
@ -330,24 +328,25 @@ func (r *Registry) GetRepositoryData(indexEp, remote string) (*RepositoryData, e
}, nil }, nil
} }
// Push a local image to the registry func (r *Registry) PushImageChecksumRegistry(imgData *ImgData, registry string, token []string) error {
func (r *Registry) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, registry string, token []string) error {
// FIXME: try json with UTF8 utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgData.ID+"/checksum")
req, err := http.NewRequest("PUT", registry+"images/"+imgData.ID+"/json", bytes.NewReader(jsonRaw))
req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgData.ID+"/checksum", nil)
if err != nil { if err != nil {
return err return err
} }
req.Header.Add("Content-type", "application/json")
req.Header.Set("Authorization", "Token "+strings.Join(token, ",")) req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
req.Header.Set("X-Docker-Checksum", imgData.Checksum) req.Header.Set("X-Docker-Checksum", imgData.Checksum)
r.setUserAgent(req)
utils.Debugf("Setting checksum for %s: %s", imgData.ID, imgData.Checksum)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return fmt.Errorf("Failed to upload metadata: %s", err) return fmt.Errorf("Failed to upload metadata: %s", err)
} }
defer res.Body.Close() defer res.Body.Close()
if len(res.Cookies()) > 0 {
r.client.Jar.SetCookies(req.URL, res.Cookies())
}
if res.StatusCode != 200 { if res.StatusCode != 200 {
errBody, err := ioutil.ReadAll(res.Body) errBody, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
@ -364,38 +363,66 @@ func (r *Registry) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, regis
return nil return nil
} }
func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, token []string) error { // Push a local image to the registry
req, err := http.NewRequest("PUT", registry+"images/"+imgID+"/layer", layer) func (r *Registry) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, registry string, token []string) error {
utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgData.ID+"/json")
req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgData.ID+"/json", bytes.NewReader(jsonRaw))
if err != nil { if err != nil {
return err return err
} }
req.Header.Add("Content-type", "application/json")
req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
res, err := doWithCookies(r.client, req)
if err != nil {
return fmt.Errorf("Failed to upload metadata: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
errBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return utils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
}
var jsonBody map[string]string
if err := json.Unmarshal(errBody, &jsonBody); err != nil {
errBody = []byte(err.Error())
} else if jsonBody["error"] == "Image already exists" {
return ErrAlreadyExists
}
return utils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata: %s", res.StatusCode, errBody), res)
}
return nil
}
func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, token []string, jsonRaw []byte) (checksum string, err error) {
utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgID+"/layer")
tarsumLayer := &utils.TarSum{Reader: layer}
req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgID+"/layer", tarsumLayer)
if err != nil {
return "", err
}
req.ContentLength = -1 req.ContentLength = -1
req.TransferEncoding = []string{"chunked"} req.TransferEncoding = []string{"chunked"}
req.Header.Set("Authorization", "Token "+strings.Join(token, ",")) req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
r.setUserAgent(req)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return fmt.Errorf("Failed to upload layer: %s", err) return "", fmt.Errorf("Failed to upload layer: %s", err)
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
errBody, err := ioutil.ReadAll(res.Body) errBody, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return fmt.Errorf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err) return "", utils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
} }
return fmt.Errorf("Received HTTP code %d while uploading layer: %s", res.StatusCode, errBody) return "", utils.NewHTTPRequestError(fmt.Sprintf("Received HTTP code %d while uploading layer: %s", res.StatusCode, errBody), res)
} }
return nil return tarsumLayer.Sum(jsonRaw), nil
}
func (r *Registry) opaqueRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
return nil, err
}
req.URL.Opaque = strings.Replace(urlStr, req.URL.Scheme+":", "", 1)
return req, err
} }
// push a tag on the registry. // push a tag on the registry.
@ -403,14 +430,14 @@ func (r *Registry) opaqueRequest(method, urlStr string, body io.Reader) (*http.R
func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token []string) error { func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token []string) error {
// "jsonify" the string // "jsonify" the string
revision = "\"" + revision + "\"" revision = "\"" + revision + "\""
path := fmt.Sprintf("repositories/%s/tags/%s", remote, tag)
req, err := r.opaqueRequest("PUT", registry+"repositories/"+remote+"/tags/"+tag, strings.NewReader(revision)) req, err := r.reqFactory.NewRequest("PUT", registry+path, strings.NewReader(revision))
if err != nil { if err != nil {
return err return err
} }
req.Header.Add("Content-type", "application/json") req.Header.Add("Content-type", "application/json")
req.Header.Set("Authorization", "Token "+strings.Join(token, ",")) req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
r.setUserAgent(req)
req.ContentLength = int64(len(revision)) req.ContentLength = int64(len(revision))
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
@ -418,13 +445,25 @@ func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token
} }
res.Body.Close() res.Body.Close()
if res.StatusCode != 200 && res.StatusCode != 201 { if res.StatusCode != 200 && res.StatusCode != 201 {
return fmt.Errorf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, remote) return utils.NewHTTPRequestError(fmt.Sprintf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, remote), res)
} }
return nil return nil
} }
func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData, validate bool, regs []string) (*RepositoryData, error) { func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData, validate bool, regs []string) (*RepositoryData, error) {
imgListJSON, err := json.Marshal(imgList) cleanImgList := []*ImgData{}
if validate {
for _, elem := range imgList {
if elem.Checksum != "" {
cleanImgList = append(cleanImgList, elem)
}
}
} else {
cleanImgList = imgList
}
imgListJSON, err := json.Marshal(cleanImgList)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -432,18 +471,16 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
if validate { if validate {
suffix = "images" suffix = "images"
} }
u := fmt.Sprintf("%srepositories/%s/%s", indexEp, remote, suffix) u := fmt.Sprintf("%srepositories/%s/%s", indexEp, remote, suffix)
utils.Debugf("PUT %s", u) utils.Debugf("[registry] PUT %s", u)
utils.Debugf("Image list pushed to index:\n%s\n", imgListJSON) utils.Debugf("Image list pushed to index:\n%s\n", imgListJSON)
req, err := r.opaqueRequest("PUT", u, bytes.NewReader(imgListJSON)) req, err := r.reqFactory.NewRequest("PUT", u, bytes.NewReader(imgListJSON))
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
req.ContentLength = int64(len(imgListJSON)) req.ContentLength = int64(len(imgListJSON))
req.Header.Set("X-Docker-Token", "true") req.Header.Set("X-Docker-Token", "true")
r.setUserAgent(req)
if validate { if validate {
req.Header["X-Docker-Endpoints"] = regs req.Header["X-Docker-Endpoints"] = regs
} }
@ -457,14 +494,13 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
// Redirect if necessary // Redirect if necessary
for res.StatusCode >= 300 && res.StatusCode < 400 { for res.StatusCode >= 300 && res.StatusCode < 400 {
utils.Debugf("Redirected to %s\n", res.Header.Get("Location")) utils.Debugf("Redirected to %s\n", res.Header.Get("Location"))
req, err = r.opaqueRequest("PUT", res.Header.Get("Location"), bytes.NewReader(imgListJSON)) req, err = r.reqFactory.NewRequest("PUT", res.Header.Get("Location"), bytes.NewReader(imgListJSON))
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
req.ContentLength = int64(len(imgListJSON)) req.ContentLength = int64(len(imgListJSON))
req.Header.Set("X-Docker-Token", "true") req.Header.Set("X-Docker-Token", "true")
r.setUserAgent(req)
if validate { if validate {
req.Header["X-Docker-Endpoints"] = regs req.Header["X-Docker-Endpoints"] = regs
} }
@ -483,7 +519,7 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, fmt.Errorf("Error: Status %d trying to push repository %s: %s", res.StatusCode, remote, errBody) return nil, utils.NewHTTPRequestError(fmt.Sprintf("Error: Status %d trying to push repository %s: %s", res.StatusCode, remote, errBody), res)
} }
if res.Header.Get("X-Docker-Token") != "" { if res.Header.Get("X-Docker-Token") != "" {
tokens = res.Header["X-Docker-Token"] tokens = res.Header["X-Docker-Token"]
@ -507,7 +543,7 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, fmt.Errorf("Error: Status %d trying to push checksums %s: %s", res.StatusCode, remote, errBody) return nil, utils.NewHTTPRequestError(fmt.Sprintf("Error: Status %d trying to push checksums %s: %s", res.StatusCode, remote, errBody), res)
} }
} }
@ -519,7 +555,7 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
func (r *Registry) SearchRepositories(term string) (*SearchResults, error) { func (r *Registry) SearchRepositories(term string) (*SearchResults, error) {
u := auth.IndexServerAddress() + "search?q=" + url.QueryEscape(term) u := auth.IndexServerAddress() + "search?q=" + url.QueryEscape(term)
req, err := http.NewRequest("GET", u, nil) req, err := r.reqFactory.NewRequest("GET", u, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -529,7 +565,7 @@ func (r *Registry) SearchRepositories(term string) (*SearchResults, error) {
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
return nil, fmt.Errorf("Unexepected status code %d", res.StatusCode) return nil, utils.NewHTTPRequestError(fmt.Sprintf("Unexepected status code %d", res.StatusCode), res)
} }
rawData, err := ioutil.ReadAll(res.Body) rawData, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
@ -573,50 +609,10 @@ type ImgData struct {
type Registry struct { type Registry struct {
client *http.Client client *http.Client
authConfig *auth.AuthConfig authConfig *auth.AuthConfig
baseVersions []VersionInfo reqFactory *utils.HTTPRequestFactory
baseVersionsStr string
} }
func validVersion(version VersionInfo) bool { func NewRegistry(root string, authConfig *auth.AuthConfig, factory *utils.HTTPRequestFactory) (r *Registry, err error) {
stopChars := " \t\r\n/"
if strings.ContainsAny(version.Name(), stopChars) {
return false
}
if strings.ContainsAny(version.Version(), stopChars) {
return false
}
return true
}
// Convert versions to a string and append the string to the string base.
//
// Each VersionInfo will be converted to a string in the format of
// "product/version", where the "product" is get from the Name() method, while
// version is get from the Version() method. Several pieces of verson information
// will be concatinated and separated by space.
func appendVersions(base string, versions ...VersionInfo) string {
if len(versions) == 0 {
return base
}
var buf bytes.Buffer
if len(base) > 0 {
buf.Write([]byte(base))
}
for _, v := range versions {
if !validVersion(v) {
continue
}
buf.Write([]byte(v.Name()))
buf.Write([]byte("/"))
buf.Write([]byte(v.Version()))
buf.Write([]byte(" "))
}
return buf.String()
}
func NewRegistry(root string, authConfig *auth.AuthConfig, baseVersions ...VersionInfo) (r *Registry, err error) {
httpTransport := &http.Transport{ httpTransport := &http.Transport{
DisableKeepAlives: true, DisableKeepAlives: true,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
@ -632,7 +628,7 @@ func NewRegistry(root string, authConfig *auth.AuthConfig, baseVersions ...Versi
if err != nil { if err != nil {
return nil, err return nil, err
} }
r.baseVersions = baseVersions
r.baseVersionsStr = appendVersions("", baseVersions...) r.reqFactory = factory
return r, nil return r, nil
} }

346
docs/registry_mock_test.go Normal file
View file

@ -0,0 +1,346 @@
package registry
import (
"encoding/json"
"fmt"
"github.com/dotcloud/docker/utils"
"github.com/gorilla/mux"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"time"
)
var (
testHttpServer *httptest.Server
testLayers = map[string]map[string]string{
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20": {
"json": `{"id":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
"comment":"test base image","created":"2013-03-23T12:53:11.10432-07:00",
"container_config":{"Hostname":"","User":"","Memory":0,"MemorySwap":0,
"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,
"PortSpecs":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,
"Env":null,"Cmd":null,"Dns":null,"Image":"","Volumes":null,
"VolumesFrom":"","Entrypoint":null},"Size":424242}`,
"checksum_simple": "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
"checksum_tarsum": "tarsum+sha256:4409a0685741ca86d38df878ed6f8cbba4c99de5dc73cd71aef04be3bb70be7c",
"ancestry": `["77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20"]`,
"layer": string([]byte{
0x1f, 0x8b, 0x08, 0x08, 0x0e, 0xb0, 0xee, 0x51, 0x02, 0x03, 0x6c, 0x61, 0x79, 0x65,
0x72, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xd2, 0x31, 0x0e, 0xc2, 0x30, 0x0c, 0x05,
0x50, 0xcf, 0x9c, 0xc2, 0x27, 0x48, 0xed, 0x38, 0x4e, 0xce, 0x13, 0x44, 0x2b, 0x66,
0x62, 0x24, 0x8e, 0x4f, 0xa0, 0x15, 0x63, 0xb6, 0x20, 0x21, 0xfc, 0x96, 0xbf, 0x78,
0xb0, 0xf5, 0x1d, 0x16, 0x98, 0x8e, 0x88, 0x8a, 0x2a, 0xbe, 0x33, 0xef, 0x49, 0x31,
0xed, 0x79, 0x40, 0x8e, 0x5c, 0x44, 0x85, 0x88, 0x33, 0x12, 0x73, 0x2c, 0x02, 0xa8,
0xf0, 0x05, 0xf7, 0x66, 0xf5, 0xd6, 0x57, 0x69, 0xd7, 0x7a, 0x19, 0xcd, 0xf5, 0xb1,
0x6d, 0x1b, 0x1f, 0xf9, 0xba, 0xe3, 0x93, 0x3f, 0x22, 0x2c, 0xb6, 0x36, 0x0b, 0xf6,
0xb0, 0xa9, 0xfd, 0xe7, 0x94, 0x46, 0xfd, 0xeb, 0xd1, 0x7f, 0x2c, 0xc4, 0xd2, 0xfb,
0x97, 0xfe, 0x02, 0x80, 0xe4, 0xfd, 0x4f, 0x77, 0xae, 0x6d, 0x3d, 0x81, 0x73, 0xce,
0xb9, 0x7f, 0xf3, 0x04, 0x41, 0xc1, 0xab, 0xc6, 0x00, 0x0a, 0x00, 0x00,
}),
},
"42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d": {
"json": `{"id":"42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
"parent":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
"comment":"test base image","created":"2013-03-23T12:55:11.10432-07:00",
"container_config":{"Hostname":"","User":"","Memory":0,"MemorySwap":0,
"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,
"PortSpecs":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,
"Env":null,"Cmd":null,"Dns":null,"Image":"","Volumes":null,
"VolumesFrom":"","Entrypoint":null},"Size":424242}`,
"checksum_simple": "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
"checksum_tarsum": "tarsum+sha256:68fdb56fb364f074eec2c9b3f85ca175329c4dcabc4a6a452b7272aa613a07a2",
"ancestry": `["42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20"]`,
"layer": string([]byte{
0x1f, 0x8b, 0x08, 0x08, 0xbd, 0xb3, 0xee, 0x51, 0x02, 0x03, 0x6c, 0x61, 0x79, 0x65,
0x72, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xd1, 0x31, 0x0e, 0xc2, 0x30, 0x0c, 0x05,
0x50, 0xcf, 0x9c, 0xc2, 0x27, 0x48, 0x9d, 0x38, 0x8e, 0xcf, 0x53, 0x51, 0xaa, 0x56,
0xea, 0x44, 0x82, 0xc4, 0xf1, 0x09, 0xb4, 0xea, 0x98, 0x2d, 0x48, 0x08, 0xbf, 0xe5,
0x2f, 0x1e, 0xfc, 0xf5, 0xdd, 0x00, 0xdd, 0x11, 0x91, 0x8a, 0xe0, 0x27, 0xd3, 0x9e,
0x14, 0xe2, 0x9e, 0x07, 0xf4, 0xc1, 0x2b, 0x0b, 0xfb, 0xa4, 0x82, 0xe4, 0x3d, 0x93,
0x02, 0x0a, 0x7c, 0xc1, 0x23, 0x97, 0xf1, 0x5e, 0x5f, 0xc9, 0xcb, 0x38, 0xb5, 0xee,
0xea, 0xd9, 0x3c, 0xb7, 0x4b, 0xbe, 0x7b, 0x9c, 0xf9, 0x23, 0xdc, 0x50, 0x6e, 0xb9,
0xb8, 0xf2, 0x2c, 0x5d, 0xf7, 0x4f, 0x31, 0xb6, 0xf6, 0x4f, 0xc7, 0xfe, 0x41, 0x55,
0x63, 0xdd, 0x9f, 0x89, 0x09, 0x90, 0x6c, 0xff, 0xee, 0xae, 0xcb, 0xba, 0x4d, 0x17,
0x30, 0xc6, 0x18, 0xf3, 0x67, 0x5e, 0xc1, 0xed, 0x21, 0x5d, 0x00, 0x0a, 0x00, 0x00,
}),
},
}
testRepositories = map[string]map[string]string{
"foo42/bar": {
"latest": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
},
}
)
func init() {
r := mux.NewRouter()
r.HandleFunc("/v1/_ping", handlerGetPing).Methods("GET")
r.HandleFunc("/v1/images/{image_id:[^/]+}/{action:json|layer|ancestry}", handlerGetImage).Methods("GET")
r.HandleFunc("/v1/images/{image_id:[^/]+}/{action:json|layer|checksum}", handlerPutImage).Methods("PUT")
r.HandleFunc("/v1/repositories/{repository:.+}/tags", handlerGetDeleteTags).Methods("GET", "DELETE")
r.HandleFunc("/v1/repositories/{repository:.+}/tags/{tag:.+}", handlerGetTag).Methods("GET")
r.HandleFunc("/v1/repositories/{repository:.+}/tags/{tag:.+}", handlerPutTag).Methods("PUT")
r.HandleFunc("/v1/users{null:.*}", handlerUsers).Methods("GET", "POST", "PUT")
r.HandleFunc("/v1/repositories/{repository:.+}{action:/images|/}", handlerImages).Methods("GET", "PUT", "DELETE")
r.HandleFunc("/v1/repositories/{repository:.+}/auth", handlerAuth).Methods("PUT")
r.HandleFunc("/v1/search", handlerSearch).Methods("GET")
testHttpServer = httptest.NewServer(handlerAccessLog(r))
}
func handlerAccessLog(handler http.Handler) http.Handler {
logHandler := func(w http.ResponseWriter, r *http.Request) {
utils.Debugf("%s \"%s %s\"", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
}
return http.HandlerFunc(logHandler)
}
func makeURL(req string) string {
return testHttpServer.URL + req
}
func writeHeaders(w http.ResponseWriter) {
h := w.Header()
h.Add("Server", "docker-tests/mock")
h.Add("Expires", "-1")
h.Add("Content-Type", "application/json")
h.Add("Pragma", "no-cache")
h.Add("Cache-Control", "no-cache")
h.Add("X-Docker-Registry-Version", "0.0.0")
h.Add("X-Docker-Registry-Config", "mock")
}
func writeResponse(w http.ResponseWriter, message interface{}, code int) {
writeHeaders(w)
w.WriteHeader(code)
body, err := json.Marshal(message)
if err != nil {
io.WriteString(w, err.Error())
return
}
w.Write(body)
}
func readJSON(r *http.Request, dest interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(body, dest)
}
func apiError(w http.ResponseWriter, message string, code int) {
body := map[string]string{
"error": message,
}
writeResponse(w, body, code)
}
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
if a == b {
return
}
if len(message) == 0 {
message = fmt.Sprintf("%v != %v", a, b)
}
t.Fatal(message)
}
func requiresAuth(w http.ResponseWriter, r *http.Request) bool {
writeCookie := func() {
value := fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano())
cookie := &http.Cookie{Name: "session", Value: value, MaxAge: 3600}
http.SetCookie(w, cookie)
//FIXME(sam): this should be sent only on Index routes
value = fmt.Sprintf("FAKE-TOKEN-%d", time.Now().UnixNano())
w.Header().Add("X-Docker-Token", value)
}
if len(r.Cookies()) > 0 {
writeCookie()
return true
}
if len(r.Header.Get("Authorization")) > 0 {
writeCookie()
return true
}
w.Header().Add("WWW-Authenticate", "token")
apiError(w, "Wrong auth", 401)
return false
}
func handlerGetPing(w http.ResponseWriter, r *http.Request) {
writeResponse(w, true, 200)
}
func handlerGetImage(w http.ResponseWriter, r *http.Request) {
if !requiresAuth(w, r) {
return
}
vars := mux.Vars(r)
layer, exists := testLayers[vars["image_id"]]
if !exists {
http.NotFound(w, r)
return
}
writeHeaders(w)
layer_size := len(layer["layer"])
w.Header().Add("X-Docker-Size", strconv.Itoa(layer_size))
io.WriteString(w, layer[vars["action"]])
}
func handlerPutImage(w http.ResponseWriter, r *http.Request) {
if !requiresAuth(w, r) {
return
}
vars := mux.Vars(r)
image_id := vars["image_id"]
action := vars["action"]
layer, exists := testLayers[image_id]
if !exists {
if action != "json" {
http.NotFound(w, r)
return
}
layer = make(map[string]string)
testLayers[image_id] = layer
}
if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
if checksum != layer["checksum_simple"] && checksum != layer["checksum_tarsum"] {
apiError(w, "Wrong checksum", 400)
return
}
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
apiError(w, fmt.Sprintf("Error: %s", err), 500)
return
}
layer[action] = string(body)
writeResponse(w, true, 200)
}
func handlerGetDeleteTags(w http.ResponseWriter, r *http.Request) {
if !requiresAuth(w, r) {
return
}
repositoryName := mux.Vars(r)["repository"]
tags, exists := testRepositories[repositoryName]
if !exists {
apiError(w, "Repository not found", 404)
}
if r.Method == "DELETE" {
delete(testRepositories, repositoryName)
writeResponse(w, true, 200)
return
}
writeResponse(w, tags, 200)
}
func handlerGetTag(w http.ResponseWriter, r *http.Request) {
if !requiresAuth(w, r) {
return
}
vars := mux.Vars(r)
repositoryName := vars["repository"]
tagName := vars["tag"]
tags, exists := testRepositories[repositoryName]
if !exists {
apiError(w, "Repository not found", 404)
}
tag, exists := tags[tagName]
if !exists {
apiError(w, "Tag not found", 404)
}
writeResponse(w, tag, 200)
}
func handlerPutTag(w http.ResponseWriter, r *http.Request) {
if !requiresAuth(w, r) {
return
}
vars := mux.Vars(r)
repositoryName := vars["repository"]
tagName := vars["tag"]
tags, exists := testRepositories[repositoryName]
if !exists {
tags := make(map[string]string)
testRepositories[repositoryName] = tags
}
tagValue := ""
readJSON(r, tagValue)
tags[tagName] = tagValue
writeResponse(w, true, 200)
}
func handlerUsers(w http.ResponseWriter, r *http.Request) {
code := 200
if r.Method == "POST" {
code = 201
} else if r.Method == "PUT" {
code = 204
}
writeResponse(w, "", code)
}
func handlerImages(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(testHttpServer.URL)
w.Header().Add("X-Docker-Endpoints", u.Host)
w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
if r.Method == "PUT" {
if strings.HasSuffix(r.URL.Path, "images") {
writeResponse(w, "", 204)
return
}
writeResponse(w, "", 200)
return
}
if r.Method == "DELETE" {
writeResponse(w, "", 204)
return
}
images := []map[string]string{}
for image_id, layer := range testLayers {
image := make(map[string]string)
image["id"] = image_id
image["checksum"] = layer["checksum_tarsum"]
image["Tag"] = "latest"
images = append(images, image)
}
writeResponse(w, images, 200)
}
func handlerAuth(w http.ResponseWriter, r *http.Request) {
writeResponse(w, "OK", 200)
}
func handlerSearch(w http.ResponseWriter, r *http.Request) {
writeResponse(w, "{}", 200)
}
func TestPing(t *testing.T) {
res, err := http.Get(makeURL("/v1/_ping"))
if err != nil {
t.Fatal(err)
}
assertEqual(t, res.StatusCode, 200, "")
assertEqual(t, res.Header.Get("X-Docker-Registry-Config"), "mock",
"This is not a Mocked Registry")
}
/* Uncomment this to test Mocked Registry locally with curl
* WARNING: Don't push on the repos uncommented, it'll block the tests
*
func TestWait(t *testing.T) {
log.Println("Test HTTP server ready and waiting:", testHttpServer.URL)
c := make(chan int)
<-c
}
//*/

View file

@ -1,168 +1,198 @@
package registry package registry
// import ( import (
// "crypto/rand" "github.com/dotcloud/docker/auth"
// "encoding/hex" "github.com/dotcloud/docker/utils"
// "github.com/dotcloud/docker" "strings"
// "github.com/dotcloud/docker/auth" "testing"
// "io/ioutil" )
// "os"
// "path"
// "testing"
// )
// func newTestRuntime() (*Runtime, error) { var (
// root, err := ioutil.TempDir("", "docker-test") IMAGE_ID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
// if err != nil { TOKEN = []string{"fake-token"}
// return nil, err REPO = "foo42/bar"
// } )
// if err := os.Remove(root); err != nil {
// return nil, err
// }
// if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) { func spawnTestRegistry(t *testing.T) *Registry {
// return nil, err authConfig := &auth.AuthConfig{}
// } r, err := NewRegistry("", authConfig, utils.NewHTTPRequestFactory())
if err != nil {
t.Fatal(err)
}
return r
}
// return runtime, nil func TestPingRegistryEndpoint(t *testing.T) {
// } err := pingRegistryEndpoint(makeURL("/v1/"))
if err != nil {
t.Fatal(err)
}
}
// func TestPull(t *testing.T) { func TestGetRemoteHistory(t *testing.T) {
// os.Setenv("DOCKER_INDEX_URL", "") r := spawnTestRegistry(t)
// runtime, err := newTestRuntime() hist, err := r.GetRemoteHistory(IMAGE_ID, makeURL("/v1/"), TOKEN)
// if err != nil { if err != nil {
// t.Fatal(err) t.Fatal(err)
// } }
// defer nuke(runtime) 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[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
"Unexpected second ancestry")
}
// err = runtime.graph.PullRepository(ioutil.Discard, "busybox", "", runtime.repositories, nil) func TestLookupRemoteImage(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) found := r.LookupRemoteImage(IMAGE_ID, makeURL("/v1/"), TOKEN)
// } assertEqual(t, found, true, "Expected remote lookup to succeed")
// img, err := runtime.repositories.LookupImage("busybox") found = r.LookupRemoteImage("abcdef", makeURL("/v1/"), TOKEN)
// if err != nil { assertEqual(t, found, false, "Expected remote lookup to fail")
// t.Fatal(err) }
// }
// // Try to run something on this image to make sure the layer's been downloaded properly. func TestGetRemoteImageJSON(t *testing.T) {
// config, _, err := docker.ParseRun([]string{img.Id, "echo", "Hello World"}, runtime.capabilities) r := spawnTestRegistry(t)
// if err != nil { json, size, err := r.GetRemoteImageJSON(IMAGE_ID, makeURL("/v1/"), TOKEN)
// t.Fatal(err) if err != nil {
// } t.Fatal(err)
}
assertEqual(t, size, 154, "Expected size 154")
if len(json) <= 0 {
t.Fatal("Expected non-empty json")
}
// b := NewBuilder(runtime) _, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), TOKEN)
// container, err := b.Create(config) if err == nil {
// if err != nil { t.Fatal("Expected image not found error")
// t.Fatal(err) }
// } }
// if err := container.Start(); err != nil {
// t.Fatal(err)
// }
// if status := container.Wait(); status != 0 { func TestGetRemoteImageLayer(t *testing.T) {
// t.Fatalf("Expected status code 0, found %d instead", status) r := spawnTestRegistry(t)
// } data, err := r.GetRemoteImageLayer(IMAGE_ID, makeURL("/v1/"), TOKEN)
// } if err != nil {
t.Fatal(err)
}
if data == nil {
t.Fatal("Expected non-nil data result")
}
// func TestPullTag(t *testing.T) { _, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), TOKEN)
// os.Setenv("DOCKER_INDEX_URL", "") if err == nil {
// runtime, err := newTestRuntime() t.Fatal("Expected image not found error")
// if err != nil { }
// t.Fatal(err) }
// }
// defer nuke(runtime)
// err = runtime.graph.PullRepository(ioutil.Discard, "ubuntu", "12.04", runtime.repositories, nil) func TestGetRemoteTags(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, REPO, TOKEN)
// } if err != nil {
// _, err = runtime.repositories.LookupImage("ubuntu:12.04") t.Fatal(err)
// 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)
// img2, err := runtime.repositories.LookupImage("ubuntu:12.10") _, err = r.GetRemoteTags([]string{makeURL("/v1/")}, "foo42/baz", TOKEN)
// if img2 != nil { if err == nil {
// t.Fatalf("Expected nil image but found %v instead", img2.Id) t.Fatal("Expected error when fetching tags for bogus repo")
// } }
// } }
// func login(runtime *Runtime) error { func TestGetRepositoryData(t *testing.T) {
// authConfig := auth.NewAuthConfig("unittester", "surlautrerivejetattendrai", "noise+unittester@dotcloud.com", runtime.root) r := spawnTestRegistry(t)
// runtime.authConfig = authConfig data, err := r.GetRepositoryData(makeURL("/v1/"), "foo42/bar")
// _, err := auth.Login(authConfig) if err != nil {
// return err t.Fatal(err)
// } }
assertEqual(t, len(data.ImgList), 2, "Expected 2 images in ImgList")
assertEqual(t, len(data.Endpoints), 1, "Expected one endpoint in Endpoints")
}
// func TestPush(t *testing.T) { func TestPushImageJSONRegistry(t *testing.T) {
// os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com") r := spawnTestRegistry(t)
// defer os.Setenv("DOCKER_INDEX_URL", "") imgData := &ImgData{
// runtime, err := newTestRuntime() ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
// if err != nil { Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
// t.Fatal(err) }
// }
// defer nuke(runtime)
// err = login(runtime) err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"), TOKEN)
// if err != nil { if err != nil {
// t.Fatal(err) t.Fatal(err)
// } }
}
// err = runtime.graph.PullRepository(ioutil.Discard, "joffrey/busybox", "", runtime.repositories, nil) func TestPushImageLayerRegistry(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) layer := strings.NewReader("")
// } _, err := r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN, []byte{})
// tokenBuffer := make([]byte, 16) if err != nil {
// _, err = rand.Read(tokenBuffer) t.Fatal(err)
// if err != nil { }
// t.Fatal(err) }
// }
// token := hex.EncodeToString(tokenBuffer)[:29]
// config, _, err := ParseRun([]string{"joffrey/busybox", "touch", "/" + token}, runtime.capabilities)
// if err != nil {
// t.Fatal(err)
// }
// b := NewBuilder(runtime) func TestResolveRepositoryName(t *testing.T) {
// container, err := b.Create(config) _, _, err := ResolveRepositoryName("https://github.com/dotcloud/docker")
// if err != nil { assertEqual(t, err, ErrInvalidRepositoryName, "Expected error invalid repo name")
// t.Fatal(err) ep, repo, err := ResolveRepositoryName("fooo/bar")
// } if err != nil {
// if err := container.Start(); err != nil { t.Fatal(err)
// t.Fatal(err) }
// } assertEqual(t, ep, auth.IndexServerAddress(), "Expected endpoint to be index server address")
assertEqual(t, repo, "fooo/bar", "Expected resolved repo to be foo/bar")
// if status := container.Wait(); status != 0 { u := makeURL("")[7:]
// t.Fatalf("Expected status code 0, found %d instead", status) ep, repo, err = ResolveRepositoryName(u + "/private/moonbase")
// } if err != nil {
t.Fatal(err)
}
assertEqual(t, ep, "http://"+u+"/v1/", "Expected endpoint to be "+u)
assertEqual(t, repo, "private/moonbase", "Expected endpoint to be private/moonbase")
}
// img, err := b.Commit(container, "unittester/"+token, "", "", "", nil) func TestPushRegistryTag(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) err := r.PushRegistryTag("foo42/bar", IMAGE_ID, "stable", makeURL("/v1/"), TOKEN)
// } if err != nil {
t.Fatal(err)
}
}
// repo := runtime.repositories.Repositories["unittester/"+token] func TestPushImageJSONIndex(t *testing.T) {
// err = runtime.graph.PushRepository(ioutil.Discard, "unittester/"+token, repo, runtime.authConfig) r := spawnTestRegistry(t)
// if err != nil { imgData := []*ImgData{
// t.Fatal(err) &ImgData{
// } ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
},
&ImgData{
ID: "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
},
}
ep := makeURL("/v1/")
repoData, err := r.PushImageJSONIndex(ep, "foo42/bar", imgData, false, nil)
if err != nil {
t.Fatal(err)
}
if repoData == nil {
t.Fatal("Expected RepositoryData object")
}
repoData, err = r.PushImageJSONIndex(ep, "foo42/bar", imgData, true, []string{ep})
if err != nil {
t.Fatal(err)
}
if repoData == nil {
t.Fatal("Expected RepositoryData object")
}
}
// // Remove image so we can pull it again func TestSearchRepositories(t *testing.T) {
// if err := runtime.graph.Delete(img.Id); err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) results, err := r.SearchRepositories("supercalifragilisticepsialidocious")
// } if err != nil {
t.Fatal(err)
// err = runtime.graph.PullRepository(ioutil.Discard, "unittester/"+token, "", runtime.repositories, runtime.authConfig) }
// if err != nil { if results == nil {
// t.Fatal(err) t.Fatal("Expected non-nil SearchResults object")
// } }
assertEqual(t, results.NumResults, 0, "Expected 0 search results")
// layerPath, err := img.layer() }
// if err != nil {
// t.Fatal(err)
// }
// if _, err := os.Stat(path.Join(layerPath, token)); err != nil {
// t.Fatalf("Error while trying to retrieve token file: %v", err)
// }
// }