Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add unit tests #8

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/apis/v1alpha1/variable.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package v1alpha1

import (
"github.com/jinzhu/copier"
)

// Variable defines an arbitrary JMESPath context variable that can be defined inline.
// +k8s:deepcopy-gen=false
type Variable struct {
// Value is any arbitrary object.
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
Value Any `json:"value,omitempty"`
Value interface{} `json:"value,omitempty"`
}

func (in *Variable) DeepCopy() *Variable {
out := &Variable{}
if err := copier.Copy(out, in); err != nil {
panic("deep copy failed")
}
return out
}
20 changes: 1 addition & 19 deletions pkg/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 89 additions & 92 deletions pkg/commands/root_test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,93 @@
package commands

// func Test_TfPlan(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/tf-plan/tf.plan.json",
// "--pre-process",
// "planned_values.root_module.resources",
// "--policy",
// "../../testdata/tf-plan/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }
import (
"bytes"
"io"
"os"
"testing"

// func Test_PayloadYaml(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/payload-yaml/payload.yaml",
// "--pre-process",
// "planned_values.root_module.resources",
// "--policy",
// "../../testdata/payload-yaml/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }
"github.com/stretchr/testify/assert"
)

// func Test_FooBar(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/foo-bar/payload.yaml",
// "--policy",
// "../../testdata/foo-bar/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

// func Test_Scripted(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/scripted/payload.yaml",
// "--policy",
// "../../testdata/scripted/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

// func Test_PodNoLatest(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/pod-no-latest/payload.yaml",
// "--policy",
// "../../testdata/pod-no-latest/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

// func Test_PodAllLatest(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/pod-all-latest/payload.yaml",
// "--policy",
// "../../testdata/pod-all-latest/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

