Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting a copy file of sca.json when scan runnig with ScaResolver (AST-48074) #924

Merged
merged 15 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1362,10 +1362,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
Expand Down
58 changes: 58 additions & 0 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
package commands

import (
"archive/zip"
"bytes"
"fmt"
"log"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -1188,6 +1192,60 @@ 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 := os.CreateTemp("", "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
OrShamirCM marked this conversation as resolved.
Show resolved Hide resolved
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")
}

func TestFilterMatched(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading