-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathmetrics_test.go
131 lines (103 loc) · 4.51 KB
/
metrics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package metrics
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/aws/amazon-vpc-cni-k8s/pkg/publisher"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
)
type testMetricsTarget struct {
metricFile string
interestingMetrics map[string]metricsConvert
}
func (target *testMetricsTarget) getLogger() logger.Logger {
return logger.DefaultLogger()
}
func newTestMetricsTarget(metricFile string, interestingMetrics map[string]metricsConvert) *testMetricsTarget {
return &testMetricsTarget{
metricFile: metricFile,
interestingMetrics: interestingMetrics}
}
func (target *testMetricsTarget) grabMetricsFromTarget(ctx context.Context, targetName string) ([]byte, error) {
testMetrics, _ := os.ReadFile(target.metricFile)
return testMetrics, nil
}
func (target *testMetricsTarget) getInterestingMetrics() map[string]metricsConvert {
return target.interestingMetrics
}
func (target *testMetricsTarget) getCWMetricsPublisher() publisher.Publisher {
return nil
}
func (target *testMetricsTarget) getTargetList(ctx context.Context) ([]string, error) {
return []string{target.metricFile}, nil
}
func (target *testMetricsTarget) submitCloudWatch() bool {
return false
}
func (target *testMetricsTarget) submitPrometheus() bool {
return false
}
func TestAPIServerMetric(t *testing.T) {
testTarget := newTestMetricsTarget("cni_test1.data", InterestingCNIMetrics)
ctx := context.Background()
_, _, resetDetected, err := metricsListGrabAggregateConvert(ctx, testTarget)
assert.NoError(t, err)
assert.True(t, resetDetected)
actions := InterestingCNIMetrics["awscni_assigned_ip_addresses"].actions
// verify awscni_assigned_ip_addresses value
assert.Equal(t, 1.0, actions[0].data.curSingleDataPoint)
// verify awscni_ec2api_req_count value
actions = InterestingCNIMetrics["awscni_ec2api_req_count"].actions
assert.Equal(t, 21.0, actions[0].data.curSingleDataPoint)
// verify awscni_ec2api_error_count value
actions = InterestingCNIMetrics["awscni_ec2api_error_count"].actions
assert.Equal(t, 0.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_total_ip_addresses"].actions
// verify awscni_total_ip_addresses value
assert.Equal(t, 10.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_aws_api_error_count"].actions
// verify awscni_aws_api_error_count value
assert.Equal(t, 14.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_eni_allocated"].actions
// verify awscni_eni_allocated value
assert.Equal(t, 2.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_add_ip_req_count"].actions
// verify awscni_add_ip_req_count value
assert.Equal(t, 100.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_aws_api_latency_ms"].actions
// verify apiserver_request_latencies_bucket
assert.Equal(t, "awsAPILatency", actions[0].cwMetricName)
assert.Equal(t, 1.0, actions[0].data.curSingleDataPoint)
assert.Equal(t, 0.0, actions[0].data.lastSingleDataPoint)
}
func TestAPIServerMetricwithPDenabled(t *testing.T) {
testTarget := newTestMetricsTarget("cni_test2.data", InterestingCNIMetrics)
ctx := context.Background()
_, _, _, err := metricsListGrabAggregateConvert(ctx, testTarget)
assert.NoError(t, err)
actions := InterestingCNIMetrics["awscni_assigned_ip_addresses"].actions
// verify awscni_assigned_ip_addresses value
assert.Equal(t, 1.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_total_ip_addresses"].actions
// verify awscni_total_ip_addresses value
assert.Equal(t, 16.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_total_ipv4_prefixes"].actions
// verify awscni_total_ipv4_prefixes value
assert.Equal(t, 1.0, actions[0].data.curSingleDataPoint)
actions = InterestingCNIMetrics["awscni_assigned_ip_per_cidr"].actions
// verify awscni_assigned_ip_per_cidr value
assert.Equal(t, 1.0, actions[0].data.curSingleDataPoint)
}