walletunlocker: fix unit test flake by closing DB

This fixes a unit test flake that occurred sometimes if the temporary
directory was attempted to be deleted but the wallet or macaroon DB
hasn't been closed yet.

````
--- FAIL: TestChangeWalletPasswordNewRootkey (1.63s)
    testing.go:1097: TempDir RemoveAll cleanup: unlinkat /tmp/TestChangeWalletPasswordNewRootkey3063283009/001/mainnet: directory not empty
FAIL
FAIL	github.com/lightningnetwork/lnd/walletunlocker	6.171s
FAIL
````
This commit is contained in:
Oliver Gugger 2023-02-06 12:23:40 +01:00
parent 0cf0a7dd3b
commit 2086f09489
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -404,16 +404,17 @@ func TestUnlockWallet(t *testing.T) {
// Send a fake macaroon that should be returned in the response // Send a fake macaroon that should be returned in the response
// in the async code above. // in the async code above.
service.MacResponseChan <- testMac service.MacResponseChan <- testMac
require.NoError(t, unlockMsg.UnloadWallet())
case <-time.After(defaultTestTimeout): case <-time.After(defaultTestTimeout):
t.Fatalf("password not received") t.Fatalf("password not received")
} }
} }
// TestChangeWalletPasswordNewRootkey tests that we can successfully change the // TestChangeWalletPasswordNewRootKey tests that we can successfully change the
// wallet's password needed to unlock it and rotate the root key for the // wallet's password needed to unlock it and rotate the root key for the
// macaroons in the same process. // macaroons in the same process.
func TestChangeWalletPasswordNewRootkey(t *testing.T) { func TestChangeWalletPasswordNewRootKey(t *testing.T) {
t.Parallel() t.Parallel()
// testDir is empty, meaning wallet was not created from before. // testDir is empty, meaning wallet was not created from before.
@ -503,6 +504,7 @@ func TestChangeWalletPasswordNewRootkey(t *testing.T) {
// Send a fake macaroon that should be returned in the response // Send a fake macaroon that should be returned in the response
// in the async code above. // in the async code above.
service.MacResponseChan <- testMac service.MacResponseChan <- testMac
require.NoError(t, unlockMsg.UnloadWallet())
case <-time.After(defaultTestTimeout): case <-time.After(defaultTestTimeout):
t.Fatalf("password not received") t.Fatalf("password not received")
@ -615,42 +617,50 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
func doChangePassword(service *walletunlocker.UnlockerService, testDir string, func doChangePassword(service *walletunlocker.UnlockerService, testDir string,
req *lnrpc.ChangePasswordRequest, errChan chan error) { req *lnrpc.ChangePasswordRequest, errChan chan error) {
// When providing the correct wallet's current password and a // When providing the correct wallet's current password and a new
// new password that meets the length requirement, the password // password that meets the length requirement, the password change
// change should succeed. // should succeed.
ctx := context.Background() ctx := context.Background()
response, err := service.ChangePassword(ctx, req) response, err := service.ChangePassword(ctx, req)
if err != nil { if err != nil {
errChan <- fmt.Errorf("could not change password: %v", err) errChan <- fmt.Errorf("could not change password: %w", err)
return return
} }
if !bytes.Equal(response.AdminMacaroon, testMac) { if !bytes.Equal(response.AdminMacaroon, testMac) {
errChan <- fmt.Errorf("mismatched macaroon: expected "+ errChan <- fmt.Errorf("mismatched macaroon: expected %x, got "+
"%x, got %x", testMac, response.AdminMacaroon) "%x", testMac, response.AdminMacaroon)
} }
// Close the macaroon DB and try to open it and read the root // Close the macaroon DB and try to open it and read the root key with
// key with the new password. // the new password.
store, err := openOrCreateTestMacStore( store, err := openOrCreateTestMacStore(
testDir, &testPassword, testNetParams, testDir, &testPassword, testNetParams,
) )
if err != nil { if err != nil {
errChan <- fmt.Errorf("could not create test store: %v", err) errChan <- fmt.Errorf("could not create test store: %w", err)
return return
} }
_, _, err = store.RootKey(defaultRootKeyIDContext) _, _, err = store.RootKey(defaultRootKeyIDContext)
if err != nil { if err != nil {
errChan <- fmt.Errorf("could not get root key: %v", err) errChan <- fmt.Errorf("could not get root key: %w", err)
return return
} }
// Do cleanup now. Since we are in a go func, the defer at the // Do cleanup now. Since we are in a go func, the defer at the top of
// top of the outer would not work, because it would delete // the outer would not work, because it would delete the directory
// the directory before we could check the content in here. // before we could check the content in here.
err = store.Close() err = store.Close()
if err != nil { if err != nil {
errChan <- fmt.Errorf("could not close store: %v", err) errChan <- fmt.Errorf("could not close store: %w", err)
return
}
// The backend database isn't closed automatically if the store is
// closed, do that now manually.
err = store.Backend.Close()
if err != nil {
errChan <- fmt.Errorf("could not close db: %w", err)
return return
} }
} }