mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-16 11:39:57 +00:00
* Wraps syestem info / posture checks into a goroutine with timeout e.checks = checks is set before doing the SyncMeta, so if it fails next time isCheckEquals compares true and bypasses the update. This is to avoid another repeating the 15 seconds hang. The checks will be synced on reconnect or posture checks changes push from mgmt. * Propagate context to OS calls that can leverage its cancellation / timeout * Distinguish timeout from cancellation in logs * Dont log twice * Block on timeout failure and reapply the exclude_ips * Refactor for complexity
101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
func Test_LocalWTVersion(t *testing.T) {
|
|
got := GetInfo(context.TODO())
|
|
want := "development"
|
|
assert.Equal(t, want, got.NetbirdVersion)
|
|
}
|
|
|
|
func Test_UIVersion(t *testing.T) {
|
|
ctx := context.Background()
|
|
want := "netbird-desktop-ui/development"
|
|
ctx = metadata.NewOutgoingContext(ctx, map[string][]string{
|
|
"user-agent": {want},
|
|
})
|
|
|
|
got := GetInfo(ctx)
|
|
assert.Equal(t, want, got.UIVersion)
|
|
}
|
|
|
|
func Test_CustomHostname(t *testing.T) {
|
|
// nolint
|
|
ctx := context.WithValue(context.Background(), DeviceNameCtxKey, "custom-host")
|
|
want := "custom-host"
|
|
|
|
got := GetInfo(ctx)
|
|
assert.Equal(t, want, got.Hostname)
|
|
}
|
|
|
|
func TestGetInfoWithChecksTimeout_Success(t *testing.T) {
|
|
info, ok := GetInfoWithChecksTimeout(context.Background(), 30*time.Second, nil)
|
|
assert.True(t, ok, "expected gathering to complete within the timeout")
|
|
assert.NotNil(t, info)
|
|
}
|
|
|
|
func TestGetInfoWithChecksTimeout_Timeout(t *testing.T) {
|
|
// A 1ns budget expires before the (real) system-info gathering can finish, so the
|
|
// caller must get (nil, false) instead of blocking on the in-flight goroutine.
|
|
info, ok := GetInfoWithChecksTimeout(context.Background(), time.Nanosecond, nil)
|
|
assert.False(t, ok, "expected timeout to be reported")
|
|
assert.Nil(t, info)
|
|
}
|
|
|
|
func Test_NetAddresses(t *testing.T) {
|
|
addr, err := networkAddresses()
|
|
if err != nil {
|
|
t.Errorf("failed to discover network addresses: %s", err)
|
|
}
|
|
if len(addr) == 0 {
|
|
t.Errorf("no network addresses found")
|
|
}
|
|
}
|
|
|
|
func TestInfo_RemoveAddresses(t *testing.T) {
|
|
addr := func(cidr string) NetworkAddress {
|
|
return NetworkAddress{NetIP: netip.MustParsePrefix(cidr)}
|
|
}
|
|
|
|
info := &Info{
|
|
NetworkAddresses: []NetworkAddress{
|
|
addr("192.168.1.7/24"),
|
|
addr("100.76.70.97/32"), // overlay v4 (host mask /32)
|
|
addr("2001:818:c51b:4800:845:a65d:ae6f:623f/64"), // real global v6
|
|
addr("fd00:1234::1/64"), // overlay v6
|
|
},
|
|
}
|
|
|
|
// Overlay addresses as the engine knows them, with a different mask (/16, /64).
|
|
info.removeAddresses(
|
|
netip.MustParseAddr("100.76.70.97"),
|
|
netip.MustParseAddr("fd00:1234::1"),
|
|
)
|
|
|
|
want := []string{"192.168.1.7/24", "2001:818:c51b:4800:845:a65d:ae6f:623f/64"}
|
|
if len(info.NetworkAddresses) != len(want) {
|
|
t.Fatalf("got %d addresses, want %d: %v", len(info.NetworkAddresses), len(want), info.NetworkAddresses)
|
|
}
|
|
for i, w := range want {
|
|
if got := info.NetworkAddresses[i].NetIP.String(); got != w {
|
|
t.Errorf("address[%d] = %s, want %s", i, got, w)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInfo_RemoveAddresses_NoOp(t *testing.T) {
|
|
info := &Info{NetworkAddresses: []NetworkAddress{{NetIP: netip.MustParsePrefix("10.0.0.1/24")}}}
|
|
info.removeAddresses()
|
|
if len(info.NetworkAddresses) != 1 {
|
|
t.Errorf("expected no change with empty input, got %v", info.NetworkAddresses)
|
|
}
|
|
}
|