mirror of
https://github.com/netbirdio/netbird.git
synced 2025-02-07 14:00:12 +01:00
add posture checks tests
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
parent
fc7157f82f
commit
41acacfba5
@ -121,20 +121,36 @@ func initTestPostureChecksAccount(am *DefaultAccountManager) (*Account, error) {
|
|||||||
return am.Store.GetAccount(context.Background(), account.Id)
|
return am.Store.GetAccount(context.Background(), account.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
func TestPostureCheckAccountPeersUpdate(t *testing.T) {
|
||||||
manager, account, peer1, peer2, peer3 := setupNetworkMapTest(t)
|
manager, account, peer1, peer2, peer3 := setupNetworkMapTest(t)
|
||||||
|
|
||||||
err := manager.SaveGroup(context.Background(), account.Id, userID, &group.Group{
|
err := manager.SaveGroups(context.Background(), account.Id, userID, []*group.Group{
|
||||||
ID: "group-id",
|
{
|
||||||
|
ID: "groupA",
|
||||||
Name: "GroupA",
|
Name: "GroupA",
|
||||||
Peers: []string{peer1.ID, peer2.ID, peer3.ID},
|
Peers: []string{peer1.ID, peer2.ID, peer3.ID},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "groupB",
|
||||||
|
Name: "GroupB",
|
||||||
|
Peers: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "groupC",
|
||||||
|
Name: "GroupC",
|
||||||
|
Peers: []string{},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
updMsg := manager.peersUpdateManager.CreateChannel(context.Background(), peer1.ID)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
manager.peersUpdateManager.CloseChannel(context.Background(), peer1.ID)
|
||||||
|
})
|
||||||
|
|
||||||
postureCheck := posture.Checks{
|
postureCheck := posture.Checks{
|
||||||
ID: "versionCheck",
|
ID: "postureCheck",
|
||||||
Name: "Version Check",
|
Name: "postureCheck",
|
||||||
Description: "NetBird Version Check",
|
|
||||||
AccountID: account.Id,
|
AccountID: account.Id,
|
||||||
Checks: posture.ChecksDefinition{
|
Checks: posture.ChecksDefinition{
|
||||||
NBVersionCheck: &posture.NBVersionCheck{
|
NBVersionCheck: &posture.NBVersionCheck{
|
||||||
@ -143,11 +159,6 @@ func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
updMsg := manager.peersUpdateManager.CreateChannel(context.Background(), peer1.ID)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
manager.peersUpdateManager.CloseChannel(context.Background(), peer1.ID)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Saving unused posture check should not update account peers and not send peer update
|
// Saving unused posture check should not update account peers and not send peer update
|
||||||
t.Run("saving unused posture check", func(t *testing.T) {
|
t.Run("saving unused posture check", func(t *testing.T) {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
@ -166,14 +177,37 @@ func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Updating unused posture check should not update account peers and not send peer update
|
||||||
|
t.Run("updating unused posture check", func(t *testing.T) {
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
peerShouldNotReceiveUpdate(t, updMsg)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
postureCheck.Checks = posture.ChecksDefinition{
|
||||||
|
NBVersionCheck: &posture.NBVersionCheck{
|
||||||
|
MinVersion: "0.29.0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := manager.SavePostureChecks(context.Background(), account.Id, userID, &postureCheck)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
policy := Policy{
|
policy := Policy{
|
||||||
ID: "policy",
|
ID: "policyA",
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Rules: []*PolicyRule{
|
Rules: []*PolicyRule{
|
||||||
{
|
{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Sources: []string{"group-id"},
|
Sources: []string{"groupA"},
|
||||||
Destinations: []string{"group-id"},
|
Destinations: []string{"groupA"},
|
||||||
Bidirectional: true,
|
Bidirectional: true,
|
||||||
Action: PolicyTrafficActionAccept,
|
Action: PolicyTrafficActionAccept,
|
||||||
},
|
},
|
||||||
@ -181,15 +215,15 @@ func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
|||||||
SourcePostureChecks: []string{postureCheck.ID},
|
SourcePostureChecks: []string{postureCheck.ID},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adding posture check to policy should trigger update account peers and send peer update
|
// Linking posture check to policy should trigger update account peers and send peer update
|
||||||
t.Run("adding posture check to policy", func(t *testing.T) {
|
t.Run("linking posture check to policy with peers", func(t *testing.T) {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
peerShouldReceiveUpdate(t, updMsg)
|
peerShouldReceiveUpdate(t, updMsg)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := manager.SavePolicy(context.Background(), account.Id, userID, &policy)
|
err := manager.SavePolicy(context.Background(), account.Id, userID, &policy, false)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -197,14 +231,18 @@ func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
|||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Updating used posture checks should update account peers and send peer update
|
// Updating linked posture checks should update account peers and send peer update
|
||||||
t.Run("updating used posture check", func(t *testing.T) {
|
t.Run("updating linked to posture check with peers", func(t *testing.T) {
|
||||||
postureCheck.Checks = posture.ChecksDefinition{
|
postureCheck.Checks = posture.ChecksDefinition{
|
||||||
NBVersionCheck: &posture.NBVersionCheck{
|
NBVersionCheck: &posture.NBVersionCheck{
|
||||||
MinVersion: "0.28.6",
|
MinVersion: "0.29.0",
|
||||||
|
},
|
||||||
|
ProcessCheck: &posture.ProcessCheck{
|
||||||
|
Processes: []posture.Process{
|
||||||
|
{LinuxPath: "/usr/bin/netbird", MacPath: "/usr/local/bin/netbird"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +262,7 @@ func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Saving unchanged posture check should trigger account peers update and not send peer update
|
// Saving unchanged posture check should not trigger account peers update and not send peer update
|
||||||
// since there is no change in the network map
|
// since there is no change in the network map
|
||||||
t.Run("saving unchanged posture check", func(t *testing.T) {
|
t.Run("saving unchanged posture check", func(t *testing.T) {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
@ -253,7 +291,150 @@ func TestPostureCheckAccountPeerUpdate(t *testing.T) {
|
|||||||
|
|
||||||
policy.SourcePostureChecks = []string{}
|
policy.SourcePostureChecks = []string{}
|
||||||
|
|
||||||
err := manager.SavePolicy(context.Background(), account.Id, userID, &policy)
|
err := manager.SavePolicy(context.Background(), account.Id, userID, &policy, true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Deleting unused posture check should not trigger account peers update and not send peer update
|
||||||
|
t.Run("deleting unused posture check", func(t *testing.T) {
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
peerShouldNotReceiveUpdate(t, updMsg)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err := manager.DeletePostureChecks(context.Background(), account.Id, "postureCheck", userID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
err = manager.SavePostureChecks(context.Background(), account.Id, userID, &postureCheck)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Updating linked posture check to policy with no peers should not trigger account peers update and not send peer update
|
||||||
|
t.Run("updating linked posture check to policy with no peers", func(t *testing.T) {
|
||||||
|
policy = Policy{
|
||||||
|
ID: "policyB",
|
||||||
|
Enabled: true,
|
||||||
|
Rules: []*PolicyRule{
|
||||||
|
{
|
||||||
|
Enabled: true,
|
||||||
|
Sources: []string{"groupB"},
|
||||||
|
Destinations: []string{"groupC"},
|
||||||
|
Bidirectional: true,
|
||||||
|
Action: PolicyTrafficActionAccept,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
SourcePostureChecks: []string{postureCheck.ID},
|
||||||
|
}
|
||||||
|
err = manager.SavePolicy(context.Background(), account.Id, userID, &policy, false)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
peerShouldNotReceiveUpdate(t, updMsg)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
postureCheck.Checks = posture.ChecksDefinition{
|
||||||
|
NBVersionCheck: &posture.NBVersionCheck{
|
||||||
|
MinVersion: "0.29.0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := manager.SavePostureChecks(context.Background(), account.Id, userID, &postureCheck)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Updating linked posture check to policy where destination has peers but source does not
|
||||||
|
// should trigger account peers update and send peer update
|
||||||
|
t.Run("updating linked posture check to policy where destination has peers but source does not", func(t *testing.T) {
|
||||||
|
policy = Policy{
|
||||||
|
ID: "policyB",
|
||||||
|
Enabled: true,
|
||||||
|
Rules: []*PolicyRule{
|
||||||
|
{
|
||||||
|
Enabled: true,
|
||||||
|
Sources: []string{"groupB"},
|
||||||
|
Destinations: []string{"groupA"},
|
||||||
|
Bidirectional: true,
|
||||||
|
Action: PolicyTrafficActionAccept,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
SourcePostureChecks: []string{postureCheck.ID},
|
||||||
|
}
|
||||||
|
err = manager.SavePolicy(context.Background(), account.Id, userID, &policy, true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
peerShouldReceiveUpdate(t, updMsg)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
postureCheck.Checks = posture.ChecksDefinition{
|
||||||
|
NBVersionCheck: &posture.NBVersionCheck{
|
||||||
|
MinVersion: "0.29.0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := manager.SavePostureChecks(context.Background(), account.Id, userID, &postureCheck)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Updating linked posture check to policy where source has peers but destination does not,
|
||||||
|
// should trigger account peers update and send peer update
|
||||||
|
t.Run("updating linked posture check to policy where source has peers but destination does not", func(t *testing.T) {
|
||||||
|
policy = Policy{
|
||||||
|
ID: "policyB",
|
||||||
|
Enabled: true,
|
||||||
|
Rules: []*PolicyRule{
|
||||||
|
{
|
||||||
|
Enabled: true,
|
||||||
|
Sources: []string{"groupA"},
|
||||||
|
Destinations: []string{"groupB"},
|
||||||
|
Bidirectional: true,
|
||||||
|
Action: PolicyTrafficActionAccept,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
SourcePostureChecks: []string{postureCheck.ID},
|
||||||
|
}
|
||||||
|
err = manager.SavePolicy(context.Background(), account.Id, userID, &policy, true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
peerShouldReceiveUpdate(t, updMsg)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
postureCheck.Checks = posture.ChecksDefinition{
|
||||||
|
NBVersionCheck: &posture.NBVersionCheck{
|
||||||
|
MinVersion: "0.29.0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := manager.SavePostureChecks(context.Background(), account.Id, userID, &postureCheck)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
Loading…
Reference in New Issue
Block a user