Skip to content

Commit

Permalink
Make start of routes watch fallible
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <[email protected]>
  • Loading branch information
hwipl committed Dec 13, 2023
1 parent 71690f1 commit c95f854
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
30 changes: 22 additions & 8 deletions internal/routes/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import (
"golang.org/x/sys/unix"
)

// Watcher is the watcher interface.
type Watcher interface {
Start() error
Stop()
Probes() chan struct{}
}

// Watch waits for routing update events and then probes the
// trusted https servers.
type Watch struct {
events chan netlink.RouteUpdate
probes chan struct{}
done chan struct{}
}
Expand All @@ -24,17 +32,11 @@ func (w *Watch) sendProbe() {

// start starts the Watch.
func (w *Watch) start() {
// register for route update events
events := make(chan netlink.RouteUpdate)
if err := netlink.RouteSubscribe(events, w.done); err != nil {
log.WithError(err).Fatal("TND route subscribe error")
}

// run initial probe
w.sendProbe()

// handle route update events
for e := range events {
for e := range w.events {
switch e.Type {
case unix.RTM_NEWROUTE:
log.WithField("dst", e.Dst).Debug("TND got route NEW event")
Expand All @@ -45,9 +47,20 @@ func (w *Watch) start() {
}
}

// netlinkRouteSubscribe is netlink.RouteSubscribe for testing.
var netlinkRouteSubscribe = netlink.RouteSubscribe

// Start starts the Watch.
func (w *Watch) Start() {
func (w *Watch) Start() error {
// register for route update events
if err := netlinkRouteSubscribe(w.events, w.done); err != nil {
log.WithError(err).Error("TND route subscribe error")
return err
}

// start watcher
go w.start()
return nil
}

// Stop stops the Watch.
Expand All @@ -67,6 +80,7 @@ func (w *Watch) Probes() chan struct{} {
// NewWatch returns a new Watch.
func NewWatch(probes chan struct{}) *Watch {
return &Watch{
events: make(chan netlink.RouteUpdate),
probes: probes,
done: make(chan struct{}),
}
Expand Down
35 changes: 30 additions & 5 deletions internal/routes/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package routes

import "testing"
import (
"errors"
"testing"

"github.com/vishvananda/netlink"
)

// TestWatchStartStop tests Start and Stop of Watch.
func TestWatchStartStop(_ *testing.T) {
func TestWatchStartStop(t *testing.T) {
probes := make(chan struct{})
rw := NewWatch(probes)
rw.Start()
rw.Stop()

t.Run("subscribe error", func(t *testing.T) {
defer func() { netlinkRouteSubscribe = netlink.RouteSubscribe }()
netlinkRouteSubscribe = func(chan<- netlink.RouteUpdate, <-chan struct{}) error {
return errors.New("test error")
}

rw := NewWatch(probes)
if err := rw.Start(); err == nil {
t.Error("start should fail")
}
})

t.Run("no errors", func(t *testing.T) {
rw := NewWatch(probes)
if err := rw.Start(); err != nil {
t.Errorf("start should not fail: %v", err)
}
rw.Stop()
})
}

// TestWatchProbes tests Probes of Watch.
Expand All @@ -23,6 +45,9 @@ func TestWatchProbes(t *testing.T) {
func TestNewWatch(t *testing.T) {
probes := make(chan struct{})
rw := NewWatch(probes)
if rw.events == nil {
t.Errorf("got nil, want != nil")
}
if rw.probes != probes {
t.Errorf("got %p, want %p", rw.probes, probes)
}
Expand Down

0 comments on commit c95f854

Please sign in to comment.