1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/pkg/imageverification/match/match_test.go
Vishal Choudhary f68706cab2
feat: add cel library for image verification (#12233)
* feat: concurrently add images to context

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add cel library for image verification

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: add tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: ci

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: linter

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: type conv

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: linter

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
2025-02-26 00:56:17 +00:00

85 lines
1.6 KiB
Go

package match
import (
"testing"
"github.com/kyverno/kyverno/api/policies.kyverno.io/v1alpha1"
"github.com/stretchr/testify/assert"
)
func Test_Match(t *testing.T) {
tests := []struct {
name string
imageRules []v1alpha1.ImageRule
image string
wantResult bool
wantErr bool
}{
{
name: "standard pass",
imageRules: []v1alpha1.ImageRule{
{
Glob: "ghcr.io/*",
},
{
CELExpression: "ref == \"ghcr.io/kyverno/kyverno\"",
},
},
image: "ghcr.io/kyverno/kyverno",
wantResult: true,
wantErr: false,
},
{
name: "standard fail",
imageRules: []v1alpha1.ImageRule{
{
Glob: "ghcr.io/*",
},
{
CELExpression: "ref == \"ghcr.io/kyverno/kyverno\"",
},
},
image: "kyverno/kyverno",
wantResult: false,
wantErr: false,
},
{
name: "second rule matches",
imageRules: []v1alpha1.ImageRule{
{
Glob: "index.docker.io/*",
},
{
CELExpression: "ref == \"ghcr.io/kyverno/kyverno\"",
},
},
image: "ghcr.io/kyverno/kyverno",
wantResult: true,
wantErr: false,
},
{
name: "invalid cel expression",
imageRules: []v1alpha1.ImageRule{
{
CELExpression: "\"foo\"",
},
},
image: "ghcr.io/kyverno/kyverno",
wantResult: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := CompileMatches(tt.imageRules)
assert.NoError(t, err)
matched, err := Match(c, tt.image)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.wantResult, matched)
}
})
}
}