file_stream_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 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. package file
  15. import (
  16. "os"
  17. "path/filepath"
  18. "reflect"
  19. "testing"
  20. "github.com/benbjohnson/clock"
  21. "github.com/lf-edge/ekuiper/internal/compressor"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/io/mock"
  24. "github.com/lf-edge/ekuiper/internal/topo/context"
  25. "github.com/lf-edge/ekuiper/internal/topo/transform"
  26. "github.com/lf-edge/ekuiper/pkg/api"
  27. "github.com/lf-edge/ekuiper/pkg/message"
  28. )
  29. func TestFileSinkCompress_Collect(t *testing.T) {
  30. tests := []struct {
  31. name string
  32. ft FileType
  33. fname string
  34. content []byte
  35. compress string
  36. }{
  37. {
  38. name: "lines",
  39. ft: LINES_TYPE,
  40. fname: "test_lines",
  41. content: []byte("{\"key\":\"value1\"}\n{\"key\":\"value2\"}"),
  42. },
  43. {
  44. name: "json",
  45. ft: JSON_TYPE,
  46. fname: "test_json",
  47. content: []byte(`[{"key":"value1"},{"key":"value2"}]`),
  48. },
  49. {
  50. name: "lines",
  51. ft: LINES_TYPE,
  52. fname: "test_lines",
  53. content: []byte("{\"key\":\"value1\"}\n{\"key\":\"value2\"}"),
  54. compress: GZIP,
  55. },
  56. {
  57. name: "json",
  58. ft: JSON_TYPE,
  59. fname: "test_json",
  60. content: []byte(`[{"key":"value1"},{"key":"value2"}]`),
  61. compress: GZIP,
  62. },
  63. {
  64. name: "lines",
  65. ft: LINES_TYPE,
  66. fname: "test_lines",
  67. content: []byte("{\"key\":\"value1\"}\n{\"key\":\"value2\"}"),
  68. compress: ZSTD,
  69. },
  70. {
  71. name: "json",
  72. ft: JSON_TYPE,
  73. fname: "test_json",
  74. content: []byte(`[{"key":"value1"},{"key":"value2"}]`),
  75. compress: ZSTD,
  76. },
  77. }
  78. // Create a stream context for testing
  79. contextLogger := conf.Log.WithField("rule", "test2")
  80. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  81. tf, _ := transform.GenTransform("", "json", "", "", "", []string{})
  82. vCtx := context.WithValue(ctx, context.TransKey, tf)
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. // Create a temporary file for testing
  86. tmpfile, err := os.CreateTemp("", tt.fname)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. defer os.Remove(tmpfile.Name())
  91. // Create a file sink with the temporary file path
  92. sink := &fileSink{}
  93. f := message.FormatJson
  94. if tt.ft == CSV_TYPE {
  95. f = message.FormatDelimited
  96. }
  97. err = sink.Configure(map[string]interface{}{
  98. "path": tmpfile.Name(),
  99. "fileType": tt.ft,
  100. "hasHeader": true,
  101. "format": f,
  102. "rollingNamePattern": "none",
  103. "compression": tt.compress,
  104. })
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. err = sink.Open(ctx)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. // Test collecting a map item
  113. m := map[string]interface{}{"key": "value1"}
  114. if err := sink.Collect(vCtx, m); err != nil {
  115. t.Errorf("unexpected error: %s", err)
  116. }
  117. // Test collecting another map item
  118. m = map[string]interface{}{"key": "value2"}
  119. if err := sink.Collect(ctx, m); err != nil {
  120. t.Errorf("unexpected error: %s", err)
  121. }
  122. if err = sink.Close(ctx); err != nil {
  123. t.Errorf("unexpected close error: %s", err)
  124. }
  125. contents, err := os.ReadFile(tmpfile.Name())
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. if tt.compress != "" {
  130. decompressor, _ := compressor.GetDecompressor(tt.compress)
  131. decompress, err := decompressor.Decompress(contents)
  132. if err != nil {
  133. t.Errorf("%v", err)
  134. }
  135. if !reflect.DeepEqual(decompress, tt.content) {
  136. t.Errorf("\nexpected\t %q \nbut got\t\t %q", tt.content, string(contents))
  137. }
  138. } else {
  139. if !reflect.DeepEqual(contents, tt.content) {
  140. t.Errorf("\nexpected\t %q \nbut got\t\t %q", tt.content, string(contents))
  141. }
  142. }
  143. // Read the contents of the temporary file and check if they match the collected items
  144. r := &FileSource{}
  145. dir := filepath.Dir(tmpfile.Name())
  146. filename := filepath.Base(tmpfile.Name())
  147. p := map[string]interface{}{
  148. "path": filepath.Join(dir),
  149. "decompression": tt.compress,
  150. "fileType": tt.ft,
  151. }
  152. err = r.Configure(filename, p)
  153. if err != nil {
  154. t.Errorf(err.Error())
  155. return
  156. }
  157. meta := map[string]interface{}{
  158. "file": filepath.Join(dir, filename),
  159. }
  160. mc := conf.Clock.(*clock.Mock)
  161. exp := []api.SourceTuple{
  162. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"key": "value1"}, meta, mc.Now()),
  163. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"key": "value2"}, meta, mc.Now()),
  164. }
  165. mock.TestSourceOpen(r, exp, t)
  166. })
  167. }
  168. }