forked from 0xERR0R/blocky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_writer_test.go
154 lines (127 loc) · 3.38 KB
/
file_writer_test.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package querylog
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"io"
"io/fs"
"os"
"time"
"github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("FileWriter", func() {
var (
tmpDir *helpertest.TmpFolder
err error
writer *FileWriter
)
JustBeforeEach(func() {
tmpDir = helpertest.NewTmpFolder("fileWriter")
})
Describe("CSV writer", func() {
When("target dir does not exist", func() {
It("should return error", func() {
_, err = NewCSVWriter("wrongdir", false, 0)
Expect(err).Should(HaveOccurred())
})
})
When("New log entry was created", func() {
It("should be logged in one file", func() {
writer, err = NewCSVWriter(tmpDir.Path, false, 0)
Expect(err).Should(Succeed())
By("entry for client 1", func() {
writer.Write(&LogEntry{
ClientNames: []string{"client1"},
Start: time.Now(),
DurationMs: 20,
})
})
By("entry for client 2", func() {
writer.Write(&LogEntry{
ClientNames: []string{"client2"},
Start: time.Now(),
DurationMs: 20,
})
})
Eventually(func(g Gomega) int {
return len(readCsv(tmpDir.JoinPath(
fmt.Sprintf("%s_ALL.log", time.Now().Format("2006-01-02")))))
}).Should(Equal(2))
})
It("should be logged in separate files per client", func() {
writer, err = NewCSVWriter(tmpDir.Path, true, 0)
Expect(err).Should(Succeed())
By("entry for client 1", func() {
writer.Write(&LogEntry{
ClientNames: []string{"client1"},
Start: time.Now(),
DurationMs: 20,
})
})
By("entry for client 2", func() {
writer.Write(&LogEntry{
ClientNames: []string{"client2"},
Start: time.Now(),
DurationMs: 20,
})
})
Eventually(func(g Gomega) int {
return len(readCsv(tmpDir.JoinPath(
fmt.Sprintf("%s_client1.log", time.Now().Format("2006-01-02")))))
}).Should(Equal(1))
Eventually(func(g Gomega) int {
return len(readCsv(tmpDir.JoinPath(
fmt.Sprintf("%s_client2.log", time.Now().Format("2006-01-02")))))
}).Should(Equal(1))
})
})
When("Cleanup is called", func() {
It("should delete old files", func() {
writer, err = NewCSVWriter(tmpDir.Path, false, 1)
Expect(err).Should(Succeed())
By("entry today", func() {
writer.Write(&LogEntry{
ClientNames: []string{"client1"},
Start: time.Now(),
DurationMs: 20,
})
})
By("entry 2 days ago", func() {
writer.Write(&LogEntry{
ClientNames: []string{"client1"},
Start: time.Now().AddDate(0, 0, -3),
DurationMs: 20,
})
})
Eventually(func(g Gomega) ([]fs.DirEntry, error) {
return os.ReadDir(tmpDir.Path)
}, "20s", "1s").Should(HaveLen(2))
writer.CleanUp()
Eventually(func(g Gomega) ([]fs.DirEntry, error) {
return os.ReadDir(tmpDir.Path)
}, "20s", "1s").Should(HaveLen(1))
})
})
})
})
func readCsv(file string) [][]string {
var result [][]string
csvFile, err := os.Open(file)
Expect(err).Should(Succeed())
reader := csv.NewReader(bufio.NewReader(csvFile))
reader.Comma = '\t'
for {
line, err := reader.Read()
if errors.Is(err, io.EOF) {
break
} else if err != nil {
log.Log().Fatal("can't read line", err)
}
result = append(result, line)
}
return result
}