...
1
16
17 package stats
18
19 import (
20 "testing"
21 "time"
22
23 cadvisorapiv1 "github.com/google/cadvisor/info/v1"
24 cadvisorapiv2 "github.com/google/cadvisor/info/v2"
25 "github.com/stretchr/testify/assert"
26
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
29 )
30
31 func TestCustomMetrics(t *testing.T) {
32 spec := []cadvisorapiv1.MetricSpec{
33 {
34 Name: "qos",
35 Type: cadvisorapiv1.MetricGauge,
36 Format: cadvisorapiv1.IntType,
37 Units: "per second",
38 },
39 {
40 Name: "cpuLoad",
41 Type: cadvisorapiv1.MetricCumulative,
42 Format: cadvisorapiv1.FloatType,
43 Units: "count",
44 },
45 }
46 timestamp1 := time.Now()
47 timestamp2 := time.Now().Add(time.Minute)
48 metrics := map[string][]cadvisorapiv1.MetricVal{
49 "qos": {
50 {
51 Timestamp: timestamp1,
52 IntValue: 10,
53 },
54 {
55 Timestamp: timestamp2,
56 IntValue: 100,
57 },
58 },
59 "cpuLoad": {
60 {
61 Timestamp: timestamp1,
62 FloatValue: 1.2,
63 },
64 {
65 Timestamp: timestamp2,
66 FloatValue: 2.1,
67 },
68 },
69 }
70 cInfo := cadvisorapiv2.ContainerInfo{
71 Spec: cadvisorapiv2.ContainerSpec{
72 CustomMetrics: spec,
73 },
74 Stats: []*cadvisorapiv2.ContainerStats{
75 {
76 CustomMetrics: metrics,
77 },
78 },
79 }
80 assert.Contains(t, cadvisorInfoToUserDefinedMetrics(&cInfo),
81 statsapi.UserDefinedMetric{
82 UserDefinedMetricDescriptor: statsapi.UserDefinedMetricDescriptor{
83 Name: "qos",
84 Type: statsapi.MetricGauge,
85 Units: "per second",
86 },
87 Time: metav1.NewTime(timestamp2),
88 Value: 100,
89 },
90 statsapi.UserDefinedMetric{
91 UserDefinedMetricDescriptor: statsapi.UserDefinedMetricDescriptor{
92 Name: "cpuLoad",
93 Type: statsapi.MetricCumulative,
94 Units: "count",
95 },
96 Time: metav1.NewTime(timestamp2),
97 Value: 2.1,
98 })
99 }
100
View as plain text