-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathfile_base.go
87 lines (76 loc) · 1.91 KB
/
file_base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package test
import (
"bufio"
"os"
"path/filepath"
"testing"
"time"
"github.com/ozontech/file.d/pipeline"
"github.com/stretchr/testify/assert"
)
// The file_base contains helpers function for testing file base output plugins
// output/file and output/s3
type Msg []byte
func SendPack(t *testing.T, p *pipeline.Pipeline, msgs []Msg) int64 {
t.Helper()
var sent int64 = 0
for _, m := range msgs {
_ = p.In(0, "test", 0, m, false)
// count \n
sent += int64(len(m)) + 1
}
return sent
}
func ClearDir(t *testing.T, dir string) {
t.Helper()
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("could not delete dirs and files after tests, error: %s", err.Error())
}
}
func GetMatches(t *testing.T, pattern string) []string {
t.Helper()
matches, err := filepath.Glob(pattern)
assert.NoError(t, err)
return matches
}
func CheckZero(t *testing.T, target string, msg string) int64 {
t.Helper()
info, err := os.Stat(target)
assert.NoErrorf(t, err, "there is no target: %s", target, msg)
assert.NotNil(t, info, msg)
assert.Zero(t, info.Size(), msg)
return info.Size()
}
func CheckNotZero(t *testing.T, target string, msg string) int64 {
t.Helper()
info, err := os.Stat(target)
assert.NoError(t, err, msg)
assert.NotNil(t, info, msg)
if info.Size() == 0 {
assert.NotZero(t, info.Size(), msg)
}
return info.Size()
}
func CountLines(t *testing.T, pattern string) int {
matches, err := filepath.Glob(pattern)
assert.NoError(t, err)
lineCount := 0
for _, match := range matches {
file, err := os.Open(match)
assert.NoError(t, err, "can't open file")
fileScanner := bufio.NewScanner(file)
for fileScanner.Scan() {
lineCount++
}
}
return lineCount
}
func WaitProcessEvents(t *testing.T, count int, checkInterval, maxTime time.Duration, pattern string) {
tf := time.Now().Add(maxTime)
for tf.After(time.Now()) {
if count == CountLines(t, pattern) {
return
}
time.Sleep(checkInterval)
}
}