manager_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 service
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/binder"
  17. "github.com/lf-edge/ekuiper/internal/binder/function"
  18. "reflect"
  19. "testing"
  20. )
  21. var m *Manager
  22. func init() {
  23. serviceManager, err := InitManager()
  24. if err != nil {
  25. panic(err)
  26. }
  27. err = function.Initialize([]binder.FactoryEntry{{Name: "external service", Factory: serviceManager}})
  28. if err != nil {
  29. panic(err)
  30. }
  31. m = GetManager()
  32. m.InitByFiles()
  33. }
  34. func TestInitByFiles(t *testing.T) {
  35. //expects
  36. name := "sample"
  37. info := &serviceInfo{
  38. About: &about{
  39. Author: &author{
  40. Name: "EMQ",
  41. Email: "contact@emqx.io",
  42. Company: "EMQ Technologies Co., Ltd",
  43. Website: "https://www.emqx.io",
  44. },
  45. HelpUrl: &fileLanguage{
  46. English: "https://github.com/lf-edge/ekuiper/blob/master/docs/en_US/plugins/functions/functions.md",
  47. Chinese: "https://github.com/lf-edge/ekuiper/blob/master/docs/zh_CN/plugins/functions/functions.md",
  48. },
  49. Description: &fileLanguage{
  50. English: "Sample external services for test only",
  51. Chinese: "示例外部函数配置,仅供测试",
  52. },
  53. },
  54. Interfaces: map[string]*interfaceInfo{
  55. "tsrpc": {
  56. Addr: "tcp://localhost:50051",
  57. Protocol: GRPC,
  58. Schema: &schemaInfo{
  59. SchemaType: PROTOBUFF,
  60. SchemaFile: "hw.proto",
  61. },
  62. Functions: []string{
  63. "helloFromGrpc",
  64. "ComputeFromGrpc",
  65. "getFeatureFromGrpc",
  66. "objectDetectFromGrpc",
  67. "getStatusFromGrpc",
  68. "notUsedRpc",
  69. },
  70. },
  71. "tsrest": {
  72. Addr: "http://localhost:51234",
  73. Protocol: REST,
  74. Schema: &schemaInfo{
  75. SchemaType: PROTOBUFF,
  76. SchemaFile: "hw.proto",
  77. },
  78. Options: map[string]interface{}{
  79. "insecureSkipVerify": true,
  80. "headers": map[string]interface{}{
  81. "Accept-Charset": "utf-8",
  82. },
  83. },
  84. Functions: []string{
  85. "helloFromRest",
  86. "ComputeFromRest",
  87. "getFeatureFromRest",
  88. "objectDetectFromRest",
  89. "getStatusFromRest",
  90. "restEncodedJson",
  91. },
  92. },
  93. "tsmsgpack": {
  94. Addr: "tcp://localhost:50000",
  95. Protocol: MSGPACK,
  96. Schema: &schemaInfo{
  97. SchemaType: PROTOBUFF,
  98. SchemaFile: "hw.proto",
  99. },
  100. Functions: []string{
  101. "helloFromMsgpack",
  102. "ComputeFromMsgpack",
  103. "getFeatureFromMsgpack",
  104. "objectDetectFromMsgpack",
  105. "getStatusFromMsgpack",
  106. "notUsedMsgpack",
  107. },
  108. },
  109. },
  110. }
  111. funcs := map[string]*functionContainer{
  112. "ListShelves": {
  113. ServiceName: "httpSample",
  114. InterfaceName: "bookshelf",
  115. MethodName: "ListShelves",
  116. },
  117. "CreateShelf": {
  118. ServiceName: "httpSample",
  119. InterfaceName: "bookshelf",
  120. MethodName: "CreateShelf",
  121. },
  122. "GetShelf": {
  123. ServiceName: "httpSample",
  124. InterfaceName: "bookshelf",
  125. MethodName: "GetShelf",
  126. },
  127. "DeleteShelf": {
  128. ServiceName: "httpSample",
  129. InterfaceName: "bookshelf",
  130. MethodName: "DeleteShelf",
  131. },
  132. "ListBooks": {
  133. ServiceName: "httpSample",
  134. InterfaceName: "bookshelf",
  135. MethodName: "ListBooks",
  136. },
  137. "createBook": {
  138. ServiceName: "httpSample",
  139. InterfaceName: "bookshelf",
  140. MethodName: "CreateBook",
  141. },
  142. "GetBook": {
  143. ServiceName: "httpSample",
  144. InterfaceName: "bookshelf",
  145. MethodName: "GetBook",
  146. },
  147. "DeleteBook": {
  148. ServiceName: "httpSample",
  149. InterfaceName: "bookshelf",
  150. MethodName: "DeleteBook",
  151. },
  152. "GetMessage": {
  153. ServiceName: "httpSample",
  154. InterfaceName: "messaging",
  155. MethodName: "GetMessage",
  156. },
  157. "SearchMessage": {
  158. ServiceName: "httpSample",
  159. InterfaceName: "messaging",
  160. MethodName: "SearchMessage",
  161. },
  162. "UpdateMessage": {
  163. ServiceName: "httpSample",
  164. InterfaceName: "messaging",
  165. MethodName: "UpdateMessage",
  166. },
  167. "PatchMessage": {
  168. ServiceName: "httpSample",
  169. InterfaceName: "messaging",
  170. MethodName: "PatchMessage",
  171. },
  172. "helloFromGrpc": {
  173. ServiceName: "sample",
  174. InterfaceName: "tsrpc",
  175. MethodName: "SayHello",
  176. },
  177. "helloFromRest": {
  178. ServiceName: "sample",
  179. InterfaceName: "tsrest",
  180. MethodName: "SayHello",
  181. },
  182. "helloFromMsgpack": {
  183. ServiceName: "sample",
  184. InterfaceName: "tsmsgpack",
  185. MethodName: "SayHello",
  186. },
  187. "objectDetectFromGrpc": {
  188. ServiceName: "sample",
  189. InterfaceName: "tsrpc",
  190. MethodName: "object_detection",
  191. },
  192. "objectDetectFromRest": {
  193. ServiceName: "sample",
  194. InterfaceName: "tsrest",
  195. MethodName: "object_detection",
  196. },
  197. "objectDetectFromMsgpack": {
  198. ServiceName: "sample",
  199. InterfaceName: "tsmsgpack",
  200. MethodName: "object_detection",
  201. },
  202. "getFeatureFromGrpc": {
  203. ServiceName: "sample",
  204. InterfaceName: "tsrpc",
  205. MethodName: "get_feature",
  206. },
  207. "getFeatureFromRest": {
  208. ServiceName: "sample",
  209. InterfaceName: "tsrest",
  210. MethodName: "get_feature",
  211. },
  212. "getFeatureFromMsgpack": {
  213. ServiceName: "sample",
  214. InterfaceName: "tsmsgpack",
  215. MethodName: "get_feature",
  216. },
  217. "getStatusFromGrpc": {
  218. ServiceName: "sample",
  219. InterfaceName: "tsrpc",
  220. MethodName: "getStatus",
  221. },
  222. "getStatusFromRest": {
  223. ServiceName: "sample",
  224. InterfaceName: "tsrest",
  225. MethodName: "getStatus",
  226. },
  227. "getStatusFromMsgpack": {
  228. ServiceName: "sample",
  229. InterfaceName: "tsmsgpack",
  230. MethodName: "getStatus",
  231. },
  232. "ComputeFromGrpc": {
  233. ServiceName: "sample",
  234. InterfaceName: "tsrpc",
  235. MethodName: "Compute",
  236. },
  237. "ComputeFromRest": {
  238. ServiceName: "sample",
  239. InterfaceName: "tsrest",
  240. MethodName: "Compute",
  241. },
  242. "ComputeFromMsgpack": {
  243. ServiceName: "sample",
  244. InterfaceName: "tsmsgpack",
  245. MethodName: "Compute",
  246. },
  247. "notUsedRpc": {
  248. ServiceName: "sample",
  249. InterfaceName: "tsrpc",
  250. MethodName: "RestEncodedJson",
  251. },
  252. "restEncodedJson": {
  253. ServiceName: "sample",
  254. InterfaceName: "tsrest",
  255. MethodName: "RestEncodedJson",
  256. },
  257. "notUsedMsgpack": {
  258. ServiceName: "sample",
  259. InterfaceName: "tsmsgpack",
  260. MethodName: "RestEncodedJson",
  261. },
  262. }
  263. actualService := &serviceInfo{}
  264. ok, err := m.serviceKV.Get(name, actualService)
  265. if err != nil {
  266. t.Error(err)
  267. t.FailNow()
  268. }
  269. if !ok {
  270. t.Errorf("service %s not found", name)
  271. t.FailNow()
  272. }
  273. if !reflect.DeepEqual(info, actualService) {
  274. t.Errorf("service info mismatch, expect %v but got %v", info, actualService)
  275. }
  276. actualKeys, _ := m.functionKV.Keys()
  277. if len(funcs) != len(actualKeys) {
  278. t.Errorf("functions info mismatch: expect %d funcs but got %v", len(funcs), actualKeys)
  279. }
  280. for f, c := range funcs {
  281. actualFunc := &functionContainer{}
  282. ok, err := m.functionKV.Get(f, actualFunc)
  283. if err != nil {
  284. t.Error(err)
  285. break
  286. }
  287. if !ok {
  288. t.Errorf("function %s not found", f)
  289. break
  290. }
  291. if !reflect.DeepEqual(c, actualFunc) {
  292. t.Errorf("func info mismatch, expect %v but got %v", c, actualFunc)
  293. }
  294. }
  295. }