From 06ae35afb984eb6b993679fc05e2a7452939aa8b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 21 Jan 2025 16:01:14 +0100 Subject: [PATCH] libnetwork/types: TestUtilGetHostPartIP: use gotest.tools, table-tests Also rename the test to TestGetHostPartIP, removing "Util" from the name. Signed-off-by: Sebastiaan van Stijn --- libnetwork/types/types_test.go | 35 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/libnetwork/types/types_test.go b/libnetwork/types/types_test.go index 91ddbbf87d..301771cde1 100644 --- a/libnetwork/types/types_test.go +++ b/libnetwork/types/types_test.go @@ -129,8 +129,8 @@ func TestCompareIPMask(t *testing.T) { } } -func TestUtilGetHostPartIP(t *testing.T) { - input := []struct { +func TestGetHostPartIP(t *testing.T) { + tests := []struct { ip net.IP mask net.IPMask host net.IP @@ -163,28 +163,25 @@ func TestUtilGetHostPartIP(t *testing.T) { }, } - for _, i := range input { - h, err := GetHostPartIP(i.ip, i.mask) - if err != nil { - t.Fatal(err) - } - if !i.host.Equal(h) { - t.Fatalf("Failed to return expected host ip. Expected: %s. Got: %s", i.host, h) - } + for _, tc := range tests { + h, err := GetHostPartIP(tc.ip, tc.mask) + assert.NilError(t, err) + assert.Assert(t, tc.host.Equal(h), "Failed to return expected host ip. Expected: %s. Got: %s", tc.host, h) } + const expectedErr = "cannot compute host portion ip address because ip and mask are not compatible" + // ip as v6 and mask as v4 are not compatible - if _, err := GetHostPartIP(net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), []byte{0xff, 0xff, 0xff, 0}); err == nil { - t.Fatalf("Unexpected success") - } + _, err := GetHostPartIP(net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), []byte{0xff, 0xff, 0xff, 0}) + assert.Check(t, is.ErrorContains(err, expectedErr)) + // ip as v4 and non conventional mask - if _, err := GetHostPartIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0}); err == nil { - t.Fatalf("Unexpected success") - } + _, err = GetHostPartIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0}) + assert.Check(t, is.ErrorContains(err, expectedErr)) + // ip as v4 and non conventional mask - if _, err := GetHostPartIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0}); err == nil { - t.Fatalf("Unexpected success") - } + _, err = GetHostPartIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0}) + assert.Check(t, is.ErrorContains(err, expectedErr)) } func TestUtilGetBroadcastIP(t *testing.T) {