Merge pull request #7384 from guggero/close-mac-db

walletunlocker: fix unit test flake by closing DB
This commit is contained in:
Oliver Gugger
2023-02-06 16:57:42 +01:00
committed by GitHub
2 changed files with 31 additions and 18 deletions

View File

@@ -344,8 +344,11 @@ in the lnwire package](https://github.com/lightningnetwork/lnd/pull/7303)
issue found in `bitcoindnotify/bitcoind_test.go`.
* Add methods to easily check if an invoice [is AMP or
Keysend](https://github.com/lightningnetwork/lnd/pull/7334).
Keysend](https://github.com/lightningnetwork/lnd/pull/7334).
* [Fixed a unit test flake in the wallet
unlocker](https://github.com/lightningnetwork/lnd/pull/7384).
## Watchtowers
* [Create a towerID-to-sessionID index in the wtclient DB to improve the

View File

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