forked from kyverno/kyverno
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: anushkamittal2001 <[email protected]>
- Loading branch information
1 parent
01672ff
commit 841b1d9
Showing
13 changed files
with
287 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package webhook | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1" | ||
v1 "github.com/kyverno/kyverno/api/kyverno/v1" | ||
admissionregistrationv1 "k8s.io/api/admissionregistration/v1" | ||
) | ||
|
||
func TestGetMinimumOperations(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
inputMap map[string]bool | ||
expectedResult []admissionregistrationv1.OperationType | ||
}{ | ||
{ | ||
name: "Test Case 1", | ||
inputMap: map[string]bool{ | ||
"CREATE": true, | ||
"UPDATE": false, | ||
"DELETE": true, | ||
}, | ||
expectedResult: []admissionregistrationv1.OperationType{"CREATE", "DELETE"}, | ||
}, | ||
{ | ||
name: "Test Case 2", | ||
inputMap: map[string]bool{ | ||
"CREATE": false, | ||
"UPDATE": false, | ||
"DELETE": false, | ||
"CONNECT": true, | ||
}, | ||
expectedResult: []admissionregistrationv1.OperationType{"CONNECT"}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
result := getMinimumOperations(testCase.inputMap) | ||
if !reflect.DeepEqual(result, testCase.expectedResult) { | ||
t.Errorf("Expected %v, but got %v", testCase.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestComputeOperationsForMutatingWebhookConf(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
rules []kyvernov1.Rule | ||
expectedResult map[string]bool | ||
}{ | ||
{ | ||
name: "Test Case 1", | ||
rules: []kyvernov1.Rule{ | ||
{ | ||
MatchResources: kyvernov1.MatchResources{ | ||
ResourceDescription: kyvernov1.ResourceDescription{ | ||
Operations: []v1.AdmissionOperation{"CREATE"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResult: map[string]bool{ | ||
"CREATE": true, | ||
}, | ||
}, | ||
{ | ||
name: "Test Case 2", | ||
rules: []kyvernov1.Rule{ | ||
{ | ||
MatchResources: kyvernov1.MatchResources{}, | ||
ExcludeResources: kyvernov1.MatchResources{}, | ||
}, | ||
{ | ||
MatchResources: kyvernov1.MatchResources{}, | ||
ExcludeResources: kyvernov1.MatchResources{}, | ||
}, | ||
}, | ||
expectedResult: map[string]bool{ | ||
webhookCreate: true, | ||
webhookUpdate: true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
result := computeOperationsForMutatingWebhookConf(testCase.rules, make(map[string]bool)) | ||
if !reflect.DeepEqual(result, testCase.expectedResult) { | ||
t.Errorf("Expected %v, but got %v", testCase.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestComputeOperationsForValidatingWebhookConf(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
rules []kyvernov1.Rule | ||
expectedResult map[string]bool | ||
}{ | ||
{ | ||
name: "Test Case 1", | ||
rules: []kyvernov1.Rule{ | ||
{ | ||
MatchResources: kyvernov1.MatchResources{ | ||
ResourceDescription: kyvernov1.ResourceDescription{ | ||
Operations: []v1.AdmissionOperation{"CREATE"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ExcludeResources: kyvernov1.MatchResources{ | ||
ResourceDescription: kyvernov1.ResourceDescription{ | ||
Operations: []v1.AdmissionOperation{"DELETE"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResult: map[string]bool{ | ||
"CREATE": true, | ||
"DELETE": true, | ||
}, | ||
}, | ||
{ | ||
name: "Test Case 2", | ||
rules: []kyvernov1.Rule{ | ||
{ | ||
MatchResources: kyvernov1.MatchResources{}, | ||
ExcludeResources: kyvernov1.MatchResources{}, | ||
}, | ||
{ | ||
MatchResources: kyvernov1.MatchResources{}, | ||
ExcludeResources: kyvernov1.MatchResources{}, | ||
}, | ||
}, | ||
expectedResult: map[string]bool{ | ||
webhookCreate: true, | ||
webhookUpdate: true, | ||
}, | ||
}, | ||
{ | ||
name: "Test Case 3", | ||
rules: []kyvernov1.Rule{ | ||
{ | ||
MatchResources: kyvernov1.MatchResources{ | ||
ResourceDescription: kyvernov1.ResourceDescription{ | ||
Operations: []v1.AdmissionOperation{"CREATE", "UPDATE"}, | ||
}, | ||
}, | ||
ExcludeResources: kyvernov1.MatchResources{ | ||
ResourceDescription: kyvernov1.ResourceDescription{ | ||
Operations: []v1.AdmissionOperation{"DELETE"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResult: map[string]bool{ | ||
webhookCreate: true, | ||
webhookUpdate: true, | ||
"DELETE": true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
result := computeOperationsForValidatingWebhookConf(testCase.rules, make(map[string]bool)) | ||
if !reflect.DeepEqual(result, testCase.expectedResult) { | ||
t.Errorf("Expected %v, but got %v", testCase.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,6 @@ webhooks: | |
operations: | ||
- CREATE | ||
- UPDATE | ||
- DELETE | ||
- CONNECT | ||
resources: | ||
- '*/scale' | ||
scope: '*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,6 @@ webhooks: | |
operations: | ||
- CREATE | ||
- UPDATE | ||
- DELETE | ||
- CONNECT | ||
resources: | ||
- '*/*' | ||
scope: '*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
apply: | ||
- policy.yaml | ||
assert: | ||
- policy-assert.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
assert: | ||
- webhooks.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Description | ||
|
||
This test verifies that operations configured dynmically are correct |
9 changes: 9 additions & 0 deletions
9
test/conformance/kuttl/webhooks/dynamic-op/policy-assert.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: require-labels | ||
status: | ||
conditions: | ||
- reason: Succeeded | ||
status: "True" | ||
type: Ready |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: require-labels | ||
annotations: | ||
pod-policies.kyverno.io/autogen-controllers: none | ||
spec: | ||
validationFailureAction: Audit | ||
background: false | ||
rules: | ||
- name: require-team | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- '*/scale' | ||
operations: | ||
- CREATE | ||
validate: | ||
message: 'The label `team` is required.' | ||
pattern: | ||
metadata: | ||
labels: | ||
team: '?*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: ValidatingWebhookConfiguration | ||
metadata: | ||
labels: | ||
webhook.kyverno.io/managed-by: kyverno | ||
name: kyverno-resource-validating-webhook-cfg | ||
webhooks: | ||
- rules: | ||
- apiGroups: | ||
- '*' | ||
apiVersions: | ||
- '*' | ||
operations: | ||
- CREATE | ||
resources: | ||
- '*/scale' | ||
scope: '*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,6 @@ webhooks: | |
operations: | ||
- CREATE | ||
- UPDATE | ||
- DELETE | ||
- CONNECT | ||
resources: | ||
- pods/attach | ||
- pods/binding | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters