file_source_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. package file
  15. import (
  16. "fmt"
  17. "github.com/benbjohnson/clock"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/io/mock"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "io"
  22. "os"
  23. "path/filepath"
  24. "testing"
  25. "time"
  26. )
  27. func TestJsonFile(t *testing.T) {
  28. path, err := os.Getwd()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. meta := map[string]interface{}{
  33. "file": filepath.Join(path, "test", "test.json"),
  34. }
  35. mc := conf.Clock.(*clock.Mock)
  36. exp := []api.SourceTuple{
  37. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(1), "name": "John Doe"}, meta, mc.Now()),
  38. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(2), "name": "Jane Doe"}, meta, mc.Now()),
  39. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(3), "name": "John Smith"}, meta, mc.Now()),
  40. }
  41. p := map[string]interface{}{
  42. "path": filepath.Join(path, "test"),
  43. }
  44. r := &FileSource{}
  45. err = r.Configure("test.json", p)
  46. if err != nil {
  47. t.Errorf(err.Error())
  48. return
  49. }
  50. mock.TestSourceOpen(r, exp, t)
  51. }
  52. func TestJsonFolder(t *testing.T) {
  53. path, err := os.Getwd()
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. mc := conf.Clock.(*clock.Mock)
  58. moveToFolder := filepath.Join(path, "test", "moveTo")
  59. exp := []api.SourceTuple{
  60. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(1), "name": "John Doe", "height": 1.82}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f1.json")}, mc.Now()),
  61. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(2), "name": "Jane Doe", "height": 1.65}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f1.json")}, mc.Now()),
  62. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(3), "name": "Will Doe", "height": 1.76}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f2.json")}, mc.Now()),
  63. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(4), "name": "Dude Doe", "height": 1.92}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f3.json")}, mc.Now()),
  64. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(5), "name": "Jane Doe", "height": 1.72}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f3.json")}, mc.Now()),
  65. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(6), "name": "John Smith", "height": 2.22}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f3.json")}, mc.Now()),
  66. }
  67. p := map[string]interface{}{
  68. "path": filepath.Join(path, "test"),
  69. "actionAfterRead": 2,
  70. "moveTo": moveToFolder,
  71. }
  72. r := &FileSource{}
  73. err = r.Configure("json", p)
  74. if err != nil {
  75. t.Errorf(err.Error())
  76. return
  77. }
  78. mock.TestSourceOpen(r, exp, t)
  79. // wait for the move to finish
  80. time.Sleep(100 * time.Millisecond)
  81. files, err := os.ReadDir(moveToFolder)
  82. if err != nil {
  83. t.Error(err)
  84. }
  85. if len(files) != 3 {
  86. t.Errorf("expect 3 files in moveTo folder, but got %d", len(files))
  87. }
  88. for _, f := range files {
  89. os.Rename(filepath.Join(moveToFolder, f.Name()), filepath.Join(path, "test", "json", f.Name()))
  90. }
  91. }
  92. func TestCSVFolder(t *testing.T) {
  93. // Move test files to temp folder
  94. path, err := os.Getwd()
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. testFolder := filepath.Join(path, "test", "csvTemp")
  99. err = os.MkdirAll(testFolder, 0755)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. files, err := os.ReadDir(filepath.Join(path, "test", "csv"))
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. for _, f := range files {
  108. err = copy(filepath.Join(path, "test", "csv", f.Name()), filepath.Join(testFolder, f.Name()))
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. }
  113. mc := conf.Clock.(*clock.Mock)
  114. // Start testing
  115. exp := []api.SourceTuple{
  116. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"@": "#", "id": "1", "ts": "1670170500", "value": "161.927872"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "a.csv")}, mc.Now()),
  117. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"@": "#", "id": "2", "ts": "1670170900", "value": "176"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "a.csv")}, mc.Now()),
  118. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": "33", "ts": "1670270500", "humidity": "89"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "b.csv")}, mc.Now()),
  119. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": "44", "ts": "1670270900", "humidity": "76"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "b.csv")}, mc.Now()),
  120. }
  121. p := map[string]interface{}{
  122. "fileType": "csv",
  123. "path": filepath.Join(path, "test"),
  124. "actionAfterRead": 1,
  125. "hasHeader": true,
  126. "delimiter": "\t",
  127. "ignoreStartLines": 3,
  128. "ignoreEndLines": 1,
  129. }
  130. r := &FileSource{}
  131. err = r.Configure("csvTemp", p)
  132. if err != nil {
  133. t.Errorf(err.Error())
  134. return
  135. }
  136. mock.TestSourceOpen(r, exp, t)
  137. // wait for file deleted takes effect
  138. time.Sleep(100 * time.Millisecond)
  139. files, err = os.ReadDir(testFolder)
  140. if err != nil {
  141. t.Error(err)
  142. }
  143. if len(files) != 0 {
  144. t.Errorf("expect 0 files in csvTemp folder, but got %d", len(files))
  145. }
  146. }
  147. func copy(src, dst string) error {
  148. sourceFileStat, err := os.Stat(src)
  149. if err != nil {
  150. return err
  151. }
  152. if !sourceFileStat.Mode().IsRegular() {
  153. return fmt.Errorf("%s is not a regular file", src)
  154. }
  155. source, err := os.Open(src)
  156. if err != nil {
  157. return err
  158. }
  159. defer source.Close()
  160. destination, err := os.Create(dst)
  161. if err != nil {
  162. return err
  163. }
  164. defer destination.Close()
  165. _, err = io.Copy(destination, source)
  166. return err
  167. }
  168. func TestCSVFile(t *testing.T) {
  169. path, err := os.Getwd()
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. mc := conf.Clock.(*clock.Mock)
  174. exp := []api.SourceTuple{
  175. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ns": "@", "id": "id", "ts": "ts", "number": "value"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}, mc.Now()),
  176. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ns": "#", "id": "1", "ts": "1670170500", "number": "161.927872"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}, mc.Now()),
  177. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ns": "#", "id": "2", "ts": "1670170900", "number": "176"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}, mc.Now()),
  178. }
  179. p := map[string]interface{}{
  180. "fileType": "csv",
  181. "path": filepath.Join(path, "test", "csv"),
  182. "delimiter": "\t",
  183. "ignoreStartLines": 3,
  184. "ignoreEndLines": 1,
  185. "columns": []string{"ns", "id", "ts", "number"},
  186. }
  187. r := &FileSource{}
  188. err = r.Configure("a.csv", p)
  189. if err != nil {
  190. t.Errorf(err.Error())
  191. return
  192. }
  193. mock.TestSourceOpen(r, exp, t)
  194. }
  195. func TestJsonLines(t *testing.T) {
  196. path, err := os.Getwd()
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. meta := map[string]interface{}{
  201. "file": filepath.Join(path, "test", "test.lines"),
  202. }
  203. mc := conf.Clock.(*clock.Mock)
  204. exp := []api.SourceTuple{
  205. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(1), "name": "John Doe"}, meta, mc.Now()),
  206. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(2), "name": "Jane Doe"}, meta, mc.Now()),
  207. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"id": float64(3), "name": "John Smith"}, meta, mc.Now()),
  208. }
  209. p := map[string]interface{}{
  210. "path": filepath.Join(path, "test"),
  211. "fileType": "lines",
  212. }
  213. r := &FileSource{}
  214. err = r.Configure("test.lines", p)
  215. if err != nil {
  216. t.Errorf(err.Error())
  217. return
  218. }
  219. mock.TestSourceOpen(r, exp, t)
  220. }