manager_test.go 5.2 KB

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