errors_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package distribution // import "github.com/docker/docker/distribution"
  2. import (
  3. "errors"
  4. "strings"
  5. "syscall"
  6. "testing"
  7. "github.com/docker/distribution/registry/api/errcode"
  8. "github.com/docker/distribution/registry/client"
  9. )
  10. var errUnexpected = errors.New("some totally unexpected error")
  11. var alwaysContinue = []error{
  12. &client.UnexpectedHTTPResponseError{},
  13. errcode.Errors{},
  14. errUnexpected,
  15. // nested
  16. errcode.Errors{errUnexpected},
  17. }
  18. var continueFromMirrorEndpoint = []error{
  19. imageConfigPullError{},
  20. errcode.Error{},
  21. // nested
  22. errcode.Errors{errcode.Error{}},
  23. }
  24. var neverContinue = []error{
  25. errors.New(strings.ToLower(syscall.ESRCH.Error())), // No such process
  26. }
  27. func TestContinueOnError_NonMirrorEndpoint(t *testing.T) {
  28. for _, err := range alwaysContinue {
  29. if !continueOnError(err, false) {
  30. t.Errorf("Should continue from non-mirror endpoint: %T: '%s'", err, err.Error())
  31. }
  32. }
  33. for _, err := range continueFromMirrorEndpoint {
  34. if continueOnError(err, false) {
  35. t.Errorf("Should only continue from mirror endpoint: %T: '%s'", err, err.Error())
  36. }
  37. }
  38. }
  39. func TestContinueOnError_MirrorEndpoint(t *testing.T) {
  40. var errs []error
  41. errs = append(errs, alwaysContinue...)
  42. errs = append(errs, continueFromMirrorEndpoint...)
  43. for _, err := range errs {
  44. if !continueOnError(err, true) {
  45. t.Errorf("Should continue from mirror endpoint: %T: '%s'", err, err.Error())
  46. }
  47. }
  48. }
  49. func TestContinueOnError_NeverContinue(t *testing.T) {
  50. for _, isMirrorEndpoint := range []bool{true, false} {
  51. for _, err := range neverContinue {
  52. if continueOnError(err, isMirrorEndpoint) {
  53. t.Errorf("Should never continue: %T: '%s'", err, err.Error())
  54. }
  55. }
  56. }
  57. }