Skip to content

Commit

Permalink
issue-5100: added test for cluster policy
Browse files Browse the repository at this point in the history
Signed-off-by: SANSKARJAIN2 <[email protected]>
  • Loading branch information
SANSKARJAIN2 committed Nov 9, 2022
1 parent 585985a commit 3c52cab
Showing 1 changed file with 117 additions and 6 deletions.
123 changes: 117 additions & 6 deletions pkg/webhooks/resource/generation/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func Test_handleUpdateGenerateTargetResource(t *testing.T) {

tests := []struct {
name string
namespacePolicy bool
ur runtime.Object
triggerResourceJson []byte
generatedResourceJson []byte
Expand All @@ -320,7 +321,8 @@ func Test_handleUpdateGenerateTargetResource(t *testing.T) {
expectedUrState kyvernov1beta1.UpdateRequestState
}{
{
name: "valid generated source updated",
name: "valid generated source updated",
namespacePolicy: true,
policyJson: []byte(`{
"apiVersion": "kyverno.io/v2beta1",
"kind": "Policy",
Expand Down Expand Up @@ -421,6 +423,108 @@ func Test_handleUpdateGenerateTargetResource(t *testing.T) {
}
}`),
},
{
name: "valid generated source updated-cluster policy",
namespacePolicy: false,
policyJson: []byte(`{
"apiVersion":"kyverno.io/v1",
"kind":"ClusterPolicy",
"metadata":{
"name":"pol-sync-clone"
},
"spec":{
"rules":[
{
"name":"gen-zk",
"match":{
"any":[
{
"resources":{
"kinds":[
"ConfigMap"
]
}
}
]
},
"generate":{
"apiVersion":"v1",
"kind":"Secret",
"name":"myclonedsecret",
"namespace":"poltest",
"synchronize":true,
"clone":{
"namespace":"poltest",
"name":"regcred"
}
}
}
]
}
}`),
ur: &kyvernov1beta1.UpdateRequest{
ObjectMeta: metav1.ObjectMeta{
Name: "ur-valid",
Namespace: config.KyvernoNamespace(),
},
Status: kyvernov1beta1.UpdateRequestStatus{
State: kyvernov1beta1.Completed,
},
},
urName: "ur-valid",
targetList: "ConfigMapList",
triggerResource: "comfigmaps",
sourceList: "SecretList",
generatedResourceJson: []byte(`
{
"apiVersion":"v1",
"data":{
"foo":"YmFy"
},
"kind":"Secret",
"metadata":{
"labels":{
"app.kubernetes.io/managed-by":"kyverno",
"kyverno.io/generated-by-kind":"ConfigMap",
"kyverno.io/generated-by-name":"cm-2",
"kyverno.io/generated-by-namespace":"poltest",
"policy.kyverno.io/gr-name":"ur-valid",
"policy.kyverno.io/policy-kind":"Cluster",
"policy.kyverno.io/policy-name":"pol-sync-clone",
"policy.kyverno.io/synchronize":"enable"
},
"name":"myclonedsecret",
"namespace":"poltest"
}
}
`),
sourceResource: "secrets",
expectedUrState: kyvernov1beta1.Pending,
sourceResourceJson: []byte(`
{
"apiVersion": "v1",
"data": {
"foo": "bar"
},
"kind": "Secret",
"metadata": {
"name": "regcred",
"namespace": "poltest"
}
}
`),
triggerResourceJson: []byte(`{
"apiVersion": "v1",
"data": {
"sj": "js"
},
"kind": "ConfigMap",
"metadata": {
"name": "cm-2",
"namespace": "poltest"
}
}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -441,10 +545,6 @@ func Test_handleUpdateGenerateTargetResource(t *testing.T) {
sourceResourceUnstructured, err = unstructuredUtils.ConvertToUnstructured(tt.sourceResourceJson)
assert.NilError(t, err)

var nsPolicy kyvernov1.Policy
err = json.Unmarshal(tt.policyJson, &nsPolicy)
assert.NilError(t, err)

clientObjects := []clientObject{
clientObject{
object: triggerUnstructured,
Expand All @@ -457,8 +557,19 @@ func Test_handleUpdateGenerateTargetResource(t *testing.T) {
resourceList: tt.sourceList,
},
}
var objects []runtime.Object
if tt.namespacePolicy {
var nsPolicy kyvernov1.Policy
err = json.Unmarshal(tt.policyJson, &nsPolicy)
assert.NilError(t, err)
objects = append(objects, &nsPolicy, tt.ur)
} else {
var clsPolicy kyvernov1.ClusterPolicy
err = json.Unmarshal(tt.policyJson, &clsPolicy)
assert.NilError(t, err)
objects = append(objects, &clsPolicy, tt.ur)
}

objects := []runtime.Object{&nsPolicy, tt.ur}
gh, fakeUrLister, err := newFakeGenerateHandler(&ctx, logger, objects, clientObjects)
assert.NilError(t, err)
request := &v1.AdmissionRequest{
Expand Down

0 comments on commit 3c52cab

Please sign in to comment.