Fix parsing regex, #1318
This commit is contained in:
@@ -64,7 +64,7 @@ func splitKeyParams(paramStr string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseFilter(filter string) (*regexp.Regexp, error) {
|
func parseFilter(filter string) (*regexp.Regexp, error) {
|
||||||
regex := regexp.MustCompile(`^/(.+)/(.*)$`)
|
regex := regexp.MustCompile(`^/(.+)/([imsU]*)$`)
|
||||||
flagRE := regexp.MustCompile("[imsU]+")
|
flagRE := regexp.MustCompile("[imsU]+")
|
||||||
|
|
||||||
matches := regex.FindStringSubmatch(filter)
|
matches := regex.FindStringSubmatch(filter)
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package zabbix
|
package zabbix
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExpandItemName(t *testing.T) {
|
func TestExpandItemName(t *testing.T) {
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user