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

Issue 110 tests improvements connlist pkg #250

Merged
merged 15 commits into from
Oct 22, 2023
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
2 changes: 1 addition & 1 deletion cmd/netpolicy/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func newCommandDiff() *cobra.Command {
c.Flags().StringVarP(&dir1, "dir1", "", "", "Original Resources path to be compared")
c.Flags().StringVarP(&dir2, "dir2", "", "", "New Resources path to compare with original resources path")
supportedDiffFormats := strings.Join(diff.ValidDiffFormats, ",")
c.Flags().StringVarP(&outFormat, "output", "o", common.DefaultFormat, "Required output format ("+supportedDiffFormats+")")
c.Flags().StringVarP(&outFormat, "output", "o", common.DefaultFormat, getOutputFormatDescription(supportedDiffFormats))
// out file
c.Flags().StringVarP(&outFile, "file", "f", "", "Write output to specified file")
return c
Expand Down
6 changes: 5 additions & 1 deletion cmd/netpolicy/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var (
outFile string // output file
)

func getOutputFormatDescription(validFormats string) string {
return fmt.Sprintf("Required output format (%s)", validFormats)
}

func runListCommand() error {
var conns []connlist.Peer2PeerConnection
var err error
Expand Down Expand Up @@ -131,7 +135,7 @@ defined`,
"Focus connections of specified workload in the output (<workload-name> or <workload-namespace/workload-name>)")
// output format - default txt
supportedFormats := strings.Join(connlist.ValidFormats, ",")
c.Flags().StringVarP(&output, "output", "o", common.DefaultFormat, "Required output format ("+supportedFormats+")")
c.Flags().StringVarP(&output, "output", "o", common.DefaultFormat, getOutputFormatDescription(supportedFormats))
// out file
c.Flags().StringVarP(&outFile, "file", "f", "", "Write output to specified file")
return c
Expand Down
15 changes: 15 additions & 0 deletions pkg/netpol/common/netpol_commands_common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package common

import "os"

// NetpolError holds information about a single error/warning that occurred during running
// connectivity analysis command (list or diff)
type NetpolError interface {
Expand All @@ -24,3 +26,16 @@ const (
DotHeader = "digraph {"
DotClosing = "}"
)

// WriteToFile generates output to given file
func WriteToFile(output, filePath string) error {
fp, err := os.Create(filePath)
if err != nil {
return err
}
_, err = fp.WriteString(output)
if err != nil {
return err
}
return nil
}
544 changes: 307 additions & 237 deletions pkg/netpol/connlist/connlist_test.go

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions pkg/netpol/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,15 +1039,8 @@ func addNewPod(namespace, name string, labels map[string]string) (*v1.Pod, error
}

func writeRes(res, fileName string) {
_, err := os.Create(fileName)
if err != nil {
fmt.Printf("error creating file: %v", err)
return
}
b := []byte(res)
err = os.WriteFile(fileName, b, 0o600)
if err != nil {
fmt.Printf("error WriteFile: %v", err)
if err := common.WriteToFile(res, fileName); err != nil {
fmt.Printf("error writing to file: %v", err)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ingress-controller} => frontend/asset-cache[Deployment] : TCP 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
backend/checkout[Deployment] => backend/recommendation[Deployment] : TCP 8080
backend/recommendation[Deployment] => backend/catalog[Deployment] : TCP 8080
backend/reports[Deployment] => backend/recommendation[Deployment] : TCP 8080
frontend/webapp[Deployment] => backend/recommendation[Deployment] : TCP 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ingress-controller} => frontend/asset-cache[Deployment] : TCP 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ingress-controller} => frontend/asset-cache[Deployment] : TCP 8080
{ingress-controller} => frontend/blog[Deployment] : TCP 8080
{ingress-controller} => frontend/webapp[Deployment] : TCP 8080
{ingress-controller} => zeroday/zeroday[Deployment] : TCP 8080
13 changes: 13 additions & 0 deletions tests/k8s_ingress_test/details-v1-79f774bdb9_connlist_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
0.0.0.0-255.255.255.255 => default/details-v1-79f774bdb9[ReplicaSet] : All Connections
default/details-v1-79f774bdb9[ReplicaSet] => 0.0.0.0-255.255.255.255 : All Connections
default/details-v1-79f774bdb9[ReplicaSet] => default/productpage-v1-6b746f74dc[ReplicaSet] : All Connections
default/details-v1-79f774bdb9[ReplicaSet] => default/ratings-v1-b6994bb9[ReplicaSet] : All Connections
default/details-v1-79f774bdb9[ReplicaSet] => default/reviews-v1-545db77b95[ReplicaSet] : All Connections
default/details-v1-79f774bdb9[ReplicaSet] => default/reviews-v2-7bf8c9648f[ReplicaSet] : All Connections
default/details-v1-79f774bdb9[ReplicaSet] => default/reviews-v3-84779c7bbc[ReplicaSet] : All Connections
default/productpage-v1-6b746f74dc[ReplicaSet] => default/details-v1-79f774bdb9[ReplicaSet] : All Connections
default/ratings-v1-b6994bb9[ReplicaSet] => default/details-v1-79f774bdb9[ReplicaSet] : All Connections
default/reviews-v1-545db77b95[ReplicaSet] => default/details-v1-79f774bdb9[ReplicaSet] : All Connections
default/reviews-v2-7bf8c9648f[ReplicaSet] => default/details-v1-79f774bdb9[ReplicaSet] : All Connections
default/reviews-v3-84779c7bbc[ReplicaSet] => default/details-v1-79f774bdb9[ReplicaSet] : All Connections
{ingress-controller} => default/details-v1-79f774bdb9[ReplicaSet] : TCP 9080
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default/checkoutservice[Deployment] => default/emailservice[Deployment] : TCP 8080