Merge pull request #2716 from chaunceyjiang/ut

Add UT for imageoverride
This commit is contained in:
karmada-bot 2022-11-01 17:04:43 +08:00 committed by GitHub
commit 8d5f56b8b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 30 deletions

View File

@ -15015,7 +15015,7 @@
"default": ""
},
"predicate": {
"description": "Predicate filters images before applying the rule.\n\nDefaults to nil, in that case, the system will automatically detect image fields if the resource type is Pod, ReplicaSet, Deployment or StatefulSet by following rule:\n - Pod: spec/containers/\u003cN\u003e/image\n - ReplicaSet: spec/template/spec/containers/\u003cN\u003e/image\n - Deployment: spec/template/spec/containers/\u003cN\u003e/image\n - StatefulSet: spec/template/spec/containers/\u003cN\u003e/image\nIn addition, all images will be processed if the resource object has more than one containers.\n\nIf not nil, only images matches the filters will be processed.",
"description": "Predicate filters images before applying the rule.\n\nDefaults to nil, in that case, the system will automatically detect image fields if the resource type is Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet or Job by following rule:\n - Pod: spec/containers/\u003cN\u003e/image\n - ReplicaSet: spec/template/spec/containers/\u003cN\u003e/image\n - Deployment: spec/template/spec/containers/\u003cN\u003e/image\n - DaemonSet: spec/template/spec/containers/\u003cN\u003e/image\n - StatefulSet: spec/template/spec/containers/\u003cN\u003e/image\n - Job: spec/template/spec/containers/\u003cN\u003e/image\nIn addition, all images will be processed if the resource object has more than one container.\n\nIf not nil, only images matches the filters will be processed.",
"$ref": "#/definitions/com.github.karmada-io.karmada.pkg.apis.policy.v1alpha1.ImagePredicate"
},
"value": {

View File

@ -174,15 +174,16 @@ spec:
description: "Predicate filters images before applying
the rule. \n Defaults to nil, in that case, the
system will automatically detect image fields if
the resource type is Pod, ReplicaSet, Deployment
or StatefulSet by following rule: - Pod: spec/containers/<N>/image
- ReplicaSet: spec/template/spec/containers/<N>/image
the resource type is Pod, ReplicaSet, Deployment,
StatefulSet, DaemonSet or Job by following rule:
- Pod: spec/containers/<N>/image - ReplicaSet: spec/template/spec/containers/<N>/image
- Deployment: spec/template/spec/containers/<N>/image
- DaemonSet: spec/template/spec/containers/<N>/image
- StatefulSet: spec/template/spec/containers/<N>/image
In addition, all images will be processed if the
resource object has more than one containers. \n
If not nil, only images matches the filters will
be processed."
- Job: spec/template/spec/containers/<N>/image In
addition, all images will be processed if the resource
object has more than one container. \n If not nil,
only images matches the filters will be processed."
properties:
path:
description: Path indicates the path of target
@ -495,13 +496,16 @@ spec:
description: "Predicate filters images before applying the
rule. \n Defaults to nil, in that case, the system will
automatically detect image fields if the resource type
is Pod, ReplicaSet, Deployment or StatefulSet by following
rule: - Pod: spec/containers/<N>/image - ReplicaSet: spec/template/spec/containers/<N>/image
is Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet
or Job by following rule: - Pod: spec/containers/<N>/image
- ReplicaSet: spec/template/spec/containers/<N>/image
- Deployment: spec/template/spec/containers/<N>/image
- StatefulSet: spec/template/spec/containers/<N>/image
In addition, all images will be processed if the resource
object has more than one containers. \n If not nil, only
images matches the filters will be processed."
- DaemonSet: spec/template/spec/containers/<N>/image -
StatefulSet: spec/template/spec/containers/<N>/image -
Job: spec/template/spec/containers/<N>/image In addition,
all images will be processed if the resource object has
more than one container. \n If not nil, only images matches
the filters will be processed."
properties:
path:
description: Path indicates the path of target field

View File

@ -174,15 +174,16 @@ spec:
description: "Predicate filters images before applying
the rule. \n Defaults to nil, in that case, the
system will automatically detect image fields if
the resource type is Pod, ReplicaSet, Deployment
or StatefulSet by following rule: - Pod: spec/containers/<N>/image
- ReplicaSet: spec/template/spec/containers/<N>/image
the resource type is Pod, ReplicaSet, Deployment,
StatefulSet, DaemonSet or Job by following rule:
- Pod: spec/containers/<N>/image - ReplicaSet: spec/template/spec/containers/<N>/image
- Deployment: spec/template/spec/containers/<N>/image
- DaemonSet: spec/template/spec/containers/<N>/image
- StatefulSet: spec/template/spec/containers/<N>/image
In addition, all images will be processed if the
resource object has more than one containers. \n
If not nil, only images matches the filters will
be processed."
- Job: spec/template/spec/containers/<N>/image In
addition, all images will be processed if the resource
object has more than one container. \n If not nil,
only images matches the filters will be processed."
properties:
path:
description: Path indicates the path of target
@ -495,13 +496,16 @@ spec:
description: "Predicate filters images before applying the
rule. \n Defaults to nil, in that case, the system will
automatically detect image fields if the resource type
is Pod, ReplicaSet, Deployment or StatefulSet by following
rule: - Pod: spec/containers/<N>/image - ReplicaSet: spec/template/spec/containers/<N>/image
is Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet
or Job by following rule: - Pod: spec/containers/<N>/image
- ReplicaSet: spec/template/spec/containers/<N>/image
- Deployment: spec/template/spec/containers/<N>/image
- StatefulSet: spec/template/spec/containers/<N>/image
In addition, all images will be processed if the resource
object has more than one containers. \n If not nil, only
images matches the filters will be processed."
- DaemonSet: spec/template/spec/containers/<N>/image -
StatefulSet: spec/template/spec/containers/<N>/image -
Job: spec/template/spec/containers/<N>/image In addition,
all images will be processed if the resource object has
more than one container. \n If not nil, only images matches
the filters will be processed."
properties:
path:
description: Path indicates the path of target field

View File

@ -132,12 +132,14 @@ type ImageOverrider struct {
// Predicate filters images before applying the rule.
//
// Defaults to nil, in that case, the system will automatically detect image fields if the resource type is
// Pod, ReplicaSet, Deployment or StatefulSet by following rule:
// Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet or Job by following rule:
// - Pod: spec/containers/<N>/image
// - ReplicaSet: spec/template/spec/containers/<N>/image
// - Deployment: spec/template/spec/containers/<N>/image
// - DaemonSet: spec/template/spec/containers/<N>/image
// - StatefulSet: spec/template/spec/containers/<N>/image
// In addition, all images will be processed if the resource object has more than one containers.
// - Job: spec/template/spec/containers/<N>/image
// In addition, all images will be processed if the resource object has more than one container.
//
// If not nil, only images matches the filters will be processed.
// +optional

View File

@ -2754,7 +2754,7 @@ func schema_pkg_apis_policy_v1alpha1_ImageOverrider(ref common.ReferenceCallback
Properties: map[string]spec.Schema{
"predicate": {
SchemaProps: spec.SchemaProps{
Description: "Predicate filters images before applying the rule.\n\nDefaults to nil, in that case, the system will automatically detect image fields if the resource type is Pod, ReplicaSet, Deployment or StatefulSet by following rule:\n - Pod: spec/containers/<N>/image\n - ReplicaSet: spec/template/spec/containers/<N>/image\n - Deployment: spec/template/spec/containers/<N>/image\n - StatefulSet: spec/template/spec/containers/<N>/image\nIn addition, all images will be processed if the resource object has more than one containers.\n\nIf not nil, only images matches the filters will be processed.",
Description: "Predicate filters images before applying the rule.\n\nDefaults to nil, in that case, the system will automatically detect image fields if the resource type is Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet or Job by following rule:\n - Pod: spec/containers/<N>/image\n - ReplicaSet: spec/template/spec/containers/<N>/image\n - Deployment: spec/template/spec/containers/<N>/image\n - DaemonSet: spec/template/spec/containers/<N>/image\n - StatefulSet: spec/template/spec/containers/<N>/image\n - Job: spec/template/spec/containers/<N>/image\nIn addition, all images will be processed if the resource object has more than one container.\n\nIf not nil, only images matches the filters will be processed.",
Ref: ref("github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.ImagePredicate"),
},
},

View File

@ -188,6 +188,25 @@ func generateDeploymentYamlWithTwoContainer() *unstructured.Unstructured {
}}}}}}}
}
func generateJobYaml() *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]interface{}{
"name": "pi",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"image": "perl:5.34.0",
"name": "perl",
},
}}}}}}
}
func TestParseJSONPatchesByImageOverrider(t *testing.T) {
type args struct {
rawObj *unstructured.Unstructured
@ -199,6 +218,47 @@ func TestParseJSONPatchesByImageOverrider(t *testing.T) {
want []overrideOption
wantErr bool
}{
{
name: "imageOverrider with empty predicate, resource kind: Job, component: Registry, operator: add",
args: args{
rawObj: generateJobYaml(),
imageOverrider: &policyv1alpha1.ImageOverrider{
Component: "Registry",
Operator: "add",
Value: "k8s.gcr.io",
},
},
want: []overrideOption{
{
Op: "replace",
Path: "/spec/template/spec/containers/0/image",
Value: "k8s.gcr.io/perl:5.34.0",
},
},
wantErr: false,
},
{
name: "imageOverrider with predicate, resource kind: Job, component: Registry, operator: add",
args: args{
rawObj: generateJobYaml(),
imageOverrider: &policyv1alpha1.ImageOverrider{
Predicate: &policyv1alpha1.ImagePredicate{
Path: "/spec/template/spec/containers/0/image",
},
Component: "Registry",
Operator: "add",
Value: "k8s.gcr.io",
},
},
want: []overrideOption{
{
Op: "replace",
Path: "/spec/template/spec/containers/0/image",
Value: "k8s.gcr.io/perl:5.34.0",
},
},
wantErr: false,
},
{
name: "imageOverrider with empty predicate, resource kind: Deployment, component: Registry, operator: add",
args: args{