1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

move utils.Fataler to pkg/log.Fataler

Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
This commit is contained in:
Erik Hollensbe 2014-08-13 15:13:21 -07:00
parent a02f67be5b
commit ac63d925d2
4 changed files with 43 additions and 41 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/docker/docker/builtins" "github.com/docker/docker/builtins"
"github.com/docker/docker/daemon" "github.com/docker/docker/daemon"
"github.com/docker/docker/engine" "github.com/docker/docker/engine"
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/runconfig" "github.com/docker/docker/runconfig"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
) )
@ -28,7 +29,7 @@ import (
// Create a temporary daemon suitable for unit testing. // Create a temporary daemon suitable for unit testing.
// Call t.Fatal() at the first error. // Call t.Fatal() at the first error.
func mkDaemon(f utils.Fataler) *daemon.Daemon { func mkDaemon(f log.Fataler) *daemon.Daemon {
eng := newTestEngine(f, false, "") eng := newTestEngine(f, false, "")
return mkDaemonFromEngine(eng, f) return mkDaemonFromEngine(eng, f)
// FIXME: // FIXME:
@ -37,7 +38,7 @@ func mkDaemon(f utils.Fataler) *daemon.Daemon {
// [...] // [...]
} }
func createNamedTestContainer(eng *engine.Engine, config *runconfig.Config, f utils.Fataler, name string) (shortId string) { func createNamedTestContainer(eng *engine.Engine, config *runconfig.Config, f log.Fataler, name string) (shortId string) {
job := eng.Job("create", name) job := eng.Job("create", name)
if err := job.ImportEnv(config); err != nil { if err := job.ImportEnv(config); err != nil {
f.Fatal(err) f.Fatal(err)
@ -50,23 +51,23 @@ func createNamedTestContainer(eng *engine.Engine, config *runconfig.Config, f ut
return engine.Tail(outputBuffer, 1) return engine.Tail(outputBuffer, 1)
} }
func createTestContainer(eng *engine.Engine, config *runconfig.Config, f utils.Fataler) (shortId string) { func createTestContainer(eng *engine.Engine, config *runconfig.Config, f log.Fataler) (shortId string) {
return createNamedTestContainer(eng, config, f, "") return createNamedTestContainer(eng, config, f, "")
} }
func startContainer(eng *engine.Engine, id string, t utils.Fataler) { func startContainer(eng *engine.Engine, id string, t log.Fataler) {
job := eng.Job("start", id) job := eng.Job("start", id)
if err := job.Run(); err != nil { if err := job.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func containerRun(eng *engine.Engine, id string, t utils.Fataler) { func containerRun(eng *engine.Engine, id string, t log.Fataler) {
startContainer(eng, id, t) startContainer(eng, id, t)
containerWait(eng, id, t) containerWait(eng, id, t)
} }
func containerFileExists(eng *engine.Engine, id, dir string, t utils.Fataler) bool { func containerFileExists(eng *engine.Engine, id, dir string, t log.Fataler) bool {
c := getContainer(eng, id, t) c := getContainer(eng, id, t)
if err := c.Mount(); err != nil { if err := c.Mount(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -81,7 +82,7 @@ func containerFileExists(eng *engine.Engine, id, dir string, t utils.Fataler) bo
return true return true
} }
func containerAttach(eng *engine.Engine, id string, t utils.Fataler) (io.WriteCloser, io.ReadCloser) { func containerAttach(eng *engine.Engine, id string, t log.Fataler) (io.WriteCloser, io.ReadCloser) {
c := getContainer(eng, id, t) c := getContainer(eng, id, t)
i, err := c.StdinPipe() i, err := c.StdinPipe()
if err != nil { if err != nil {
@ -94,31 +95,31 @@ func containerAttach(eng *engine.Engine, id string, t utils.Fataler) (io.WriteCl
return i, o return i, o
} }
func containerWait(eng *engine.Engine, id string, t utils.Fataler) int { func containerWait(eng *engine.Engine, id string, t log.Fataler) int {
ex, _ := getContainer(eng, id, t).State.WaitStop(-1 * time.Second) ex, _ := getContainer(eng, id, t).State.WaitStop(-1 * time.Second)
return ex return ex
} }
func containerWaitTimeout(eng *engine.Engine, id string, t utils.Fataler) error { func containerWaitTimeout(eng *engine.Engine, id string, t log.Fataler) error {
_, err := getContainer(eng, id, t).State.WaitStop(500 * time.Millisecond) _, err := getContainer(eng, id, t).State.WaitStop(500 * time.Millisecond)
return err return err
} }
func containerKill(eng *engine.Engine, id string, t utils.Fataler) { func containerKill(eng *engine.Engine, id string, t log.Fataler) {
if err := eng.Job("kill", id).Run(); err != nil { if err := eng.Job("kill", id).Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func containerRunning(eng *engine.Engine, id string, t utils.Fataler) bool { func containerRunning(eng *engine.Engine, id string, t log.Fataler) bool {
return getContainer(eng, id, t).State.IsRunning() return getContainer(eng, id, t).State.IsRunning()
} }
func containerAssertExists(eng *engine.Engine, id string, t utils.Fataler) { func containerAssertExists(eng *engine.Engine, id string, t log.Fataler) {
getContainer(eng, id, t) getContainer(eng, id, t)
} }
func containerAssertNotExists(eng *engine.Engine, id string, t utils.Fataler) { func containerAssertNotExists(eng *engine.Engine, id string, t log.Fataler) {
daemon := mkDaemonFromEngine(eng, t) daemon := mkDaemonFromEngine(eng, t)
if c := daemon.Get(id); c != nil { if c := daemon.Get(id); c != nil {
t.Fatal(fmt.Errorf("Container %s should not exist", id)) t.Fatal(fmt.Errorf("Container %s should not exist", id))
@ -127,7 +128,7 @@ func containerAssertNotExists(eng *engine.Engine, id string, t utils.Fataler) {
// assertHttpNotError expect the given response to not have an error. // assertHttpNotError expect the given response to not have an error.
// Otherwise the it causes the test to fail. // Otherwise the it causes the test to fail.
func assertHttpNotError(r *httptest.ResponseRecorder, t utils.Fataler) { func assertHttpNotError(r *httptest.ResponseRecorder, t log.Fataler) {
// Non-error http status are [200, 400) // Non-error http status are [200, 400)
if r.Code < http.StatusOK || r.Code >= http.StatusBadRequest { if r.Code < http.StatusOK || r.Code >= http.StatusBadRequest {
t.Fatal(fmt.Errorf("Unexpected http error: %v", r.Code)) t.Fatal(fmt.Errorf("Unexpected http error: %v", r.Code))
@ -136,14 +137,14 @@ func assertHttpNotError(r *httptest.ResponseRecorder, t utils.Fataler) {
// assertHttpError expect the given response to have an error. // assertHttpError expect the given response to have an error.
// Otherwise the it causes the test to fail. // Otherwise the it causes the test to fail.
func assertHttpError(r *httptest.ResponseRecorder, t utils.Fataler) { func assertHttpError(r *httptest.ResponseRecorder, t log.Fataler) {
// Non-error http status are [200, 400) // Non-error http status are [200, 400)
if !(r.Code < http.StatusOK || r.Code >= http.StatusBadRequest) { if !(r.Code < http.StatusOK || r.Code >= http.StatusBadRequest) {
t.Fatal(fmt.Errorf("Unexpected http success code: %v", r.Code)) t.Fatal(fmt.Errorf("Unexpected http success code: %v", r.Code))
} }
} }
func getContainer(eng *engine.Engine, id string, t utils.Fataler) *daemon.Container { func getContainer(eng *engine.Engine, id string, t log.Fataler) *daemon.Container {
daemon := mkDaemonFromEngine(eng, t) daemon := mkDaemonFromEngine(eng, t)
c := daemon.Get(id) c := daemon.Get(id)
if c == nil { if c == nil {
@ -152,7 +153,7 @@ func getContainer(eng *engine.Engine, id string, t utils.Fataler) *daemon.Contai
return c return c
} }
func mkDaemonFromEngine(eng *engine.Engine, t utils.Fataler) *daemon.Daemon { func mkDaemonFromEngine(eng *engine.Engine, t log.Fataler) *daemon.Daemon {
iDaemon := eng.Hack_GetGlobalVar("httpapi.daemon") iDaemon := eng.Hack_GetGlobalVar("httpapi.daemon")
if iDaemon == nil { if iDaemon == nil {
panic("Legacy daemon field not set in engine") panic("Legacy daemon field not set in engine")
@ -164,7 +165,7 @@ func mkDaemonFromEngine(eng *engine.Engine, t utils.Fataler) *daemon.Daemon {
return daemon return daemon
} }
func newTestEngine(t utils.Fataler, autorestart bool, root string) *engine.Engine { func newTestEngine(t log.Fataler, autorestart bool, root string) *engine.Engine {
if root == "" { if root == "" {
if dir, err := newTestDirectory(unitTestStoreBase); err != nil { if dir, err := newTestDirectory(unitTestStoreBase); err != nil {
t.Fatal(err) t.Fatal(err)
@ -196,7 +197,7 @@ func newTestEngine(t utils.Fataler, autorestart bool, root string) *engine.Engin
return eng return eng
} }
func NewTestEngine(t utils.Fataler) *engine.Engine { func NewTestEngine(t log.Fataler) *engine.Engine {
return newTestEngine(t, false, "") return newTestEngine(t, false, "")
} }

View file

@ -20,6 +20,12 @@ const (
debug debug
) )
// A common interface to access the Fatal method of
// both testing.B and testing.T.
type Fataler interface {
Fatal(args ...interface{})
}
func (p priority) String() string { func (p priority) String() string {
switch p { switch p {
case fatal: case fatal:

View file

@ -17,6 +17,7 @@ import (
"time" "time"
"github.com/docker/docker/pkg/httputils" "github.com/docker/docker/pkg/httputils"
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/pkg/tarsum" "github.com/docker/docker/pkg/tarsum"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
) )
@ -52,7 +53,7 @@ func NewSession(authConfig *AuthConfig, factory *utils.HTTPRequestFactory, index
return nil, err return nil, err
} }
if info.Standalone { if info.Standalone {
utils.Debugf("Endpoint %s is eligible for private registry registry. Enabling decorator.", indexEndpoint) log.Debugf("Endpoint %s is eligible for private registry registry. Enabling decorator.", indexEndpoint)
dec := utils.NewHTTPAuthDecorator(authConfig.Username, authConfig.Password) dec := utils.NewHTTPAuthDecorator(authConfig.Username, authConfig.Password)
factory.AddDecorator(dec) factory.AddDecorator(dec)
} }
@ -91,7 +92,7 @@ func (r *Session) GetRemoteHistory(imgID, registry string, token []string) ([]st
return nil, fmt.Errorf("Error while reading the http response: %s", err) return nil, fmt.Errorf("Error while reading the http response: %s", err)
} }
utils.Debugf("Ancestry: %s", jsonString) log.Debugf("Ancestry: %s", jsonString)
history := new([]string) history := new([]string)
if err := json.Unmarshal(jsonString, history); err != nil { if err := json.Unmarshal(jsonString, history); err != nil {
return nil, err return nil, err
@ -105,13 +106,13 @@ func (r *Session) LookupRemoteImage(imgID, registry string, token []string) bool
req, err := r.reqFactory.NewRequest("GET", registry+"images/"+imgID+"/json", nil) req, err := r.reqFactory.NewRequest("GET", registry+"images/"+imgID+"/json", nil)
if err != nil { if err != nil {
utils.Errorf("Error in LookupRemoteImage %s", err) log.Errorf("Error in LookupRemoteImage %s", err)
return false return false
} }
setTokenAuth(req, token) setTokenAuth(req, token)
res, _, err := r.doRequest(req) res, _, err := r.doRequest(req)
if err != nil { if err != nil {
utils.Errorf("Error in LookupRemoteImage %s", err) log.Errorf("Error in LookupRemoteImage %s", err)
return false return false
} }
res.Body.Close() res.Body.Close()
@ -184,10 +185,10 @@ func (r *Session) GetRemoteImageLayer(imgID, registry string, token []string, im
} }
if res.Header.Get("Accept-Ranges") == "bytes" && imgSize > 0 { if res.Header.Get("Accept-Ranges") == "bytes" && imgSize > 0 {
utils.Debugf("server supports resume") log.Debugf("server supports resume")
return httputils.ResumableRequestReaderWithInitialResponse(client, req, 5, imgSize, res), nil return httputils.ResumableRequestReaderWithInitialResponse(client, req, 5, imgSize, res), nil
} }
utils.Debugf("server doesn't support resume") log.Debugf("server doesn't support resume")
return res.Body, nil return res.Body, nil
} }
@ -210,7 +211,7 @@ func (r *Session) GetRemoteTags(registries []string, repository string, token []
return nil, err return nil, err
} }
utils.Debugf("Got status code %d from %s", res.StatusCode, endpoint) log.Debugf("Got status code %d from %s", res.StatusCode, endpoint)
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 && res.StatusCode != 404 { if res.StatusCode != 200 && res.StatusCode != 404 {
@ -255,7 +256,7 @@ func (r *Session) GetRepositoryData(remote string) (*RepositoryData, error) {
indexEp := r.indexEndpoint indexEp := r.indexEndpoint
repositoryTarget := fmt.Sprintf("%srepositories/%s/images", indexEp, remote) repositoryTarget := fmt.Sprintf("%srepositories/%s/images", indexEp, remote)
utils.Debugf("[registry] Calling GET %s", repositoryTarget) log.Debugf("[registry] Calling GET %s", repositoryTarget)
req, err := r.reqFactory.NewRequest("GET", repositoryTarget, nil) req, err := r.reqFactory.NewRequest("GET", repositoryTarget, nil)
if err != nil { if err != nil {
@ -324,7 +325,7 @@ func (r *Session) GetRepositoryData(remote string) (*RepositoryData, error) {
func (r *Session) PushImageChecksumRegistry(imgData *ImgData, registry string, token []string) error { func (r *Session) PushImageChecksumRegistry(imgData *ImgData, registry string, token []string) error {
utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgData.ID+"/checksum") log.Debugf("[registry] Calling PUT %s", registry+"images/"+imgData.ID+"/checksum")
req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgData.ID+"/checksum", nil) req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgData.ID+"/checksum", nil)
if err != nil { if err != nil {
@ -361,7 +362,7 @@ func (r *Session) PushImageChecksumRegistry(imgData *ImgData, registry string, t
// Push a local image to the registry // Push a local image to the registry
func (r *Session) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, registry string, token []string) error { func (r *Session) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, registry string, token []string) error {
utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgData.ID+"/json") log.Debugf("[registry] Calling PUT %s", registry+"images/"+imgData.ID+"/json")
req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgData.ID+"/json", bytes.NewReader(jsonRaw)) req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgData.ID+"/json", bytes.NewReader(jsonRaw))
if err != nil { if err != nil {
@ -396,7 +397,7 @@ func (r *Session) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, regist
func (r *Session) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, token []string, jsonRaw []byte) (checksum string, checksumPayload string, err error) { func (r *Session) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, token []string, jsonRaw []byte) (checksum string, checksumPayload string, err error) {
utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgID+"/layer") log.Debugf("[registry] Calling PUT %s", registry+"images/"+imgID+"/layer")
tarsumLayer := &tarsum.TarSum{Reader: layer} tarsumLayer := &tarsum.TarSum{Reader: layer}
h := sha256.New() h := sha256.New()
@ -483,8 +484,8 @@ func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, 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("[registry] PUT %s", u) log.Debugf("[registry] PUT %s", u)
utils.Debugf("Image list pushed to index:\n%s", imgListJSON) log.Debugf("Image list pushed to index:\n%s", imgListJSON)
req, err := r.reqFactory.NewRequest("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
@ -505,7 +506,7 @@ func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate
// 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", res.Header.Get("Location")) log.Debugf("Redirected to %s", res.Header.Get("Location"))
req, err = r.reqFactory.NewRequest("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
@ -534,7 +535,7 @@ func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate
} }
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"]
utils.Debugf("Auth token: %v", tokens) log.Debugf("Auth token: %v", tokens)
} else { } else {
return nil, fmt.Errorf("Index response didn't contain an access token") return nil, fmt.Errorf("Index response didn't contain an access token")
} }
@ -565,7 +566,7 @@ func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate
} }
func (r *Session) SearchRepositories(term string) (*SearchResults, error) { func (r *Session) SearchRepositories(term string) (*SearchResults, error) {
utils.Debugf("Index server: %s", r.indexEndpoint) log.Debugf("Index server: %s", r.indexEndpoint)
u := r.indexEndpoint + "search?q=" + url.QueryEscape(term) u := r.indexEndpoint + "search?q=" + url.QueryEscape(term)
req, err := r.reqFactory.NewRequest("GET", u, nil) req, err := r.reqFactory.NewRequest("GET", u, nil)
if err != nil { if err != nil {

View file

@ -28,12 +28,6 @@ type KeyValuePair struct {
Value string Value string
} }
// A common interface to access the Fatal method of
// both testing.B and testing.T.
type Fataler interface {
Fatal(args ...interface{})
}
// Go is a basic promise implementation: it wraps calls a function in a goroutine, // Go is a basic promise implementation: it wraps calls a function in a goroutine,
// and returns a channel which will later return the function's return value. // and returns a channel which will later return the function's return value.
func Go(f func() error) chan error { func Go(f func() error) chan error {