Skip to content

Commit 1027f80

Browse files
authored
fix: fix KustomizeImage Match function to pass added unit tests (#1044)
Signed-off-by: Cheng Fang <cfang@redhat.com>
1 parent edabb0f commit 1027f80

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

registry-scanner/pkg/image/kustomize.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ func (i KustomizeImage) delim() string {
2020
// if the image name matches (i.e. up to the first delimiter)
2121
func (i KustomizeImage) Match(j KustomizeImage) bool {
2222
delim := j.delim()
23-
if !strings.Contains(string(j), delim) {
24-
return false
25-
}
26-
return strings.HasPrefix(string(i), strings.Split(string(j), delim)[0])
23+
imageName, _, _ := strings.Cut(string(i), delim)
24+
otherImageName, _, _ := strings.Cut(string(j), delim)
25+
return imageName == otherImageName
2726
}
2827

2928
type KustomizeImages []KustomizeImage

registry-scanner/pkg/image/kustomize_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9+
func TestKustomizeImage_Match(t *testing.T) {
10+
// no prefix
11+
assert.False(t, KustomizeImage("foo=1").Match("bar=1"))
12+
// mismatched delimiter
13+
assert.False(t, KustomizeImage("foo=1").Match("bar:1"))
14+
assert.False(t, KustomizeImage("foo:1").Match("bar=1"))
15+
assert.False(t, KustomizeImage("foobar:2").Match("foo:2"))
16+
assert.False(t, KustomizeImage("foobar@2").Match("foo@2"))
17+
// matches
18+
assert.True(t, KustomizeImage("foo=1").Match("foo=2"))
19+
assert.True(t, KustomizeImage("foo:1").Match("foo:2"))
20+
assert.True(t, KustomizeImage("foo@1").Match("foo@2"))
21+
assert.True(t, KustomizeImage("nginx").Match("nginx"))
22+
}
23+
924
func Test_KustomizeImages_Find(t *testing.T) {
1025
images := KustomizeImages{
1126
"a/b:1.0",
@@ -17,10 +32,10 @@ func Test_KustomizeImages_Find(t *testing.T) {
1732
for _, image := range images {
1833
assert.True(t, images.Find(image) >= 0)
1934
}
20-
for _, image := range []string{"a/b:2", "x/y=foo.bar"} {
35+
for _, image := range []string{"a/b", "a/b:2", "x/y=foo.bar"} {
2136
assert.True(t, images.Find(KustomizeImage(image)) >= 0)
2237
}
23-
for _, image := range []string{"a/b", "x", "x/y"} {
38+
for _, image := range []string{"x", "x/y"} {
2439
assert.Equal(t, -1, images.Find(KustomizeImage(image)))
2540
}
2641
}

0 commit comments

Comments
 (0)