|
3 | 3 | package commands |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "archive/zip" |
| 7 | + "bytes" |
6 | 8 | "fmt" |
| 9 | + "log" |
| 10 | + "os" |
7 | 11 | "reflect" |
8 | 12 | "strings" |
9 | 13 | "testing" |
@@ -1188,6 +1192,60 @@ func TestValidateContainerImageFormat(t *testing.T) { |
1188 | 1192 | } |
1189 | 1193 | } |
1190 | 1194 |
|
| 1195 | +func Test_WhenScaResolverAndResultsFileExist_ThenAddScaResultsShouldRemoveThemAfterAddingToZip(t *testing.T) { |
| 1196 | + // Step 1: Create a temporary file to simulate the SCA results file and check for errors. |
| 1197 | + tempFile, err := os.CreateTemp("", "sca_results_test") |
| 1198 | + assert.NilError(t, err) |
| 1199 | + |
| 1200 | + // Step 2: Schedule deletion of the temporary file after the test completes. |
| 1201 | + defer os.Remove(tempFile.Name()) |
| 1202 | + |
| 1203 | + // Step 3: Define the path for scaResolverResultsFile, adding ".json" extension. |
| 1204 | + scaResolverResultsFile = tempFile.Name() + ".json" |
| 1205 | + |
| 1206 | + // Step 4: Create scaResolverResultsFile on disk to simulate its existence before running addScaResults. |
| 1207 | + _, err = os.Create(scaResolverResultsFile) |
| 1208 | + assert.NilError(t, err, "Expected scaResolverResultsFile to be created") |
| 1209 | + |
| 1210 | + // Step 5: Define and create scaResultsFile (without ".json" extension) to simulate another required file. |
| 1211 | + scaResultsFile := strings.TrimSuffix(scaResolverResultsFile, ".json") |
| 1212 | + _, err = os.Create(scaResultsFile) |
| 1213 | + assert.NilError(t, err, "Expected scaResultsFile to be created") |
| 1214 | + |
| 1215 | + // Step 6: Set up a buffer to collect the zip file's contents. |
| 1216 | + var buffer bytes.Buffer |
| 1217 | + zipWriter := zip.NewWriter(&buffer) |
| 1218 | + |
| 1219 | + // Step 7: Redirect log output to logBuffer to capture logs for validation. |
| 1220 | + var logBuffer bytes.Buffer |
| 1221 | + log.SetOutput(&logBuffer) |
| 1222 | + |
| 1223 | + // Step 8 : Ensure log output is reset to standard error after the test completes. |
| 1224 | + defer func() { |
| 1225 | + log.SetOutput(os.Stderr) |
| 1226 | + }() |
| 1227 | + |
| 1228 | + // Step 9: Call addScaResults, which should add results to the zipWriter and delete temporary files. |
| 1229 | + err = addScaResults(zipWriter) |
| 1230 | + assert.NilError(t, err) |
| 1231 | + |
| 1232 | + // Step 10: Close the zip writer to complete the writing process. |
| 1233 | + zipWriter.Close() |
| 1234 | + |
| 1235 | + // Step 11: Check if scaResolverResultsFile was successfully deleted after addScaResults ran. |
| 1236 | + _, err = os.Stat(scaResolverResultsFile) |
| 1237 | + assert.Assert(t, os.IsNotExist(err), "Expected scaResolverResultsFile to be deleted") |
| 1238 | + |
| 1239 | + // Step 12: Check if scaResultsFile was successfully deleted as well. |
| 1240 | + _, err = os.Stat(scaResultsFile) |
| 1241 | + assert.Assert(t, os.IsNotExist(err), "Expected scaResultsFile to be deleted") |
| 1242 | + |
| 1243 | + // Step 13: Validate log output to confirm the success message for file removal is present. |
| 1244 | + logOutput := logBuffer.String() |
| 1245 | + t.Logf("Log output:\n%s", logOutput) |
| 1246 | + assert.Assert(t, strings.Contains(logOutput, "Successfully removed file"), "Expected success log for file removal") |
| 1247 | +} |
| 1248 | + |
1191 | 1249 | func TestFilterMatched(t *testing.T) { |
1192 | 1250 | tests := []struct { |
1193 | 1251 | name string |
|
0 commit comments