Browse Source

stackdriver: fix broken substring. also adds tests

Erik Sundell 7 years ago
parent
commit
68332c5951
2 changed files with 18 additions and 2 deletions
  1. 5 2
      pkg/tsdb/stackdriver/stackdriver.go
  2. 13 0
      pkg/tsdb/stackdriver/stackdriver_test.go

+ 5 - 2
pkg/tsdb/stackdriver/stackdriver.go

@@ -170,8 +170,11 @@ func reverse(s string) string {
 }
 
 func interpolateFilterWildcards(value string) string {
-	if strings.HasSuffix(value, "*") && strings.HasPrefix(value, "*") {
-		value = strings.Replace(value, "*", "", 1)
+	re := regexp.MustCompile("[*]")
+	matches := re.FindAllStringIndex(value, -1)
+	logger.Info("len", "len", len(matches))
+	if len(matches) == 2 && strings.HasSuffix(value, "*") && strings.HasPrefix(value, "*") {
+		value = strings.Replace(value, "*", "", -1)
 		value = fmt.Sprintf(`has_substring("%s")`, value)
 	} else if strings.HasPrefix(value, "*") {
 		value = strings.Replace(value, "*", "", 1)

+ 13 - 0
pkg/tsdb/stackdriver/stackdriver_test.go

@@ -342,6 +342,19 @@ func TestStackdriver(t *testing.T) {
 				})
 			})
 		})
+
+		Convey("when interpolating filter wildcards", func() {
+			Convey("and wildcard is used in the beginning and the end of the word", func() {
+				Convey("and theres no wildcard in the middle of the word", func() {
+					value := interpolateFilterWildcards("*-central1*")
+					So(value, ShouldEqual, `has_substring("-central1")`)
+				})
+				Convey("and there is a wildcard in the middle of the word", func() {
+					value := interpolateFilterWildcards("*-cent*ral1*")
+					So(value, ShouldNotStartWith, `has_substring`)
+				})
+			})
+		})
 	})
 }