regex matchstring error parsing

Signed-off-by: Anshul Ahuja <anshulahuja@microsoft.com>
pull/6452/head
Anshul Ahuja 2023-07-13 10:10:37 +05:30
parent 4a28b3b16f
commit 16ec2db1f7
3 changed files with 38 additions and 1 deletions

1
.gitignore vendored
View File

@ -53,3 +53,4 @@ tilt-resources/cloud
# test generated files
test/e2e/report.xml
coverage.out
__debug_bin*

View File

@ -85,7 +85,10 @@ func (r *ResourceModifierRule) Apply(obj *unstructured.Unstructured, groupResour
return nil
}
if r.Conditions.ResourceNameRegex != "" {
match, _ := regexp.MatchString(r.Conditions.ResourceNameRegex, obj.GetName())
match, err := regexp.MatchString(r.Conditions.ResourceNameRegex, obj.GetName())
if err != nil {
return errors.Errorf("error in matching regex %s", err.Error())
}
if !match {
return nil
}

View File

@ -144,6 +144,39 @@ func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) {
wantErr bool
wantObj *unstructured.Unstructured
}{
{
name: "Invalid Regex throws error",
fields: fields{
Version: "v1",
ResourceModifierRules: []ResourceModifierRule{
{
Conditions: Conditions{
GroupKind: "persistentvolumeclaims",
ResourceNameRegex: "[a-z",
Namespaces: []string{"foo"},
},
Patches: []JSONPatch{
{
Operation: "test",
Path: "/spec/storageClassName",
Value: "standard",
},
{
Operation: "replace",
Path: "/spec/storageClassName",
Value: "premium",
},
},
},
},
},
args: args{
obj: pvcStandardSc.DeepCopy(),
groupResource: "persistentvolumeclaims",
},
wantErr: true,
wantObj: pvcStandardSc.DeepCopy(),
},
{
name: "pvc with standard storage class should be patched to premium",
fields: fields{