errors_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package 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/api/v2"
  9. "github.com/docker/distribution/registry/client"
  10. )
  11. var alwaysContinue = []error{
  12. &client.UnexpectedHTTPResponseError{},
  13. // Some errcode.Errors that don't disprove the existence of a V1 image
  14. errcode.Error{Code: errcode.ErrorCodeUnauthorized},
  15. errcode.Error{Code: v2.ErrorCodeManifestUnknown},
  16. errcode.Error{Code: v2.ErrorCodeNameUnknown},
  17. errors.New("some totally unexpected error"),
  18. }
  19. var continueFromMirrorEndpoint = []error{
  20. ImageConfigPullError{},
  21. // Some other errcode.Error that doesn't indicate we should search for a V1 image.
  22. errcode.Error{Code: errcode.ErrorCodeTooManyRequests},
  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. 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. }
  58. func TestContinueOnError_UnnestsErrors(t *testing.T) {
  59. // ContinueOnError should evaluate nested errcode.Errors.
  60. // Assumes that v2.ErrorCodeNameUnknown is a continueable error code.
  61. err := errcode.Errors{errcode.Error{Code: v2.ErrorCodeNameUnknown}}
  62. if !continueOnError(err, false) {
  63. t.Fatal("ContinueOnError should unnest, base return value on errcode.Errors")
  64. }
  65. // Assumes that errcode.ErrorCodeTooManyRequests is not a V1-fallback indication
  66. err = errcode.Errors{errcode.Error{Code: errcode.ErrorCodeTooManyRequests}}
  67. if continueOnError(err, false) {
  68. t.Fatal("ContinueOnError should unnest, base return value on errcode.Errors")
  69. }
  70. }