From c013076c0e7ec7d03edfe27379f0d08cb2adea1e Mon Sep 17 00:00:00 2001 From: nitishfy Date: Mon, 16 Dec 2024 20:00:05 +0530 Subject: [PATCH] add symlink test case Signed-off-by: nitishfy --- internal/directives/file_deleter.go | 10 ++++-- internal/directives/file_deleter_test.go | 42 +++++++++++++++++++++--- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/internal/directives/file_deleter.go b/internal/directives/file_deleter.go index de012d5a5..aea564450 100644 --- a/internal/directives/file_deleter.go +++ b/internal/directives/file_deleter.go @@ -3,10 +3,12 @@ package directives import ( "context" "fmt" - kargoapi "github.com/akuity/kargo/api/v1alpha1" + "os" + securejoin "github.com/cyphar/filepath-securejoin" "github.com/xeipuuv/gojsonschema" - "os" + + kargoapi "github.com/akuity/kargo/api/v1alpha1" ) func init() { @@ -71,6 +73,10 @@ func removePath(path string) error { return err } + if fi.Mode()&os.ModeSymlink != 0 { + return os.Remove(path) + } + if fi.IsDir() { return os.RemoveAll(path) } diff --git a/internal/directives/file_deleter_test.go b/internal/directives/file_deleter_test.go index b044ef81b..dad7e78df 100644 --- a/internal/directives/file_deleter_test.go +++ b/internal/directives/file_deleter_test.go @@ -2,12 +2,14 @@ package directives import ( "context" - kargoapi "github.com/akuity/kargo/api/v1alpha1" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "os" "path/filepath" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + kargoapi "github.com/akuity/kargo/api/v1alpha1" ) func Test_fileDeleter_runPromotionStep(t *testing.T) { @@ -30,7 +32,7 @@ func Test_fileDeleter_runPromotionStep(t *testing.T) { cfg: DeleteConfig{ Path: "input.txt", }, - assertions: func(t *testing.T, workDir string, result PromotionStepResult, err error) { + assertions: func(t *testing.T, _ string, result PromotionStepResult, err error) { assert.NoError(t, err) assert.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, result) @@ -70,8 +72,38 @@ func Test_fileDeleter_runPromotionStep(t *testing.T) { assert.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, result) }, }, - } + { + name: "removes symlink only", + setupFiles: func(t *testing.T) string { + tmpDir := t.TempDir() + inDir := filepath.Join(tmpDir, "input") + require.NoError(t, os.Mkdir(inDir, 0o755)) + + filePath := filepath.Join(inDir, "input.txt") + require.NoError(t, os.WriteFile(filePath, []byte("test content"), 0o600)) + + symlinkPath := filepath.Join(inDir, "symlink.txt") + require.NoError(t, os.Symlink("input.txt", symlinkPath)) + + return tmpDir + }, + cfg: DeleteConfig{ + Path: "input/symlink.txt", + }, + assertions: func(t *testing.T, workDir string, result PromotionStepResult, err error) { + assert.NoError(t, err) + require.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, result) + + _, statErr := os.Stat(filepath.Join(workDir, "input.txt")) + assert.NoError(t, statErr) + + _, statErr = os.Lstat(filepath.Join(workDir, "symlink.txt")) + assert.Error(t, statErr) + assert.True(t, os.IsNotExist(statErr)) + }, + }, + } runner := &fileDeleter{} for _, tt := range tests {