github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/logger/file_test.go (about)

     1  // Copyright 2017 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package logger
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestScanOldLogFiles(t *testing.T) {
    18  	dir, err := os.MkdirTemp("", "log-rotation-test")
    19  	require.NoError(t, err)
    20  	defer os.RemoveAll(dir)
    21  	fmt.Println(dir)
    22  	p := filepath.Join(dir, "keybase.kbfs.log")
    23  
    24  	// hack for test only
    25  	originalLocal := time.Local
    26  	time.Local = time.FixedZone("Minus6", -6*3600)
    27  	defer func() { time.Local = originalLocal }()
    28  
    29  	logFilenames := []string{
    30  		filepath.Join(dir, "keybase.kbfs.log-20170213T162521-20170213T163321"),
    31  		filepath.Join(dir, "keybase.kbfs.log-20170213T163321-20170213T164105"),
    32  		filepath.Join(dir, "keybase.kbfs.log-20170213T163321-0700-20170214T142252-0600"),
    33  		filepath.Join(dir, "keybase.kbfs.log-20170214T142252-0600-20170214T142713-0600"),
    34  		filepath.Join(dir, "keybase.kbfs.log-20170214T142713-0600-20170214T143359-0600"),
    35  		filepath.Join(dir, "keybase.kbfs.log-20170214T143359-0600-20170214T144159-0600"),
    36  		filepath.Join(dir, "keybase.kbfs.log-20170217T000000+0600-20170217T000000+0600"),
    37  	}
    38  	nonLogFilenames := []string{
    39  		filepath.Join(dir, "O_O"),
    40  		filepath.Join(dir, "keybase.kbfs-20170213T162521-20170213T163321"),
    41  		filepath.Join(dir, "keybase.kbfs-20170214T142129-0600-20170214T142252-0600"),
    42  	}
    43  	for _, fn := range append(logFilenames, nonLogFilenames...) {
    44  		require.NoError(t, os.WriteFile(fn, []byte("hello"), 0644))
    45  	}
    46  
    47  	fNames, err := scanOldLogFiles(p)
    48  	require.NoError(t, err)
    49  	require.Len(t, fNames, len(logFilenames))
    50  	for i, n := range fNames {
    51  		require.Equal(t, n, logFilenames[i])
    52  	}
    53  }