package main import ( "fmt" "os" "testing" "github.com/stretchr/testify/assert" ) func TestTruncate(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, } lf.path = tmpLf // Continue from here 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, canCompress: true, } fd, _ := os.Open(tmpLf) lf.handle = fd isEmpty := isFileEmpty(t, tmpLf) assert.Equal(t, isEmpty, false) err := lf.rotate() 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) assert.NotEqual(t, lf.handle, fd) } func TestNewLogger(t *testing.T) { t.Run("load logging file - do not rotate", func(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) }) t.Run("load logging file - rotate", func(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) }) t.Run("create new logging file", func(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) }) } } }