Skip to content

Commit

Permalink
Merge pull request #153 from kubescape/bug_fix/repo_scan
Browse files Browse the repository at this point in the history
add if for cases that the apiVersion is not string
  • Loading branch information
amirmalka authored Nov 17, 2024
2 parents 343b8c3 + a3b34ac commit 030b343
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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)
}
})
}
}

0 comments on commit 030b343

Please sign in to comment.