Skip to content

Commit

Permalink
Merge pull request #29 from thaJeztah/remove_gotest_tools
Browse files Browse the repository at this point in the history
remove gotest.tools dependency
  • Loading branch information
justincormack authored Jan 13, 2023
2 parents 624d3f5 + 7ec5809 commit d2a9113
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 59 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module github.com/moby/ipvs
go 1.13

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/vishvananda/netlink v1.1.0
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527
gotest.tools/v3 v3.0.2
)
16 changes: 0 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444 h1:/d2cWp6PSamH4jDPFLyO150psQdqvtoNX8Zjg3AQ31g=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
146 changes: 105 additions & 41 deletions ipvs_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//go:build linux
// +build linux

package ipvs

import (
"net"
"reflect"
"runtime"
"syscall"
"testing"
Expand All @@ -13,8 +15,6 @@ import (
"github.com/vishvananda/netlink"
"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

var (
Expand Down Expand Up @@ -63,7 +63,9 @@ func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, check
var dstFound bool

dstArray, err := i.GetDestinations(s)
assert.NilError(t, err)
if err != nil {
t.Fatalf("Failed to get destination; %v", err)
}

for _, dst := range dstArray {
if dst.Address.Equal(d.Address) && dst.Port == d.Port &&
Expand All @@ -75,12 +77,12 @@ func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, check
}

switch checkPresent {
case true: //The test expects the service to be present
case true: // The test expects the service to be present
if !dstFound {

t.Fatalf("Did not find the service %s in ipvs output", d.Address.String())
}
case false: //The test expects that the service should not be present
case false: // The test expects that the service should not be present
if dstFound {
t.Fatalf("Did not find the destination %s fwdMethod %s in ipvs output", d.Address.String(), lookupFwMethod(d.ConnectionFlags))
}
Expand All @@ -91,7 +93,9 @@ func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, check
func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {

svcArray, err := i.GetServices()
assert.NilError(t, err)
if err != nil {
t.Fatalf("Failed to get service; %v", err)
}

var svcFound bool

Expand All @@ -104,12 +108,12 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {
}

switch checkPresent {
case true: //The test expects the service to be present
case true: // The test expects the service to be present
if !svcFound {

t.Fatalf("Did not find the service %s in ipvs output", s.Address.String())
}
case false: //The test expects that the service should not be present
case false: // The test expects that the service should not be present
if svcFound {
t.Fatalf("Did not expect the service %s in ipvs output", s.Address.String())
}
Expand All @@ -119,15 +123,21 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {

func TestGetFamily(t *testing.T) {
id, err := getIPVSFamily()
assert.NilError(t, err)
assert.Check(t, 0 != id)
if err != nil {
t.Fatal("Failed to get IPVS family:", err)
}
if id == 0 {
t.Error("IPVS family was 0")
}
}

func TestService(t *testing.T) {
defer setupTestOSContext(t)()

i, err := New("")
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create IPVS handle:", err)
}

for _, protocol := range protocols {
for _, schedMethod := range schedMethods {
Expand Down Expand Up @@ -169,7 +179,9 @@ func TestService(t *testing.T) {
}

err := i.NewService(&s)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create service:", err)
}
checkService(t, i, &s, true)
for _, updateSchedMethod := range schedMethods {
if updateSchedMethod == schedMethod {
Expand All @@ -178,18 +190,30 @@ func TestService(t *testing.T) {

s.SchedName = updateSchedMethod
err = i.UpdateService(&s)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to update service:", err)
}
checkService(t, i, &s, true)

scopy, err := i.GetService(&s)
assert.NilError(t, err)
assert.Check(t, is.Equal((*scopy).Address.String(), s.Address.String()))
assert.Check(t, is.Equal((*scopy).Port, s.Port))
assert.Check(t, is.Equal((*scopy).Protocol, s.Protocol))
if err != nil {
t.Fatal("Failed to get service:", err)
}
if expected := (*scopy).Address.String(); expected != s.Address.String() {
t.Errorf("expected: %v, got: %v", expected, s.Address.String())
}
if expected := (*scopy).Port; expected != s.Port {
t.Errorf("expected: %v, got: %v", expected, s.Port)
}
if expected := (*scopy).Protocol; expected != s.Protocol {
t.Errorf("expected: %v, got: %v", expected, s.Protocol)
}
}

err = i.DelService(&s)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to delete service:", err)
}
checkService(t, i, &s, false)
}
}
Expand Down Expand Up @@ -217,16 +241,22 @@ func TestService(t *testing.T) {
for _, svc := range svcs {
if !i.IsServicePresent(&svc) {
err = i.NewService(&svc)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create service:", err)
}
checkService(t, i, &svc, true)
} else {
t.Errorf("svc: %v exists", svc)
}
}
err = i.Flush()
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to flush:", err)
}
got, err := i.GetServices()
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to get service:", err)
}
if len(got) != 0 {
t.Errorf("Unexpected services after flush")
}
Expand All @@ -240,27 +270,37 @@ func createDummyInterface(t *testing.T) {
}

err := netlink.LinkAdd(dummy)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to add link:", err)
}

dummyLink, err := netlink.LinkByName("dummy")
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to get dummy link:", err)
}

ip, ipNet, err := net.ParseCIDR("10.1.1.1/24")
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to parse CIDR:", err)
}

ipNet.IP = ip

ipAddr := &netlink.Addr{IPNet: ipNet, Label: ""}
err = netlink.AddrAdd(dummyLink, ipAddr)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to add IP address:", err)
}
}

