gocustomurls/logger_test.go

137 lines
2.8 KiB
Go
Raw Normal View History

2024-06-24 05:55:08 +00:00
package main
import (
"fmt"
2024-06-24 17:40:04 +00:00
"os"
2024-06-24 05:55:08 +00:00
"testing"
2024-06-24 17:40:04 +00:00
"github.com/stretchr/testify/assert"
2024-06-24 05:55:08 +00:00
)
func TestTruncate(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/app_over_size.log"
2024-06-24 17:40:04 +00:00
tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir)
2024-06-24 05:55:08 +00:00
2024-06-24 17:40:04 +00:00
cpFileForTest(t, rulesJsonFp, tmpLf)
2024-06-24 05:55:08 +00:00
2024-06-24 17:40:04 +00:00
lf := &LogFile{
path: tmpLf,
2024-06-24 05:55:08 +00:00
}
2024-06-24 17:40:04 +00:00
lf.path = tmpLf
2024-06-24 05:55:08 +00:00
// Continue from here
2024-06-24 17:40:04 +00:00
isEmpty := isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, false)
err := lf.truncate()
assert.Equal(t, err, nil)
isEmpty = isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, true)
}
func TestMakeCopyTo(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/app_over_size.log"
tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir)
cpFileForTest(t, rulesJsonFp, tmpLf)
newLocation := fmt.Sprintf("%s/tmp/app.1.log", tmpDir)
lf := &LogFile{
path: tmpLf,
}
lf.path = tmpLf
isEmpty := isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, false)
err := lf.makeCopyTo(newLocation)
assert.Equal(t, err, nil)
isEmpty = isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, false)
ok := areFilesTheSame(t, lf.path, newLocation)
assert.Equal(t, ok, true)
}
func TestPrettyByteSize(t *testing.T) {
tests := map[string]struct {
input int
want string
}{
"KB": {input: 5675, want: "5.5KB"},
"MB": {input: 7060600, want: "6.7MB"},
"GB": {input: 8000000000, want: "7.5GB"},
"TB": {input: 1300007000000, want: "1.2TB"},
"EB": {input: 1300007000000000000, want: "1.1EB"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := prettyByteSize(int64(tc.input))
assert.Equal(t, got, tc.want)
})
}
}
func TestCompressFile(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/app_over_size.log"
tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir)
cpFileForTest(t, rulesJsonFp, tmpLf)
expected := fmt.Sprintf("%s/tmp/app.log.gz", tmpDir)
err := compressOldFile(tmpLf)
assert.Equal(t, err, nil)
exists := doesFileExist(tmpLf)
assert.Equal(t, exists, false)
exists = doesFileExist(expected)
assert.Equal(t, exists, true)
}
func TestRotate(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/app_over_size.log"
tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir)
cpFileForTest(t, rulesJsonFp, tmpLf)
lf := &LogFile{
path: tmpLf,
}
fd, _ := os.Open(tmpLf)
lf.handle = fd
isEmpty := isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, false)
err := lf.rotate(true)
assert.Equal(t, err, nil)
exists := doesFileExist(tmpLf)
assert.Equal(t, exists, true)
isEmpty = isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, true)
expected := walkMatch(t, tmpDir, "*.gz")
assert.NotEmpty(t, expected)
t.Logf("%+v", expected)
2024-06-24 05:55:08 +00:00
2024-06-24 17:40:04 +00:00
assert.NotEqual(t, lf.handle, fd)
2024-06-24 05:55:08 +00:00
}