-
Notifications
You must be signed in to change notification settings - Fork 135
support CIDR and IPv6 #772
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||
| package lib | ||||||
|
|
||||||
| import ( | ||||||
| "testing" | ||||||
|
|
||||||
| partitionTypes "github.com/litmuschaos/litmus-go/pkg/generic/pod-network-partition/types" | ||||||
| ) | ||||||
|
|
||||||
| func Test_SetExceptIPs_CIDRHandling(t *testing.T) { | ||||||
| np := &NetworkPolicy{} | ||||||
| exp := &partitionTypes.ExperimentDetails{ | ||||||
| DestinationIPs: "10.0.0.5,10.0.0.0/24,10.0.1.0/28, 2001:db8::1,2001:db8::/64,10.0.0.5,2001:db8::1/64", | ||||||
| } | ||||||
| if err := np.setExceptIPs(exp); err != nil { | ||||||
| t.Fatalf("unexpected error: %v", err) | ||||||
| } | ||||||
| want := []string{ | ||||||
| "10.0.0.5/32", | ||||||
| "10.0.0.0/24", | ||||||
| "10.0.1.0/28", | ||||||
| "2001:db8::1/128", | ||||||
| "2001:db8::/64", | ||||||
| "2001:db8::1/64", | ||||||
| } | ||||||
| got := np.ExceptIPs | ||||||
| if len(got) != len(want) { | ||||||
| t.Fatalf("len mismatch got=%v want=%v", got, want) | ||||||
| } | ||||||
| for i := range want { | ||||||
| if got[i] != want[i] { | ||||||
| t.Fatalf("index %d got=%s want=%s full=%v", i, got[i], want[i], got) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func Test_normalizeIPOrCIDR(t *testing.T) { | ||||||
| cases := map[string]string{ | ||||||
| "10.1.1.1": "10.1.1.1/32", | ||||||
| "10.1.1.0/28": "10.1.1.0/28", | ||||||
| "2001:db8::1": "2001:db8::1/128", | ||||||
| "2001:db8::/64": "2001:db8::/64", | ||||||
| } | ||||||
| for in, expect := range cases { | ||||||
| out, err := normalizeIPOrCIDR(in) | ||||||
| if err != nil { | ||||||
| t.Fatalf("unexpected err for %s: %v", in, err) | ||||||
| } | ||||||
| if out != expect { | ||||||
| t.Fatalf("normalize %s got %s want %s", in, out, expect) | ||||||
| } | ||||||
| } | ||||||
| bad := []string{"", "foo", "10.0.0.0/33", "2001:db8::/129"} | ||||||
| for _, in := range bad { | ||||||
| if _, err := normalizeIPOrCIDR(in); err == nil && in != "" { | ||||||
|
||||||
| if _, err := normalizeIPOrCIDR(in); err == nil && in != "" { | |
| if _, err := normalizeIPOrCIDR(in); err == nil { |
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.
I have changed the test file, now it shows no error for empty string.
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.
This comment is incomplete and doesn't clearly describe what the function does. It should be expanded to: "normalizeIPOrCIDR validates and normalizes IP addresses or CIDR blocks, adding appropriate subnet masks (/32 for IPv4, /128 for IPv6) to plain IP addresses."
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.
I have added these comments.