manager_test.go 5.3 KB

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