github.com/afbjorklund/moby@v20.10.5+incompatible/distribution/errors_test.go (about)

     1  package distribution // import "github.com/docker/docker/distribution"
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"syscall"
     7  	"testing"
     8  
     9  	"github.com/docker/distribution/registry/api/errcode"
    10  	v2 "github.com/docker/distribution/registry/api/v2"
    11  	"github.com/docker/distribution/registry/client"
    12  )
    13  
    14  var alwaysContinue = []error{
    15  	&client.UnexpectedHTTPResponseError{},
    16  
    17  	// Some errcode.Errors that don't disprove the existence of a V1 image
    18  	errcode.Error{Code: errcode.ErrorCodeUnauthorized},
    19  	errcode.Error{Code: v2.ErrorCodeManifestUnknown},
    20  	errcode.Error{Code: v2.ErrorCodeNameUnknown},
    21  
    22  	errors.New("some totally unexpected error"),
    23  }
    24  
    25  var continueFromMirrorEndpoint = []error{
    26  	ImageConfigPullError{},
    27  
    28  	// Some other errcode.Error that doesn't indicate we should search for a V1 image.
    29  	errcode.Error{Code: errcode.ErrorCodeTooManyRequests},
    30  }
    31  
    32  var neverContinue = []error{
    33  	errors.New(strings.ToLower(syscall.ESRCH.Error())), // No such process
    34  }
    35  
    36  func TestContinueOnError_NonMirrorEndpoint(t *testing.T) {
    37  	for _, err := range alwaysContinue {
    38  		if !continueOnError(err, false) {
    39  			t.Errorf("Should continue from non-mirror endpoint: %T: '%s'", err, err.Error())
    40  		}
    41  	}
    42  
    43  	for _, err := range continueFromMirrorEndpoint {
    44  		if continueOnError(err, false) {
    45  			t.Errorf("Should only continue from mirror endpoint: %T: '%s'", err, err.Error())
    46  		}
    47  	}
    48  }
    49  
    50  func TestContinueOnError_MirrorEndpoint(t *testing.T) {
    51  	var errs []error
    52  	errs = append(errs, alwaysContinue...)
    53  	errs = append(errs, continueFromMirrorEndpoint...)
    54  	for _, err := range errs {
    55  		if !continueOnError(err, true) {
    56  			t.Errorf("Should continue from mirror endpoint: %T: '%s'", err, err.Error())
    57  		}
    58  	}
    59  }
    60  
    61  func TestContinueOnError_NeverContinue(t *testing.T) {
    62  	for _, isMirrorEndpoint := range []bool{true, false} {
    63  		for _, err := range neverContinue {
    64  			if continueOnError(err, isMirrorEndpoint) {
    65  				t.Errorf("Should never continue: %T: '%s'", err, err.Error())
    66  			}
    67  		}
    68  	}
    69  }
    70  
    71  func TestContinueOnError_UnnestsErrors(t *testing.T) {
    72  	// ContinueOnError should evaluate nested errcode.Errors.
    73  
    74  	// Assumes that v2.ErrorCodeNameUnknown is a continueable error code.
    75  	err := errcode.Errors{errcode.Error{Code: v2.ErrorCodeNameUnknown}}
    76  	if !continueOnError(err, false) {
    77  		t.Fatal("ContinueOnError should unnest, base return value on errcode.Errors")
    78  	}
    79  
    80  	// Assumes that errcode.ErrorCodeTooManyRequests is not a V1-fallback indication
    81  	err = errcode.Errors{errcode.Error{Code: errcode.ErrorCodeTooManyRequests}}
    82  	if continueOnError(err, false) {
    83  		t.Fatal("ContinueOnError should unnest, base return value on errcode.Errors")
    84  	}
    85  }