Adding trafficsplit test to stat_summary_test.go (#3252)

This PR adds a test for trafficsplits to stat_summary_test.go.

Because the test requires a consistent order for returned rows, trafficsplit
rows in stat_summary.go are now sorted by apex + leaf name before being
returned.
This commit is contained in:
Carol A. Scott 2019-08-14 14:48:46 -07:00 committed by GitHub
parent cc3c53fa73
commit 9c62b65c6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"sort"
"github.com/deislabs/smi-sdk-go/pkg/apis/split/v1alpha1"
proto "github.com/golang/protobuf/proto"
@ -332,6 +333,9 @@ func (s *grpcServer) trafficSplitResourceQuery(ctx context.Context, req *pb.Stat
}
}
// sort rows before returning in order to have a consistent order for tests
rows = sortTrafficSplitRows(rows)
rsp := pb.StatTable{
Table: &pb.StatTable_PodGroup_{
PodGroup: &pb.StatTable_PodGroup{
@ -343,6 +347,15 @@ func (s *grpcServer) trafficSplitResourceQuery(ctx context.Context, req *pb.Stat
return resourceResult{res: &rsp, err: nil}
}
func sortTrafficSplitRows(rows []*pb.StatTable_PodGroup_Row) []*pb.StatTable_PodGroup_Row {
sort.Slice(rows, func(i, j int) bool {
key1 := rows[i].TsStats.Apex + rows[i].TsStats.Leaf
key2 := rows[j].TsStats.Apex + rows[j].TsStats.Leaf
return key1 < key2
})
return rows
}
func (s *grpcServer) nonK8sResourceQuery(ctx context.Context, req *pb.StatSummaryRequest) resourceResult {
var requestMetrics map[rKey]*pb.BasicStats
if !req.SkipStats {

View File

@ -46,6 +46,22 @@ func genPromSample(resName string, resType string, resNs string, isDst bool) *mo
}
}
func genTrafficSplitPromSample(resName, resNs string) *model.Sample {
labelName := model.LabelName("dst_service")
namespaceLabel := model.LabelName("namespace")
return &model.Sample{
Metric: model.Metric{
labelName: model.LabelValue(resName),
namespaceLabel: model.LabelValue(resNs),
"classification": model.LabelValue("success"),
"tls": model.LabelValue("false"),
},
Value: 123,
Timestamp: 456,
}
}
func genEmptyResponse() pb.StatSummaryResponse {
return pb.StatSummaryResponse{
Response: &pb.StatSummaryResponse_Ok_{ // https://github.com/golang/protobuf/issues/205
@ -628,6 +644,78 @@ status:
testStatSummary(t, expectations)
})
t.Run("Successfully performs a query based on resource type TrafficSplit", func(t *testing.T) {
expectations := []statSumExpected{
{
expectedStatRPC: expectedStatRPC{
err: nil,
k8sConfigs: []string{`
apiVersion: v1
kind: Service
metadata:
name: service-1
namespace: default
labels:
app: authors
project: booksapp
spec:
selector:
app: authors
clusterIP: None
ports:
- name: service
port: 7001
`, `
apiVersion: v1
kind: Service
metadata:
name: service-2
namespace: default
labels:
app: authors-clone
project: booksapp
spec:
selector:
app: authors-clone
clusterIP: None
ports:
- name: service
port: 7009
`, `
apiVersion: split.smi-spec.io/v1alpha1
kind: TrafficSplit
metadata:
name: authors-split
namespace: default
spec:
service: apex_name
backends:
- service: service-1
weight: 900m
- service: service-2
weight: 100m
`,
},
mockPromResponse: model.Vector{
genTrafficSplitPromSample("service-1", "default"),
genTrafficSplitPromSample("service-2", "default"),
},
},
req: pb.StatSummaryRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Namespace: "default",
Type: pkgK8s.TrafficSplit,
},
},
TimeWindow: "1m",
},
expectedResponse: GenStatTsResponse("authors-split", pkgK8s.TrafficSplit, []string{"default"}, true, true),
},
}
testStatSummary(t, expectations)
})
t.Run("Queries prometheus for TCP stats when requested", func(t *testing.T) {
expectations := []statSumExpected{