-
Notifications
You must be signed in to change notification settings - Fork 0
/
logmanager_test.go
158 lines (125 loc) · 3.24 KB
/
logmanager_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
155
156
157
158
package logwhale
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type LogManagerTestSuite struct {
suite.Suite
lm *LogManager
ctx context.Context
}
func (s *LogManagerTestSuite) SetupTest() {
s.ctx = context.Background()
var err error
s.lm, err = NewLogManager(s.ctx)
s.NoError(err)
}
func (s *LogManagerTestSuite) TearDownTest() {
s.lm.Close()
}
func (s *LogManagerTestSuite) TestAddRemoveFile() {
s.NoError(s.lm.ctx.Err())
of, err := os.CreateTemp(os.TempDir(), "test_log")
defer of.Close()
s.NoError(err)
_, _, err = s.lm.AddLogFile(of.Name())
s.NoError(err)
_, exists := s.lm.logsWatched[of.Name()]
s.True(exists)
err = s.lm.RemoveLogFile(of.Name())
s.NoError(err)
}
func (s *LogManagerTestSuite) TestErrorOnCreateLogManager() {
s.ctx = nil
_, err := NewLogManager(s.ctx)
s.Error(err)
}
func (s *LogManagerTestSuite) TestErrorOnAddLogFile() {
dirname, err := os.MkdirTemp(os.TempDir(), "testdir")
s.NoError(err)
dataChan, errorChan, err := s.lm.AddLogFile(dirname)
s.Nil(dataChan)
s.Nil(errorChan)
s.Error(err)
err = os.Remove(dirname)
s.NoError(err)
}
func (s *LogManagerTestSuite) TestCleanup() {
of, err := os.CreateTemp(os.TempDir(), "test_log")
defer of.Close()
s.NoError(err)
_, _, err = s.lm.AddLogFile(of.Name())
s.NoError(err)
_, err = of.WriteString("test line\n")
s.NoError(err)
s.NoError(s.lm.Close())
s.True(s.lm.closed)
s.Equal(0, len(s.lm.logsWatched)) // Not strictly necessary but we should clean up the logsWatched map
s.Equal(0, len(s.lm.pathsWatched))
}
func (s *LogManagerTestSuite) TestRunAndClose() {
of, err := os.CreateTemp(os.TempDir(), "test_log")
defer of.Close()
s.NoError(err)
dataChan, errorChan, err := s.lm.AddLogFile(of.Name())
s.NoError(err)
_, err = of.WriteString("test line\n")
s.NoError(err)
time.Sleep(100 * time.Millisecond)
select {
case data := <-dataChan:
s.Equal("test line", string(data))
case err := <-errorChan:
s.Fail("unexpected error: %v", err)
default:
s.Fail("expected data, got none")
}
}
func (s *LogManagerTestSuite) TestRemoveLogFile() {
of, err := os.CreateTemp(os.TempDir(), "test_log")
defer of.Close()
s.NoError(err)
dataChan, errorChan, err := s.lm.AddLogFile(of.Name())
s.NoError(err)
_, err = of.WriteString("test line\n")
s.NoError(err)
time.Sleep(100 * time.Millisecond)
select {
case data := <-dataChan:
s.Equal("test line", string(data))
case err := <-errorChan:
s.Fail("unexpected error: %v", err)
default:
s.Fail("expected data, got none")
}
err = of.Close()
s.NoError(err)
err = os.Remove(of.Name())
s.NoError(err)
time.Sleep(100 * time.Millisecond)
select {
case err := <-errorChan:
s.Equal(fmt.Sprintf("state: File removed msg: file removed: %s", of.Name()), err.Error())
default:
s.Fail("expected error, got none")
}
}
func (s *LogManagerTestSuite) TestFileNotExist() {
fp := os.TempDir() + "doesnotexist"
_, errorChan, err := s.lm.AddLogFile(fp)
s.NoError(err)
time.Sleep(100 * time.Millisecond)
select {
case err := <-errorChan:
s.Equal(fmt.Sprintf("state: File does not exist msg: file does not exist: %s", fp), err.Error())
default:
s.Fail("expected error, got none")
}
}
func TestLogManagerTestSuite(t *testing.T) {
suite.Run(t, new(LogManagerTestSuite))
}