Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add if for cases that the apiVersion is not string #153

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion objectsenvelopes/hostsensor/hostsensordataenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

50 changes: 50 additions & 0 deletions objectsenvelopes/hostsensor/hostsensordataenvelope_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

Loading