Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
WakeupTsai authored and hwchiu committed Jul 27, 2018
1 parent c9a857d commit e17465f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/prometheuscontroller/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func queryExpr(expr string, queryLabels map[string]string) string {

var tmp string
for key, value := range queryLabels {
tmp = fmt.Sprintf(`,%s=~"%s"`, key, value)
tmp = fmt.Sprintf(`%s,%s=~"%s"`, tmp, key, value)
}
expr = fmt.Sprintf("{%s%s}", expr, tmp)

Expand Down
68 changes: 52 additions & 16 deletions src/prometheuscontroller/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"testing"
"time"

"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/suite"
)

Expand All @@ -23,9 +21,6 @@ type PrometheusExprTestSuite struct {
}

func (suite *PrometheusExprTestSuite) SetupSuite() {
cf := config.MustRead("../../config/testing.json")
suite.sp = serviceprovider.New(cf)
suite.containerName = "cadvisor"
}

func (suite *PrometheusExprTestSuite) TearDownSuite() {
Expand All @@ -39,18 +34,59 @@ func TestPrometheusExprSuite(t *testing.T) {
suite.Run(t, new(PrometheusExprTestSuite))
}

func (suite *PrometheusExprTestSuite) TestGetElements() {
func (suite *PrometheusExprTestSuite) TestBasicExpr() {
expression := Expression{}
expression.Metrics = []string{"kube_pod_container_info"}
expression.QueryLabels = map[string]string{"namespace": "vortex"}
expression.Metrics = []string{"a", "b", "c"}

str := basicExpr(expression.Metrics)
str = queryExpr(str, expression.QueryLabels)
results, err := query(suite.sp, str)
suite.NoError(err)
suite.NotEqual(0, float32(results[0].Value))

// Get nil if the result is empty
results, _ = query(suite.sp, "")
suite.Equal(model.Vector(nil), results)
suite.Equal(`__name__=~"a|b|c"`, str)

str = basicExpr(nil)
suite.Equal("", str)
}

func (suite *PrometheusExprTestSuite) TestQueryExpr() {
queryLabels := map[string]string{}
queryLabels["aKey"] = "aValue"
queryLabels["bKey"] = "bValue"
queryLabels["cKey"] = "cValue"
str := queryExpr("TEST_STRING", queryLabels)
suite.Equal(`{TEST_STRING,aKey=~"aValue",bKey=~"bValue",cKey=~"cValue"}`, str)

str = queryExpr("TEST_STRING", nil)
suite.Equal("TEST_STRING", str)
}

func (suite *PrometheusExprTestSuite) TestSumByExpr() {
sumByLabels := []string{"a", "b", "c"}
str := sumByExpr("TEST_STRING", sumByLabels)
suite.Equal(`sum by(a,b,c)(TEST_STRING)`, str)
}

func (suite *PrometheusExprTestSuite) TestSumExpr() {
str := sumExpr("TEST_STRING")
suite.Equal(`sum(TEST_STRING)`, str)
}

func (suite *PrometheusExprTestSuite) TestDurationExpr() {
var duration = "1h"
str := durationExpr("TEST_STRING", duration)
suite.Equal(`TEST_STRING[1h]`, str)
}

func (suite *PrometheusExprTestSuite) TestRateExpr() {
var str = rateExpr("TEST_STRING")
suite.Equal(`rate(TEST_STRING)`, str)
}

func (suite *PrometheusExprTestSuite) TestEqualExpr() {
var value = 1234.567
str := equalExpr("TEST_STRING", value)
suite.Equal(`TEST_STRING==1234.567`, str)
}

func (suite *PrometheusExprTestSuite) TestMultiplyExpr() {
value := 1234.567
str := multiplyExpr("TEST_STRING", value)
suite.Equal(`TEST_STRING*1234.567`, str)
}
2 changes: 0 additions & 2 deletions src/prometheuscontroller/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"time"

"github.com/linkernetworks/logger"
"github.com/linkernetworks/vortex/src/serviceprovider"
pv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -38,7 +37,6 @@ func query(sp *serviceprovider.Container, expression string) (model.Vector, erro
func queryRange(sp *serviceprovider.Container, expression string) (model.Matrix, error) {
api := sp.Prometheus.API

logger.Infof("%v", expression)
rangeSet := pv1.Range{Start: time.Now().Add(-time.Minute * timeGap), End: time.Now(), Step: time.Second * queryResolution}
result, err := api.QueryRange(context.Background(), expression, rangeSet)

Expand Down

0 comments on commit e17465f

Please sign in to comment.