Skip to content

Commit

Permalink
Fix parsing regex, #1318
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Feb 16, 2022
1 parent f4fad8d commit 0a4714f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/zabbix/utils.go
Expand Up @@ -64,7 +64,7 @@ func splitKeyParams(paramStr string) []string {
}

func parseFilter(filter string) (*regexp.Regexp, error) {
regex := regexp.MustCompile(`^/(.+)/(.*)$`)
regex := regexp.MustCompile(`^/(.+)/([imsU]*)$`)
flagRE := regexp.MustCompile("[imsU]+")

matches := regex.FindStringSubmatch(filter)
Expand Down
38 changes: 37 additions & 1 deletion pkg/zabbix/utils_test.go
@@ -1,8 +1,9 @@
package zabbix

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExpandItemName(t *testing.T) {
Expand Down Expand Up @@ -55,3 +56,38 @@ func TestExpandItemName(t *testing.T) {
})
}
}

func TestParseFilter(t *testing.T) {
tests := []struct {
name string
filter string
expectNoError bool
expectedError string
}{
{
name: "Simple regexp",
filter: "/.*/",
expectNoError: true,
expectedError: "",
},
{
name: "Not a regex",
filter: "/var/lib/mysql: Total space",
expectNoError: true,
expectedError: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := parseFilter(tt.filter)
if tt.expectNoError {
assert.NoError(t, err)
}
if tt.expectedError != "" {
assert.Error(t, err)
assert.EqualError(t, err, tt.expectedError)
}
})
}
}

0 comments on commit 0a4714f

Please sign in to comment.