generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactor implementation #10
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b25948
Refactor implementation
yokaze eb5ba6d
Apply suggestions from code review
yokaze 0e43978
Fix compare result
yokaze 31822e4
Update cmd/list.go
yokaze 5bb2a0b
Reflect comments
yokaze a1ac70d
Modify compare procedure
yokaze eea9c1d
Add comment
yokaze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,94 @@ | ||
package e2e | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testJson(g Gomega, target []byte, expected string) { | ||
var t, e interface{} | ||
err := json.Unmarshal(target, &t) | ||
g.Expect(err).NotTo(HaveOccurred(), "actual: %s", target) | ||
|
||
err = json.Unmarshal([]byte(expected), &e) | ||
g.Expect(err).NotTo(HaveOccurred(), "expected: %s", expected) | ||
|
||
if !reflect.DeepEqual(t, e) { | ||
err := fmt.Errorf("compare failed. actual: %s, expected: %s", target, expected) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
} | ||
|
||
func testList() { | ||
cases := []struct { | ||
Selector string | ||
Expected string | ||
}{ | ||
{ | ||
Selector: "test=self", | ||
Expected: `[{ | ||
"direction": "EGRESS", | ||
"kind": "CiliumNetworkPolicy", | ||
"namespace": "default", | ||
"name": "l3-egress" | ||
}]`, | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
EGRESS,CiliumNetworkPolicy,default,l3-egress | ||
EGRESS,CiliumNetworkPolicy,default,l4-egress | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline`, | ||
}, | ||
{ | ||
Selector: "test=l3-ingress-explicit-allow", | ||
Expected: `[{ | ||
"direction": "INGRESS", | ||
"kind": "CiliumNetworkPolicy", | ||
"namespace": "default", | ||
"name": "l3-ingress-explicit-allow" | ||
}]`, | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-ingress-explicit-allow`, | ||
}, | ||
{ | ||
Selector: "test=l3-ingress-no-rule", | ||
Expected: `[]`, | ||
Selector: "test=l3-ingress-implicit-deny", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline`, | ||
}, | ||
{ | ||
Selector: "test=l3-ingress-explicit-deny", | ||
Expected: `[{ | ||
"direction": "INGRESS", | ||
"kind": "CiliumNetworkPolicy", | ||
"namespace": "default", | ||
"name": "l3-ingress-explicit-deny" | ||
}]`, | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-ingress-explicit-deny`, | ||
}, | ||
{ | ||
Selector: "test=l3-egress-implicit-deny", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline`, | ||
}, | ||
{ | ||
Selector: "test=l3-egress-explicit-deny", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline`, | ||
}, | ||
{ | ||
Selector: "test=l4-ingress-explicit-allow-any", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l4-ingress-explicit-allow-any`, | ||
}, | ||
{ | ||
Selector: "test=l4-ingress-explicit-allow-tcp", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l4-ingress-explicit-allow-tcp`, | ||
}, | ||
{ | ||
Selector: "test=l4-ingress-explicit-deny-any", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l4-ingress-explicit-deny-any`, | ||
}, | ||
{ | ||
Selector: "test=l4-ingress-explicit-deny-udp", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l4-ingress-explicit-deny-udp`, | ||
}, | ||
{ | ||
Selector: "test=l4-egress-explicit-deny-any", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline`, | ||
}, | ||
{ | ||
Selector: "test=l4-egress-explicit-deny-tcp", | ||
Expected: `EGRESS,CiliumNetworkPolicy,default,l3-baseline | ||
INGRESS,CiliumNetworkPolicy,default,l3-baseline`, | ||
}, | ||
} | ||
|
||
It("should list applied policies", func() { | ||
for _, c := range cases { | ||
podName := onePodByLabelSelector(Default, "default", c.Selector) | ||
result := runViewerSafe(Default, nil, "list", "-o=json", podName) | ||
testJson(Default, result, c.Expected) | ||
result := runViewerSafe(Default, nil, "list", "-o=json", "--no-headers", podName) | ||
result = jqSafe(Default, result, "-r", ".[] | [.direction, .kind, .namespace, .name] | @csv") | ||
resultString := strings.Replace(string(result), `"`, "", -1) | ||
Expect(resultString).To(Equal(c.Expected), "compare failed. selector: %s, actual: %s, expected: %s", c.Selector, resultString, c.Expected) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# NetworkPolicy Configuration for Test Pods | ||
|
||
| Target | From self (Egress) | To pod (Ingress) | | ||
|-|-|-| | ||
| l3-ingress-explicit-allow | allow | allow | | ||
| l3-ingress-implicit-deny | allow | - | | ||
| l3-ingress-explicit-deny | allow | deny | | ||
| l3-egress-implicit-deny | - | - | | ||
| l3-egress-explicit-deny | deny | - | | ||
| l4-ingress-explicit-allow-any | allow (L4) | allow (L4) | | ||
| l4-ingress-explicit-allow-tcp | allow (L4) | allow (L4) | | ||
| l4-ingress-explicit-deny-any | allow (L4) | deny (L4) | | ||
| l4-ingress-explicit-deny-udp | allow (L4) | deny (L4) | | ||
| l4-egress-explicit-deny-any | deny (L4) | - | | ||
| l4-egress-explicit-deny-tcp | deny (L4) | - | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if the first
ret
is negative, it will be overwritten by otherstring.Compare()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chez-shanpu
It should be blocked by
if ret == 0
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yokaze I was mistaken. thank you :) lgtm