-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathlogging_test.go
83 lines (57 loc) · 1.8 KB
/
logging_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
package embeddedpostgres
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type customLogger struct {
logLines []byte
}
func (cl *customLogger) Write(p []byte) (n int, err error) {
cl.logLines = append(cl.logLines, p...)
return len(p), nil
}
func Test_SyncedLogger_CreateError(t *testing.T) {
logger := customLogger{}
_, err := newSyncedLogger("/not-exists-anywhere", &logger)
assert.Error(t, err)
}
func Test_SyncedLogger_ErrorDuringFlush(t *testing.T) {
logger := customLogger{}
sl, slErr := newSyncedLogger("", &logger)
assert.NoError(t, slErr)
rmFileErr := os.Remove(sl.file.Name())
assert.NoError(t, rmFileErr)
err := sl.flush()
assert.Error(t, err)
}
func Test_SyncedLogger_NoErrorDuringFlush(t *testing.T) {
logger := customLogger{}
sl, slErr := newSyncedLogger("", &logger)
assert.NoError(t, slErr)
err := os.WriteFile(sl.file.Name(), []byte("some logs\non a new line"), os.ModeAppend)
assert.NoError(t, err)
err = sl.flush()
assert.NoError(t, err)
assert.Equal(t, "some logs\non a new line", string(logger.logLines))
}
func Test_readLogsOrTimeout(t *testing.T) {
logFile, err := ioutil.TempFile("", "prepare_database_test_log")
if err != nil {
panic(err)
}
logContent, err := readLogsOrTimeout(logFile)
assert.NoError(t, err)
assert.Equal(t, []byte(""), logContent)
_, _ = logFile.Write([]byte("and here are the logs!"))
logContent, err = readLogsOrTimeout(logFile)
assert.NoError(t, err)
assert.Equal(t, []byte("and here are the logs!"), logContent)
require.NoError(t, os.Remove(logFile.Name()))
logContent, err = readLogsOrTimeout(logFile)
assert.Equal(t, []byte("logs could not be read"), logContent)
assert.EqualError(t, err, fmt.Sprintf("open %s: no such file or directory", logFile.Name()))
}