customizations_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package dynamodb_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "testing"
  8. "github.com/aws/aws-sdk-go/aws"
  9. "github.com/aws/aws-sdk-go/aws/awserr"
  10. "github.com/aws/aws-sdk-go/aws/client"
  11. "github.com/aws/aws-sdk-go/aws/request"
  12. "github.com/aws/aws-sdk-go/awstesting/unit"
  13. "github.com/aws/aws-sdk-go/service/dynamodb"
  14. )
  15. var db *dynamodb.DynamoDB
  16. func TestMain(m *testing.M) {
  17. db = dynamodb.New(unit.Session, &aws.Config{
  18. MaxRetries: aws.Int(2),
  19. })
  20. db.Handlers.Send.Clear() // mock sending
  21. os.Exit(m.Run())
  22. }
  23. func mockCRCResponse(svc *dynamodb.DynamoDB, status int, body, crc string) (req *request.Request) {
  24. header := http.Header{}
  25. header.Set("x-amz-crc32", crc)
  26. req, _ = svc.ListTablesRequest(nil)
  27. req.Handlers.Build.RemoveByName("crr.endpointdiscovery")
  28. req.Handlers.Send.PushBack(func(*request.Request) {
  29. req.HTTPResponse = &http.Response{
  30. ContentLength: int64(len(body)),
  31. StatusCode: status,
  32. Body: ioutil.NopCloser(bytes.NewReader([]byte(body))),
  33. Header: header,
  34. }
  35. })
  36. req.Send()
  37. return
  38. }
  39. func TestDefaultRetryRules(t *testing.T) {
  40. d := dynamodb.New(unit.Session, &aws.Config{MaxRetries: aws.Int(-1)})
  41. if e, a := 10, d.MaxRetries(); e != a {
  42. t.Errorf("expect %d max retries, got %d", e, a)
  43. }
  44. }
  45. func TestCustomRetryRules(t *testing.T) {
  46. d := dynamodb.New(unit.Session, &aws.Config{MaxRetries: aws.Int(2)})
  47. if e, a := 2, d.MaxRetries(); e != a {
  48. t.Errorf("expect %d max retries, got %d", e, a)
  49. }
  50. }
  51. type testCustomRetryer struct {
  52. client.DefaultRetryer
  53. }
  54. func TestCustomRetry_FromConfig(t *testing.T) {
  55. d := dynamodb.New(unit.Session, &aws.Config{
  56. Retryer: testCustomRetryer{client.DefaultRetryer{NumMaxRetries: 9}},
  57. })
  58. if _, ok := d.Retryer.(testCustomRetryer); !ok {
  59. t.Errorf("expect retryer to be testCustomRetryer, but got %T", d.Retryer)
  60. }
  61. if e, a := 9, d.MaxRetries(); e != a {
  62. t.Errorf("expect %d max retries from custom retryer, got %d", e, a)
  63. }
  64. }
  65. func TestValidateCRC32NoHeaderSkip(t *testing.T) {
  66. req := mockCRCResponse(db, 200, "{}", "")
  67. if req.Error != nil {
  68. t.Errorf("expect no error, got %v", req.Error)
  69. }
  70. }
  71. func TestValidateCRC32InvalidHeaderSkip(t *testing.T) {
  72. req := mockCRCResponse(db, 200, "{}", "ABC")
  73. if req.Error != nil {
  74. t.Errorf("expect no error, got %v", req.Error)
  75. }
  76. }
  77. func TestValidateCRC32AlreadyErrorSkip(t *testing.T) {
  78. req := mockCRCResponse(db, 400, "{}", "1234")
  79. if req.Error == nil {
  80. t.Fatalf("expect error, but got none")
  81. }
  82. aerr := req.Error.(awserr.Error)
  83. if aerr.Code() == "CRC32CheckFailed" {
  84. t.Errorf("expect error code not to be CRC32CheckFailed")
  85. }
  86. }
  87. func TestValidateCRC32IsValid(t *testing.T) {
  88. req := mockCRCResponse(db, 200, `{"TableNames":["A"]}`, "3090163698")
  89. if req.Error != nil {
  90. t.Fatalf("expect no error, got %v", req.Error)
  91. }
  92. // CRC check does not affect output parsing
  93. out := req.Data.(*dynamodb.ListTablesOutput)
  94. if e, a := "A", *out.TableNames[0]; e != a {
  95. t.Errorf("expect %q table name, got %q", e, a)
  96. }
  97. }
  98. func TestValidateCRC32DoesNotMatch(t *testing.T) {
  99. req := mockCRCResponse(db, 200, "{}", "1234")
  100. if req.Error == nil {
  101. t.Fatalf("expect error, but got none")
  102. }
  103. req.Handlers.Build.RemoveByName("crr.endpointdiscovery")
  104. aerr := req.Error.(awserr.Error)
  105. if e, a := "CRC32CheckFailed", aerr.Code(); e != a {
  106. t.Errorf("expect %s error code, got %s", e, a)
  107. }
  108. if e, a := 2, req.RetryCount; e != a {
  109. t.Errorf("expect %d retry count, got %d", e, a)
  110. }
  111. }
  112. func TestValidateCRC32DoesNotMatchNoComputeChecksum(t *testing.T) {
  113. svc := dynamodb.New(unit.Session, &aws.Config{
  114. MaxRetries: aws.Int(2),
  115. DisableComputeChecksums: aws.Bool(true),
  116. })
  117. svc.Handlers.Send.Clear() // mock sending
  118. req := mockCRCResponse(svc, 200, `{"TableNames":["A"]}`, "1234")
  119. if req.Error != nil {
  120. t.Fatalf("expect no error, got %v", req.Error)
  121. }
  122. if e, a := 0, req.RetryCount; e != a {
  123. t.Errorf("expect %d retry count, got %d", e, a)
  124. }
  125. // CRC check disabled. Does not affect output parsing
  126. out := req.Data.(*dynamodb.ListTablesOutput)
  127. if e, a := "A", *out.TableNames[0]; e != a {
  128. t.Errorf("expect %q table name, got %q", e, a)
  129. }
  130. }