github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/file_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package log
     7  
     8  import (
     9  	"compress/gzip"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/gitbundle/modules/util"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestFileLoggerFails(t *testing.T) {
    24  	tmpDir, err := os.MkdirTemp("", "TestFileLogger")
    25  	assert.NoError(t, err)
    26  	defer util.RemoveAll(tmpDir)
    27  
    28  	prefix := "TestPrefix "
    29  	level := INFO
    30  	flags := LstdFlags | LUTC | Lfuncname
    31  	// filename := filepath.Join(tmpDir, "test.log")
    32  
    33  	fileLogger := NewFileLogger()
    34  	// realFileLogger, ok := fileLogger.(*FileLogger)
    35  	// assert.True(t, ok)
    36  
    37  	// Fail if there is bad json
    38  	err = fileLogger.Init("{")
    39  	assert.Error(t, err)
    40  
    41  	// Fail if there is no filename
    42  	err = fileLogger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"filename\":\"%s\"}", prefix, level.String(), flags, ""))
    43  	assert.Error(t, err)
    44  
    45  	// Fail if the file isn't a filename
    46  	err = fileLogger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"filename\":\"%s\"}", prefix, level.String(), flags, filepath.ToSlash(tmpDir)))
    47  	assert.Error(t, err)
    48  }
    49  
    50  func TestFileLogger(t *testing.T) {
    51  	tmpDir, err := os.MkdirTemp("", "TestFileLogger")
    52  	assert.NoError(t, err)
    53  	defer util.RemoveAll(tmpDir)
    54  
    55  	prefix := "TestPrefix "
    56  	level := INFO
    57  	flags := LstdFlags | LUTC | Lfuncname
    58  	filename := filepath.Join(tmpDir, "test.log")
    59  
    60  	fileLogger := NewFileLogger()
    61  	realFileLogger, ok := fileLogger.(*FileLogger)
    62  	assert.Equal(t, true, ok)
    63  
    64  	location, _ := time.LoadLocation("EST")
    65  
    66  	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
    67  
    68  	dateString := date.UTC().Format("2006/01/02 15:04:05")
    69  
    70  	event := Event{
    71  		level:    INFO,
    72  		msg:      "TEST MSG",
    73  		caller:   "CALLER",
    74  		filename: "FULL/FILENAME",
    75  		line:     1,
    76  		time:     date,
    77  	}
    78  
    79  	expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    80  
    81  	fileLogger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"filename\":\"%s\",\"maxsize\":%d,\"compress\":false}", prefix, level.String(), flags, filepath.ToSlash(filename), len(expected)*2))
    82  
    83  	assert.Equal(t, flags, realFileLogger.Flags)
    84  	assert.Equal(t, level, realFileLogger.Level)
    85  	assert.Equal(t, level, fileLogger.GetLevel())
    86  
    87  	fileLogger.LogEvent(&event)
    88  	fileLogger.Flush()
    89  	logData, err := os.ReadFile(filename)
    90  	assert.NoError(t, err)
    91  	assert.Equal(t, expected, string(logData))
    92  
    93  	event.level = DEBUG
    94  	fileLogger.LogEvent(&event)
    95  	fileLogger.Flush()
    96  	logData, err = os.ReadFile(filename)
    97  	assert.NoError(t, err)
    98  	assert.Equal(t, expected, string(logData))
    99  
   100  	event.level = TRACE
   101  	fileLogger.LogEvent(&event)
   102  	fileLogger.Flush()
   103  	logData, err = os.ReadFile(filename)
   104  	assert.NoError(t, err)
   105  	assert.Equal(t, expected, string(logData))
   106  
   107  	event.level = WARN
   108  	expected += fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   109  	fileLogger.LogEvent(&event)
   110  	fileLogger.Flush()
   111  	logData, err = os.ReadFile(filename)
   112  	assert.NoError(t, err)
   113  	assert.Equal(t, expected, string(logData))
   114  
   115  	// Should rotate
   116  	fileLogger.LogEvent(&event)
   117  	fileLogger.Flush()
   118  	logData, err = os.ReadFile(filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1))
   119  	assert.NoError(t, err)
   120  	assert.Equal(t, expected, string(logData))
   121  
   122  	logData, err = os.ReadFile(filename)
   123  	assert.NoError(t, err)
   124  	expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   125  	assert.Equal(t, expected, string(logData))
   126  
   127  	for num := 2; num <= 999; num++ {
   128  		file, err := os.OpenFile(filename+fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num), os.O_RDONLY|os.O_CREATE, 0o666)
   129  		assert.NoError(t, err)
   130  		file.Close()
   131  	}
   132  	err = realFileLogger.DoRotate()
   133  	assert.Error(t, err)
   134  
   135  	expected += fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   136  	fileLogger.LogEvent(&event)
   137  	fileLogger.Flush()
   138  	logData, err = os.ReadFile(filename)
   139  	assert.NoError(t, err)
   140  	assert.Equal(t, expected, string(logData))
   141  
   142  	// Should fail to rotate
   143  	expected += fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   144  	fileLogger.LogEvent(&event)
   145  	fileLogger.Flush()
   146  	logData, err = os.ReadFile(filename)
   147  	assert.NoError(t, err)
   148  	assert.Equal(t, expected, string(logData))
   149  
   150  	fileLogger.Close()
   151  }
   152  
   153  func TestCompressFileLogger(t *testing.T) {
   154  	tmpDir, err := os.MkdirTemp("", "TestFileLogger")
   155  	assert.NoError(t, err)
   156  	defer util.RemoveAll(tmpDir)
   157  
   158  	prefix := "TestPrefix "
   159  	level := INFO
   160  	flags := LstdFlags | LUTC | Lfuncname
   161  	filename := filepath.Join(tmpDir, "test.log")
   162  
   163  	fileLogger := NewFileLogger()
   164  	realFileLogger, ok := fileLogger.(*FileLogger)
   165  	assert.True(t, ok)
   166  
   167  	location, _ := time.LoadLocation("EST")
   168  
   169  	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
   170  
   171  	dateString := date.UTC().Format("2006/01/02 15:04:05")
   172  
   173  	event := Event{
   174  		level:    INFO,
   175  		msg:      "TEST MSG",
   176  		caller:   "CALLER",
   177  		filename: "FULL/FILENAME",
   178  		line:     1,
   179  		time:     date,
   180  	}
   181  
   182  	expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   183  
   184  	fileLogger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"filename\":\"%s\",\"maxsize\":%d,\"compress\":true}", prefix, level.String(), flags, filepath.ToSlash(filename), len(expected)*2))
   185  
   186  	fileLogger.LogEvent(&event)
   187  	fileLogger.Flush()
   188  	logData, err := os.ReadFile(filename)
   189  	assert.NoError(t, err)
   190  	assert.Equal(t, expected, string(logData))
   191  
   192  	event.level = WARN
   193  	expected += fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   194  	fileLogger.LogEvent(&event)
   195  	fileLogger.Flush()
   196  	logData, err = os.ReadFile(filename)
   197  	assert.NoError(t, err)
   198  	assert.Equal(t, expected, string(logData))
   199  
   200  	// Should rotate
   201  	fileLogger.LogEvent(&event)
   202  	fileLogger.Flush()
   203  
   204  	for num := 2; num <= 999; num++ {
   205  		file, err := os.OpenFile(filename+fmt.Sprintf(".%s.%03d.gz", time.Now().Format("2006-01-02"), num), os.O_RDONLY|os.O_CREATE, 0o666)
   206  		assert.NoError(t, err)
   207  		file.Close()
   208  	}
   209  	err = realFileLogger.DoRotate()
   210  	assert.Error(t, err)
   211  }
   212  
   213  func TestCompressOldFile(t *testing.T) {
   214  	tmpDir, err := os.MkdirTemp("", "TestFileLogger")
   215  	assert.NoError(t, err)
   216  	defer util.RemoveAll(tmpDir)
   217  	fname := filepath.Join(tmpDir, "test")
   218  	nonGzip := filepath.Join(tmpDir, "test-nonGzip")
   219  
   220  	f, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY, 0o660)
   221  	assert.NoError(t, err)
   222  	ng, err := os.OpenFile(nonGzip, os.O_CREATE|os.O_WRONLY, 0o660)
   223  	assert.NoError(t, err)
   224  
   225  	for i := 0; i < 999; i++ {
   226  		f.WriteString("This is a test file\n")
   227  		ng.WriteString("This is a test file\n")
   228  	}
   229  	f.Close()
   230  	ng.Close()
   231  
   232  	err = compressOldLogFile(fname, -1)
   233  	assert.NoError(t, err)
   234  
   235  	_, err = os.Lstat(fname + ".gz")
   236  	assert.NoError(t, err)
   237  
   238  	f, err = os.Open(fname + ".gz")
   239  	assert.NoError(t, err)
   240  	zr, err := gzip.NewReader(f)
   241  	assert.NoError(t, err)
   242  	data, err := io.ReadAll(zr)
   243  	assert.NoError(t, err)
   244  	original, err := os.ReadFile(nonGzip)
   245  	assert.NoError(t, err)
   246  	assert.Equal(t, original, data)
   247  }