Skip to content

Commit

Permalink
handling case of json output which can not be saved under its test dir
Browse files Browse the repository at this point in the history
  • Loading branch information
shireenf-ibm committed Nov 19, 2023
1 parent e2fe5d2 commit f20ee2b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
29 changes: 18 additions & 11 deletions pkg/cli/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ func TestCommandsFailExecute(t *testing.T) {
// TestListCommandOutput tests the output of legal list command
func TestListCommandOutput(t *testing.T) {
cases := []struct {
dirName string
focusWorkload string
format string
outputFile string
dirName string
focusWorkload string
format string
outputFile string
specialExpectedOutFile string
}{
// when focusWorkload is empty, output should be the connlist of the dir
// when format is empty - output should be in defaultFormat (txt)
Expand All @@ -212,11 +213,12 @@ func TestListCommandOutput(t *testing.T) {
dirName: "acs-security-demos",
focusWorkload: "ingress-controller",
},
// {
// dirName: "onlineboutique_workloads",
// focusWorkload: "emailservice",
// format: "json",
// },
{
dirName: "onlineboutique_workloads",
focusWorkload: "emailservice",
format: "json",
specialExpectedOutFile: filepath.Join("tests_outputs", "onlineboutique_workloads_emailservice.json"),
},
{
dirName: "onlineboutique_workloads",
focusWorkload: "emailservice",
Expand Down Expand Up @@ -244,14 +246,19 @@ func TestListCommandOutput(t *testing.T) {
}
for _, tt := range cases {
tt := tt
specialOutFileFlag := false
testName, expectedOutputFileName := getListCmdTestNameAndExpectedOutputFile(tt.dirName, tt.focusWorkload, tt.format)
if tt.specialExpectedOutFile != "" {
expectedOutputFileName = tt.specialExpectedOutFile
specialOutFileFlag = true
}
t.Run(testName, func(t *testing.T) {
args := []string{"list", "--dirpath", filepath.Join(testutils.GetTestsDirWithDepth(currentDirDepth), tt.dirName)}
args = append(args, addCmdOptionalArgs(tt.format, tt.outputFile, tt.focusWorkload)...)
actualOut, err := buildAndExecuteCommand(args)
require.Nil(t, err, "test: %q", testName)
testutils.CheckActualVsExpectedOutputMatch(t, tt.dirName, expectedOutputFileName, actualOut, testInfo(testName), tt.outputFile,
currentDirDepth)
currentDirDepth, specialOutFileFlag)
})
}
}
Expand Down Expand Up @@ -302,7 +309,7 @@ func TestDiffCommandOutput(t *testing.T) {
require.Nil(t, err, "test: %q", testName)
expectedOutputFileName := getDiffCmdExpectedOutputFile(tt.dir1, tt.format)
testutils.CheckActualVsExpectedOutputMatch(t, tt.dir2, expectedOutputFileName, actualOut, testInfo(testName), tt.outputFile,
currentDirDepth)
currentDirDepth, false)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"src": "default/checkoutservice[Deployment]",
"dst": "default/emailservice[Deployment]",
"conn": "TCP 8080"
}
]
17 changes: 0 additions & 17 deletions pkg/cli/tests_outputs/test_legal_list.txt

This file was deleted.

5 changes: 4 additions & 1 deletion pkg/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ func DiffTestName(ref1, ref2 string) string {
// if not generates actual output file
// if --update flag is on, writes the actual output to the expected output file
func CheckActualVsExpectedOutputMatch(t *testing.T, dirName, expectedOutputFileName, actualOutput, testInfo, outFile string,
currDirDepth int) {
currDirDepth int, specialOutputFilePath bool) {
expectedOutputFile := filepath.Join(GetTestsDirWithDepth(currDirDepth), dirName, expectedOutputFileName)
if specialOutputFilePath { // expected output file is given as a path (not under test dir)
expectedOutputFile = expectedOutputFileName
}
// if the --update flag is on (then generate/ override the expected output file with the actualOutput)
if *update {
err := output.WriteToFile(actualOutput, expectedOutputFile)
Expand Down
4 changes: 2 additions & 2 deletions pkg/netpol/connlist/connlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestConnListFromDir(t *testing.T) {
out, err := pTest.analyzer.ConnectionsListToString(res)
require.Nil(t, err, pTest.testInfo)
testutils.CheckActualVsExpectedOutputMatch(t, tt.testDirName,
pTest.expectedOutputFileName, out, pTest.testInfo, "", testutils.StandardPkgLevelDepth)
pTest.expectedOutputFileName, out, pTest.testInfo, "", testutils.StandardPkgLevelDepth, false)
}
})
}
Expand All @@ -102,7 +102,7 @@ func TestConnListFromResourceInfos(t *testing.T) {
out, err := pTest.analyzer.ConnectionsListToString(res)
require.Nil(t, err, pTest.testInfo)
testutils.CheckActualVsExpectedOutputMatch(t, tt.testDirName,
pTest.expectedOutputFileName, out, pTest.testInfo, "", testutils.StandardPkgLevelDepth)
pTest.expectedOutputFileName, out, pTest.testInfo, "", testutils.StandardPkgLevelDepth, false)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/netpol/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDiff(t *testing.T) {
actualOutput, err := pTest.analyzer.ConnectivityDiffToString(diffRes)
require.Nil(t, err, pTest.testInfo)
testutils.CheckActualVsExpectedOutputMatch(t, tt.secondDirName,
pTest.expectedOutputFileName, actualOutput, pTest.testInfo, "", testutils.StandardPkgLevelDepth)
pTest.expectedOutputFileName, actualOutput, pTest.testInfo, "", testutils.StandardPkgLevelDepth, false)
}
}
})
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestDiffOutputWithArgNamesOption(t *testing.T) {
require.Nil(t, err)
testName := "TsetOutputWithArgNamesOption." + format
testutils.CheckActualVsExpectedOutputMatch(t, ref2,
testName, res, testName, "", testutils.StandardPkgLevelDepth)
testName, res, testName, "", testutils.StandardPkgLevelDepth, false)
}
}

Expand Down

0 comments on commit f20ee2b

Please sign in to comment.