mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-11-08 12:26:36 +00:00
Move the resourcemodifiers package from internal/ to pkg/ so that external plugins, such as Restore Item Action (RIA) plugins, can import and reuse Velero's built-in resource modifier implementation. Due to Go's internal package visibility restrictions, external plugins cannot import packages located under internal/. Relocating resourcemodifiers to pkg/ allows plugins to evaluate and apply resource modifier rules to in-memory resource objects prior to validation or restore execution, ensuring consistent behavior with the Velero server without duplicate code. Update all internal import paths and add an unreleased changelog entry. Fixes #10668 Signed-off-by: Adam Zhang <adam.zhang@broadcom.com>
234 lines
5.2 KiB
Go
234 lines
5.2 KiB
Go
/*
|
|
Copyright The Velero Contributors.
|
|
|
|
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.
|
|
*/
|
|
package resourcemodifiers
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestResourceModifiers_Validate(t *testing.T) {
|
|
type fields struct {
|
|
Version string
|
|
ResourceModifierRules []ResourceModifierRule
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "correct version, non 0 length ResourceModifierRules",
|
|
fields: fields{
|
|
Version: "v1",
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
{
|
|
Conditions: Conditions{
|
|
GroupResource: "persistentvolumeclaims",
|
|
ResourceNameRegex: ".*",
|
|
Namespaces: []string{"bar", "foo"},
|
|
},
|
|
Patches: []JSONPatch{
|
|
{
|
|
Operation: "replace",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "incorrect version, non 0 length ResourceModifierRules",
|
|
fields: fields{
|
|
Version: "v2",
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
{
|
|
Conditions: Conditions{
|
|
GroupResource: "persistentvolumeclaims",
|
|
ResourceNameRegex: ".*",
|
|
Namespaces: []string{"bar", "foo"},
|
|
},
|
|
Patches: []JSONPatch{
|
|
{
|
|
Operation: "replace",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "correct version, 0 length ResourceModifierRules",
|
|
fields: fields{
|
|
Version: "v1",
|
|
ResourceModifierRules: []ResourceModifierRule{},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "patch has invalid operation",
|
|
fields: fields{
|
|
Version: "v1",
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
{
|
|
Conditions: Conditions{
|
|
GroupResource: "persistentvolumeclaims",
|
|
ResourceNameRegex: ".*",
|
|
Namespaces: []string{"bar", "foo"},
|
|
},
|
|
Patches: []JSONPatch{
|
|
{
|
|
Operation: "invalid",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Condition has empty GroupResource",
|
|
fields: fields{
|
|
Version: "v1",
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
{
|
|
Conditions: Conditions{
|
|
GroupResource: "",
|
|
ResourceNameRegex: ".*",
|
|
Namespaces: []string{"bar", "foo"},
|
|
},
|
|
Patches: []JSONPatch{
|
|
{
|
|
Operation: "invalid",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "More than one patch type in a rule",
|
|
fields: fields{
|
|
Version: "v1",
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
{
|
|
Conditions: Conditions{
|
|
GroupResource: "*",
|
|
},
|
|
Patches: []JSONPatch{
|
|
{
|
|
Operation: "test",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
},
|
|
MergePatches: []JSONMergePatch{
|
|
{
|
|
PatchData: `{"metadata":{"labels":{"a":null}}}`,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
p := &ResourceModifiers{
|
|
Version: tt.fields.Version,
|
|
ResourceModifierRules: tt.fields.ResourceModifierRules,
|
|
}
|
|
if err := p.Validate(); (err != nil) != tt.wantErr {
|
|
t.Errorf("ResourceModifiers.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestJsonPatch_Validate(t *testing.T) {
|
|
type fields struct {
|
|
Operation string
|
|
Path string
|
|
Value string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "not empty operation, path, and new value, valid scenario",
|
|
fields: fields{
|
|
Operation: "replace",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "empty operation throws error",
|
|
fields: fields{
|
|
Operation: "",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty path throws error",
|
|
fields: fields{
|
|
Operation: "replace",
|
|
Path: "",
|
|
Value: "premium",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid operation throws error",
|
|
fields: fields{
|
|
Operation: "invalid",
|
|
Path: "/spec/storageClassName",
|
|
Value: "premium",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
p := &JSONPatch{
|
|
Operation: tt.fields.Operation,
|
|
Path: tt.fields.Path,
|
|
Value: tt.fields.Value,
|
|
}
|
|
if err := p.Validate(); (err != nil) != tt.wantErr {
|
|
t.Errorf("JsonPatch.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|