sink_node_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. // Copyright 2022-2023 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. //go:build template || !core
  15. // +build template !core
  16. package node
  17. import (
  18. "errors"
  19. "fmt"
  20. "os"
  21. "path/filepath"
  22. "reflect"
  23. "testing"
  24. "time"
  25. "github.com/benbjohnson/clock"
  26. "github.com/lf-edge/ekuiper/internal/conf"
  27. "github.com/lf-edge/ekuiper/internal/schema"
  28. "github.com/lf-edge/ekuiper/internal/testx"
  29. "github.com/lf-edge/ekuiper/internal/topo/context"
  30. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  31. "github.com/lf-edge/ekuiper/internal/topo/transform"
  32. "github.com/lf-edge/ekuiper/internal/xsql"
  33. )
  34. func init() {
  35. testx.InitEnv()
  36. }
  37. func TestBatchSink(t *testing.T) {
  38. mc := conf.Clock.(*clock.Mock)
  39. conf.InitConf()
  40. transform.RegisterAdditionalFuncs()
  41. tests := []struct {
  42. config map[string]interface{}
  43. data []map[string]interface{}
  44. result [][]byte
  45. }{
  46. {
  47. config: map[string]interface{}{
  48. "batchSize": 2,
  49. },
  50. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}, {"ab": "hello3"}},
  51. result: [][]byte{[]byte(`[{"ab":"hello1"},{"ab":"hello2"}]`)},
  52. },
  53. {
  54. config: map[string]interface{}{
  55. "lingerInterval": 1000,
  56. },
  57. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}, {"ab": "hello3"}},
  58. result: [][]byte{[]byte(`[{"ab":"hello1"},{"ab":"hello2"},{"ab":"hello3"}]`)},
  59. },
  60. }
  61. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  62. contextLogger := conf.Log.WithField("rule", "TestBatchSink")
  63. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  64. for i, tt := range tests {
  65. mc.Set(mc.Now())
  66. mockSink := mocknode.NewMockSink()
  67. s := NewSinkNodeWithSink("mockSink", mockSink, tt.config)
  68. s.Open(ctx, make(chan error))
  69. s.input <- tt.data
  70. for i := 0; i < 10; i++ {
  71. mc.Add(1 * time.Second)
  72. time.Sleep(1 * time.Second)
  73. // wait until mockSink get results
  74. if len(mockSink.GetResults()) > 0 {
  75. break
  76. }
  77. }
  78. results := mockSink.GetResults()
  79. if !reflect.DeepEqual(tt.result, results) {
  80. t.Errorf("%d \tresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, results)
  81. }
  82. }
  83. }
  84. func TestSinkTemplate_Apply(t *testing.T) {
  85. conf.InitConf()
  86. transform.RegisterAdditionalFuncs()
  87. tests := []struct {
  88. config map[string]interface{}
  89. data []map[string]interface{}
  90. result [][]byte
  91. }{
  92. {
  93. config: map[string]interface{}{
  94. "sendSingle": true,
  95. "dataTemplate": `{"wrapper":"w1","content":{{toJson .}},"ab":"{{.ab}}"}`,
  96. },
  97. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  98. result: [][]byte{[]byte(`{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`), []byte(`{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`)},
  99. }, {
  100. config: map[string]interface{}{
  101. "dataTemplate": `{"wrapper":"arr","content":{{json .}},"content0":{{json (index . 0)}},ab0":"{{index . 0 "ab"}}"}`,
  102. },
  103. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  104. result: [][]byte{[]byte(`{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`)},
  105. }, {
  106. config: map[string]interface{}{
  107. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.ab}}</li>{{end}}</ul>`,
  108. },
  109. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  110. result: [][]byte{[]byte(`<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`)},
  111. }, {
  112. config: map[string]interface{}{
  113. "dataTemplate": `{"content":{{toJson .}}}`,
  114. },
  115. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  116. result: [][]byte{[]byte(`{"content":[{"ab":"hello1"},{"ab":"hello2"}]}`)},
  117. }, {
  118. config: map[string]interface{}{
  119. "sendSingle": true,
  120. "dataTemplate": `{"newab":"{{.ab}}"}`,
  121. },
  122. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  123. result: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
  124. }, {
  125. config: map[string]interface{}{
  126. "sendSingle": true,
  127. "dataTemplate": `{"newab":"{{.ab}}"}`,
  128. },
  129. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  130. result: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
  131. }, {
  132. config: map[string]interface{}{
  133. "sendSingle": true,
  134. "dataTemplate": `{"__meta":{{toJson .__meta}},"temp":{{.temperature}}}`,
  135. },
  136. data: []map[string]interface{}{{"temperature": 33, "humidity": 70, "__meta": xsql.Metadata{"messageid": 45, "other": "mock"}}},
  137. result: [][]byte{[]byte(`{"__meta":{"messageid":45,"other":"mock"},"temp":33}`)},
  138. }, {
  139. config: map[string]interface{}{
  140. "dataTemplate": `[{"__meta":{{toJson (index . 0 "__meta")}},"temp":{{index . 0 "temperature"}}}]`,
  141. },
  142. data: []map[string]interface{}{{"temperature": 33, "humidity": 70, "__meta": xsql.Metadata{"messageid": 45, "other": "mock"}}},
  143. result: [][]byte{[]byte(`[{"__meta":{"messageid":45,"other":"mock"},"temp":33}]`)},
  144. }, {
  145. config: map[string]interface{}{
  146. "dataTemplate": `[{{range $index, $ele := .}}{{if $index}},{{end}}{"result":{{add $ele.temperature $ele.humidity}}}{{end}}]`,
  147. },
  148. data: []map[string]interface{}{{"temperature": 33, "humidity": 70}, {"temperature": 22.0, "humidity": 50}, {"temperature": 11, "humidity": 90}},
  149. result: [][]byte{[]byte(`[{"result":103},{"result":72},{"result":101}]`)},
  150. }, {
  151. config: map[string]interface{}{
  152. "dataTemplate": `{{$counter := 0}}{{range $index, $ele := .}}{{if ne 90 $ele.humidity}}{{$counter = add $counter 1}}{{end}}{{end}}{"result":{{$counter}}}`,
  153. },
  154. data: []map[string]interface{}{{"temperature": 33, "humidity": 70}, {"temperature": 22.0, "humidity": 50}, {"temperature": 11, "humidity": 90}},
  155. result: [][]byte{[]byte(`{"result":2}`)},
  156. }, {
  157. config: map[string]interface{}{
  158. "dataTemplate": `{"a":"{{base64 .a}}","b":"{{base64 .b}}","c":"{{b64enc .c}}","d":"{{b64enc .d}}","e":"{{base64 .e}}"}`,
  159. "sendSingle": true,
  160. },
  161. data: []map[string]interface{}{{"a": 1, "b": 3.1415, "c": "hello", "d": "{\"hello\" : 3}", "e": map[string]interface{}{"humidity": 20, "temperature": 30}}},
  162. result: [][]byte{[]byte(`{"a":"MQ==","b":"My4xNDE1","c":"aGVsbG8=","d":"eyJoZWxsbyIgOiAzfQ==","e":"eyJodW1pZGl0eSI6MjAsInRlbXBlcmF0dXJlIjozMH0="}`)},
  163. },
  164. }
  165. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  166. contextLogger := conf.Log.WithField("rule", "TestSinkTemplate_Apply")
  167. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  168. for i, tt := range tests {
  169. mockSink := mocknode.NewMockSink()
  170. s := NewSinkNodeWithSink("mockSink", mockSink, tt.config)
  171. s.Open(ctx, make(chan error))
  172. s.input <- tt.data
  173. time.Sleep(1 * time.Second)
  174. results := mockSink.GetResults()
  175. if !reflect.DeepEqual(tt.result, results) {
  176. t.Errorf("%d \tresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, results)
  177. }
  178. }
  179. }
  180. func TestOmitEmpty_Apply(t *testing.T) {
  181. conf.InitConf()
  182. tests := []struct {
  183. config map[string]interface{}
  184. data []map[string]interface{}
  185. result [][]byte
  186. }{
  187. { // 0
  188. config: map[string]interface{}{
  189. "sendSingle": true,
  190. "omitIfEmpty": true,
  191. },
  192. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  193. result: [][]byte{[]byte(`{"ab":"hello1"}`), []byte(`{"ab":"hello2"}`)},
  194. }, { // 1
  195. config: map[string]interface{}{
  196. "sendSingle": false,
  197. "omitIfEmpty": true,
  198. },
  199. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  200. result: [][]byte{[]byte(`[{"ab":"hello1"},{"ab":"hello2"}]`)},
  201. }, { // 2
  202. config: map[string]interface{}{
  203. "sendSingle": false,
  204. "omitIfEmpty": false,
  205. },
  206. data: []map[string]interface{}{},
  207. result: [][]byte{[]byte(`[]`)},
  208. }, { // 3
  209. config: map[string]interface{}{
  210. "sendSingle": false,
  211. "omitIfEmpty": false,
  212. },
  213. data: nil,
  214. result: [][]byte{[]byte(`null`)},
  215. }, { // 4
  216. config: map[string]interface{}{
  217. "sendSingle": true,
  218. "omitIfEmpty": false,
  219. },
  220. data: []map[string]interface{}{},
  221. result: nil,
  222. }, { // 5
  223. config: map[string]interface{}{
  224. "sendSingle": false,
  225. "omitIfEmpty": true,
  226. },
  227. data: []map[string]interface{}{},
  228. result: nil,
  229. }, { // 6
  230. config: map[string]interface{}{
  231. "sendSingle": false,
  232. "omitIfEmpty": true,
  233. },
  234. data: nil,
  235. result: nil,
  236. }, { // 7
  237. config: map[string]interface{}{
  238. "sendSingle": true,
  239. "omitIfEmpty": false,
  240. },
  241. data: []map[string]interface{}{},
  242. result: nil,
  243. }, { // 8
  244. config: map[string]interface{}{
  245. "sendSingle": true,
  246. "omitIfEmpty": true,
  247. },
  248. data: []map[string]interface{}{{"ab": "hello1"}, {}},
  249. result: [][]byte{[]byte(`{"ab":"hello1"}`)},
  250. }, { // 9
  251. config: map[string]interface{}{
  252. "sendSingle": true,
  253. "omitIfEmpty": false,
  254. },
  255. data: []map[string]interface{}{{"ab": "hello1"}, {}},
  256. result: [][]byte{[]byte(`{"ab":"hello1"}`), []byte(`{}`)},
  257. },
  258. }
  259. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  260. contextLogger := conf.Log.WithField("rule", "TestOmitEmpty_Apply")
  261. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  262. for i, tt := range tests {
  263. mockSink := mocknode.NewMockSink()
  264. s := NewSinkNodeWithSink("mockSink", mockSink, tt.config)
  265. s.Open(ctx, make(chan error))
  266. s.input <- tt.data
  267. time.Sleep(100 * time.Millisecond)
  268. results := mockSink.GetResults()
  269. if !reflect.DeepEqual(tt.result, results) {
  270. t.Errorf("%d \tresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, results)
  271. }
  272. }
  273. }
  274. func TestFormat_Apply(t *testing.T) {
  275. conf.InitConf()
  276. etcDir, err := conf.GetDataLoc()
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. etcDir = filepath.Join(etcDir, "schemas", "protobuf")
  281. err = os.MkdirAll(etcDir, os.ModePerm)
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. // Copy init.proto
  286. bytesRead, err := os.ReadFile("../../schema/test/test1.proto")
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. err = os.WriteFile(filepath.Join(etcDir, "test1.proto"), bytesRead, 0o755)
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. defer func() {
  295. err = os.RemoveAll(etcDir)
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. }()
  300. err = schema.InitRegistry()
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. transform.RegisterAdditionalFuncs()
  305. tests := []struct {
  306. name string
  307. config map[string]interface{}
  308. data []map[string]interface{}
  309. result [][]byte
  310. }{
  311. {
  312. name: "test normal protobuf format",
  313. config: map[string]interface{}{
  314. "sendSingle": true,
  315. "format": `protobuf`,
  316. "schemaId": "test1.Person",
  317. },
  318. data: []map[string]interface{}{{
  319. "name": "test",
  320. "id": 1,
  321. "email": "Dddd",
  322. }},
  323. result: [][]byte{{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64}},
  324. }, {
  325. name: "test dateTemplate + protobuf format",
  326. config: map[string]interface{}{
  327. "sendSingle": true,
  328. "dataTemplate": `{"name":"test","email":"{{.ab}}","id":1}`,
  329. "format": `protobuf`,
  330. "schemaId": "test1.Person",
  331. },
  332. data: []map[string]interface{}{{"ab": "Dddd"}},
  333. result: [][]byte{{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64}},
  334. },
  335. }
  336. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  337. contextLogger := conf.Log.WithField("rule", "TestSinkFormat_Apply")
  338. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  339. for i, tt := range tests {
  340. t.Run(tt.name, func(t *testing.T) {
  341. mockSink := mocknode.NewMockSink()
  342. s := NewSinkNodeWithSink("mockSink", mockSink, tt.config)
  343. s.Open(ctx, make(chan error))
  344. s.input <- tt.data
  345. var results [][]byte
  346. time.Sleep(100 * time.Millisecond)
  347. results = mockSink.GetResults()
  348. if !reflect.DeepEqual(tt.result, results) {
  349. t.Errorf("%d \tresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.result, results)
  350. }
  351. })
  352. }
  353. }
  354. func TestConfig(t *testing.T) {
  355. tests := []struct {
  356. config map[string]interface{}
  357. sconf *SinkConf
  358. err error
  359. }{
  360. {
  361. config: map[string]interface{}{
  362. "sendSingle": true,
  363. },
  364. sconf: &SinkConf{
  365. Concurrency: 1,
  366. SendSingle: true,
  367. Format: "json",
  368. BufferLength: 1024,
  369. SinkConf: conf.SinkConf{
  370. MemoryCacheThreshold: 1024,
  371. MaxDiskCache: 1024000,
  372. BufferPageSize: 256,
  373. EnableCache: false,
  374. ResendInterval: 0,
  375. CleanCacheAtStop: false,
  376. },
  377. },
  378. }, {
  379. config: map[string]interface{}{
  380. "enableCache": true,
  381. "memoryCacheThreshold": 2,
  382. "bufferPageSize": 2,
  383. "sendSingle": true,
  384. "maxDiskCache": 6,
  385. "resendInterval": 10,
  386. },
  387. sconf: &SinkConf{
  388. Concurrency: 1,
  389. SendSingle: true,
  390. Format: "json",
  391. BufferLength: 1024,
  392. SinkConf: conf.SinkConf{
  393. MemoryCacheThreshold: 2,
  394. MaxDiskCache: 6,
  395. BufferPageSize: 2,
  396. EnableCache: true,
  397. ResendInterval: 10,
  398. CleanCacheAtStop: false,
  399. },
  400. },
  401. }, {
  402. config: map[string]interface{}{
  403. "enableCache": true,
  404. "memoryCacheThreshold": 2,
  405. "bufferPageSize": 2,
  406. "runAsync": true,
  407. "maxDiskCache": 6,
  408. "resendInterval": 10,
  409. },
  410. err: errors.New("cache is not supported for async sink, do not use enableCache and runAsync properties together"),
  411. }, {
  412. config: map[string]interface{}{
  413. "enableCache": true,
  414. "memoryCacheThreshold": 256,
  415. "bufferLength": 10,
  416. "maxDiskCache": 6,
  417. "resendInterval": 10,
  418. },
  419. err: errors.New("invalid cache properties: maxDiskCacheTooSmall:maxDiskCache must be greater than bufferPageSize"),
  420. }, {
  421. config: map[string]interface{}{
  422. "enableCache": true,
  423. "memoryCacheThreshold": 7,
  424. "bufferPageSize": 3,
  425. "sendSingle": true,
  426. "maxDiskCache": 21,
  427. "resendInterval": 10,
  428. },
  429. err: errors.New("invalid cache properties: memoryCacheThresholdNotMultiple:memoryCacheThreshold must be a multiple of bufferPageSize"),
  430. }, {
  431. config: map[string]interface{}{
  432. "enableCache": true,
  433. "memoryCacheThreshold": 9,
  434. "bufferPageSize": 3,
  435. "sendSingle": true,
  436. "maxDiskCache": 22,
  437. "resendInterval": 10,
  438. },
  439. err: errors.New("invalid cache properties: maxDiskCacheNotMultiple:maxDiskCache must be a multiple of bufferPageSize"),
  440. },
  441. }
  442. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  443. contextLogger := conf.Log.WithField("rule", "TestConfig")
  444. conf.InitConf()
  445. for i, tt := range tests {
  446. mockSink := NewSinkNode(fmt.Sprintf("test_%d", i), "mockSink", tt.config)
  447. sconf, err := mockSink.parseConf(contextLogger)
  448. if !reflect.DeepEqual(tt.err, err) {
  449. t.Errorf("%d \terror mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.err, err)
  450. } else if !reflect.DeepEqual(tt.sconf, sconf) {
  451. t.Errorf("%d \tresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.sconf, sconf)
  452. }
  453. }
  454. }
  455. func TestSinkNode_reset(t *testing.T) {
  456. mockSink := mocknode.NewMockSink()
  457. s := NewSinkNodeWithSink("mockSink", mockSink, nil)
  458. s.reset()
  459. if s.statManagers != nil {
  460. t.Errorf("reset() failed")
  461. }
  462. }
  463. func Test_getSink(t *testing.T) {
  464. _, err := getSink("mock", map[string]interface{}{"sendSingle": true, "omitIfEmpty": true})
  465. if err == nil {
  466. t.Errorf("getSink() failed")
  467. }
  468. }
  469. func Test_itemToMap(t *testing.T) {
  470. type args struct {
  471. item interface{}
  472. }
  473. tests := []struct {
  474. name string
  475. args args
  476. want []map[string]interface{}
  477. }{
  478. {
  479. name: "test1",
  480. args: args{
  481. item: errors.New("test"),
  482. },
  483. want: []map[string]interface{}{
  484. {"error": "test"},
  485. },
  486. },
  487. {
  488. name: "test2",
  489. args: args{
  490. item: "test2",
  491. },
  492. want: []map[string]interface{}{
  493. {"error": fmt.Sprintf("result is not a map slice but found %#v", "test2")},
  494. },
  495. },
  496. {
  497. name: "test3",
  498. args: args{
  499. item: xsql.Row(&xsql.Tuple{Emitter: "a", Message: map[string]interface{}{"a": 1, "b": "2"}, Timestamp: conf.GetNowInMilli(), Metadata: nil}),
  500. },
  501. want: []map[string]interface{}{
  502. {"a": 1, "b": "2"},
  503. },
  504. },
  505. {
  506. name: "test4",
  507. args: args{
  508. item: xsql.Collection(&xsql.WindowTuples{Content: []xsql.TupleRow{
  509. &xsql.Tuple{Emitter: "a", Message: map[string]interface{}{"a": 1, "b": "2"}, Timestamp: conf.GetNowInMilli(), Metadata: nil},
  510. }}),
  511. },
  512. want: []map[string]interface{}{
  513. {"a": 1, "b": "2"},
  514. },
  515. },
  516. }
  517. for _, tt := range tests {
  518. t.Run(tt.name, func(t *testing.T) {
  519. if got := itemToMap(tt.args.item); !reflect.DeepEqual(got, tt.want) {
  520. t.Errorf("itemToMap() = %v, want %v", got, tt.want)
  521. }
  522. })
  523. }
  524. }
  525. func TestSinkFields_Apply(t *testing.T) {
  526. conf.InitConf()
  527. transform.RegisterAdditionalFuncs()
  528. tests := []struct {
  529. dt string
  530. format string
  531. schemaId string
  532. delimiter string
  533. dataField string
  534. fields []string
  535. data interface{}
  536. result [][]byte
  537. }{
  538. {
  539. format: "json",
  540. fields: []string{"a", "b"},
  541. data: map[string]interface{}{"a": "1", "b": "2", "c": "3"},
  542. result: [][]byte{[]byte(`{"a":"1","b":"2"}`)},
  543. },
  544. {
  545. format: "json",
  546. fields: []string{"a", "b"},
  547. data: []map[string]interface{}{{"a": "1", "b": "2", "c": "3"}},
  548. result: [][]byte{[]byte(`[{"a":"1","b":"2"}]`)},
  549. },
  550. {
  551. format: "delimited",
  552. delimiter: ",",
  553. fields: []string{"a", "b"},
  554. data: map[string]interface{}{"a": "1", "b": "2", "c": "3"},
  555. result: [][]byte{[]byte(`1,2`)},
  556. },
  557. {
  558. format: "delimited",
  559. delimiter: ",",
  560. fields: []string{"b", "c", "a"},
  561. data: map[string]interface{}{"a": "1", "b": "2", "c": "3"},
  562. result: [][]byte{[]byte(`2,3,1`)},
  563. },
  564. {
  565. format: "json",
  566. schemaId: "",
  567. fields: []string{"ax", "bx"},
  568. dt: `{"ax": {{.a}}, "bx": {{.b}}}`,
  569. data: map[string]interface{}{"a": "1", "b": "2", "c": "3"},
  570. result: [][]byte{[]byte(`{"ax":1,"bx":2}`)},
  571. },
  572. {
  573. format: "json",
  574. schemaId: "",
  575. fields: []string{"a", "b"},
  576. dt: `{"ax": {{.a}}, "bx": {{.b}}}`,
  577. data: map[string]interface{}{"a": "1", "b": "2", "c": "3"},
  578. result: [][]byte{[]byte(`{"a":null,"b":null}`)},
  579. },
  580. {
  581. format: "json",
  582. dataField: "device",
  583. fields: []string{"a", "b"},
  584. data: map[string]interface{}{"device": map[string]interface{}{"a": "1", "b": "2", "c": "3"}, "a": 11, "b": 22, "c": 33},
  585. result: [][]byte{[]byte(`{"a":"1","b":"2"}`)},
  586. },
  587. {
  588. format: "delimited",
  589. delimiter: ",",
  590. fields: []string{"a", "b"},
  591. dataField: "device",
  592. data: map[string]interface{}{"device": map[string]interface{}{"a": "1", "b": "2", "c": "3"}, "a": 11, "b": 22, "c": 33},
  593. result: [][]byte{[]byte(`1,2`)},
  594. },
  595. {
  596. format: "json",
  597. schemaId: "",
  598. fields: []string{"a", "b"},
  599. dt: `{"device": {"a": {{.a}}}}`,
  600. dataField: "device",
  601. data: map[string]interface{}{"a": "1", "b": "2", "c": "3"},
  602. result: [][]byte{[]byte(`{"a":1,"b":null}`)},
  603. },
  604. }
  605. contextLogger := conf.Log.WithField("rule", "TestSinkFields_Apply")
  606. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  607. for i, tt := range tests {
  608. tf, _ := transform.GenTransform(tt.dt, tt.format, tt.schemaId, tt.delimiter, tt.dataField, tt.fields)
  609. vCtx := context.WithValue(ctx, context.TransKey, tf)
  610. mockSink := mocknode.NewMockSink()
  611. mockSink.Collect(vCtx, tt.data)
  612. time.Sleep(1 * time.Second)
  613. results := mockSink.GetResults()
  614. if !reflect.DeepEqual(tt.result, results) {
  615. t.Errorf("%d \tresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, results)
  616. }
  617. }
  618. }