diff --git a/pkg/cli/command_test.go b/pkg/cli/command_test.go index ea656c51..b37d6a03 100644 --- a/pkg/cli/command_test.go +++ b/pkg/cli/command_test.go @@ -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) @@ -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", @@ -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) }) } } @@ -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) }) } } diff --git a/pkg/cli/tests_outputs/onlineboutique_workloads_emailservice.json b/pkg/cli/tests_outputs/onlineboutique_workloads_emailservice.json new file mode 100644 index 00000000..f3c3fec9 --- /dev/null +++ b/pkg/cli/tests_outputs/onlineboutique_workloads_emailservice.json @@ -0,0 +1,7 @@ +[ + { + "src": "default/checkoutservice[Deployment]", + "dst": "default/emailservice[Deployment]", + "conn": "TCP 8080" + } +] \ No newline at end of file diff --git a/pkg/cli/tests_outputs/test_legal_list.txt b/pkg/cli/tests_outputs/test_legal_list.txt deleted file mode 100644 index a6e0c030..00000000 --- a/pkg/cli/tests_outputs/test_legal_list.txt +++ /dev/null @@ -1,17 +0,0 @@ -0.0.0.0-255.255.255.255 => default/redis-cart-78746d49dc[ReplicaSet] : All Connections -default/checkoutservice-69c8ff664b[ReplicaSet] => default/cartservice-74f56fd4b[ReplicaSet] : TCP 7070 -default/checkoutservice-69c8ff664b[ReplicaSet] => default/currencyservice-77654bbbdd[ReplicaSet] : TCP 7000 -default/checkoutservice-69c8ff664b[ReplicaSet] => default/emailservice-54c7c5d9d[ReplicaSet] : TCP 8080 -default/checkoutservice-69c8ff664b[ReplicaSet] => default/paymentservice-bbcbdc6b6[ReplicaSet] : TCP 50051 -default/checkoutservice-69c8ff664b[ReplicaSet] => default/productcatalogservice-68765d49b6[ReplicaSet] : TCP 3550 -default/checkoutservice-69c8ff664b[ReplicaSet] => default/shippingservice-5bd985c46d[ReplicaSet] : TCP 50051 -default/frontend-99684f7f8[ReplicaSet] => default/adservice-77d5cd745d[ReplicaSet] : TCP 9555 -default/frontend-99684f7f8[ReplicaSet] => default/cartservice-74f56fd4b[ReplicaSet] : TCP 7070 -default/frontend-99684f7f8[ReplicaSet] => default/checkoutservice-69c8ff664b[ReplicaSet] : TCP 5050 -default/frontend-99684f7f8[ReplicaSet] => default/currencyservice-77654bbbdd[ReplicaSet] : TCP 7000 -default/frontend-99684f7f8[ReplicaSet] => default/productcatalogservice-68765d49b6[ReplicaSet] : TCP 3550 -default/frontend-99684f7f8[ReplicaSet] => default/recommendationservice-5f8c456796[ReplicaSet] : TCP 8080 -default/frontend-99684f7f8[ReplicaSet] => default/shippingservice-5bd985c46d[ReplicaSet] : TCP 50051 -default/loadgenerator-555fbdc87d[ReplicaSet] => default/frontend-99684f7f8[ReplicaSet] : TCP 8080 -default/recommendationservice-5f8c456796[ReplicaSet] => default/productcatalogservice-68765d49b6[ReplicaSet] : TCP 3550 -default/redis-cart-78746d49dc[ReplicaSet] => 0.0.0.0-255.255.255.255 : All Connections \ No newline at end of file diff --git a/pkg/internal/testutils/testutils.go b/pkg/internal/testutils/testutils.go index 3945ceae..a9400c5a 100644 --- a/pkg/internal/testutils/testutils.go +++ b/pkg/internal/testutils/testutils.go @@ -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) diff --git a/pkg/netpol/connlist/connlist_test.go b/pkg/netpol/connlist/connlist_test.go index 4dc15c91..1f335a93 100644 --- a/pkg/netpol/connlist/connlist_test.go +++ b/pkg/netpol/connlist/connlist_test.go @@ -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) } }) } @@ -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) } }) } diff --git a/pkg/netpol/diff/diff_test.go b/pkg/netpol/diff/diff_test.go index 87ca7956..3c41a18c 100644 --- a/pkg/netpol/diff/diff_test.go +++ b/pkg/netpol/diff/diff_test.go @@ -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) } } }) @@ -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) } }