Skip to content

Commit

Permalink
chanbackup: test archiving chan backups
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdulkbk committed Dec 22, 2024
1 parent 6172a79 commit 375e0fb
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions chanbackup/backupfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,83 @@ func TestExtractMulti(t *testing.T) {
assertMultiEqual(t, &unpackedMulti, freshUnpackedMulti)
}
}

func TestCreateArchiveFile(t *testing.T) {
t.Parallel()

// First, we'll create a temporary directory for our test files.
tempDir := t.TempDir()
archiveDir := filepath.Join(tempDir, "chan-backup-archives")

// Next, we'll create a test backup file and write some content to it.
backupFile := filepath.Join(tempDir, "channel.backup")
testContent := []byte("test backup content")
err := os.WriteFile(backupFile, testContent, 0644)
require.NoError(t, err)

tests := []struct {
name string
setup func()
archDir string
fileName string
wantError bool
}{
{
name: "successful archive",
setup: func() {
// Ensure archive dir is clean and writable
_ = os.RemoveAll(archiveDir)
},
archDir: archiveDir,
fileName: backupFile,
},
{
name: "non-existent source file",
archDir: archiveDir,
fileName: "nonexistent.backup",
wantError: true,
},
{
name: "invalid archive directory permissions",
setup: func() {
// Clean up first
_ = os.RemoveAll(archiveDir)

// Create dir with no write permissions
err := os.MkdirAll(archiveDir, 0500)
require.NoError(t, err)
},
archDir: archiveDir,
fileName: backupFile,
wantError: true,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if tc.setup != nil {
tc.setup()
}

err := createArchiveFile(tc.archDir, tc.fileName)
if tc.wantError {
require.Error(t, err)
return
}

require.NoError(t, err)

// Verify archive exists and content matches
files, err := os.ReadDir(tc.archDir)
require.NoError(t, err)
require.Len(t, files, 1)

archivedContent, err := os.ReadFile(
filepath.Join(tc.archDir, files[0].Name()),
)
require.NoError(t, err)
require.Equal(t, testContent, archivedContent)
})
}
}

0 comments on commit 375e0fb

Please sign in to comment.