Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Oct 17, 2023
1 parent d1987b7 commit ddb06fd
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 130 deletions.
2 changes: 1 addition & 1 deletion pkg/data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestCrds(t *testing.T) {
data := Crds()
{
file, err := fs.Stat(data, "crds/json.kyverno.io_policies.yaml")
file, err := fs.Stat(data, "crds/json.kyverno.io_validationpolicies.yaml")
assert.NoError(t, err)
assert.NotNil(t, file)
assert.False(t, file.IsDir())
Expand Down
1 change: 0 additions & 1 deletion pkg/engine/template/kyverno/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ var (
objectFromLists = "object_from_lists"
random = "random"
x509_decode = "x509_decode"
imageNormalize = "image_normalize"
)

func GetBareFunctions() []gojmespath.FunctionEntry {
Expand Down
13 changes: 6 additions & 7 deletions test/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ make install-crds
```bash
kubectl apply -f - <<EOF
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: test
spec:
rules:
- name: foo-bar-4
validate:
assert:
all:
- check:
foo:
bar: 4
assert:
all:
- check:
foo:
bar: 4
EOF
```

Expand Down
39 changes: 19 additions & 20 deletions test/dockerfile/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: check-dockerfile
spec:
rules:
- name: no-external
validate:
assert:
all:
- message: "HTTP calls are not allowed"
check:
~.(Stages[].Commands[].Args[].Value):
(contains(@, 'https://') || contains(@, 'http://')): false
- message: "HTTP calls are not allowed"
check:
~.(Stages[].Commands[].CmdLine[]):
(contains(@, 'https://') || contains(@, 'http://')): false
- message: "curl is not allowed"
check:
~.(Stages[].Commands[].CmdLine[]):
(contains(@, 'curl')): false
- message: "wget is not allowed"
check:
~.(Stages[].Commands[].CmdLine[]):
(contains(@, 'wget')): false
assert:
all:
- message: "HTTP calls are not allowed"
check:
~.(Stages[].Commands[].Args[].Value):
(contains(@, 'https://') || contains(@, 'http://')): false
- message: "HTTP calls are not allowed"
check:
~.(Stages[].Commands[].CmdLine[]):
(contains(@, 'https://') || contains(@, 'http://')): false
- message: "curl is not allowed"
check:
~.(Stages[].Commands[].CmdLine[]):
(contains(@, 'curl')): false
- message: "wget is not allowed"
check:
~.(Stages[].Commands[].CmdLine[]):
(contains(@, 'wget')): false
47 changes: 23 additions & 24 deletions test/escaped/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: test
spec:
rules:
- name: foo-bar-4
validate:
assert:
all:
- check:
foo:
\(bar)\: 4
- check:
foo:
\(bar)\->test:
($test): 4
- check:
foo:
\(bar)->test\: 6
- check:
foo:
\(bar)->test\->test:
($test): 6
- check:
foo:
\~foos\:
- 1
- 2
- 3
assert:
all:
- check:
foo:
\(bar)\: 4
- check:
foo:
\(bar)\->test:
($test): 4
- check:
foo:
\(bar)->test\: 6
- check:
foo:
\(bar)->test\->test:
($test): 6
- check:
foo:
\~foos\:
- 1
- 2
- 3
13 changes: 6 additions & 7 deletions test/foo-bar/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: test
spec:
rules:
- name: foo-bar-4
validate:
assert:
all:
- check:
foo:
bar: 4
assert:
all:
- check:
foo:
bar: 4
15 changes: 7 additions & 8 deletions test/payload-yaml/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: required-s3-tags
spec:
Expand All @@ -12,10 +12,9 @@ spec:
- name: tags
variable:
Team: Kyverno
validate:
assert:
all:
- message: Bucket `{{ name }}` ({{ address }}) does not have the required tags {{ to_string($tags) }}
check:
values:
tags: ($tags)
assert:
all:
- message: Bucket `{{ name }}` ({{ address }}) does not have the required tags {{ to_string($tags) }}
check:
values:
tags: ($tags)
19 changes: 9 additions & 10 deletions test/pod-all-latest/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: test
spec:
Expand All @@ -14,12 +14,11 @@ spec:
any:
- apiVersion: v1
kind: Pod
validate:
assert:
all:
- check:
~.(spec.containers[*].image):
# an image tag is required
(contains(@, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with(@, $tag)): true
assert:
all:
- check:
~.(spec.containers[*].image):
# an image tag is required
(contains(@, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with(@, $tag)): true
51 changes: 25 additions & 26 deletions test/pod-no-latest/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: test
spec:
Expand All @@ -12,28 +12,27 @@ spec:
any:
- apiVersion: v1
kind: Pod
validate:
assert:
all:
- check:
spec:
~foo.containers->foos:
(at($foos, $foo).image)->foo:
# an image tag is required
(contains($foo, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with($foo, $tag)): false
- check:
spec:
~.containers->foo:
image:
# an image tag is required
(contains(@, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with(@, ':latest')): false
- check:
~index.(spec.containers[*].image)->images:
# an image tag is required
(contains(@, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with(@, ':latest')): false
assert:
all:
- check:
spec:
~foo.containers->foos:
(at($foos, $foo).image)->foo:
# an image tag is required
(contains($foo, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with($foo, $tag)): false
- check:
spec:
~.containers->foo:
image:
# an image tag is required
(contains(@, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with(@, ':latest')): false
- check:
~index.(spec.containers[*].image)->images:
# an image tag is required
(contains(@, ':')): true
# using a mutable image tag e.g. 'latest' is not allowed
(ends_with(@, ':latest')): false
21 changes: 10 additions & 11 deletions test/scripted/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: test
spec:
rules:
- name: foo-bar-4
validate:
assert:
all:
- check:
foo:
(bar > `3`): true
(!baz): false
- check:
foo:
(bar + bat): 10
assert:
all:
- check:
foo:
(bar > `3`): true
(!baz): false
- check:
foo:
(bar + bat): 10
15 changes: 7 additions & 8 deletions test/tf-plan/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: required-s3-tags
spec:
Expand All @@ -12,10 +12,9 @@ spec:
- name: tags
variable:
Team: Kyverno
validate:
assert:
all:
- message: Bucket `{{ name }}` ({{ address }}) does not have the required tags {{ to_string($tags) }}
check:
values:
tags: ($tags)
assert:
all:
- message: Bucket `{{ name }}` ({{ address }}) does not have the required tags {{ to_string($tags) }}
check:
values:
tags: ($tags)
13 changes: 6 additions & 7 deletions test/wildcard/policy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: json.kyverno.io/v1alpha1
kind: Policy
kind: ValidationPolicy
metadata:
name: required-s3-tags
spec:
Expand All @@ -11,9 +11,8 @@ spec:
exclude:
any:
- (wildcard('bypass-*', name)): true
validate:
assert:
all:
- check:
tags:
(wildcard('?*', Team)): false
assert:
all:
- check:
tags:
(wildcard('?*', Team)): false

0 comments on commit ddb06fd

Please sign in to comment.