manager_test.go 6.6 KB

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