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 } + 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) + } + }) + } +} +