Skip to content

Commit 3557249

Browse files
committed
🐛 Refactor asset duplication check to use reusable IsSubsetOf function and add unit tests
1 parent 90eb2f7 commit 3557249

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

discovery/discovery.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"go.mondoo.com/mql/v13/cli/execruntime"
1616
"go.mondoo.com/mql/v13/internal/workerpool"
1717
"go.mondoo.com/mql/v13/llx"
18+
"go.mondoo.com/mql/v13/utils/slicesx"
1819
"go.mondoo.com/mql/v13/logger"
1920
"go.mondoo.com/mql/v13/providers"
2021
inventory "go.mondoo.com/mql/v13/providers-sdk/v1/inventory"
@@ -68,7 +69,7 @@ func (d *DiscoveredAssets) Add(asset *inventory.Asset, runtime *providers.Runtim
6869
}
6970

7071
for _, existing := range d.Assets {
71-
if isSubsetOf(asset.PlatformIds, existing.Asset.PlatformIds) {
72+
if slicesx.IsSubsetOf(asset.PlatformIds, existing.Asset.PlatformIds) {
7273
log.Debug().Str("asset", asset.Name).Strs("platform-ids", asset.PlatformIds).Msg("discovery> skipping duplicate asset")
7374
return false
7475
}
@@ -79,22 +80,6 @@ func (d *DiscoveredAssets) Add(asset *inventory.Asset, runtime *providers.Runtim
7980
return true
8081
}
8182

82-
// isSubsetOf returns true if every element in sub exists in super.
83-
func isSubsetOf(sub, super []string) bool {
84-
if len(sub) > len(super) {
85-
return false
86-
}
87-
set := make(map[string]struct{}, len(super))
88-
for _, id := range super {
89-
set[id] = struct{}{}
90-
}
91-
for _, id := range sub {
92-
if _, ok := set[id]; !ok {
93-
return false
94-
}
95-
}
96-
return true
97-
}
9883

9984
func (d *DiscoveredAssets) AddError(asset *inventory.Asset, err error) {
10085
d.assetsLock.Lock()

utils/slicesx/subset.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package slicesx
5+
6+
// IsSubsetOf returns true if every element in sub exists in super.
7+
func IsSubsetOf[T comparable](sub, super []T) bool {
8+
if len(sub) > len(super) {
9+
return false
10+
}
11+
set := make(map[T]struct{}, len(super))
12+
for _, v := range super {
13+
set[v] = struct{}{}
14+
}
15+
for _, v := range sub {
16+
if _, ok := set[v]; !ok {
17+
return false
18+
}
19+
}
20+
return true
21+
}

utils/slicesx/subset_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package slicesx
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestIsSubsetOf(t *testing.T) {
13+
t.Run("equal sets", func(t *testing.T) {
14+
assert.True(t, IsSubsetOf([]string{"a", "b"}, []string{"a", "b"}))
15+
})
16+
17+
t.Run("proper subset", func(t *testing.T) {
18+
assert.True(t, IsSubsetOf([]string{"a"}, []string{"a", "b", "c"}))
19+
})
20+
21+
t.Run("not a subset", func(t *testing.T) {
22+
assert.False(t, IsSubsetOf([]string{"a", "d"}, []string{"a", "b", "c"}))
23+
})
24+
25+
t.Run("sub longer than super", func(t *testing.T) {
26+
assert.False(t, IsSubsetOf([]string{"a", "b", "c"}, []string{"a"}))
27+
})
28+
29+
t.Run("empty sub is always a subset", func(t *testing.T) {
30+
assert.True(t, IsSubsetOf([]string{}, []string{"a", "b"}))
31+
})
32+
33+
t.Run("both empty", func(t *testing.T) {
34+
assert.True(t, IsSubsetOf([]string{}, []string{}))
35+
})
36+
37+
t.Run("works with ints", func(t *testing.T) {
38+
assert.True(t, IsSubsetOf([]int{1, 2}, []int{1, 2, 3}))
39+
assert.False(t, IsSubsetOf([]int{1, 4}, []int{1, 2, 3}))
40+
})
41+
}

0 commit comments

Comments
 (0)