manager_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package portable
  15. import (
  16. "errors"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/plugin"
  19. "github.com/lf-edge/ekuiper/internal/plugin/portable/runtime"
  20. "net/http"
  21. "net/http/httptest"
  22. "os"
  23. "path"
  24. "path/filepath"
  25. "reflect"
  26. "testing"
  27. )
  28. // Test only install API. Install from file is tested in the integration test in test/portable_rule_test
  29. func init() {
  30. InitManager()
  31. }
  32. func TestManager_Install(t *testing.T) {
  33. s := httptest.NewServer(
  34. http.FileServer(http.Dir("../testzips")),
  35. )
  36. defer s.Close()
  37. endpoint := s.URL
  38. data := []struct {
  39. n string
  40. u string
  41. v string
  42. err error
  43. }{
  44. { // 0
  45. n: "",
  46. u: "",
  47. err: errors.New("invalid name : should not be empty"),
  48. }, { // 1
  49. n: "zipMissJson",
  50. u: endpoint + "/functions/misc.zip",
  51. err: errors.New("fail to install plugin: missing or invalid json file zipMissJson.json"),
  52. }, { // 2
  53. n: "urlerror",
  54. u: endpoint + "/sinks/nozip",
  55. err: errors.New("invalid uri " + endpoint + "/sinks/nozip"),
  56. }, { // 3
  57. n: "wrong",
  58. u: endpoint + "/portables/wrong.zip",
  59. err: errors.New("fail to install plugin: missing mirror.exe"),
  60. }, { // 4
  61. n: "wrongname",
  62. u: endpoint + "/portables/mirror.zip",
  63. err: errors.New("fail to install plugin: missing or invalid json file wrongname.json"),
  64. }, { // 5
  65. n: "mirror2",
  66. u: endpoint + "/portables/mirror.zip",
  67. },
  68. }
  69. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  70. for i, tt := range data {
  71. p := &plugin.IOPlugin{
  72. Name: tt.n,
  73. File: tt.u,
  74. }
  75. err := manager.Register(p)
  76. if !reflect.DeepEqual(tt.err, err) {
  77. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  78. } else if tt.err == nil {
  79. err := checkFileForMirror(manager.pluginDir, manager.pluginConfDir, true)
  80. if err != nil {
  81. t.Errorf("%d: error : %s\n\n", i, err)
  82. }
  83. }
  84. }
  85. }
  86. func TestManager_Read(t *testing.T) {
  87. expPlugins := []*PluginInfo{
  88. {
  89. PluginMeta: runtime.PluginMeta{
  90. Name: "mirror2",
  91. Version: "v1.0.0",
  92. Language: "go",
  93. Executable: filepath.Clean(path.Join(manager.pluginDir, "mirror2", "mirror2")),
  94. },
  95. Sources: []string{"randomGo"},
  96. Sinks: []string{"fileGo"},
  97. Functions: []string{"echoGo"},
  98. },
  99. }
  100. result := manager.List()
  101. if len(result) != 3 {
  102. t.Errorf("list result mismatch:\n exp=%v\n got=%v\n\n", expPlugins, result)
  103. }
  104. _, ok := manager.GetPluginInfo("mirror3")
  105. if ok {
  106. t.Error("find inexist plugin mirror3")
  107. }
  108. pi, ok := manager.GetPluginInfo("mirror2")
  109. if !ok {
  110. t.Error("can't find plugin mirror2")
  111. }
  112. if !reflect.DeepEqual(expPlugins[0], pi) {
  113. t.Errorf("Get plugin mirror2 mismatch:\n exp=%v\n got=%v", expPlugins[0], pi)
  114. }
  115. _, ok = manager.GetPluginMeta(plugin.SOURCE, "echoGo")
  116. if ok {
  117. t.Error("find inexist source symbol echo")
  118. }
  119. m, ok := manager.GetPluginMeta(plugin.SINK, "fileGo")
  120. if !ok {
  121. t.Error("can't find sink symbol fileGo")
  122. }
  123. if !reflect.DeepEqual(&(expPlugins[0].PluginMeta), m) {
  124. t.Errorf("Get sink symbol mismatch:\n exp=%v\n got=%v", expPlugins[0].PluginMeta, m)
  125. }
  126. }
  127. // This will start channel, so test it in integration tests.
  128. //func TestFactory(t *testing.T){
  129. // _, err := manager.Source("alss")
  130. // expErr := fmt.Errorf("can't find random")
  131. // if !reflect.DeepEqual(expErr, err){
  132. // t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", expErr, err)
  133. // }
  134. // src, _ := manager.Source("randomGo")
  135. // if src == nil {
  136. // t.Errorf("can't get source randomGo")
  137. // }
  138. // snk, _ := manager.Sink("fileGo")
  139. // if snk == nil {
  140. // t.Errorf("can't get sink fileGo")
  141. // }
  142. // fun, _ := manager.Function("echoGo")
  143. // if fun == nil {
  144. // t.Errorf("can't get function echoGo")
  145. // }
  146. // ok := manager.HasFunctionSet("echoGo")
  147. // if !ok {
  148. // t.Errorf("can't check function set")
  149. // }
  150. //}
  151. func TestDelete(t *testing.T) {
  152. err := manager.Delete("mirror2")
  153. if err != nil {
  154. t.Errorf("delete plugin error: %v", err)
  155. }
  156. err = checkFileForMirror(manager.pluginDir, manager.pluginConfDir, false)
  157. if err != nil {
  158. t.Errorf("error : %s\n\n", err)
  159. }
  160. }
  161. func checkFileForMirror(pluginDir, etcDir string, exist bool) error {
  162. requiredFiles := []string{
  163. path.Join(pluginDir, "mirror2", "mirror2"),
  164. path.Join(pluginDir, "mirror2", "mirror2.json"),
  165. path.Join(etcDir, "sources", "randomGo.yaml"),
  166. path.Join(etcDir, "sources", "randomGo.json"),
  167. path.Join(etcDir, "functions", "echoGo.json"),
  168. path.Join(etcDir, "sinks", "fileGo.json"),
  169. }
  170. for _, file := range requiredFiles {
  171. _, err := os.Stat(file)
  172. if exist && err != nil {
  173. return err
  174. } else if !exist && err == nil {
  175. return fmt.Errorf("file still exists: %s", file)
  176. }
  177. }
  178. return nil
  179. }