@ -8,41 +8,33 @@ import (
"testing"
"testing"
"time"
"time"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/errors"
)
)
func TestSIGHUP ( t * testing . T ) {
func TestSIGHUP ( t * testing . T ) {
// First, create an AutoFile writing to a tempfile dir
// First, create an AutoFile writing to a tempfile dir
file , err := ioutil . TempFile ( "" , "sighup_test" )
file , err := ioutil . TempFile ( "" , "sighup_test" )
if err != nil {
t . Fatalf ( "Error creating tempfile: %v" , err )
}
if err := file . Close ( ) ; err != nil {
t . Fatalf ( "Error closing tempfile: %v" , err )
}
require . NoError ( t , err )
err = file . Close ( )
require . NoError ( t , err )
name := file . Name ( )
name := file . Name ( )
// Here is the actual AutoFile
// Here is the actual AutoFile
af , err := OpenAutoFile ( name )
af , err := OpenAutoFile ( name )
if err != nil {
t . Fatalf ( "Error creating autofile: %v" , err )
}
require . NoError ( t , err )
// Write to the file.
// Write to the file.
_ , err = af . Write ( [ ] byte ( "Line 1\n" ) )
_ , err = af . Write ( [ ] byte ( "Line 1\n" ) )
if err != nil {
t . Fatalf ( "Error writing to autofile: %v" , err )
}
require . NoError ( t , err )
_ , err = af . Write ( [ ] byte ( "Line 2\n" ) )
_ , err = af . Write ( [ ] byte ( "Line 2\n" ) )
if err != nil {
t . Fatalf ( "Error writing to autofile: %v" , err )
}
require . NoError ( t , err )
// Move the file over
// Move the file over
err = os . Rename ( name , name + "_old" )
err = os . Rename ( name , name + "_old" )
if err != nil {
t . Fatalf ( "Error moving autofile: %v" , err )
}
require . NoError ( t , err )
// Send SIGHUP to self.
// Send SIGHUP to self.
oldSighupCounter := atomic . LoadInt32 ( & sighupCounter )
oldSighupCounter := atomic . LoadInt32 ( & sighupCounter )
@ -55,16 +47,11 @@ func TestSIGHUP(t *testing.T) {
// Write more to the file.
// Write more to the file.
_ , err = af . Write ( [ ] byte ( "Line 3\n" ) )
_ , err = af . Write ( [ ] byte ( "Line 3\n" ) )
if err != nil {
t . Fatalf ( "Error writing to autofile: %v" , err )
}
require . NoError ( t , err )
_ , err = af . Write ( [ ] byte ( "Line 4\n" ) )
_ , err = af . Write ( [ ] byte ( "Line 4\n" ) )
if err != nil {
t . Fatalf ( "Error writing to autofile: %v" , err )
}
if err := af . Close ( ) ; err != nil {
t . Fatalf ( "Error closing autofile" )
}
require . NoError ( t , err )
err = af . Close ( )
require . NoError ( t , err )
// Both files should exist
// Both files should exist
if body := cmn . MustReadFile ( name + "_old" ) ; string ( body ) != "Line 1\nLine 2\n" {
if body := cmn . MustReadFile ( name + "_old" ) ; string ( body ) != "Line 1\nLine 2\n" {
@ -74,3 +61,33 @@ func TestSIGHUP(t *testing.T) {
t . Errorf ( "Unexpected body %s" , body )
t . Errorf ( "Unexpected body %s" , body )
}
}
}
}
// Manually modify file permissions, close, and reopen using autofile:
// We expect the file permissions to be changed back to the intended perms.
func TestOpenAutoFilePerms ( t * testing . T ) {
file , err := ioutil . TempFile ( "" , "permission_test" )
require . NoError ( t , err )
err = file . Close ( )
require . NoError ( t , err )
name := file . Name ( )
// open and change permissions
af , err := OpenAutoFile ( name )
require . NoError ( t , err )
err = af . file . Chmod ( 0755 )
require . NoError ( t , err )
err = af . Close ( )
require . NoError ( t , err )
// reopen and expect an ErrPermissionsChanged as Cause
af , err = OpenAutoFile ( name )
require . Error ( t , err )
if e , ok := err . ( * errors . ErrPermissionsChanged ) ; ok {
t . Logf ( "%v" , e )
} else {
t . Errorf ( "unexpected error %v" , e )
}
err = af . Close ( )
require . NoError ( t , err )
}