-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
migration-create-mutator_test.go
112 lines (93 loc) · 3.21 KB
/
migration-create-mutator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Red Hat, Inc.
*
*/
package mutators_test
import (
"encoding/json"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
admissionv1 "k8s.io/api/admission/v1"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/kubevirt/pkg/apimachinery/patch"
"kubevirt.io/kubevirt/pkg/pointer"
"kubevirt.io/kubevirt/pkg/virt-api/webhooks/mutating-webhook/mutators"
)
var _ = Describe("VirtualMachineInstanceMigration Mutator", func() {
It("Should mutate the VirtualMachineInstanceMigration object", func() {
migration := newMigration()
admissionReview, err := newAdmissionReviewForVMIMCreation(migration)
Expect(err).ToNot(HaveOccurred())
mutator := &mutators.MigrationCreateMutator{}
expectedJSONPatch, err := expectedJSONPatchForVMIMCreation(
expectedMigrationObjectMeta(migration.ObjectMeta, migration.Spec.VMIName),
)
Expect(err).NotTo(HaveOccurred())
Expect(mutator.Mutate(admissionReview)).To(Equal(
&admissionv1.AdmissionResponse{
Allowed: true,
PatchType: pointer.P(admissionv1.PatchTypeJSONPatch),
Patch: expectedJSONPatch,
},
))
})
})
func newMigration() *v1.VirtualMachineInstanceMigration {
return &v1.VirtualMachineInstanceMigration{
ObjectMeta: k8smetav1.ObjectMeta{
Labels: map[string]string{"test": "test"},
},
Spec: v1.VirtualMachineInstanceMigrationSpec{
VMIName: "testVmi",
},
}
}
func newAdmissionReviewForVMIMCreation(migration *v1.VirtualMachineInstanceMigration) (*admissionv1.AdmissionReview, error) {
migrationBytes, err := json.Marshal(migration)
if err != nil {
return nil, err
}
return &admissionv1.AdmissionReview{
Request: &admissionv1.AdmissionRequest{
Resource: k8smetav1.GroupVersionResource{
Group: v1.VirtualMachineInstanceMigrationGroupVersionKind.Group,
Version: v1.VirtualMachineInstanceMigrationGroupVersionKind.Version,
Resource: "virtualmachineinstancemigrations",
},
Object: runtime.RawExtension{
Raw: migrationBytes,
},
},
}, nil
}
func expectedMigrationObjectMeta(currentObjectMeta k8smetav1.ObjectMeta, vmiName string) k8smetav1.ObjectMeta {
expectedObjectMeta := currentObjectMeta
expectedObjectMeta.Labels[v1.MigrationSelectorLabel] = vmiName
expectedObjectMeta.Finalizers = []string{v1.VirtualMachineInstanceMigrationFinalizer}
return expectedObjectMeta
}
func expectedJSONPatchForVMIMCreation(expectedObjectMeta k8smetav1.ObjectMeta) ([]byte, error) {
return patch.GeneratePatchPayload(
patch.PatchOperation{
Op: patch.PatchReplaceOp,
Path: "/metadata",
Value: expectedObjectMeta,
},
)
}