Skip to content

Conversation

danwinship
Copy link
Contributor

originally written for #328, later reused in #330, but anyway, we currently have no tests of the parsing code, so let's fix that

/kind test
/sig network
/assign @aojea

@k8s-ci-robot k8s-ci-robot added the sig/network Categorizes an issue or PR as relevant to SIG Network. label Sep 30, 2025
@k8s-ci-robot
Copy link
Contributor

@danwinship: The label(s) kind/test cannot be applied, because the repository doesn't have them.

In response to this:

originally written for #328, later reused in #330, but anyway, we currently have no tests of the parsing code, so let's fix that

/kind test
/sig network
/assign @aojea

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot requested a review from aojea September 30, 2025 21:08
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 30, 2025
@k8s-ci-robot k8s-ci-robot requested a review from dims September 30, 2025 21:08
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Sep 30, 2025
ip: "bad ip",
family: IPFamilyUnknown,
},
func checkOneIPFamily(t *testing.T, ip string, expectedFamily, family IPFamily, isIPv4, isIPv6 bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, this aggregate all the repetitiva assertions on the existing test

},
{
desc: "a byte slice of length other than 4 or 16 is an invalid net.IP",
ips: []net.IP{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

net/ips_test.go Outdated
Comment on lines 335 to 337
// This tests that if you try to parse an "ifaddr-style" CIDR string with
// ParseCIDRSloppy, the *net.IPNet return value has the bits beyond the
// prefix length masked out.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the golang std parser behavior https://github.com/golang/go/blob/eb1c7f6e69b0e62067ff22a0656cedff792c8438/src/net/ip.go#L542-L550 , it this comment makes it sound like this is something specific of our fork, but IIUIC is the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I can clarify that

Comment on lines +31 to +32
skipFamily bool
skipParse bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if instead of using these fields that makes the test data have to understand how it is used, we use different subsets and then the test can build the final slice?

// Instead of one big slice with skip fields:
var testIPs = []testCase{
    {desc: "...", skipParse: true, skipFamily: false, ...},
    ...
}

// Create subsets for each test:
var goodTestIPsForParse = []testCase{ ... }
var goodTestIPsForFamily = []testCase{ ... }
var badTestIPsForParse = []testCase{ ... }
var badTestIPsForFamily = []testCase{ ... }

so the test will look like

func TestParseIPSloppy(t *testing.T) {
    for _, tc := range goodTestIPsForParse {
        t.Run(tc.desc, func(t *testing.T) {
            ...
        })
    }
    for _, tc := range badTestIPsForParse {
        t.Run(tc.desc, func(t *testing.T) {
            ...
        })
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That still "makes the test data have to understand how it is used" though, it just moves the understanding from the individual-test-case level to the test-case-array level. Right?

Also, the whole point of this was to very clearly have one set of test data. When I first started the netutilsv2 branch, I copied the parsing tests over from utilvalidation in k/k, and then wrote some new tests for the conversion functions. But I realized that there ended up being cases that got tested for IPFamilyOf and AddrFromIP but not for ParseIP, or that got tested for ParsePrefix and IsIPv4CIDR but not IPNetFromPrefix, etc. I wanted to make sure I was testing all of the edge cases for all of the functions, and that they all agreed on which IPs/CIDRs are "good" and which are "bad".

The skip fields were a necessary kludge added at the end, when I realized that we couldn't have completely uniform testing given (a) in the netutilsv1 API, the functions don't entirely agree on which values are good and bad (fixed in v2), and (b) the API expectation that ifaddr-style CIDRs are accepted-but-truncated. But there are only 4 skips out of 40 test cases, because overall, the goal is still to test all the test cases against every function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair, is not that my alternative is much better

Comment on lines +91 to +92
// A /32 IPMask is equivalent to 255.255.255.255
func() net.IP { _, ipnet, _ := net.ParseCIDR("1.2.3.4/32"); return net.IP(ipnet.Mask) }(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you really got me here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a somewhat goofy case... I could remove it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is ok, sorry, I was going through the last pass and I got interrupted, let me finish but I think is good to merge

}
t.Run(tc.desc, func(t *testing.T) {
for i, ipnet := range tc.ipnets {
errStr := ipnet.String()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious about what will be this output so I printed it, basically this test that the stringified version of the an invalid ipnet is not parseable

parse_test.go:93: errStr="<nil>"
=== RUN   TestParseCIDRSloppy/IPNet_containing_invalid_IP_is_invalid
    parse_test.go:93: errStr="<nil>"
=== RUN   TestParseCIDRSloppy/IPNet_containing_non-CIDR_Mask_is_invalid
    parse_test.go:93: errStr="192.168.0.0/ff00ff00"
=== RUN   TestParseCIDRSloppy/IPNet_containing_IPv6_IP_and_IPv4_Mask_is_invalid
    parse_test.go:93: errStr="<nil>"
--- PASS: TestParseCIDRSloppy (0.00s)

@aojea
Copy link
Member

aojea commented Oct 2, 2025

/lgtm
/approve

It test roundtripping of paring and multiple representations and uses a single place to have the ips and ipnets testcases that simplifies creating new cases

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 2, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aojea, danwinship

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@aojea
Copy link
Member

aojea commented Oct 2, 2025

@danwinship the PR is blocked in this job

Test / test (1.21.x, macos-latest) (pull_request)

I do not have permissions to retrigger it, @dims do you have?

otherwise I think repushing will retrigger

@dims
Copy link
Member

dims commented Oct 2, 2025

i was able to re-trigger @aojea

@k8s-ci-robot k8s-ci-robot merged commit bc988d5 into kubernetes:master Oct 2, 2025
25 of 26 checks passed
@danwinship danwinship deleted the net-tests branch October 2, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. sig/network Categorizes an issue or PR as relevant to SIG Network. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants