package cli //nolint:testpackage // shares runEntry and the hermetic config helpers import ( "os" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) const flagSkipErrors = "--skip-errors" // TestEntrySnapshotCreateSkipErrorsExitsNonZeroWhenItSkips backs up, // under --skip-errors, a source directory that does not exist, which the // walk skips. The command exits with exitCodeSkippedFiles and reports // the backup as completed, not as a failed snapshot creation. See // https://git.eeqj.de/sneak/vaultik/issues/310 and // https://git.eeqj.de/sneak/vaultik/issues/439. // //nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals func TestEntrySnapshotCreateSkipErrorsExitsNonZeroWhenItSkips(t *testing.T) { // writeBackupConfig would create the source directory. configPath := writeHermeticPruneConfig(t, false) code, stdout, stderr := runEntry(t, flagConfig, configPath, flagSkipErrors, cmdSnapshot, cmdCreate) assert.Equal(t, exitCodeSkippedFiles, code) assert.Contains(t, stdout, "Files: 1 examined, 0 backed up, 0 unchanged, 1 failed.") assert.NotContains(t, stdout, "Finished successfully.") assert.Contains(t, stderr, "Backup completed, but 1 file(s) could not be read") assert.NotContains(t, stderr, "Snapshot creation failed") } // TestEntrySnapshotRestoreSkipErrorsExitStatus backs up, deletes the // blobs from the destination, and restores under --skip-errors, so every // file is skipped. A restore that only skipped files exits with // exitCodeSkippedFiles. One that was also asked for a path the snapshot // does not hold exits 1. See // https://git.eeqj.de/sneak/vaultik/issues/439. // //nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals func TestEntrySnapshotRestoreSkipErrorsExitStatus(t *testing.T) { for _, testCase := range []struct { name string missingPaths []string want int }{ {name: "skipped files only", want: exitCodeSkippedFiles}, { name: "and a path that matches nothing", missingPaths: []string{"/no/such/path"}, want: 1, }, } { t.Run(testCase.name, func(t *testing.T) { configPath, storeDir := writeBackupConfigWithPrivateKey(t) code, _, _ := runEntry(t, flagConfig, configPath, cmdSnapshot, cmdCreate) require.Equal(t, 0, code) require.NoError(t, os.RemoveAll(filepath.Join(storeDir, blobsDir))) // writeBackupConfig puts the source directory beside the // config file. sourceDir := filepath.Join(filepath.Dir(configPath), "source") args := append([]string{ flagConfig, configPath, flagSkipErrors, cmdSnapshot, cmdRestore, onlySnapshotID(t, configPath), t.TempDir(), sourceDir, }, testCase.missingPaths...) code, _, stderr := runEntry(t, args...) assert.Equal(t, testCase.want, code) assert.Contains(t, stderr, "1 file(s) failed to restore") }) } }