From 865a028f31406d0f9b9401a3dd4abf4fba3b97e2 Mon Sep 17 00:00:00 2001 From: Yury Gargay Date: Fri, 19 Jan 2024 14:41:42 +0100 Subject: [PATCH] Add OS Version check tests --- management/server/posture/os_version.go | 4 +- management/server/posture/os_version_test.go | 75 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 management/server/posture/os_version_test.go diff --git a/management/server/posture/os_version.go b/management/server/posture/os_version.go index 8bf1f8501f8..9aebeccfdaf 100644 --- a/management/server/posture/os_version.go +++ b/management/server/posture/os_version.go @@ -35,9 +35,9 @@ func (c *OSVersionCheck) Check(peer nbpeer.Peer) error { case "ios": return checkMinVersion(peerGoOS, peer.Meta.Core, c.Ios) case "linux": - return checkMinKernelVersion(peerGoOS, peer.Meta.Core, c.Linux) + return checkMinKernelVersion(peerGoOS, peer.Meta.KernelVersion, c.Linux) case "windows": - return checkMinKernelVersion(peerGoOS, peer.Meta.Core, c.Windows) + return checkMinKernelVersion(peerGoOS, peer.Meta.KernelVersion, c.Windows) } return nil } diff --git a/management/server/posture/os_version_test.go b/management/server/posture/os_version_test.go new file mode 100644 index 00000000000..045ab21fbd8 --- /dev/null +++ b/management/server/posture/os_version_test.go @@ -0,0 +1,75 @@ +package posture + +import ( + "testing" + + "github.com/netbirdio/netbird/management/server/peer" + + "github.com/stretchr/testify/assert" +) + +func TestOSVersionCheck_Check(t *testing.T) { + tests := []struct { + name string + input peer.Peer + check OSVersionCheck + wantErr bool + }{ + { + name: "Valid Peer Linux Kernel version", + input: peer.Peer{ + Meta: peer.PeerSystemMeta{ + GoOS: "linux", + KernelVersion: "6.1.1", + }, + }, + check: OSVersionCheck{ + Linux: &MinKernelVersionCheck{ + MinKernelVersion: "6.0.0", + }, + }, + wantErr: false, + }, + { + name: "Valid Peer macOS version", + input: peer.Peer{ + Meta: peer.PeerSystemMeta{ + GoOS: "darwin", + Core: "14.2.1", + }, + }, + check: OSVersionCheck{ + Darwin: &MinVersionCheck{ + MinVersion: "13", + }, + }, + wantErr: false, + }, + { + name: "No valid Peer macOS version", + input: peer.Peer{ + Meta: peer.PeerSystemMeta{ + GoOS: "darwin", + Core: "14.2.1", + }, + }, + check: OSVersionCheck{ + Darwin: &MinVersionCheck{ + MinVersion: "15", + }, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.check.Check(tt.input) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +}