directory_upload_manager_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package awsuploader
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/cloudflare/cloudflared/logger"
  11. )
  12. type MockUploader struct {
  13. shouldFail bool
  14. }
  15. func (m *MockUploader) Upload(filepath string) error {
  16. if m.shouldFail {
  17. return errors.New("upload set to fail")
  18. }
  19. return nil
  20. }
  21. func NewMockUploader(shouldFail bool) Uploader {
  22. return &MockUploader{shouldFail: shouldFail}
  23. }
  24. func getDirectoryPath(t *testing.T) string {
  25. dir, err := os.Getwd()
  26. if err != nil {
  27. t.Fatal("couldn't create the test directory!", err)
  28. }
  29. return filepath.Join(dir, "uploads")
  30. }
  31. func setupTestDirectory(t *testing.T) string {
  32. path := getDirectoryPath(t)
  33. os.RemoveAll(path)
  34. time.Sleep(100 * time.Millisecond) //short way to wait for the OS to delete the folder
  35. err := os.MkdirAll(path, os.ModePerm)
  36. if err != nil {
  37. t.Fatal("couldn't create the test directory!", err)
  38. }
  39. return path
  40. }
  41. func createUploadManager(t *testing.T, shouldFailUpload bool) *DirectoryUploadManager {
  42. rootDirectory := setupTestDirectory(t)
  43. uploader := NewMockUploader(shouldFailUpload)
  44. logger := logger.NewOutputWriter(logger.NewMockWriteManager())
  45. shutdownC := make(chan struct{})
  46. return NewDirectoryUploadManager(logger, uploader, rootDirectory, 1*time.Second, shutdownC)
  47. }
  48. func createFile(t *testing.T, fileName string) (*os.File, string) {
  49. path := filepath.Join(getDirectoryPath(t), fileName)
  50. f, err := os.Create(path)
  51. if err != nil {
  52. t.Fatal("upload to create file for sweep test", err)
  53. }
  54. return f, path
  55. }
  56. func TestUploadSuccess(t *testing.T) {
  57. manager := createUploadManager(t, false)
  58. path := filepath.Join(getDirectoryPath(t), "test_file")
  59. if err := manager.Upload(path); err != nil {
  60. t.Fatal("the upload request method failed", err)
  61. }
  62. }
  63. func TestUploadFailure(t *testing.T) {
  64. manager := createUploadManager(t, true)
  65. path := filepath.Join(getDirectoryPath(t), "test_file")
  66. if err := manager.Upload(path); err == nil {
  67. t.Fatal("the upload request method should have failed and didn't", err)
  68. }
  69. }
  70. func TestSweepSuccess(t *testing.T) {
  71. manager := createUploadManager(t, false)
  72. f, path := createFile(t, "test_file")
  73. defer f.Close()
  74. manager.Start()
  75. time.Sleep(2 * time.Second)
  76. if _, err := os.Stat(path); os.IsExist(err) {
  77. //the file should have been deleted
  78. t.Fatal("the manager failed to delete the file", err)
  79. }
  80. }
  81. func TestSweepFailure(t *testing.T) {
  82. manager := createUploadManager(t, true)
  83. f, path := createFile(t, "test_file")
  84. defer f.Close()
  85. manager.Start()
  86. time.Sleep(2 * time.Second)
  87. _, serr := f.Stat()
  88. if serr != nil {
  89. //the file should still exist
  90. os.Remove(path)
  91. t.Fatal("the manager failed to delete the file", serr)
  92. }
  93. }
  94. func TestHighLoad(t *testing.T) {
  95. manager := createUploadManager(t, false)
  96. for i := 0; i < 30; i++ {
  97. f, _ := createFile(t, randomString(6))
  98. defer f.Close()
  99. }
  100. manager.Start()
  101. time.Sleep(4 * time.Second)
  102. directory := getDirectoryPath(t)
  103. files, err := ioutil.ReadDir(directory)
  104. if err != nil || len(files) > 0 {
  105. t.Fatalf("the manager failed to upload all the files: %s files left: %d", err, len(files))
  106. }
  107. }
  108. // LowerCase [a-z]
  109. const randSet = "abcdefghijklmnopqrstuvwxyz"
  110. // String returns a string of length 'n' from a set of letters 'lset'
  111. func randomString(n int) string {
  112. b := make([]byte, n)
  113. lsetLen := len(randSet)
  114. for i := range b {
  115. b[i] = randSet[rand.Intn(lsetLen)]
  116. }
  117. return string(b)
  118. }