mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
oldObject translation solved in autogen (#6305)
* OldObject translation solved in autogen Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com> * CronJob fixed in autogen Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com> * tests added Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com> --------- Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com> Co-authored-by: shuting <shuting@nirmata.com> Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
parent
f2eb87b344
commit
7d4ea1dcdc
2 changed files with 44 additions and 0 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -543,6 +544,47 @@ func Test_CronJobAndDeployment(t *testing.T) {
|
|||
assert.DeepEqual(t, rulePatches, expectedPatches)
|
||||
}
|
||||
|
||||
func TestUpdateGenRuleByte(t *testing.T) {
|
||||
tests := []struct {
|
||||
pbyte []byte
|
||||
kind string
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
pbyte: []byte("request.object.spec"),
|
||||
kind: "Pod",
|
||||
want: []byte("request.object.spec.template.spec"),
|
||||
},
|
||||
{
|
||||
pbyte: []byte("request.oldObject.spec"),
|
||||
kind: "Pod",
|
||||
want: []byte("request.oldObject.spec.template.spec"),
|
||||
},
|
||||
{
|
||||
pbyte: []byte("request.object.spec"),
|
||||
kind: "Cronjob",
|
||||
want: []byte("request.object.spec.jobTemplate.spec.template.spec"),
|
||||
},
|
||||
{
|
||||
pbyte: []byte("request.oldObject.spec"),
|
||||
kind: "Cronjob",
|
||||
want: []byte("request.oldObject.spec.jobTemplate.spec.template.spec"),
|
||||
},
|
||||
{
|
||||
pbyte: []byte("request.object.metadata"),
|
||||
kind: "Pod",
|
||||
want: []byte("request.object.spec.template.metadata"),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got := updateGenRuleByte(tt.pbyte, tt.kind)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("updateGenRuleByte() = %v, want %v", string(got), string(tt.want))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_UpdateVariablePath(t *testing.T) {
|
||||
dir, err := os.Getwd()
|
||||
baseDir := filepath.Dir(filepath.Dir(dir))
|
||||
|
|
|
@ -299,9 +299,11 @@ func generateCronJobRule(rule *kyvernov1.Rule, controllers string) *kyvernov1.Ru
|
|||
func updateGenRuleByte(pbyte []byte, kind string) (obj []byte) {
|
||||
if kind == "Pod" {
|
||||
obj = []byte(strings.ReplaceAll(string(pbyte), "request.object.spec", "request.object.spec.template.spec"))
|
||||
obj = []byte(strings.ReplaceAll(string(obj), "request.oldObject.spec", "request.oldObject.spec.template.spec"))
|
||||
}
|
||||
if kind == "Cronjob" {
|
||||
obj = []byte(strings.ReplaceAll(string(pbyte), "request.object.spec", "request.object.spec.jobTemplate.spec.template.spec"))
|
||||
obj = []byte(strings.ReplaceAll(string(obj), "request.oldObject.spec", "request.oldObject.spec.jobTemplate.spec.template.spec"))
|
||||
}
|
||||
obj = []byte(strings.ReplaceAll(string(obj), "request.object.metadata", "request.object.spec.template.metadata"))
|
||||
return obj
|
||||
|
|
Loading…
Reference in a new issue