Improve test infrastructure

This commit is contained in:
Brian Brazil 2015-06-20 13:32:32 +01:00 committed by Brian Brazil
parent 86a6f337f6
commit e34c52402f
4 changed files with 16 additions and 17 deletions

View File

@ -39,7 +39,7 @@ func TestMysqlGeneratesMetrics(t *testing.T) {
var count int var count int
for _, p := range acc.Points { for _, p := range acc.Points {
if strings.HasPrefix(p.Name, prefix.prefix) { if strings.HasPrefix(p.Measurement, prefix.prefix) {
count++ count++
} }
} }

View File

@ -91,7 +91,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
var found bool var found bool
for _, pnt := range acc.Points { for _, pnt := range acc.Points {
if pnt.Name == "xact_commit" { if pnt.Measurement == "xact_commit" {
if pnt.Tags["db"] == "postgres" { if pnt.Tags["db"] == "postgres" {
found = true found = true
break break

View File

@ -272,7 +272,9 @@ func TestSystemStats_GenerateStats(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
dockertags := map[string]string{ dockertags := map[string]string{
"id": "blah", "name": "blah",
"id": "",
"command": "",
} }
assert.True(t, acc.CheckTaggedValue("user", 3.1, dockertags)) assert.True(t, acc.CheckTaggedValue("user", 3.1, dockertags))

View File

@ -2,6 +2,7 @@ package testutil
import ( import (
"fmt" "fmt"
"reflect"
"time" "time"
) )
@ -18,6 +19,9 @@ type Accumulator struct {
} }
func (a *Accumulator) Add(measurement string, value interface{}, tags map[string]string) { func (a *Accumulator) Add(measurement string, value interface{}, tags map[string]string) {
if tags == nil {
tags = map[string]string{}
}
a.Points = append( a.Points = append(
a.Points, a.Points,
&Point{ &Point{
@ -70,30 +74,23 @@ func (a *Accumulator) CheckTaggedValue(measurement string, val interface{}, tags
} }
func (a *Accumulator) ValidateTaggedValue(measurement string, val interface{}, tags map[string]string) error { func (a *Accumulator) ValidateTaggedValue(measurement string, val interface{}, tags map[string]string) error {
if tags == nil {
tags = map[string]string{}
}
for _, p := range a.Points { for _, p := range a.Points {
var found bool if !reflect.DeepEqual(tags, p.Tags) {
continue
if p.Tags == nil && tags == nil {
found = true
} else {
for k, v := range p.Tags {
if tags[k] == v {
found = true
break
}
}
} }
if found && p.Measurement == measurement { if p.Measurement == measurement {
if p.Value != val { if p.Value != val {
return fmt.Errorf("%v (%T) != %v (%T)", p.Value, p.Value, val, val) return fmt.Errorf("%v (%T) != %v (%T)", p.Value, p.Value, val, val)
} }
return nil return nil
} }
} }
return fmt.Errorf("unknown value %s with tags %v", measurement, tags) return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
} }
func (a *Accumulator) ValidateValue(measurement string, val interface{}) error { func (a *Accumulator) ValidateValue(measurement string, val interface{}) error {