Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new Management service protocol (NetworkMap) #193

Merged
merged 18 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add Serial test to the engine
  • Loading branch information
braginini committed Jan 17, 2022
commit 5eab4ac94d3720eee27a1c7182fb169b34dd07b1
13 changes: 11 additions & 2 deletions client/internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Engine struct {
// signal is a Signal Service client
signal signal.Client
// mgmClient is a Management Service client
mgmClient *mgm.GrpcClient
mgmClient mgm.Client
// peerConns is a map that holds all the peers that are known to this peer
peerConns map[string]*peer.Conn

Expand Down Expand Up @@ -77,7 +77,7 @@ type Peer struct {
}

// NewEngine creates a new Connection Engine
func NewEngine(signalClient signal.Client, mgmClient *mgm.GrpcClient, config *EngineConfig, cancel context.CancelFunc, ctx context.Context) *Engine {
func NewEngine(signalClient signal.Client, mgmClient mgm.Client, config *EngineConfig, cancel context.CancelFunc, ctx context.Context) *Engine {
return &Engine{
signal: signalClient,
mgmClient: mgmClient,
Expand Down Expand Up @@ -208,6 +208,15 @@ func (e *Engine) GetPeerConnectionStatus(peerKey string) peer.ConnStatus {

return -1
}
func (e *Engine) GetPeers() []string {
e.syncMsgMux.Lock()
defer e.syncMsgMux.Unlock()
peers := []string{}
for s, _ := range e.peerConns {
peers = append(peers, s)
}
return peers
}

// GetConnectedPeers returns a connection Status or nil if peer connection wasn't found
func (e *Engine) GetConnectedPeers() []string {
Expand Down
107 changes: 106 additions & 1 deletion client/internal/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,26 @@ func TestEngine_Serial(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

engine := NewEngine(&SignalClientMock{}, nil, &EngineConfig{
updates := make(chan *mgmtProto.SyncResponse)

syncFunc := func(msgHandler func(msg *mgmtProto.SyncResponse) error) error {
loop:
for {
select {
case msg, ok := <-updates:
if !ok {
break loop
}
err := msgHandler(msg)
if err != nil {
t.Fatal(err)
}
}
}
return nil
}

engine := NewEngine(&signal.MockClient{}, &mgmt.MockClient{SyncFunc: syncFunc}, &EngineConfig{
WgIfaceName: "utun100",
WgAddr: "100.64.0.1/24",
WgPrivateKey: key,
Expand All @@ -62,6 +81,92 @@ func TestEngine_Serial(t *testing.T) {
}
}()

err = engine.Start()
if err != nil {
t.Fatal(err)
return
}

peer1 := &mgmtProto.RemotePeerConfig{
WgPubKey: "RRHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
AllowedIps: []string{"100.64.0.10/24"},
}
// 1st update with just 1 peer and serial larger than the current serial of the engine => apply update
updates <- &mgmtProto.SyncResponse{
NetworkMap: &mgmtProto.NetworkMap{
Serial: 1,
PeerConfig: nil,
RemotePeers: []*mgmtProto.RemotePeerConfig{peer1},
RemotePeersIsEmpty: false,
},
}

timeout := time.After(time.Second * 2)
for {
select {
case <-timeout:
t.Fatalf("timeout while waiting for test to finish")
default:
}

if len(engine.GetPeers()) == 1 && engine.networkSerial == 1 {
break
}
}

// 2nd update with just 2 peers and serial larger than the current serial of the engine => apply update
peer2 := &mgmtProto.RemotePeerConfig{
WgPubKey: "LLHf3Ma6z6mdLbriAJbqhX9+nM/B71lgw2+91q3LlhU=",
AllowedIps: []string{"100.64.0.11/24"},
}
updates <- &mgmtProto.SyncResponse{
NetworkMap: &mgmtProto.NetworkMap{
Serial: 2,
PeerConfig: nil,
RemotePeers: []*mgmtProto.RemotePeerConfig{peer1, peer2},
RemotePeersIsEmpty: false,
},
}

timeout = time.After(time.Second * 2)
for {
select {
case <-timeout:
t.Fatalf("timeout while waiting for test to finish")
default:
}

if len(engine.GetPeers()) == 2 && engine.networkSerial == 2 {
break
}
}

// 3rd update with just 3 peers and serial lower than the current serial of the engine => ignore update
peer3 := &mgmtProto.RemotePeerConfig{
WgPubKey: "KKHf3Ma6z6mdLbriAJbqhX9+nM/B71lgw2+91q3LlhU=",
AllowedIps: []string{"100.64.0.12/24"},
}
updates <- &mgmtProto.SyncResponse{
NetworkMap: &mgmtProto.NetworkMap{
Serial: 1,
PeerConfig: nil,
RemotePeers: []*mgmtProto.RemotePeerConfig{peer1, peer2, peer3},
RemotePeersIsEmpty: false,
},
}

timeout = time.After(time.Second * 2)
for {
select {
case <-timeout:
t.Fatalf("timeout while waiting for test to finish")
default:
}

if len(engine.GetPeers()) == 2 && engine.networkSerial == 2 {
break
}
}
}

func TestEngine_MultiplePeers(t *testing.T) {
Expand Down