2017-11-21 04:37:06 -06:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-14 07:43:25 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-30 02:32:25 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-11-21 04:37:06 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func (w *FileLogWriter) WriteLine(line string) error {
|
2022-01-06 08:28:05 -06:00
|
|
|
_, err := w.Write([]byte(line))
|
|
|
|
return err
|
2017-11-21 04:37:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogFile(t *testing.T) {
|
2020-08-14 07:43:25 -05:00
|
|
|
fileLogWrite := NewFileWriter()
|
|
|
|
require.NotNil(t, fileLogWrite)
|
2017-11-21 04:37:06 -06:00
|
|
|
|
2020-08-14 07:43:25 -05:00
|
|
|
t.Cleanup(func() {
|
2020-12-15 02:32:06 -06:00
|
|
|
err := fileLogWrite.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(fileLogWrite.Filename)
|
|
|
|
assert.NoError(t, err)
|
2020-08-14 07:43:25 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
fileLogWrite.Filename = "grafana_test.log"
|
|
|
|
err := fileLogWrite.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-17 10:09:14 -06:00
|
|
|
assert.Zero(t, fileLogWrite.maxlinesCurlines)
|
2020-08-14 07:43:25 -05:00
|
|
|
|
|
|
|
t.Run("adding lines", func(t *testing.T) {
|
|
|
|
err := fileLogWrite.WriteLine("test1\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = fileLogWrite.WriteLine("test2\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = fileLogWrite.WriteLine("test3\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-17 10:09:14 -06:00
|
|
|
assert.Equal(t, 3, fileLogWrite.maxlinesCurlines)
|
2017-11-21 04:37:06 -06:00
|
|
|
})
|
|
|
|
}
|