github.com/vipernet-xyz/tendermint-core@v0.32.0/libs/autofile/autofile_test.go (about)

     1  package autofile
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"syscall"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	tmos "github.com/tendermint/tendermint/libs/os"
    15  )
    16  
    17  func TestSIGHUP(t *testing.T) {
    18  	origDir, err := os.Getwd()
    19  	require.NoError(t, err)
    20  	defer os.Chdir(origDir)
    21  
    22  	// First, create a temporary directory and move into it
    23  	dir, err := ioutil.TempDir("", "sighup_test")
    24  	require.NoError(t, err)
    25  	defer os.RemoveAll(dir)
    26  	err = os.Chdir(dir)
    27  	require.NoError(t, err)
    28  
    29  	// Create an AutoFile in the temporary directory
    30  	name := "sighup_test"
    31  	af, err := OpenAutoFile(name)
    32  	require.NoError(t, err)
    33  	require.True(t, filepath.IsAbs(af.Path))
    34  
    35  	// Write to the file.
    36  	_, err = af.Write([]byte("Line 1\n"))
    37  	require.NoError(t, err)
    38  	_, err = af.Write([]byte("Line 2\n"))
    39  	require.NoError(t, err)
    40  
    41  	// Move the file over
    42  	err = os.Rename(name, name+"_old")
    43  	require.NoError(t, err)
    44  
    45  	// Move into a different temporary directory
    46  	otherDir, err := ioutil.TempDir("", "sighup_test_other")
    47  	require.NoError(t, err)
    48  	defer os.RemoveAll(otherDir)
    49  	err = os.Chdir(otherDir)
    50  	require.NoError(t, err)
    51  
    52  	// Send SIGHUP to self.
    53  	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
    54  
    55  	// Wait a bit... signals are not handled synchronously.
    56  	time.Sleep(time.Millisecond * 10)
    57  
    58  	// Write more to the file.
    59  	_, err = af.Write([]byte("Line 3\n"))
    60  	require.NoError(t, err)
    61  	_, err = af.Write([]byte("Line 4\n"))
    62  	require.NoError(t, err)
    63  	err = af.Close()
    64  	require.NoError(t, err)
    65  
    66  	// Both files should exist
    67  	if body := tmos.MustReadFile(filepath.Join(dir, name+"_old")); string(body) != "Line 1\nLine 2\n" {
    68  		t.Errorf("unexpected body %s", body)
    69  	}
    70  	if body := tmos.MustReadFile(filepath.Join(dir, name)); string(body) != "Line 3\nLine 4\n" {
    71  		t.Errorf("unexpected body %s", body)
    72  	}
    73  
    74  	// The current directory should be empty
    75  	files, err := ioutil.ReadDir(".")
    76  	require.NoError(t, err)
    77  	assert.Empty(t, files)
    78  }
    79  
    80  // // Manually modify file permissions, close, and reopen using autofile:
    81  // // We expect the file permissions to be changed back to the intended perms.
    82  // func TestOpenAutoFilePerms(t *testing.T) {
    83  // 	file, err := ioutil.TempFile("", "permission_test")
    84  // 	require.NoError(t, err)
    85  // 	err = file.Close()
    86  // 	require.NoError(t, err)
    87  // 	name := file.Name()
    88  
    89  // 	// open and change permissions
    90  // 	af, err := OpenAutoFile(name)
    91  // 	require.NoError(t, err)
    92  // 	err = af.file.Chmod(0755)
    93  // 	require.NoError(t, err)
    94  // 	err = af.Close()
    95  // 	require.NoError(t, err)
    96  
    97  // 	// reopen and expect an ErrPermissionsChanged as Cause
    98  // 	af, err = OpenAutoFile(name)
    99  // 	require.Error(t, err)
   100  // 	if e, ok := err.(*errors.ErrPermissionsChanged); ok {
   101  // 		t.Logf("%v", e)
   102  // 	} else {
   103  // 		t.Errorf("unexpected error %v", e)
   104  // 	}
   105  // }
   106  
   107  func TestAutoFileSize(t *testing.T) {
   108  	// First, create an AutoFile writing to a tempfile dir
   109  	f, err := ioutil.TempFile("", "sighup_test")
   110  	require.NoError(t, err)
   111  	err = f.Close()
   112  	require.NoError(t, err)
   113  
   114  	// Here is the actual AutoFile.
   115  	af, err := OpenAutoFile(f.Name())
   116  	require.NoError(t, err)
   117  
   118  	// 1. Empty file
   119  	size, err := af.Size()
   120  	require.Zero(t, size)
   121  	require.NoError(t, err)
   122  
   123  	// 2. Not empty file
   124  	data := []byte("Maniac\n")
   125  	_, err = af.Write(data)
   126  	require.NoError(t, err)
   127  	size, err = af.Size()
   128  	require.EqualValues(t, len(data), size)
   129  	require.NoError(t, err)
   130  
   131  	// 3. Not existing file
   132  	err = af.Close()
   133  	require.NoError(t, err)
   134  	err = os.Remove(f.Name())
   135  	require.NoError(t, err)
   136  	size, err = af.Size()
   137  	require.EqualValues(t, 0, size, "Expected a new file to be empty")
   138  	require.NoError(t, err)
   139  
   140  	// Cleanup
   141  	_ = os.Remove(f.Name())
   142  }