Skip to content

Commit 3003ac6

Browse files
wdhifclaude
andcommitted
test(flare): cover archiveDir failing on a missing directory
Also delete the destination when archiving fails: the zip writer has already created it, so a failure left a truncated archive behind that both misleads whoever finds it and blocks the next flare of the same second through O_EXCL. The pre-existing-file case is untouched, since that open fails before anything is created. Raises patch coverage on the changed lines from 80.0% to 85.0%, above the 80% gate. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6b61b3c commit 3003ac6

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

cmd/kubectl-datadog/flare/archive_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ func TestArchiveDirKeepsExistingArchive(t *testing.T) {
7676
require.NoError(t, err)
7777
assert.Equal(t, existing, kept)
7878
}
79+
80+
func TestArchiveDirMissingSource(t *testing.T) {
81+
destination := filepath.Join(t.TempDir(), "flare.zip")
82+
83+
assert.Error(t, archiveDir(filepath.Join(t.TempDir(), "absent"), destination))
84+
85+
_, err := os.Stat(destination)
86+
assert.True(t, os.IsNotExist(err), "a failed archive must not be left behind")
87+
}

cmd/kubectl-datadog/flare/flare.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,12 @@ func archiveDir(dir, destination string) error {
565565
err = closeErr
566566
}
567567

568+
if err != nil {
569+
// A truncated archive would mislead whoever finds it, and would block the
570+
// next flare of the same second through the O_EXCL above.
571+
_ = os.Remove(destination)
572+
}
573+
568574
return err
569575
}
570576

0 commit comments

Comments
 (0)