From 375e0fb1bb18d92664402d62d453351e8472fe25 Mon Sep 17 00:00:00 2001 From: Abdullahi Yunus Date: Sun, 22 Dec 2024 17:41:40 +0100 Subject: [PATCH] chanbackup: test archiving chan backups --- chanbackup/backupfile_test.go | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/chanbackup/backupfile_test.go b/chanbackup/backupfile_test.go index 4471bbf4f8..bf41b25742 100644 --- a/chanbackup/backupfile_test.go +++ b/chanbackup/backupfile_test.go @@ -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) + }) + } +}