You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

167 lines
3.3 KiB

  1. package dynamodb_test
  2. import (
  3. "flag"
  4. "testing"
  5. "time"
  6. "github.com/goamz/goamz/aws"
  7. "github.com/goamz/goamz/dynamodb"
  8. . "gopkg.in/check.v1"
  9. )
  10. const TIMEOUT = 3 * time.Minute
  11. var amazon = flag.Bool("amazon", false, "Enable tests against dynamodb")
  12. var local = flag.Bool("local", true, "Use DynamoDB local on 8080 instead of real server on us-east.")
  13. var dynamodb_region aws.Region
  14. var dynamodb_auth aws.Auth
  15. type DynamoDBTest struct {
  16. server *dynamodb.Server
  17. aws.Region // Exports Region
  18. TableDescriptionT dynamodb.TableDescriptionT
  19. table *dynamodb.Table
  20. }
  21. // Delete all items in the table
  22. func (s *DynamoDBTest) TearDownTest(c *C) {
  23. pk, err := s.TableDescriptionT.BuildPrimaryKey()
  24. if err != nil {
  25. c.Fatal(err)
  26. }
  27. attrs, err := s.table.Scan(nil)
  28. if err != nil {
  29. c.Fatal(err)
  30. }
  31. for _, a := range attrs {
  32. key := &dynamodb.Key{
  33. HashKey: a[pk.KeyAttribute.Name].Value,
  34. }
  35. if pk.HasRange() {
  36. key.RangeKey = a[pk.RangeAttribute.Name].Value
  37. }
  38. if ok, err := s.table.DeleteItem(key); !ok {
  39. c.Fatal(err)
  40. }
  41. }
  42. }
  43. func (s *DynamoDBTest) TearDownSuite(c *C) {
  44. // return immediately in the case of calling c.Skip() in SetUpSuite()
  45. if s.server == nil {
  46. return
  47. }
  48. // check whether the table exists
  49. if tables, err := s.server.ListTables(); err != nil {
  50. c.Fatal(err)
  51. } else {
  52. if !findTableByName(tables, s.TableDescriptionT.TableName) {
  53. return
  54. }
  55. }
  56. // Delete the table and wait
  57. if _, err := s.server.DeleteTable(s.TableDescriptionT); err != nil {
  58. c.Fatal(err)
  59. }
  60. done := make(chan bool)
  61. timeout := time.After(TIMEOUT)
  62. go func() {
  63. for {
  64. select {
  65. case <-done:
  66. return
  67. default:
  68. tables, err := s.server.ListTables()
  69. if err != nil {
  70. c.Fatal(err)
  71. }
  72. if findTableByName(tables, s.TableDescriptionT.TableName) {
  73. time.Sleep(5 * time.Second)
  74. } else {
  75. done <- true
  76. return
  77. }
  78. }
  79. }
  80. }()
  81. select {
  82. case <-done:
  83. break
  84. case <-timeout:
  85. c.Error("Expect the table to be deleted but timed out")
  86. close(done)
  87. }
  88. }
  89. func (s *DynamoDBTest) WaitUntilStatus(c *C, status string) {
  90. // We should wait until the table is in specified status because a real DynamoDB has some delay for ready
  91. done := make(chan bool)
  92. timeout := time.After(TIMEOUT)
  93. go func() {
  94. for {
  95. select {
  96. case <-done:
  97. return
  98. default:
  99. desc, err := s.table.DescribeTable()
  100. if err != nil {
  101. c.Fatal(err)
  102. }
  103. if desc.TableStatus == status {
  104. done <- true
  105. return
  106. }
  107. time.Sleep(5 * time.Second)
  108. }
  109. }
  110. }()
  111. select {
  112. case <-done:
  113. break
  114. case <-timeout:
  115. c.Errorf("Expect a status to be %s, but timed out", status)
  116. close(done)
  117. }
  118. }
  119. func setUpAuth(c *C) {
  120. if !*amazon {
  121. c.Skip("Test against amazon not enabled.")
  122. }
  123. if *local {
  124. c.Log("Using local server")
  125. dynamodb_region = aws.Region{
  126. DynamoDBEndpoint: "http://127.0.0.1:8000",
  127. DynamoDBStreamsEndpoint: "http://127.0.0.1:8000",
  128. }
  129. dynamodb_auth = aws.Auth{AccessKey: "DUMMY_KEY", SecretKey: "DUMMY_SECRET"}
  130. } else {
  131. c.Log("Using REAL AMAZON SERVER")
  132. dynamodb_region = aws.USEast
  133. auth, err := aws.EnvAuth()
  134. if err != nil {
  135. c.Fatal(err)
  136. }
  137. dynamodb_auth = auth
  138. }
  139. }
  140. func findTableByName(tables []string, name string) bool {
  141. for _, t := range tables {
  142. if t == name {
  143. return true
  144. }
  145. }
  146. return false
  147. }
  148. func Test(t *testing.T) {
  149. TestingT(t)
  150. }