github.com/rawahars/moby@v24.0.4+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  	"github.com/docker/distribution/registry/client"
    11  )
    12  
    13  var errUnexpected = errors.New("some totally unexpected error")
    14  
    15  var alwaysContinue = []error{
    16  	&client.UnexpectedHTTPResponseError{},
    17  	errcode.Errors{},
    18  	errUnexpected,
    19  	// nested
    20  	errcode.Errors{errUnexpected},
    21  }
    22  
    23  var continueFromMirrorEndpoint = []error{
    24  	imageConfigPullError{},
    25  	errcode.Error{},
    26  	// nested
    27  	errcode.Errors{errcode.Error{}},
    28  }
    29  
    30  var neverContinue = []error{
    31  	errors.New(strings.ToLower(syscall.ESRCH.Error())), // No such process
    32  }
    33  
    34  func TestContinueOnError_NonMirrorEndpoint(t *testing.T) {
    35  	for _, err := range alwaysContinue {
    36  		if !continueOnError(err, false) {
    37  			t.Errorf("Should continue from non-mirror endpoint: %T: '%s'", err, err.Error())
    38  		}
    39  	}
    40  
    41  	for _, err := range continueFromMirrorEndpoint {
    42  		if continueOnError(err, false) {
    43  			t.Errorf("Should only continue from mirror endpoint: %T: '%s'", err, err.Error())
    44  		}
    45  	}
    46  }
    47  
    48  func TestContinueOnError_MirrorEndpoint(t *testing.T) {
    49  	var errs []error
    50  	errs = append(errs, alwaysContinue...)
    51  	errs = append(errs, continueFromMirrorEndpoint...)
    52  	for _, err := range errs {
    53  		if !continueOnError(err, true) {
    54  			t.Errorf("Should continue from mirror endpoint: %T: '%s'", err, err.Error())
    55  		}
    56  	}
    57  }
    58  
    59  func TestContinueOnError_NeverContinue(t *testing.T) {
    60  	for _, isMirrorEndpoint := range []bool{true, false} {
    61  		for _, err := range neverContinue {
    62  			if continueOnError(err, isMirrorEndpoint) {
    63  				t.Errorf("Should never continue: %T: '%s'", err, err.Error())
    64  			}
    65  		}
    66  	}
    67  }