manager_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 native
  15. import (
  16. "errors"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/binder"
  19. "github.com/lf-edge/ekuiper/internal/binder/function"
  20. "github.com/lf-edge/ekuiper/internal/plugin"
  21. "github.com/lf-edge/ekuiper/internal/testx"
  22. "net/http"
  23. "net/http/httptest"
  24. "os"
  25. "path"
  26. "reflect"
  27. "sort"
  28. "testing"
  29. )
  30. func init() {
  31. testx.InitEnv()
  32. nativeManager, err := InitManager()
  33. if err != nil {
  34. panic(err)
  35. }
  36. err = function.Initialize([]binder.FactoryEntry{{Name: "native plugin", Factory: nativeManager}})
  37. if err != nil {
  38. panic(err)
  39. }
  40. }
  41. func TestManager_Register(t *testing.T) {
  42. s := httptest.NewServer(
  43. http.FileServer(http.Dir("../testzips")),
  44. )
  45. defer s.Close()
  46. endpoint := s.URL
  47. data := []struct {
  48. t plugin.PluginType
  49. n string
  50. u string
  51. v string
  52. f []string
  53. lowerSo bool
  54. err error
  55. }{
  56. {
  57. t: plugin.SOURCE,
  58. n: "",
  59. u: "",
  60. err: errors.New("invalid name : should not be empty"),
  61. }, {
  62. t: plugin.SOURCE,
  63. n: "zipMissConf",
  64. u: endpoint + "/sources/zipMissConf.zip",
  65. err: errors.New("fail to install plugin: invalid zip file: so file or conf file is missing"),
  66. }, {
  67. t: plugin.SINK,
  68. n: "urlerror",
  69. u: endpoint + "/sinks/nozip",
  70. err: errors.New("invalid uri " + endpoint + "/sinks/nozip"),
  71. }, {
  72. t: plugin.SINK,
  73. n: "zipWrongname",
  74. u: endpoint + "/sinks/zipWrongName.zip",
  75. err: errors.New("fail to install plugin: invalid zip file: so file or conf file is missing"),
  76. }, {
  77. t: plugin.FUNCTION,
  78. n: "zipMissSo",
  79. u: endpoint + "/functions/zipMissSo.zip",
  80. err: errors.New("fail to install plugin: invalid zip file: so file or conf file is missing"),
  81. }, {
  82. t: plugin.SOURCE,
  83. n: "random2",
  84. u: endpoint + "/sources/random2.zip",
  85. }, {
  86. t: plugin.SOURCE,
  87. n: "random3",
  88. u: endpoint + "/sources/random3.zip",
  89. v: "1.0.0",
  90. }, {
  91. t: plugin.SINK,
  92. n: "file2",
  93. u: endpoint + "/sinks/file2.zip",
  94. lowerSo: true,
  95. }, {
  96. t: plugin.FUNCTION,
  97. n: "echo2",
  98. u: endpoint + "/functions/echo2.zip",
  99. f: []string{"echo2", "echo3"},
  100. }, {
  101. t: plugin.FUNCTION,
  102. n: "echo2",
  103. u: endpoint + "/functions/echo2.zip",
  104. err: errors.New("invalid name echo2: duplicate"),
  105. }, {
  106. t: plugin.FUNCTION,
  107. n: "misc",
  108. u: endpoint + "/functions/echo2.zip",
  109. f: []string{"misc", "echo3"},
  110. err: errors.New("function name echo3 already exists"),
  111. }, {
  112. t: plugin.FUNCTION,
  113. n: "comp",
  114. u: endpoint + "/functions/comp.zip",
  115. },
  116. }
  117. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  118. for i, tt := range data {
  119. var p plugin.Plugin
  120. if tt.t == plugin.FUNCTION {
  121. p = &plugin.FuncPlugin{
  122. IOPlugin: plugin.IOPlugin{
  123. Name: tt.n,
  124. File: tt.u,
  125. },
  126. Functions: tt.f,
  127. }
  128. } else {
  129. p = &plugin.IOPlugin{
  130. Name: tt.n,
  131. File: tt.u,
  132. }
  133. }
  134. err := manager.Register(tt.t, p)
  135. if !reflect.DeepEqual(tt.err, err) {
  136. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  137. } else if tt.err == nil {
  138. err := checkFile(manager.pluginDir, manager.etcDir, tt.t, tt.n, tt.v, tt.lowerSo)
  139. if err != nil {
  140. t.Errorf("%d: error : %s\n\n", i, err)
  141. }
  142. }
  143. }
  144. }
  145. func TestManager_List(t *testing.T) {
  146. data := []struct {
  147. t plugin.PluginType
  148. r []string
  149. }{
  150. {
  151. t: plugin.SOURCE,
  152. r: []string{"random", "random2", "random3"},
  153. }, {
  154. t: plugin.SINK,
  155. r: []string{"file", "file2"},
  156. }, {
  157. t: plugin.FUNCTION,
  158. r: []string{"accumulateWordCount", "comp", "countPlusOne", "echo", "echo2"},
  159. },
  160. }
  161. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  162. for i, p := range data {
  163. result := manager.List(p.t)
  164. sort.Strings(result)
  165. if !reflect.DeepEqual(p.r, result) {
  166. t.Errorf("%d: result mismatch:\n exp=%v\n got=%v\n\n", i, p.r, result)
  167. }
  168. }
  169. }
  170. func TestManager_Symbols(t *testing.T) {
  171. r := []string{"accumulateWordCount", "comp", "countPlusOne", "echo", "echo2", "echo3", "misc"}
  172. result := manager.ListSymbols()
  173. sort.Strings(result)
  174. if !reflect.DeepEqual(r, result) {
  175. t.Errorf("result mismatch:\n exp=%v\n got=%v\n\n", r, result)
  176. }
  177. p, ok := manager.GetPluginBySymbol(plugin.FUNCTION, "echo3")
  178. if !ok {
  179. t.Errorf("cannot find echo3 symbol")
  180. }
  181. if p != "echo2" {
  182. t.Errorf("wrong plugin %s for echo3 symbol", p)
  183. }
  184. }
  185. func TestManager_Desc(t *testing.T) {
  186. data := []struct {
  187. t plugin.PluginType
  188. n string
  189. r map[string]interface{}
  190. }{
  191. {
  192. t: plugin.SOURCE,
  193. n: "random2",
  194. r: map[string]interface{}{
  195. "name": "random2",
  196. "version": "",
  197. },
  198. }, {
  199. t: plugin.SOURCE,
  200. n: "random3",
  201. r: map[string]interface{}{
  202. "name": "random3",
  203. "version": "1.0.0",
  204. },
  205. }, {
  206. t: plugin.FUNCTION,
  207. n: "echo2",
  208. r: map[string]interface{}{
  209. "name": "echo2",
  210. "version": "",
  211. "functions": []string{"echo2", "echo3"},
  212. },
  213. },
  214. }
  215. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  216. for i, p := range data {
  217. result, ok := manager.GetPluginInfo(p.t, p.n)
  218. if !ok {
  219. t.Errorf("%d: get error : not found\n\n", i)
  220. return
  221. }
  222. if !reflect.DeepEqual(p.r, result) {
  223. t.Errorf("%d: result mismatch:\n exp=%v\n got=%v\n\n", i, p.r, result)
  224. }
  225. }
  226. }
  227. func TestManager_Delete(t *testing.T) {
  228. data := []struct {
  229. t plugin.PluginType
  230. n string
  231. err error
  232. }{
  233. {
  234. t: plugin.SOURCE,
  235. n: "random2",
  236. }, {
  237. t: plugin.SINK,
  238. n: "file2",
  239. }, {
  240. t: plugin.FUNCTION,
  241. n: "echo2",
  242. }, {
  243. t: plugin.SOURCE,
  244. n: "random3",
  245. }, {
  246. t: plugin.FUNCTION,
  247. n: "comp",
  248. },
  249. }
  250. fmt.Printf("The test bucket size is %d.\n\n", len(data))
  251. for i, p := range data {
  252. err := manager.Delete(p.t, p.n, false)
  253. if err != nil {
  254. t.Errorf("%d: delete error : %s\n\n", i, err)
  255. }
  256. }
  257. }
  258. func checkFile(pluginDir string, etcDir string, t plugin.PluginType, name string, version string, lowerSo bool) error {
  259. var soName string
  260. if !lowerSo {
  261. soName = ucFirst(name) + ".so"
  262. if version != "" {
  263. soName = fmt.Sprintf("%s@v%s.so", ucFirst(name), version)
  264. }
  265. } else {
  266. soName = name + ".so"
  267. if version != "" {
  268. soName = fmt.Sprintf("%s@v%s.so", name, version)
  269. }
  270. }
  271. soPath := path.Join(pluginDir, plugin.PluginTypes[t], soName)
  272. _, err := os.Stat(soPath)
  273. if err != nil {
  274. return err
  275. }
  276. if t == plugin.SOURCE {
  277. etcPath := path.Join(etcDir, plugin.PluginTypes[t], name+".yaml")
  278. _, err = os.Stat(etcPath)
  279. if err != nil {
  280. return err
  281. }
  282. }
  283. return nil
  284. }