-
Notifications
You must be signed in to change notification settings - Fork 2.2k
chanbackup: archive old channel backup files #9232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guggero
merged 3 commits into
lightningnetwork:master
from
Abdulkbk:archive-channel-backups
Jan 24, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,9 @@ func assertFileDeleted(t *testing.T, filePath string) { | |
// TestUpdateAndSwap test that we're able to properly swap out old backups on | ||
// disk with new ones. Additionally, after a swap operation succeeds, then each | ||
// time we should only have the main backup file on disk, as the temporary file | ||
// has been removed. | ||
// has been removed. Finally, we check for noBackupArchive to ensure that the | ||
// archive file is created when it's set to false, and not created when it's | ||
// set to true. | ||
func TestUpdateAndSwap(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
@@ -58,7 +60,8 @@ func TestUpdateAndSwap(t *testing.T) { | |
fileName string | ||
tempFileName string | ||
|
||
oldTempExists bool | ||
oldTempExists bool | ||
noBackupArchive bool | ||
|
||
valid bool | ||
}{ | ||
|
@@ -92,9 +95,37 @@ func TestUpdateAndSwap(t *testing.T) { | |
), | ||
valid: true, | ||
}, | ||
|
||
// Test with noBackupArchive set to true - should not create | ||
// archive. | ||
{ | ||
fileName: filepath.Join( | ||
tempTestDir, DefaultBackupFileName, | ||
), | ||
tempFileName: filepath.Join( | ||
tempTestDir, DefaultTempBackupFileName, | ||
), | ||
noBackupArchive: true, | ||
valid: true, | ||
}, | ||
|
||
// Test with v set to false - should create | ||
// archive. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
{ | ||
fileName: filepath.Join( | ||
tempTestDir, DefaultBackupFileName, | ||
), | ||
tempFileName: filepath.Join( | ||
tempTestDir, DefaultTempBackupFileName, | ||
), | ||
noBackupArchive: false, | ||
valid: true, | ||
}, | ||
} | ||
for i, testCase := range testCases { | ||
backupFile := NewMultiFile(testCase.fileName) | ||
backupFile := NewMultiFile( | ||
testCase.fileName, testCase.noBackupArchive, | ||
) | ||
|
||
// To start with, we'll make a random byte slice that'll pose | ||
// as our packed multi backup. | ||
|
@@ -160,6 +191,41 @@ func TestUpdateAndSwap(t *testing.T) { | |
// Additionally, we shouldn't be able to find the temp backup | ||
// file on disk, as it should be deleted each time. | ||
assertFileDeleted(t, testCase.tempFileName) | ||
|
||
// Now check if archive was created when noBackupArchive is | ||
// false. | ||
archiveDir := filepath.Join( | ||
filepath.Dir(testCase.fileName), | ||
DefaultChanBackupArchiveDirName, | ||
) | ||
if !testCase.noBackupArchive { | ||
files, err := os.ReadDir(archiveDir) | ||
require.NoError(t, err) | ||
require.Len(t, files, 1) | ||
|
||
// Verify the archive contents match the previous | ||
// backup. | ||
archiveFile := filepath.Join( | ||
archiveDir, files[0].Name(), | ||
) | ||
// The archived content should match the previous | ||
// backup (newPackedMulti) that was just swapped out. | ||
assertBackupMatches(t, archiveFile, newPackedMulti) | ||
guggero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Clean up the archive directory. | ||
os.RemoveAll(archiveDir) | ||
|
||
continue | ||
} | ||
|
||
// When noBackupArchive is true, no new archive file should be | ||
// created. Note: In a real environment, the archive directory | ||
// might exist with older backups before the feature is | ||
// disabled, but for test simplicity (since we clean up the | ||
// directory between test cases), we verify the directory | ||
// doesn't exist at all. | ||
require.NoDirExists(t, archiveDir) | ||
|
||
} | ||
} | ||
|
||
|
@@ -238,7 +304,7 @@ func TestExtractMulti(t *testing.T) { | |
} | ||
for i, testCase := range testCases { | ||
// First, we'll make our backup file with the specified name. | ||
backupFile := NewMultiFile(testCase.fileName) | ||
backupFile := NewMultiFile(testCase.fileName, false) | ||
|
||
// With our file made, we'll now attempt to read out the | ||
// multi-file. | ||
|
@@ -274,3 +340,86 @@ func TestExtractMulti(t *testing.T) { | |
assertMultiEqual(t, &unpackedMulti, freshUnpackedMulti) | ||
} | ||
} | ||
|
||
// TestCreateArchiveFile tests that we're able to create an archive file | ||
// with a timestamped name in the specified archive directory, and copy the | ||
// contents of the main backup file to the new archive file. | ||
func TestCreateArchiveFile(t *testing.T) { | ||
Abdulkbk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
|
||
// First, we'll create a temporary directory for our test files. | ||
tempDir := t.TempDir() | ||
archiveDir := filepath.Join(tempDir, DefaultChanBackupArchiveDirName) | ||
|
||
// Next, we'll create a test backup file and write some content to it. | ||
backupFile := filepath.Join(tempDir, DefaultBackupFileName) | ||
testContent := []byte("test backup content") | ||
err := os.WriteFile(backupFile, testContent, 0644) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
setup func() | ||
noBackupArchive bool | ||
wantError bool | ||
}{ | ||
{ | ||
name: "successful archive", | ||
noBackupArchive: false, | ||
}, | ||
{ | ||
name: "skip archive when disabled", | ||
noBackupArchive: true, | ||
}, | ||
{ | ||
name: "invalid archive directory permissions", | ||
setup: func() { | ||
// Create dir with no write permissions. | ||
err := os.MkdirAll(archiveDir, 0500) | ||
require.NoError(t, err) | ||
}, | ||
noBackupArchive: false, | ||
wantError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
defer os.RemoveAll(archiveDir) | ||
if tc.setup != nil { | ||
yyforyongyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tc.setup() | ||
} | ||
|
||
multiFile := NewMultiFile( | ||
backupFile, tc.noBackupArchive, | ||
) | ||
|
||
err := multiFile.createArchiveFile() | ||
if tc.wantError { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
|
||
// If archiving is disabled, verify no archive was | ||
// created. | ||
if tc.noBackupArchive { | ||
require.NoDirExists(t, archiveDir) | ||
return | ||
} | ||
|
||
// Verify archive exists and content matches. | ||
files, err := os.ReadDir(archiveDir) | ||
require.NoError(t, err) | ||
require.Len(t, files, 1) | ||
|
||
archivedContent, err := os.ReadFile( | ||
filepath.Join(archiveDir, files[0].Name()), | ||
) | ||
require.NoError(t, err) | ||
assertBackupMatches(t, backupFile, archivedContent) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.