// func Test_Jim(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/jim/payload.json",
// "--policy",
// "../../testdata/jim/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }
func Test_Execute(t *testing.T) {
tests := []struct {
name string
payload string
preprocessors []string
policies []string
wantErr bool
out string
}{{
name: "foo-bar",
payload: "../../testdata/foo-bar/payload.yaml",
policies: []string{"../../testdata/foo-bar/policy.yaml"},
out: "../../testdata/foo-bar/out.txt",
wantErr: false,
}, {
name: "jim",
payload: "../../testdata/jim/payload.json",
policies: []string{"../../testdata/jim/policy.yaml"},
out: "../../testdata/jim/out.txt",
wantErr: false,
}, {
name: "pod-no-latest",
payload: "../../testdata/pod-no-latest/payload.yaml",
policies: []string{"../../testdata/pod-no-latest/policy.yaml"},
out: "../../testdata/pod-no-latest/out.txt",
wantErr: false,
}, {
name: "pod-all-latest",
payload: "../../testdata/pod-all-latest/payload.yaml",
policies: []string{"../../testdata/pod-all-latest/policy.yaml"},
out: "../../testdata/pod-all-latest/out.txt",
wantErr: false,
}, {
name: "scripted",
payload: "../../testdata/scripted/payload.yaml",
policies: []string{"../../testdata/scripted/policy.yaml"},
out: "../../testdata/scripted/out.txt",
wantErr: false,
}, {
name: "payload-yaml",
payload: "../../testdata/payload-yaml/payload.yaml",
preprocessors: []string{"planned_values.root_module.resources"},
policies: []string{"../../testdata/payload-yaml/policy.yaml"},
out: "../../testdata/payload-yaml/out.txt",
wantErr: false,
}, {
name: "tf-plan",
payload: "../../testdata/tf-plan/tf.plan.json",
preprocessors: []string{"planned_values.root_module.resources"},
policies: []string{"../../testdata/tf-plan/policy.yaml"},
out: "../../testdata/tf-plan/out.txt",
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
var args []string
args = append(args, "--payload", tt.payload)
for _, preprocessor := range tt.preprocessors {
args = append(args, "--pre-process", preprocessor)
}
for _, policy := range tt.policies {
args = append(args, "--policy", policy)
}
args = append(args, "--payload", tt.payload)
cmd.SetArgs(args)
b := bytes.NewBufferString("")
cmd.SetOut(b)
if err := cmd.Execute(); (err != nil) != tt.wantErr {
t.Errorf("command.Run() error = %v, wantErr %v", err, tt.wantErr)
}
actual, err := io.ReadAll(b)
assert.NoError(t, err)
if tt.out != "" {
expected, err := os.ReadFile(tt.out)
assert.NoError(t, err)
assert.Equal(t, string(expected), string(actual))
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/engine/assert/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func parseExpression(value interface{}) *expression {
statement = strings.TrimPrefix(statement, expressionPrefix)
statement = strings.TrimSuffix(statement, expressionSuffix)
engine = "jp"
} else if binding == "" {
} /* else if binding == "" {
binding = strings.TrimSpace(statement)
}
}*/
return &expression{
foreach: foreach,
statement: strings.TrimSpace(statement),
Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/file/ext_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package file

import "testing"
import (
"testing"
)

func TestIsYaml(t *testing.T) {
tests := []struct {
Expand Down
6 changes: 6 additions & 0 deletions testdata/foo-bar/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Loading policies ...
Loading payload ...
Pre processing ...
Running ( evaluating 1 resource against 1 policy ) ...
- test / foo-bar-4 / ERROR: all[0].foo: Internal error: failed to find the map index `foo`
Done
7 changes: 4 additions & 3 deletions testdata/foo-bar/policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ spec:
rules:
- name: foo-bar-4
validate:
pattern:
foo:
bar: 4
assert:
all:
- foo:
bar: 4
6 changes: 6 additions & 0 deletions testdata/jim/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Loading policies ...
Loading payload ...
Pre processing ...
Running ( evaluating 1 resource against 1 policy ) ...
- required-s3-tags / require-team-tag / FAILED: any[0].resource.tags.(wildcard('?*', Team)): Invalid value: true: Expected value: false
Done
6 changes: 6 additions & 0 deletions testdata/payload-yaml/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Loading policies ...
Loading payload ...
Pre processing ...
Running ( evaluating 1 resource against 1 policy ) ...
- required-s3-tags / require-team-tag / aws_s3_bucket.example FAILED: Bucket `example` (aws_s3_bucket.example) does not have the required tags {"Team":"Kyverno"}
Done
2 changes: 0 additions & 2 deletions testdata/payload-yaml/payload.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ planned_values:
tags:
Environment: Dev
Name: My bucket
Team: Kyverno
tags_all:
Environment: Dev
Name: My bucket
Team: Kyverno
timeouts:
sensitive_values:
cors_rule: []
Expand Down
8 changes: 5 additions & 3 deletions testdata/payload-yaml/policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ spec:
Team: Kyverno
validate:
message: Bucket `{{ resource.name }}` ({{ resource.address }}) does not have the required tags {{ to_string($tags) }}
pattern:
values:
tags: '{{ $tags }}'
assert:
all:
- resource:
values:
tags: ($tags)
6 changes: 6 additions & 0 deletions testdata/pod-all-latest/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Loading policies ...
Loading payload ...
Pre processing ...
Running ( evaluating 1 resource against 1 policy ) ...
- test / pod-no-latest / PASSED
Done
14 changes: 8 additions & 6 deletions testdata/pod-all-latest/policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ spec:
apiVersion: v1
kind: Pod
validate:
pattern:
~(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:
- resource:
~(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
6 changes: 6 additions & 0 deletions testdata/pod-no-latest/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Loading policies ...
Loading payload ...
Pre processing ...
Running ( evaluating 1 resource against 1 policy ) ...
- test / pod-no-latest / FAILED: [all[0].resource.spec.~foo.containers@foos[0].(at($foos, $foo).image)@foo.(ends_with($foo, $tag)): Invalid value: true: Expected value: false, all[0].resource.spec.~foo.containers@foos[1].(at($foos, $foo).image)@foo.(ends_with($foo, $tag)): Invalid value: true: Expected value: false, all[0].resource.spec.~foo.containers@foos[2].(at($foos, $foo).image)@foo.(ends_with($foo, $tag)): Invalid value: true: Expected value: false, all[1].resource.spec.~.containers@foo[0].image.(ends_with(@, ':latest')): Invalid value: true: Expected value: false, all[1].resource.spec.~.containers@foo[1].image.(ends_with(@, ':latest')): Invalid value: true: Expected value: false, all[1].resource.spec.~.containers@foo[2].image.(ends_with(@, ':latest')): Invalid value: true: Expected value: false, all[2].resource.~index.(spec.containers[*].image)@images[0].(ends_with(@, ':latest')): Invalid value: true: Expected value: false, all[2].resource.~index.(spec.containers[*].image)@images[1].(ends_with(@, ':latest')): Invalid value: true: Expected value: false, all[2].resource.~index.(spec.containers[*].image)@images[2].(ends_with(@, ':latest')): Invalid value: true: Expected value: false]
Done
35 changes: 20 additions & 15 deletions testdata/pod-no-latest/policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ spec:
apiVersion: v1
kind: Pod
validate:
pattern:
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
~.containers@foo:
image:
assert:
all:
- resource:
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
- resource:
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
- resource:
~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
~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
Loading