func TestDestination(t *testing.T) {
defer setupTestOSContext(t)()

createDummyInterface(t)
i, err := New("")
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create IPVS handle:", err)
}

for _, protocol := range protocols {
testDatas := []struct {
Expand Down Expand Up @@ -304,7 +344,9 @@ func TestDestination(t *testing.T) {
}

err := i.NewService(&s)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create service:", err)
}
checkService(t, i, &s, true)

s.SchedName = ""
Expand All @@ -320,7 +362,9 @@ func TestDestination(t *testing.T) {
}
destinations = append(destinations, d)
err := i.NewDestination(&s, &d)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create destination:", err)
}
checkDestination(t, i, &s, &d, true)
}

Expand All @@ -331,13 +375,17 @@ func TestDestination(t *testing.T) {
for _, d := range destinations {
d.ConnectionFlags = updateFwdMethod
err = i.UpdateDestination(&s, &d)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to update destination:", err)
}
checkDestination(t, i, &s, &d, true)
}
}
for _, d := range destinations {
err = i.DelDestination(&s, &d)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to delete destination:", err)
}
checkDestination(t, i, &s, &d, false)
}
}
Expand All @@ -350,36 +398,52 @@ func TestTimeouts(t *testing.T) {
defer setupTestOSContext(t)()

i, err := New("")
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to create IPVS handle:", err)
}

_, err = i.GetConfig()
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to get config:", err)
}

cfg := Config{66 * time.Second, 66 * time.Second, 66 * time.Second}
err = i.SetConfig(&cfg)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to set config:", err)
}

c2, err := i.GetConfig()
assert.NilError(t, err)
assert.DeepEqual(t, cfg, *c2)
if err != nil {
t.Fatal("Failed to get config:", err)
}
if !reflect.DeepEqual(*c2, cfg) {
t.Fatalf("expected: %+v, got: %+v", cfg, *c2)
}

// A timeout value 0 means that the current timeout value of the corresponding entry is preserved
cfg = Config{77 * time.Second, 0 * time.Second, 77 * time.Second}
err = i.SetConfig(&cfg)
assert.NilError(t, err)
if err != nil {
t.Fatal("Failed to set config:", err)
}

c3, err := i.GetConfig()
assert.NilError(t, err)
assert.DeepEqual(t, *c3, Config{77 * time.Second, 66 * time.Second, 77 * time.Second})
if err != nil {
t.Fatal("Failed to get config:", err)
}
expected := Config{77 * time.Second, 66 * time.Second, 77 * time.Second}
if !reflect.DeepEqual(*c3, expected) {
t.Fatalf("expected: %+v, got: %+v", expected, *c3)
}
}

// setupTestOSContext joins a new network namespace, and returns its associated
// teardown function.
//
// Example usage:
//
// defer setupTestOSContext(t)()
//
// defer setupTestOSContext(t)()
func setupTestOSContext(t *testing.T) func() {
t.Helper()
runtime.LockOSThread()
Expand All @@ -389,7 +453,7 @@ func setupTestOSContext(t *testing.T) func() {

fd, err := syscall.Open("/proc/self/ns/net", syscall.O_RDONLY, 0)
if err != nil {
t.Fatal("Failed to open netns file")
t.Fatal("Failed to open netns file:", err)
}

// Since we are switching to a new test namespace make
Expand Down
2 changes: 2 additions & 0 deletions netlink_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package ipvs
Expand Down Expand Up @@ -37,6 +38,7 @@ func Test_getIPFamily(t *testing.T) {
}

for _, testcase := range testcases {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
family, err := getIPFamily(testcase.address)
if !reflect.DeepEqual(err, testcase.expectedErr) {
Expand Down

0 comments on commit d2a9113

Please sign in to comment.