manager_test.go 7.0 KB

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