manager_test.go 6.5 KB

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