2023-07-05 08:21:32 +00:00
|
|
|
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{
|
2023-08-31 05:06:59 +00:00
|
|
|
GroupResource: "persistentvolumeclaims",
|
2023-07-05 08:21:32 +00:00
|
|
|
ResourceNameRegex: ".*",
|
|
|
|
Namespaces: []string{"bar", "foo"},
|
|
|
|
},
|
2023-07-06 05:59:33 +00:00
|
|
|
Patches: []JSONPatch{
|
2023-07-05 08:21:32 +00:00
|
|
|
{
|
|
|
|
Operation: "replace",
|
|
|
|
Path: "/spec/storageClassName",
|
2023-07-06 05:01:05 +00:00
|
|
|
Value: "premium",
|
2023-07-05 08:21:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "incorrect version, non 0 length ResourceModifierRules",
|
|
|
|
fields: fields{
|
|
|
|
Version: "v2",
|
|
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
|
|
{
|
|
|
|
Conditions: Conditions{
|
2023-08-31 05:06:59 +00:00
|
|
|
GroupResource: "persistentvolumeclaims",
|
2023-07-05 08:21:32 +00:00
|
|
|
ResourceNameRegex: ".*",
|
|
|
|
Namespaces: []string{"bar", "foo"},
|
|
|
|
},
|
2023-07-06 05:59:33 +00:00
|
|
|
Patches: []JSONPatch{
|
2023-07-05 08:21:32 +00:00
|
|
|
{
|
|
|
|
Operation: "replace",
|
|
|
|
Path: "/spec/storageClassName",
|
2023-07-06 05:01:05 +00:00
|
|
|
Value: "premium",
|
2023-07-05 08:21:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "correct version, 0 length ResourceModifierRules",
|
|
|
|
fields: fields{
|
|
|
|
Version: "v1",
|
|
|
|
ResourceModifierRules: []ResourceModifierRule{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-07-13 08:22:49 +00:00
|
|
|
{
|
|
|
|
name: "patch has invalid operation",
|
|
|
|
fields: fields{
|
|
|
|
Version: "v1",
|
|
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
|
|
{
|
|
|
|
Conditions: Conditions{
|
2023-08-31 05:06:59 +00:00
|
|
|
GroupResource: "persistentvolumeclaims",
|
2023-07-13 08:22:49 +00:00
|
|
|
ResourceNameRegex: ".*",
|
|
|
|
Namespaces: []string{"bar", "foo"},
|
|
|
|
},
|
|
|
|
Patches: []JSONPatch{
|
|
|
|
{
|
|
|
|
Operation: "invalid",
|
|
|
|
Path: "/spec/storageClassName",
|
|
|
|
Value: "premium",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-08-31 05:06:59 +00:00
|
|
|
name: "Condition has empty GroupResource",
|
2023-07-13 08:22:49 +00:00
|
|
|
fields: fields{
|
|
|
|
Version: "v1",
|
|
|
|
ResourceModifierRules: []ResourceModifierRule{
|
|
|
|
{
|
|
|
|
Conditions: Conditions{
|
2023-08-31 05:06:59 +00:00
|
|
|
GroupResource: "",
|
2023-07-13 08:22:49 +00:00
|
|
|
ResourceNameRegex: ".*",
|
|
|
|
Namespaces: []string{"bar", "foo"},
|
|
|
|
},
|
|
|
|
Patches: []JSONPatch{
|
|
|
|
{
|
|
|
|
Operation: "invalid",
|
|
|
|
Path: "/spec/storageClassName",
|
|
|
|
Value: "premium",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-07-06 05:01:05 +00:00
|
|
|
Value string
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
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",
|
2023-07-06 05:01:05 +00:00
|
|
|
Value: "premium",
|
2023-07-05 08:21:32 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty operation throws error",
|
|
|
|
fields: fields{
|
|
|
|
Operation: "",
|
|
|
|
Path: "/spec/storageClassName",
|
2023-07-06 05:01:05 +00:00
|
|
|
Value: "premium",
|
2023-07-05 08:21:32 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty path throws error",
|
|
|
|
fields: fields{
|
|
|
|
Operation: "replace",
|
|
|
|
Path: "",
|
2023-07-06 05:01:05 +00:00
|
|
|
Value: "premium",
|
2023-07-05 08:21:32 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-07-11 04:30:08 +00:00
|
|
|
{
|
|
|
|
name: "invalid operation throws error",
|
|
|
|
fields: fields{
|
|
|
|
Operation: "invalid",
|
|
|
|
Path: "/spec/storageClassName",
|
|
|
|
Value: "premium",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-07-06 05:59:33 +00:00
|
|
|
p := &JSONPatch{
|
2023-07-05 08:21:32 +00:00
|
|
|
Operation: tt.fields.Operation,
|
|
|
|
Path: tt.fields.Path,
|
2023-07-06 05:01:05 +00:00
|
|
|
Value: tt.fields.Value,
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
if err := p.Validate(); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("JsonPatch.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|