/bagman/processutil_test.go

https://github.com/APTrust/bagman · Go · 172 lines · 145 code · 23 blank · 4 comment · 53 complexity · a9630ea1197f5708135e4dd4b8d2e763 MD5 · raw file

  1. package bagman_test
  2. import (
  3. "fmt"
  4. "github.com/APTrust/bagman/bagman"
  5. "github.com/bitly/go-nsq"
  6. "github.com/crowdmob/goamz/s3"
  7. "path"
  8. "path/filepath"
  9. "os"
  10. "testing"
  11. )
  12. var testConfig string = "test"
  13. func deleteTestLogs(config bagman.Config) {
  14. processName := path.Base(os.Args[0])
  15. jsonLog := fmt.Sprintf("%s.json", processName)
  16. jsonLog = filepath.Join(config.AbsLogDirectory(), jsonLog)
  17. os.Remove(jsonLog)
  18. messageLog := fmt.Sprintf("%s.log", processName)
  19. messageLog = filepath.Join(config.AbsLogDirectory(), messageLog)
  20. os.Remove(messageLog)
  21. }
  22. func TestNewProcessUtil(t *testing.T) {
  23. procUtil := bagman.NewProcessUtil(&testConfig)
  24. defer deleteTestLogs(procUtil.Config)
  25. if procUtil.Config.ActiveConfig != "test" {
  26. t.Errorf("NewProcessUtil did not load the test config")
  27. }
  28. if procUtil.JsonLog == nil {
  29. t.Errorf("NewProcessUtil did not initialize JsonLog")
  30. }
  31. if procUtil.MessageLog == nil {
  32. t.Errorf("NewProcessUtil did not initialize MessageLog")
  33. }
  34. if procUtil.Volume == nil {
  35. t.Errorf("NewProcessUtil did not initialize Volume")
  36. }
  37. if procUtil.S3Client == nil {
  38. t.Errorf("NewProcessUtil did not initialize S3Client")
  39. }
  40. if procUtil.FluctusClient == nil {
  41. t.Errorf("NewProcessUtil did not initialize FluctusClient")
  42. }
  43. if procUtil.Succeeded() != 0 {
  44. t.Errorf("NewProcessUtil did not initialize succeeded to zero.")
  45. }
  46. if procUtil.Failed() != 0 {
  47. t.Errorf("NewProcessUtil did not initialize failed to zero.")
  48. }
  49. }
  50. func TestIncrementSucceededAndFailed(t *testing.T) {
  51. procUtil := bagman.NewProcessUtil(&testConfig)
  52. defer deleteTestLogs(procUtil.Config)
  53. initialValue := procUtil.Succeeded()
  54. for i:=0; i < 3; i++ {
  55. procUtil.IncrementSucceeded()
  56. }
  57. if procUtil.Succeeded() - initialValue != 3 {
  58. t.Errorf("Succeeded() returned %d, expected 3", procUtil.Succeeded() - initialValue)
  59. }
  60. initialValue = procUtil.Failed()
  61. for i:=0; i < 3; i++ {
  62. procUtil.IncrementFailed()
  63. }
  64. if procUtil.Failed() - initialValue != 3 {
  65. t.Errorf("Failed() returned %d, expected 3", procUtil.Failed() - initialValue)
  66. }
  67. }
  68. func TestMessageIdString(t *testing.T) {
  69. procUtil := bagman.NewProcessUtil(&testConfig)
  70. defer deleteTestLogs(procUtil.Config)
  71. messageId := nsq.MessageID{'s', 'i', 'x', 't', 'e', 'e', 'n', 's', 'i', 'x', 't', 'e', 'e', 'n', '1', '6'}
  72. if procUtil.MessageIdString(messageId) != "sixteensixteen16" {
  73. t.Errorf("MessageIdString should have returned 'sixteensixteen16', but returned '%s'",
  74. procUtil.MessageIdString(messageId))
  75. }
  76. }
  77. func TestSyncMapFunctions(t *testing.T) {
  78. procUtil := bagman.NewProcessUtil(&testConfig)
  79. defer deleteTestLogs(procUtil.Config)
  80. messageId1 := nsq.MessageID{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 's', 'd', 'f', 'g', 'h'}
  81. messageId2 := nsq.MessageID{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '9', '8', '7', '1', 'x', 'y'}
  82. messageId1String := procUtil.MessageIdString(messageId1)
  83. messageId2String := procUtil.MessageIdString(messageId2)
  84. err := procUtil.RegisterItem("Item1", messageId1)
  85. if err != nil {
  86. t.Errorf("RegisterItem returned an unexpected error: %v", err)
  87. }
  88. // Trying to register the same key with a different messageId should cause an error.
  89. err = procUtil.RegisterItem("Item1", messageId2)
  90. if err == nil {
  91. t.Errorf("RegisterItem should have returned an error but did not")
  92. }
  93. // Register new key with new messageId
  94. err = procUtil.RegisterItem("Item2", messageId2)
  95. if err != nil {
  96. t.Errorf("RegisterItem returned an unexpected error: %v", err)
  97. }
  98. // Make sure it's all there.
  99. if procUtil.MessageIdFor("Item1") != messageId1String {
  100. t.Errorf("Expected messageId '%s' for Item1, but got '%s'",
  101. messageId1String, procUtil.MessageIdFor("Item1"))
  102. }
  103. if procUtil.MessageIdFor("Item2") != messageId2String {
  104. t.Errorf("Expected messageId '%s' for Item2, but got '%s'",
  105. messageId2String, procUtil.MessageIdFor("Item2"))
  106. }
  107. // Make sure Unregister works
  108. procUtil.UnregisterItem("Item1")
  109. if procUtil.MessageIdFor("Item1") != "" {
  110. t.Errorf("Item1 was not unregistered")
  111. }
  112. procUtil.UnregisterItem("Item2")
  113. if procUtil.MessageIdFor("Item2") != "" {
  114. t.Errorf("Item2 was not unregistered")
  115. }
  116. }
  117. func TestBagAlreadyInProgress(t *testing.T) {
  118. procUtil := bagman.NewProcessUtil(&testConfig)
  119. defer deleteTestLogs(procUtil.Config)
  120. s3File := &bagman.S3File {
  121. BucketName: "aptrust.receiving.miami.edu",
  122. Key: s3.Key {
  123. Key: "big_ol_file.tar",
  124. },
  125. }
  126. messageId := nsq.MessageID{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 's', 'd', 'f', 'g', 'h'}
  127. messageIdString := procUtil.MessageIdString(messageId)
  128. if procUtil.BagAlreadyInProgress(s3File, messageIdString) == true {
  129. t.Errorf("BagAlreadyInProgress() should have returned false")
  130. }
  131. _ = procUtil.RegisterItem(s3File.BagName(), messageId)
  132. if procUtil.BagAlreadyInProgress(s3File, messageIdString) == false {
  133. t.Errorf("BagAlreadyInProgress() should have returned true")
  134. }
  135. if procUtil.BagAlreadyInProgress(s3File, "SomeRandomString") == true {
  136. t.Errorf("BagAlreadyInProgress() should have returned false")
  137. }
  138. procUtil.UnregisterItem(s3File.BagName())
  139. tarFile := filepath.Join(procUtil.Config.TarDirectory, s3File.Key.Key)
  140. file, err := os.Create(tarFile)
  141. if err != nil {
  142. t.Errorf("Could not create file necessary for testing: %v", err)
  143. }
  144. if procUtil.BagAlreadyInProgress(s3File, messageIdString) == false {
  145. t.Errorf("BagAlreadyInProgress() should have returned true")
  146. }
  147. file.Close()
  148. os.Remove(tarFile)
  149. }