github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/log_file_reader_test.go (about)

     1  package helper
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  const (
    15  	testFilePath = "./test.log"
    16  )
    17  
    18  var testFileStat StateOS
    19  
    20  func setup() error {
    21  	fmt.Println("Setting up for tests...")
    22  
    23  	os.Create(testFilePath)
    24  	fileInfo, err := os.Stat(testFilePath)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	testFileStat = GetOSState(fileInfo)
    29  	return nil
    30  }
    31  
    32  func teardown() error {
    33  	fmt.Println("Tearing down after tests...")
    34  	if _, err := os.Stat(testFilePath); os.IsNotExist(err) {
    35  		return nil
    36  	}
    37  	err := os.Remove(testFilePath)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	return nil
    42  }
    43  
    44  func getMockCheckpoint() LogFileReaderCheckPoint {
    45  	return LogFileReaderCheckPoint{
    46  		Path:   testFilePath,
    47  		Offset: 0,
    48  		State:  testFileStat,
    49  	}
    50  }
    51  
    52  func getMockLogReaderConfig() LogFileReaderConfig {
    53  	return LogFileReaderConfig{
    54  		CloseFileSec:     60,
    55  		MaxReadBlockSize: 1024,
    56  		ReadIntervalMs:   1000,
    57  		Tracker:          nil,
    58  	}
    59  }
    60  
    61  func writeContent(filePath string) error {
    62  	file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	writer := bufio.NewWriter(file)
    67  	_, err = writer.WriteString("hello world" + "\n")
    68  	if err != nil {
    69  		return err
    70  	}
    71  	err = writer.Flush()
    72  	if err != nil {
    73  		return err
    74  	}
    75  	return nil
    76  }
    77  
    78  type MockLogFileProcessor struct {
    79  }
    80  
    81  func (m *MockLogFileProcessor) Process(fileBlock []byte, noChangeInterval time.Duration) int {
    82  	return 0
    83  }
    84  
    85  func getMockLogFileProcessor() *MockLogFileProcessor {
    86  	return &MockLogFileProcessor{}
    87  }
    88  
    89  func TestCheckFileChange(t *testing.T) {
    90  	reader, err := NewLogFileReader(context.Background(), getMockCheckpoint(), getMockLogReaderConfig(), getMockLogFileProcessor())
    91  	assert.Nil(t, err)
    92  	change := reader.CheckFileChange()
    93  	assert.Equal(t, false, change)
    94  
    95  	// write to file
    96  	writeContent(testFilePath)
    97  	change = reader.CheckFileChange()
    98  	assert.Equal(t, true, change)
    99  	reader.ReadAndProcess(false)
   100  
   101  	// write to file and remove, mock the file path in logfilereader unreachable
   102  	writeContent(testFilePath)
   103  	os.Remove(testFilePath)
   104  	change = reader.CheckFileChange()
   105  	assert.Equal(t, true, change)
   106  
   107  	reader.CloseFile("finish test")
   108  }
   109  
   110  func TestMain(m *testing.M) {
   111  	err := setup()
   112  	if err != nil {
   113  		fmt.Println("Setup failed:", err)
   114  		os.Exit(1)
   115  	}
   116  	exitCode := m.Run()
   117  	err = teardown()
   118  	if err != nil {
   119  		fmt.Println("Teardown failed:", err)
   120  		os.Exit(1)
   121  	}
   122  	os.Exit(exitCode)
   123  }