Skip to content

Commit

Permalink
Harmonize assertion handling
Browse files Browse the repository at this point in the history
Also abort tests early on fatal errors
  • Loading branch information
stv0g committed Nov 19, 2022
1 parent 4db1c61 commit 30bbb28
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 47 deletions.
26 changes: 20 additions & 6 deletions stdnet/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ func TestStdNet(t *testing.T) {

t.Run("Interfaces", func(t *testing.T) {
nw, err := NewNet()
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

interfaces, err := nw.Interfaces()
assert.NoError(t, err, "should succeed")
if !assert.NoError(t, err, "should succeed") {
return
}

log.Debugf("interfaces: %+v", interfaces)
for _, ifc := range interfaces {
if ifc.Name == lo0String {
_, err := ifc.Addresses()
assert.NoError(t, err, "should succeed")
if !assert.NoError(t, err, "should succeed") {
return
}
}

if addrs, err := ifc.Addresses(); err == nil {
Expand All @@ -39,7 +47,9 @@ func TestStdNet(t *testing.T) {

t.Run("ResolveUDPAddr", func(t *testing.T) {
nw, err := NewNet()
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

udpAddr, err := nw.ResolveUDPAddr(udpString, "localhost:1234")
if !assert.NoError(t, err, "should succeed") {
Expand All @@ -51,7 +61,9 @@ func TestStdNet(t *testing.T) {

t.Run("ListenPacket", func(t *testing.T) {
nw, err := NewNet()
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

conn, err := nw.ListenPacket(udpString, "127.0.0.1:0")
if !assert.NoError(t, err, "should succeed") {
Expand All @@ -68,7 +80,9 @@ func TestStdNet(t *testing.T) {

t.Run("ListenUDP random port", func(t *testing.T) {
nw, err := NewNet()
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

srcAddr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Expand Down
5 changes: 4 additions & 1 deletion vnet/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ func TestUDPConn(t *testing.T) {

var n int
n, err = conn.Write(data)
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

assert.Equal(t, len(data), n, "should match")

loop:
Expand Down
10 changes: 6 additions & 4 deletions vnet/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ func TestNetVirtual(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, wan, "should succeed")
if !assert.NoError(t, err, "should succeed") {
return
}

err = wan.AddHost("test.pion.ly", "30.31.32.33")
assert.NoError(t, err, "should succeed")
Expand Down Expand Up @@ -711,8 +712,9 @@ func TestNetVirtual(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, wan, "should succeed")
if !assert.NoError(t, err, "should succeed") {
return
}

net1, err := NewNet(&NetConfig{
StaticIPs: []string{
Expand Down
96 changes: 64 additions & 32 deletions vnet/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func TestRouterStandalone(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
if !assert.Nil(t, err, "should succeed") {
return
}

assert.Nil(t, err, "should succeed")
assert.Equal(t, "1.2.3.0", r.ipv4Net.IP.String(), "ip should match")
assert.Equal(t, "ffffff00", r.ipv4Net.Mask.String(), "mask should match")
})
Expand All @@ -61,7 +63,9 @@ func TestRouterStandalone(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

for i := 1; i < 255; i++ {
ip, err2 := r.assignIPAddress()
Expand All @@ -81,7 +85,9 @@ func TestRouterStandalone(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

nic, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
Expand All @@ -94,6 +100,7 @@ func TestRouterStandalone(t *testing.T) {
// Now, eth0 must have one address assigned
eth0, err := nic.getInterface("eth0")
assert.Nil(t, err, "should succeed")

addrs, err := eth0.Addresses()
assert.Nil(t, err, "should succeed")
assert.Equal(t, 1, len(addrs), "should match")
Expand All @@ -107,7 +114,9 @@ func TestRouterStandalone(t *testing.T) {
CIDR: "0.0.0.0/0",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

r2, err := NewRouter(&RouterConfig{
CIDR: "192.168.0.0/24",
Expand All @@ -117,7 +126,9 @@ func TestRouterStandalone(t *testing.T) {

LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

err = r1.AddNet(r2)
assert.Nil(t, err, "should succeed")
Expand All @@ -133,7 +144,9 @@ func TestRouterStandalone(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

nic := make([]*dummyNIC, 2)
ip := make([]*net.UDPAddr, 2)
Expand Down Expand Up @@ -193,7 +206,9 @@ func TestRouterStandalone(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

nic := make([]*dummyNIC, 2)
ip := make([]*net.UDPAddr, 2)
Expand Down Expand Up @@ -272,6 +287,7 @@ func TestRouterStandalone(t *testing.T) {

err = r.Stop()
assert.Nil(t, err, "should succeed")

assert.Equal(t, int32(0), atomic.LoadInt32(&nCbs0), "should be zero")
assert.Equal(t, int32(1), atomic.LoadInt32(&nCbs1), "should be zero")
assert.Equal(t, byte(2), seq, "should be the last chunk")
Expand All @@ -293,7 +309,9 @@ func TestRouterDelay(t *testing.T) {
MaxJitter: maxJitter,
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

nic := make([]*dummyNIC, 2)
ip := make([]*net.UDPAddr, 2)
Expand Down Expand Up @@ -382,6 +400,10 @@ func TestRouterOneChild(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
if !assert.Nil(t, err, "should succeed") {
return
}

nw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
Expand All @@ -397,13 +419,18 @@ func TestRouterOneChild(t *testing.T) {
// Now, eth0 must have one address assigned
wanIP, err := getIPAddr(wanNet)
assert.Nil(t, err, "should succeed")

log.Debugf("wanIP: %s", wanIP)

// LAN
lan, err := NewRouter(&RouterConfig{
CIDR: "192.168.0.0/24",
LoggerFactory: loggerFactory,
})
if !assert.Nil(t, err, "should succeed") {
return
}

lnw, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
return
Expand All @@ -421,7 +448,9 @@ func TestRouterOneChild(t *testing.T) {
log.Debugf("lanIP: %s", lanIP)

err = wan.AddRouter(lan)
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

lanNet.onInboundChunkHandler = func(c Chunk) {
log.Debugf("lanNet received: %s", c.String())
Expand Down Expand Up @@ -481,8 +510,9 @@ func TestRouterStaticIPs(t *testing.T) {
},
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
assert.NotNil(t, lan, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

assert.Equal(t, 3, len(lan.staticIPs), "should be 3")
assert.Equal(t, "1.2.3.1", lan.staticIPs[0].String(), "should match")
Expand All @@ -501,8 +531,9 @@ func TestRouterStaticIPs(t *testing.T) {
StaticIP: demoIP,
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
assert.NotNil(t, lan, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

assert.Equal(t, 4, len(lan.staticIPs), "should be 4")
assert.Equal(t, "1.2.3.1", lan.staticIPs[0].String(), "should match")
Expand All @@ -522,7 +553,6 @@ func TestRouterStaticIPs(t *testing.T) {
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, lan, "should succeed")

assert.Equal(t, 3, len(lan.staticIPs), "should be 3")
assert.Equal(t, "1.2.3.1", lan.staticIPs[0].String(), "should match")
Expand Down Expand Up @@ -575,12 +605,7 @@ func TestRouterStaticIPs(t *testing.T) {
CIDR: "0.0.0.0/0",
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return
}
if !assert.NotNil(t, wan, "should succeed") {
return
}
assert.NoError(t, err, "should succeed")

lan, err := NewRouter(&RouterConfig{
CIDR: "192.168.0.0/24",
Expand All @@ -594,12 +619,7 @@ func TestRouterStaticIPs(t *testing.T) {
},
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return
}
if !assert.NotNil(t, lan, "should succeed") {
return
}
assert.NoError(t, err, "should succeed")

err = wan.AddRouter(lan)
if !assert.NoError(t, err, "should succeed") {
Expand Down Expand Up @@ -630,7 +650,9 @@ func TestRouterFailures(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

err = r.Stop()
assert.Error(t, err, "should fail")
Expand All @@ -641,7 +663,9 @@ func TestRouterFailures(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

nic, err := NewNet(&NetConfig{
StaticIPs: []string{
Expand All @@ -661,7 +685,9 @@ func TestRouterFailures(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

r2, err := NewRouter(&RouterConfig{
CIDR: "192.168.0.0/24",
Expand All @@ -671,7 +697,9 @@ func TestRouterFailures(t *testing.T) {

LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

err = r1.AddRouter(r2)
assert.Error(t, err, "should fail")
Expand All @@ -682,7 +710,9 @@ func TestRouterFailures(t *testing.T) {
CIDR: "1.2.3.0/24",
LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

r2, err := NewRouter(&RouterConfig{
CIDR: "192.168.0.0/24",
Expand All @@ -692,7 +722,9 @@ func TestRouterFailures(t *testing.T) {

LoggerFactory: loggerFactory,
})
assert.Nil(t, err, "should succeed")
if !assert.Nil(t, err, "should succeed") {
return
}

err = r1.AddChildRouter(r2)
assert.Error(t, err, "should fail")
Expand Down
2 changes: 0 additions & 2 deletions vnet/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestStressTestUDP(t *testing.T) {
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, wan, "should succeed")

net0, err := NewNet(&NetConfig{
StaticIPs: []string{demoIP},
Expand All @@ -46,7 +45,6 @@ func TestStressTestUDP(t *testing.T) {
LoggerFactory: loggerFactory,
})
assert.NoError(t, err, "should succeed")
assert.NotNil(t, lan, "should succeed")

net1, err := NewNet(&NetConfig{})
if !assert.NoError(t, err, "should succeed") {
Expand Down
Loading

0 comments on commit 30bbb28

Please sign in to comment.