From 69f0fd5766aebe59baed1dd320f21f66111a867a Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 4 Nov 2024 11:35:57 +0200 Subject: [PATCH 1/6] bug/remove sca temp file when using sca resolver --- internal/commands/scan.go | 7 +++++ internal/commands/scan_test.go | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/internal/commands/scan.go b/internal/commands/scan.go index f7c602d8d..45992c3e0 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -1363,10 +1363,17 @@ func runScaResolver(sourceDir, scaResolver, scaResolverParams, projectName strin func addScaResults(zipWriter *zip.Writer) error { logger.PrintIfVerbose("Included SCA Results: " + ".cxsca-results.json") dat, err := ioutil.ReadFile(scaResolverResultsFile) + scaResultsFile := strings.TrimSuffix(scaResolverResultsFile, ".json") _ = os.Remove(scaResolverResultsFile) if err != nil { return err } + removeErr := os.Remove(scaResultsFile) + if removeErr != nil { + log.Printf("Failed to remove file %s: %v", scaResultsFile, removeErr) + } else { + log.Printf("Successfully removed file %s", scaResultsFile) + } f, err := zipWriter.Create(".cxsca-results.json") if err != nil { return err diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 536447c36..e8d4a6f4c 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -3,7 +3,12 @@ package commands import ( + "archive/zip" + "bytes" "fmt" + "io/ioutil" + "log" + "os" "reflect" "strings" "testing" @@ -1187,3 +1192,47 @@ func TestValidateContainerImageFormat(t *testing.T) { }) } } + +func TestRunScaResolverAndAddScaResultsFileCleanup(t *testing.T) { + // Step 1: Set up and simulate `scaResolverResultsFile` as a global + tempFile, err := ioutil.TempFile("", "sca_results_test") + assert.NilError(t, err) + defer os.Remove(tempFile.Name()) // Clean up after test + + // Setting the global variable for testing + scaResolverResultsFile = tempFile.Name() + ".json" // Simulating the .json file creation as in `runScaResolver` + + // Create both `.json` file and the file without `.json` + _, err = os.Create(scaResolverResultsFile) // Creates scaResolverResultsFile + assert.NilError(t, err, "Expected scaResolverResultsFile to be created") + + scaResultsFile := strings.TrimSuffix(scaResolverResultsFile, ".json") + _, err = os.Create(scaResultsFile) // Creates scaResultsFile without .json + assert.NilError(t, err, "Expected scaResultsFile to be created") + + // Step 2: Set up a mock zip writer and log capture + var buffer bytes.Buffer + zipWriter := zip.NewWriter(&buffer) + var logBuffer bytes.Buffer + log.SetOutput(&logBuffer) + defer func() { + log.SetOutput(os.Stderr) // Reset after test + }() + + // Step 3: Run `addScaResults` to perform file cleanup + err = addScaResults(zipWriter) + assert.NilError(t, err) + zipWriter.Close() + + // Step 4: Check that both files were deleted + _, err = os.Stat(scaResolverResultsFile) + assert.Assert(t, os.IsNotExist(err), "Expected scaResolverResultsFile to be deleted") + + _, err = os.Stat(scaResultsFile) + assert.Assert(t, os.IsNotExist(err), "Expected scaResultsFile to be deleted") + + // Step 5: Check log output to confirm removal messages + logOutput := logBuffer.String() + t.Logf("Log output:\n%s", logOutput) + assert.Assert(t, strings.Contains(logOutput, "Successfully removed file"), "Expected success log for file removal") +} From af05fb3a79df7fb0d9a99def16a6d2780830cb46 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 4 Nov 2024 11:38:40 +0200 Subject: [PATCH 2/6] fix test --- internal/commands/scan_test.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index e8d4a6f4c..43db11283 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -1194,44 +1194,37 @@ func TestValidateContainerImageFormat(t *testing.T) { } func TestRunScaResolverAndAddScaResultsFileCleanup(t *testing.T) { - // Step 1: Set up and simulate `scaResolverResultsFile` as a global tempFile, err := ioutil.TempFile("", "sca_results_test") assert.NilError(t, err) - defer os.Remove(tempFile.Name()) // Clean up after test + defer os.Remove(tempFile.Name()) - // Setting the global variable for testing - scaResolverResultsFile = tempFile.Name() + ".json" // Simulating the .json file creation as in `runScaResolver` + scaResolverResultsFile = tempFile.Name() + ".json" - // Create both `.json` file and the file without `.json` - _, err = os.Create(scaResolverResultsFile) // Creates scaResolverResultsFile + _, err = os.Create(scaResolverResultsFile) assert.NilError(t, err, "Expected scaResolverResultsFile to be created") scaResultsFile := strings.TrimSuffix(scaResolverResultsFile, ".json") - _, err = os.Create(scaResultsFile) // Creates scaResultsFile without .json + _, err = os.Create(scaResultsFile) assert.NilError(t, err, "Expected scaResultsFile to be created") - // Step 2: Set up a mock zip writer and log capture var buffer bytes.Buffer zipWriter := zip.NewWriter(&buffer) var logBuffer bytes.Buffer log.SetOutput(&logBuffer) defer func() { - log.SetOutput(os.Stderr) // Reset after test + log.SetOutput(os.Stderr) }() - // Step 3: Run `addScaResults` to perform file cleanup err = addScaResults(zipWriter) assert.NilError(t, err) zipWriter.Close() - // Step 4: Check that both files were deleted _, err = os.Stat(scaResolverResultsFile) assert.Assert(t, os.IsNotExist(err), "Expected scaResolverResultsFile to be deleted") _, err = os.Stat(scaResultsFile) assert.Assert(t, os.IsNotExist(err), "Expected scaResultsFile to be deleted") - // Step 5: Check log output to confirm removal messages logOutput := logBuffer.String() t.Logf("Log output:\n%s", logOutput) assert.Assert(t, strings.Contains(logOutput, "Successfully removed file"), "Expected success log for file removal") From 80186551b5dfe108177b52d4d6819d2d1230d925 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Tue, 5 Nov 2024 18:31:49 +0200 Subject: [PATCH 3/6] change function test name and add comments --- internal/commands/scan_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 43db11283..fad057342 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -1193,38 +1193,55 @@ func TestValidateContainerImageFormat(t *testing.T) { } } -func TestRunScaResolverAndAddScaResultsFileCleanup(t *testing.T) { +func Test_WhenScaResolverAndResultsFileExist_ThenAddScaResultsShouldRemoveThemAfterAddingToZip(t *testing.T) { + // Step 1: Create a temporary file to simulate the SCA results file and check for errors. tempFile, err := ioutil.TempFile("", "sca_results_test") assert.NilError(t, err) + + // Step 2: Schedule deletion of the temporary file after the test completes. defer os.Remove(tempFile.Name()) + // Step 3: Define the path for scaResolverResultsFile, adding ".json" extension. scaResolverResultsFile = tempFile.Name() + ".json" + // Step 4: Create scaResolverResultsFile on disk to simulate its existence before running addScaResults. _, err = os.Create(scaResolverResultsFile) assert.NilError(t, err, "Expected scaResolverResultsFile to be created") + // Step 5: Define and create scaResultsFile (without ".json" extension) to simulate another required file. scaResultsFile := strings.TrimSuffix(scaResolverResultsFile, ".json") _, err = os.Create(scaResultsFile) assert.NilError(t, err, "Expected scaResultsFile to be created") + // Step 6: Set up a buffer to collect the zip file's contents. var buffer bytes.Buffer zipWriter := zip.NewWriter(&buffer) + + // Step 7: Redirect log output to logBuffer to capture logs for validation. var logBuffer bytes.Buffer log.SetOutput(&logBuffer) + + // Step 8: Ensure log output is reset to standard error after the test completes. defer func() { log.SetOutput(os.Stderr) }() + // Step 9: Call addScaResults, which should add results to the zipWriter and delete temporary files. err = addScaResults(zipWriter) assert.NilError(t, err) + + // Step 10: Close the zip writer to complete the writing process. zipWriter.Close() + // Step 11: Check if scaResolverResultsFile was successfully deleted after addScaResults ran. _, err = os.Stat(scaResolverResultsFile) assert.Assert(t, os.IsNotExist(err), "Expected scaResolverResultsFile to be deleted") + // Step 12: Check if scaResultsFile was successfully deleted as well. _, err = os.Stat(scaResultsFile) assert.Assert(t, os.IsNotExist(err), "Expected scaResultsFile to be deleted") + // Step 13: Validate log output to confirm the success message for file removal is present. logOutput := logBuffer.String() t.Logf("Log output:\n%s", logOutput) assert.Assert(t, strings.Contains(logOutput, "Successfully removed file"), "Expected success log for file removal") From 8b1dcbe2b46c44ef49164cf8434e79e22035fab3 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Tue, 5 Nov 2024 18:35:03 +0200 Subject: [PATCH 4/6] Fix test with comments --- internal/commands/scan_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index fad057342..d738118fb 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -1194,7 +1194,7 @@ func TestValidateContainerImageFormat(t *testing.T) { } func Test_WhenScaResolverAndResultsFileExist_ThenAddScaResultsShouldRemoveThemAfterAddingToZip(t *testing.T) { - // Step 1: Create a temporary file to simulate the SCA results file and check for errors. + // Step 1: Create a temporary file to simulate the SCA results file and check for errors. tempFile, err := ioutil.TempFile("", "sca_results_test") assert.NilError(t, err) From f930729daefe32de63e897868204ccfef134a0b3 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Tue, 12 Nov 2024 10:42:22 +0200 Subject: [PATCH 5/6] pull from main --- internal/commands/scan_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index fe1ced806..102058197 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -1221,7 +1221,7 @@ func Test_WhenScaResolverAndResultsFileExist_ThenAddScaResultsShouldRemoveThemAf var logBuffer bytes.Buffer log.SetOutput(&logBuffer) - // Step 8: Ensure log output is reset to standard error after the test completes. + // Step 8 : Ensure log output is reset to standard error after the test completes. defer func() { log.SetOutput(os.Stderr) }() From a70f9ec7a3b6b0f0fcf8b8468d4b415f41472a73 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Tue, 12 Nov 2024 11:57:27 +0200 Subject: [PATCH 6/6] Fix lint error in test --- internal/commands/scan_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 102058197..6186bccf8 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -6,7 +6,6 @@ import ( "archive/zip" "bytes" "fmt" - "io/ioutil" "log" "os" "reflect" @@ -1195,7 +1194,7 @@ func TestValidateContainerImageFormat(t *testing.T) { func Test_WhenScaResolverAndResultsFileExist_ThenAddScaResultsShouldRemoveThemAfterAddingToZip(t *testing.T) { // Step 1: Create a temporary file to simulate the SCA results file and check for errors. - tempFile, err := ioutil.TempFile("", "sca_results_test") + tempFile, err := os.CreateTemp("", "sca_results_test") assert.NilError(t, err) // Step 2: Schedule deletion of the temporary file after the test completes.