Skip to content

Commit

Permalink
Move util to test, contextual errors to util (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrownus authored Nov 11, 2021
1 parent 19a9a42 commit 4453964
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 106 deletions.
4 changes: 2 additions & 2 deletions allow_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

"github.com/slackhq/nebula/cidr"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/util"
"github.com/slackhq/nebula/test"
"github.com/stretchr/testify/assert"
)

func TestNewAllowListFromConfig(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
c := config.NewC(l)
c.Settings["allowlist"] = map[interface{}]interface{}{
"192.168.0.0": true,
Expand Down
10 changes: 5 additions & 5 deletions bits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package nebula
import (
"testing"

"github.com/slackhq/nebula/util"
"github.com/slackhq/nebula/test"
"github.com/stretchr/testify/assert"
)

func TestBits(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
b := NewBits(10)

// make sure it is the right size
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestBits(t *testing.T) {
}

func TestBitsDupeCounter(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
b := NewBits(10)
b.lostCounter.Clear()
b.dupeCounter.Clear()
Expand All @@ -101,7 +101,7 @@ func TestBitsDupeCounter(t *testing.T) {
}

func TestBitsOutOfWindowCounter(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
b := NewBits(10)
b.lostCounter.Clear()
b.dupeCounter.Clear()
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestBitsOutOfWindowCounter(t *testing.T) {
}

func TestBitsLostCounter(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
b := NewBits(10)
b.lostCounter.Clear()
b.dupeCounter.Clear()
Expand Down
4 changes: 2 additions & 2 deletions cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/golang/protobuf/proto"
"github.com/slackhq/nebula/util"
"github.com/slackhq/nebula/test"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/ed25519"
Expand Down Expand Up @@ -752,7 +752,7 @@ func TestNebulaCertificate_Copy(t *testing.T) {
assert.Nil(t, err)
cc := c.Copy()

util.AssertDeepCopyEqual(t, c, cc)
test.AssertDeepCopyEqual(t, c, cc)
}

func TestUnmarshalNebulaCertificate(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/nebula-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/util"
)

// A version string that can be set with
Expand Down Expand Up @@ -60,7 +61,7 @@ func main() {
ctrl, err := nebula.Main(c, *configTest, Build, l, nil)

switch v := err.(type) {
case nebula.ContextualError:
case util.ContextualError:
v.Log(l)
os.Exit(1)
case error:
Expand Down
3 changes: 2 additions & 1 deletion cmd/nebula/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/util"
)

// A version string that can be set with
Expand Down Expand Up @@ -54,7 +55,7 @@ func main() {
ctrl, err := nebula.Main(c, *configTest, Build, l, nil)

switch v := err.(type) {
case nebula.ContextualError:
case util.ContextualError:
v.Log(l)
os.Exit(1)
case error:
Expand Down
14 changes: 7 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"testing"
"time"

"github.com/slackhq/nebula/util"
"github.com/slackhq/nebula/test"
"github.com/stretchr/testify/assert"
)

func TestConfig_Load(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
dir, err := ioutil.TempDir("", "config-test")
// invalid yaml
c := NewC(l)
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestConfig_Load(t *testing.T) {
}

func TestConfig_Get(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
// test simple type
c := NewC(l)
c.Settings["firewall"] = map[interface{}]interface{}{"outbound": "hi"}
Expand All @@ -58,14 +58,14 @@ func TestConfig_Get(t *testing.T) {
}

func TestConfig_GetStringSlice(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
c := NewC(l)
c.Settings["slice"] = []interface{}{"one", "two"}
assert.Equal(t, []string{"one", "two"}, c.GetStringSlice("slice", []string{}))
}

func TestConfig_GetBool(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
c := NewC(l)
c.Settings["bool"] = true
assert.Equal(t, true, c.GetBool("bool", false))
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestConfig_GetBool(t *testing.T) {
}

func TestConfig_HasChanged(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
// No reload has occurred, return false
c := NewC(l)
c.Settings["test"] = "hi"
Expand All @@ -115,7 +115,7 @@ func TestConfig_HasChanged(t *testing.T) {
}

func TestConfig_ReloadConfig(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
done := make(chan bool, 1)
dir, err := ioutil.TempDir("", "config-test")
assert.Nil(t, err)
Expand Down
8 changes: 4 additions & 4 deletions connection_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"github.com/flynn/noise"
"github.com/slackhq/nebula/cert"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/test"
"github.com/slackhq/nebula/udp"
"github.com/slackhq/nebula/util"
"github.com/stretchr/testify/assert"
)

var vpnIp iputil.VpnIp

func Test_NewConnectionManagerTest(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
//_, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, localrange, _ := net.ParseCIDR("10.1.1.1/24")
Expand Down Expand Up @@ -89,7 +89,7 @@ func Test_NewConnectionManagerTest(t *testing.T) {
}

func Test_NewConnectionManagerTest2(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
//_, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, localrange, _ := net.ParseCIDR("10.1.1.1/24")
Expand Down Expand Up @@ -164,7 +164,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
// Disconnect only if disconnectInvalid: true is set.
func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
now := time.Now()
l := util.NewTestLogger()
l := test.NewLogger()
ipNet := net.IPNet{
IP: net.IPv4(172, 1, 1, 2),
Mask: net.IPMask{255, 255, 255, 0},
Expand Down
6 changes: 3 additions & 3 deletions control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/cert"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/test"
"github.com/slackhq/nebula/udp"
"github.com/slackhq/nebula/util"
"github.com/stretchr/testify/assert"
)

func TestControl_GetHostInfoByVpnIp(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
// Special care must be taken to re-use all objects provided to the hostmap and certificate in the expectedInfo object
// To properly ensure we are not exposing core memory to the caller
hm := NewHostMap(l, "test", &net.IPNet{}, make([]*net.IPNet, 0))
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestControl_GetHostInfoByVpnIp(t *testing.T) {

// Make sure we don't have any unexpected fields
assertFields(t, []string{"VpnIp", "LocalIndex", "RemoteIndex", "RemoteAddrs", "CachedPackets", "Cert", "MessageCounter", "CurrentRemote"}, thi)
util.AssertDeepCopyEqual(t, &expectedInfo, thi)
test.AssertDeepCopyEqual(t, &expectedInfo, thi)

// Make sure we don't panic if the host info doesn't have a cert yet
assert.NotPanics(t, func() {
Expand Down
20 changes: 10 additions & 10 deletions firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/firewall"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/util"
"github.com/slackhq/nebula/test"
"github.com/stretchr/testify/assert"
)

func TestNewFirewall(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
c := &cert.NebulaCertificate{}
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
conntrack := fw.Conntrack
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestNewFirewall(t *testing.T) {
}

func TestFirewall_AddRule(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
ob := &bytes.Buffer{}
l.SetOutput(ob)

Expand Down Expand Up @@ -133,7 +133,7 @@ func TestFirewall_AddRule(t *testing.T) {
}

func TestFirewall_Drop(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
ob := &bytes.Buffer{}
l.SetOutput(ob)

Expand Down Expand Up @@ -308,7 +308,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
}

func TestFirewall_Drop2(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
ob := &bytes.Buffer{}
l.SetOutput(ob)

Expand Down Expand Up @@ -367,7 +367,7 @@ func TestFirewall_Drop2(t *testing.T) {
}

func TestFirewall_Drop3(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
ob := &bytes.Buffer{}
l.SetOutput(ob)

Expand Down Expand Up @@ -453,7 +453,7 @@ func TestFirewall_Drop3(t *testing.T) {
}

func TestFirewall_DropConntrackReload(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
ob := &bytes.Buffer{}
l.SetOutput(ob)

Expand Down Expand Up @@ -635,7 +635,7 @@ func Test_parsePort(t *testing.T) {
}

func TestNewFirewallFromConfig(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
// Test a bad rule definition
c := &cert.NebulaCertificate{}
conf := config.NewC(l)
Expand Down Expand Up @@ -685,7 +685,7 @@ func TestNewFirewallFromConfig(t *testing.T) {
}

func TestAddFirewallRulesFromConfig(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
// Test adding tcp rule
conf := config.NewC(l)
mf := &mockFirewall{}
Expand Down Expand Up @@ -849,7 +849,7 @@ func TestTCPRTTTracking(t *testing.T) {
}

func TestFirewall_convertRule(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
ob := &bytes.Buffer{}
l.SetOutput(ob)

Expand Down
6 changes: 3 additions & 3 deletions handshake_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

"github.com/slackhq/nebula/header"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/test"
"github.com/slackhq/nebula/udp"
"github.com/slackhq/nebula/util"
"github.com/stretchr/testify/assert"
)

func Test_NewHandshakeManagerVpnIp(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
_, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, localrange, _ := net.ParseCIDR("10.1.1.1/24")
Expand Down Expand Up @@ -66,7 +66,7 @@ func Test_NewHandshakeManagerVpnIp(t *testing.T) {
}

func Test_NewHandshakeManagerTrigger(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
_, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, localrange, _ := net.ParseCIDR("10.1.1.1/24")
Expand Down
10 changes: 5 additions & 5 deletions lighthouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/golang/protobuf/proto"
"github.com/slackhq/nebula/header"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/test"
"github.com/slackhq/nebula/udp"
"github.com/slackhq/nebula/util"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ func TestNewLhQuery(t *testing.T) {
}

func Test_lhStaticMapping(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()
lh1 := "10.128.0.2"
lh1IP := net.ParseIP(lh1)

Expand All @@ -67,7 +67,7 @@ func Test_lhStaticMapping(t *testing.T) {
}

func BenchmarkLighthouseHandleRequest(b *testing.B) {
l := util.NewTestLogger()
l := test.NewLogger()
lh1 := "10.128.0.2"
lh1IP := net.ParseIP(lh1)

Expand Down Expand Up @@ -137,7 +137,7 @@ func BenchmarkLighthouseHandleRequest(b *testing.B) {
}

func TestLighthouse_Memory(t *testing.T) {
l := util.NewTestLogger()
l := test.NewLogger()

myUdpAddr0 := &udp.Addr{IP: net.ParseIP("10.0.0.2"), Port: 4242}
myUdpAddr1 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4242}
Expand Down Expand Up @@ -266,7 +266,7 @@ func newLHHostUpdate(fromAddr *udp.Addr, vpnIp iputil.VpnIp, addrs []*udp.Addr,

//TODO: this is a RemoteList test
//func Test_lhRemoteAllowList(t *testing.T) {
// l := NewTestLogger()
// l := NewLogger()
// c := NewConfig(l)
// c.Settings["remoteallowlist"] = map[interface{}]interface{}{
// "10.20.0.0/12": false,
Expand Down
Loading

0 comments on commit 4453964

Please sign in to comment.