github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/single_log_test.go (about)

     1  package xlog
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"strconv"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/google/safeopen"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestSingleLog(t *testing.T) {
    18  	ctx, cancel := context.WithCancel(context.TODO())
    19  	log := SingleLog(ctx, nil)
    20  	require.Nil(t, log)
    21  
    22  	log = SingleLog(nil, &FileCoreConfig{
    23  		FilePath: os.TempDir(),
    24  		Filename: filepath.Base(os.Args[0]) + "_sxlog.log",
    25  	})
    26  	require.Nil(t, log)
    27  
    28  	log = SingleLog(ctx, &FileCoreConfig{
    29  		FilePath: os.TempDir(),
    30  		Filename: filepath.Base(os.Args[0]) + "_sxlog.log",
    31  	})
    32  
    33  	for i := 0; i < 1000; i++ {
    34  		data := []byte(strconv.Itoa(i) + " " + time.Now().UTC().Format(backupDateTimeFormat) + " xlog single log write test!\n")
    35  		_, err := log.Write(data)
    36  		require.NoError(t, err)
    37  	}
    38  	err := log.Close()
    39  	require.NoError(t, err)
    40  
    41  	cancel()
    42  	err = log.Close()
    43  	require.NoError(t, err)
    44  	time.Sleep(20 * time.Millisecond)
    45  	_, err = log.Write([]byte("xlog single log write test!\n"))
    46  	require.True(t, errors.Is(err, io.EOF))
    47  
    48  	log = &singleLog{
    49  		ctx:      context.TODO(),
    50  		filename: filepath.Base(os.Args[0]) + "_sxlog.log",
    51  	}
    52  
    53  	for i := 2000; i < 3000; i++ {
    54  		data := []byte(strconv.Itoa(i) + " " + time.Now().UTC().Format(backupDateTimeFormat) + " xlog single log write test!\n")
    55  		_, err = log.Write(data)
    56  		require.NoError(t, err)
    57  	}
    58  	err = log.Close()
    59  	require.NoError(t, err)
    60  
    61  	removed := testCleanLogFiles(t, os.TempDir(), filepath.Base(os.Args[0])+"_sxlog", ".log")
    62  	require.Equal(t, 1, removed)
    63  }
    64  
    65  func TestSingleLog_PermissionDeniedAccess(t *testing.T) {
    66  	rf, err := safeopen.CreateBeneath(os.TempDir(), "pda.log")
    67  	require.NoError(t, err)
    68  	err = rf.Close()
    69  	require.NoError(t, err)
    70  
    71  	err = os.Chmod(filepath.Join(os.TempDir(), "pda.log"), 0o400)
    72  	require.NoError(t, err)
    73  
    74  	rf, err = safeopen.OpenFileBeneath(os.TempDir(), "pda.log", os.O_WRONLY|os.O_APPEND, 0o666)
    75  	require.Error(t, err) // Access denied
    76  	require.Nil(t, rf)
    77  
    78  	log := &singleLog{
    79  		ctx:      context.TODO(),
    80  		filename: "pda.log",
    81  		filePath: os.TempDir(),
    82  	}
    83  	_, err = log.Write([]byte("permission denied access!"))
    84  	require.Error(t, err) // Access denied
    85  	err = log.Close()
    86  	require.NoError(t, err)
    87  
    88  	removed := testCleanLogFiles(t, os.TempDir(), "pda", ".log")
    89  	require.Equal(t, 1, removed)
    90  }
    91  
    92  func TestSingleLog_Write_Dir(t *testing.T) {
    93  	err := os.Mkdir(filepath.Join(os.TempDir(), "pda2.log"), 0o600)
    94  	require.NoError(t, err)
    95  
    96  	log := &singleLog{
    97  		ctx:      context.TODO(),
    98  		filename: "pda2.log",
    99  		filePath: os.TempDir(),
   100  	}
   101  
   102  	_, err = log.Write([]byte("single log write dir!"))
   103  	require.Error(t, err)
   104  	err = log.Close()
   105  	require.NoError(t, err)
   106  
   107  	removed := testCleanLogFiles(t, os.TempDir(), "pda2", ".log")
   108  	require.Equal(t, 1, removed)
   109  }