ftp_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package ftp
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/smartystreets/goconvey/convey"
  8. )
  9. func fileExist(path string) bool {
  10. _, err := os.Stat(path)
  11. if err != nil {
  12. if os.IsNotExist(err) {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18. func createFile(path string) {
  19. if !fileExist(path) {
  20. // If the file doesn't exist, create it, or append to the file
  21. f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. _, err = f.Write([]byte("Hello"))
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. f.Close()
  30. }
  31. }
  32. func TestFtpRetry(t *testing.T) {
  33. var (
  34. callback func() error
  35. retry = int(0)
  36. sleep time.Duration
  37. )
  38. convey.Convey("Retry", t, func(ctx convey.C) {
  39. err := Retry(callback, retry, sleep)
  40. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  41. ctx.So(err, convey.ShouldBeNil)
  42. })
  43. })
  44. }
  45. func TestFtpFileMd5(t *testing.T) {
  46. var (
  47. path = "/tmp/testMd5.source"
  48. md5Path = "/tmp/testMd5.target"
  49. )
  50. convey.Convey("FileMd5", t, func(ctx convey.C) {
  51. createFile(path)
  52. err := d.FileMd5(path, md5Path)
  53. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  54. ctx.So(err, convey.ShouldBeNil)
  55. })
  56. })
  57. }
  58. func TestFtpUploadFile(t *testing.T) {
  59. var (
  60. cfg = d.conf.Search
  61. path = "/tmp/testMd5.source"
  62. )
  63. convey.Convey("UploadFile", t, func(ctx convey.C) {
  64. createFile(path)
  65. err := d.UploadFile(path, "testMd5.remote", cfg.FTP.RemotePgcURL)
  66. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  67. ctx.So(err, convey.ShouldBeNil)
  68. })
  69. })
  70. }