manager_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, &Plugin{
  69. Name: tt.n,
  70. File: tt.u,
  71. Callback: "",
  72. })
  73. if !reflect.DeepEqual(tt.err, err) {
  74. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  75. } else if tt.err == nil {
  76. err := checkFile(manager.pluginDir, manager.etcDir, tt.t, tt.n)
  77. if err != nil {
  78. t.Errorf("%d: error : %s\n\n", i, err)
  79. }
  80. }
  81. }
  82. }
  83. func TestManager_Delete(t *testing.T) {
  84. data := []struct {
  85. t PluginType
  86. n string
  87. err error
  88. }{
  89. {
  90. t: SOURCE,
  91. n: "random2",
  92. }, {
  93. t: SINK,
  94. n: "file",
  95. }, {
  96. t: FUNCTION,
  97. n: "echo",
  98. },
  99. }
  100. manager, err := NewPluginManager()
  101. if err != nil {
  102. t.Error(err)
  103. }
  104. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  105. for i, p := range data {
  106. err = manager.Delete(p.t, p.n)
  107. if err != nil {
  108. t.Errorf("%d: delete error : %s\n\n", i, err)
  109. }
  110. }
  111. }
  112. func checkFile(pluginDir string, etcDir string, t PluginType, name string) error {
  113. soPath := path.Join(pluginDir, PluginTypes[t], ucFirst(name)+".so")
  114. _, err := os.Stat(soPath)
  115. if err != nil {
  116. return err
  117. }
  118. if t == SOURCE {
  119. etcPath := path.Join(etcDir, PluginTypes[t], name+".yaml")
  120. _, err = os.Stat(etcPath)
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }