Merge pull request #1594 from prymitive/more-test

fix(tests): add more test coverage
This commit is contained in:
Łukasz Mierzwa
2020-04-04 15:47:12 +01:00
committed by GitHub
3 changed files with 70 additions and 8 deletions

View File

@@ -615,6 +615,20 @@ func TestFilters(t *testing.T) {
t.Errorf("[%s] GetRawText() returned %#v != %s passed as the expression", ft.Expression, f.GetRawText(), ft.Expression)
}
}
if !f.GetIsValid() {
func() {
didPanic := false
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
f.Match(&alert, 0)
if !didPanic {
t.Errorf("[%s] Match() on invalid filter didn't cause panic", ft.Expression)
}
}()
}
}
}

View File

@@ -29,14 +29,8 @@ func StringInSlice(stringArray []string, value string) bool {
func StringSliceToSHA1(stringArray []string) (string, error) {
h := sha1.New()
for _, s := range stringArray {
_, err := h.Write([]byte(s))
if err != nil {
return "", err
}
_, err = h.Write([]byte("\n"))
if err != nil {
return "", err
}
_, _ = h.Write([]byte(s))
_, _ = h.Write([]byte("\n"))
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}

View File

@@ -0,0 +1,54 @@
package verprobe_test
import (
"bytes"
"testing"
"github.com/prymitive/karma/internal/verprobe"
)
func TestDetect(t *testing.T) {
type testCaseT struct {
metrics string
version string
isError bool
}
testCases := []testCaseT{
{
metrics: "",
version: "",
},
{
metrics: "xxxx",
version: "",
isError: true,
},
{
metrics: "alertmanager_build_info 1\n",
version: "",
},
{
metrics: "alertmanager_build_info{foo=\"bar\"} 1\n",
version: "",
},
{
metrics: "alertmanager_build_info{version=\"1.2.3\"} 1\n",
version: "1.2.3",
},
}
for _, testCase := range testCases {
r := bytes.NewBufferString(testCase.metrics)
version, err := verprobe.Detect(r)
isError := err != nil
if isError != testCase.isError {
t.Errorf("Version probe error=%q, expected isError=%v", err, testCase.isError)
}
if !isError {
if version != testCase.version {
t.Errorf("Version mismatch, got %q, expected %q", version, testCase.version)
}
}
}
}