manager_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package plugins
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestManager_Register(t *testing.T) {
  11. const endpoint = "http://127.0.0.1/plugins"
  12. data := []struct {
  13. t PluginType
  14. n string
  15. u string
  16. err error
  17. }{
  18. {
  19. t: SOURCE,
  20. n: "",
  21. u: "",
  22. err: errors.New("invalid name : should not be empty"),
  23. }, {
  24. t: SOURCE,
  25. n: "zipMissConf",
  26. u: endpoint + "/sources/zipMissConf.zip",
  27. err: errors.New("fail to unzip file " + endpoint + "/sources/zipMissConf.zip: invalid zip file: so file or conf file is missing"),
  28. }, {
  29. t: SINK,
  30. n: "urlerror",
  31. u: endpoint + "/sinks/nozip",
  32. err: errors.New("invalid uri " + endpoint + "/sinks/nozip"),
  33. }, {
  34. t: SINK,
  35. n: "zipWrongname",
  36. u: endpoint + "/sinks/zipWrongName.zip",
  37. err: errors.New("fail to unzip file " + endpoint + "/sinks/zipWrongName.zip: invalid zip file: so file or conf file is missing"),
  38. }, {
  39. t: FUNCTION,
  40. n: "zipMissSo",
  41. u: endpoint + "/functions/zipMissSo.zip",
  42. err: errors.New("fail to unzip file " + endpoint + "/functions/zipMissSo.zip: invalid zip file: so file or conf file is missing"),
  43. }, {
  44. t: SOURCE,
  45. n: "random2",
  46. u: endpoint + "/sources/random2.zip",
  47. }, {
  48. t: SINK,
  49. n: "file",
  50. u: endpoint + "/sinks/file.zip",
  51. }, {
  52. t: FUNCTION,
  53. n: "echo",
  54. u: endpoint + "/functions/echo.zip",
  55. }, {
  56. t: FUNCTION,
  57. n: "echo",
  58. u: endpoint + "/functions/echo.zip",
  59. err: errors.New("invalid name echo: duplicate"),
  60. },
  61. }
  62. manager, err := NewPluginManager()
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  67. for i, tt := range data {
  68. err = manager.Register(tt.t, tt.n, tt.u, func() {})
  69. if !reflect.DeepEqual(tt.err, err) {
  70. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  71. } else if tt.err == nil {
  72. err := checkFile(manager.pluginDir, manager.etcDir, tt.t, tt.n)
  73. if err != nil {
  74. t.Errorf("%d: error : %s\n\n", i, err)
  75. }
  76. }
  77. }
  78. }
  79. func TestManager_Delete(t *testing.T) {
  80. data := []struct {
  81. t PluginType
  82. n string
  83. err error
  84. }{
  85. {
  86. t: SOURCE,
  87. n: "random2",
  88. }, {
  89. t: SINK,
  90. n: "file",
  91. }, {
  92. t: FUNCTION,
  93. n: "echo",
  94. },
  95. }
  96. manager, err := NewPluginManager()
  97. if err != nil {
  98. t.Error(err)
  99. }
  100. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  101. for i, p := range data {
  102. err = manager.Delete(p.t, p.n, func() {})
  103. if err != nil {
  104. t.Errorf("%d: delete error : %s\n\n", i, err)
  105. }
  106. }
  107. }
  108. func checkFile(pluginDir string, etcDir string, t PluginType, name string) error {
  109. soPath := path.Join(pluginDir, pluginFolders[t], ucFirst(name)+".so")
  110. _, err := os.Stat(soPath)
  111. if err != nil {
  112. return err
  113. }
  114. if t == SOURCE {
  115. etcPath := path.Join(etcDir, pluginFolders[t], name+".yaml")
  116. _, err = os.Stat(etcPath)
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. return nil
  122. }