From 0a4714f9e940ed3c14d88baca9da7460c9d0e2c9 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Wed, 16 Feb 2022 17:59:56 +0300 Subject: [PATCH] Fix parsing regex, #1318 --- pkg/zabbix/utils.go | 2 +- pkg/zabbix/utils_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pkg/zabbix/utils.go b/pkg/zabbix/utils.go index 77f4b28..2c96282 100644 --- a/pkg/zabbix/utils.go +++ b/pkg/zabbix/utils.go @@ -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) diff --git a/pkg/zabbix/utils_test.go b/pkg/zabbix/utils_test.go index 499b30c..911154a 100644 --- a/pkg/zabbix/utils_test.go +++ b/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) { @@ -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) + } + }) + } +}