From 7dc9ba4eaf746755e9151cfa9e3d92c079ba2b77 Mon Sep 17 00:00:00 2001 From: jnathangreeg Date: Sun, 17 Nov 2024 11:34:45 +0200 Subject: [PATCH 1/2] add if for cases that the apiVersion is not string Signed-off-by: jnathangreeg --- objectsenvelopes/hostsensor/hostsensordataenvelope.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/objectsenvelopes/hostsensor/hostsensordataenvelope.go b/objectsenvelopes/hostsensor/hostsensordataenvelope.go index 782ed497..5b438555 100644 --- a/objectsenvelopes/hostsensor/hostsensordataenvelope.go +++ b/objectsenvelopes/hostsensor/hostsensordataenvelope.go @@ -143,9 +143,14 @@ func IsTypeTypeHostSensor(object map[string]interface{}) bool { } if apiVersion, ok := object["apiVersion"]; ok { - if group := strings.Split(apiVersion.(string), "/"); group[0] == GroupHostSensor { + apiVersionStr, ok := apiVersion.(string) + if !ok { + return false + } + if group := strings.Split(apiVersionStr, "/"); group[0] == GroupHostSensor { return true } } return false } + From a3b34acab6ba6d2e40e96fc68cf2338c47d3f312 Mon Sep 17 00:00:00 2001 From: jnathangreeg Date: Sun, 17 Nov 2024 11:55:44 +0200 Subject: [PATCH 2/2] add unit test for the func Signed-off-by: jnathangreeg --- .../hostsensor/hostsensordataenvelope_test.go | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/objectsenvelopes/hostsensor/hostsensordataenvelope_test.go b/objectsenvelopes/hostsensor/hostsensordataenvelope_test.go index 02b71f11..00086f68 100644 --- a/objectsenvelopes/hostsensor/hostsensordataenvelope_test.go +++ b/objectsenvelopes/hostsensor/hostsensordataenvelope_test.go @@ -1 +1,51 @@ package hostsensor + +import ( + "testing" +) + +func TestIsTypeTypeHostSensor(t *testing.T) { + tests := []struct { + name string + object map[string]interface{} + want bool + }{ + { + name: "valid apiVersion", + object: map[string]interface{}{ + "apiVersion": "hostdata.kubescape.cloud/v1", + }, + want: true, + }, + { + name: "apiVersion does not match GroupHostSensor", + object: map[string]interface{}{ + "apiVersion": "someOtherGroup/v1", + }, + want: false, + }, + { + name: "apiVersion is an integer", + object: map[string]interface{}{ + "apiVersion": 12345, + }, + want: false, + }, + { + name: "missing apiVersion", + object: map[string]interface{}{ + "someOtherKey": "someValue", + }, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsTypeTypeHostSensor(tt.object); got != tt.want { + t.Errorf("%q: IsTypeTypeHostSensor() = %v, want %v", tt.name, got, tt.want) + } + }) + } +} +