distribution/errors_test.go
4f0d95fa
 package distribution // import "github.com/docker/docker/distribution"
305801f5
 
 import (
 	"errors"
 	"strings"
 	"syscall"
 	"testing"
 
 	"github.com/docker/distribution/registry/api/errcode"
 	"github.com/docker/distribution/registry/api/v2"
 	"github.com/docker/distribution/registry/client"
 )
 
e04375fb
 var alwaysContinue = []error{
305801f5
 	&client.UnexpectedHTTPResponseError{},
 
 	// Some errcode.Errors that don't disprove the existence of a V1 image
 	errcode.Error{Code: errcode.ErrorCodeUnauthorized},
 	errcode.Error{Code: v2.ErrorCodeManifestUnknown},
 	errcode.Error{Code: v2.ErrorCodeNameUnknown},
 
 	errors.New("some totally unexpected error"),
 }
 
e04375fb
 var continueFromMirrorEndpoint = []error{
305801f5
 	ImageConfigPullError{},
 
 	// Some other errcode.Error that doesn't indicate we should search for a V1 image.
 	errcode.Error{Code: errcode.ErrorCodeTooManyRequests},
 }
 
e04375fb
 var neverContinue = []error{
305801f5
 	errors.New(strings.ToLower(syscall.ESRCH.Error())), // No such process
 }
 
 func TestContinueOnError_NonMirrorEndpoint(t *testing.T) {
e04375fb
 	for _, err := range alwaysContinue {
305801f5
 		if !continueOnError(err, false) {
 			t.Errorf("Should continue from non-mirror endpoint: %T: '%s'", err, err.Error())
 		}
 	}
 
e04375fb
 	for _, err := range continueFromMirrorEndpoint {
305801f5
 		if continueOnError(err, false) {
 			t.Errorf("Should only continue from mirror endpoint: %T: '%s'", err, err.Error())
 		}
 	}
 }
 
 func TestContinueOnError_MirrorEndpoint(t *testing.T) {
f23c00d8
 	var errs []error
e04375fb
 	errs = append(errs, alwaysContinue...)
 	errs = append(errs, continueFromMirrorEndpoint...)
305801f5
 	for _, err := range errs {
 		if !continueOnError(err, true) {
 			t.Errorf("Should continue from mirror endpoint: %T: '%s'", err, err.Error())
 		}
 	}
 }
 
 func TestContinueOnError_NeverContinue(t *testing.T) {
e04375fb
 	for _, isMirrorEndpoint := range []bool{true, false} {
 		for _, err := range neverContinue {
 			if continueOnError(err, isMirrorEndpoint) {
305801f5
 				t.Errorf("Should never continue: %T: '%s'", err, err.Error())
 			}
 		}
 	}
 }
 
 func TestContinueOnError_UnnestsErrors(t *testing.T) {
 	// ContinueOnError should evaluate nested errcode.Errors.
 
 	// Assumes that v2.ErrorCodeNameUnknown is a continueable error code.
 	err := errcode.Errors{errcode.Error{Code: v2.ErrorCodeNameUnknown}}
 	if !continueOnError(err, false) {
 		t.Fatal("ContinueOnError should unnest, base return value on errcode.Errors")
 	}
 
 	// Assumes that errcode.ErrorCodeTooManyRequests is not a V1-fallback indication
 	err = errcode.Errors{errcode.Error{Code: errcode.ErrorCodeTooManyRequests}}
 	if continueOnError(err, false) {
 		t.Fatal("ContinueOnError should unnest, base return value on errcode.Errors")
 	}
 }