manager_test.go 6.6 KB

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