Skip to content

Commit 1b9885d

Browse files
committed
add unit tests for network plugin manager metrics
1 parent 0ffe89e commit 1b9885d

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

pkg/kubelet/dockershim/network/BUILD

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
@@ -46,3 +46,14 @@ filegroup(
4646
tags = ["automanaged"],
4747
visibility = ["//visibility:public"],
4848
)
49+
50+
go_test(
51+
name = "go_default_test",
52+
srcs = ["plugins_test.go"],
53+
embed = [":go_default_library"],
54+
deps = [
55+
"//pkg/kubelet/dockershim/network/metrics:go_default_library",
56+
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
57+
"//staging/src/k8s.io/component-base/metrics/testutil:go_default_library",
58+
],
59+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// +build !dockerless
2+
3+
/*
4+
Copyright 2020 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package network
20+
21+
import (
22+
"strings"
23+
"testing"
24+
"time"
25+
26+
"k8s.io/component-base/metrics/legacyregistry"
27+
"k8s.io/component-base/metrics/testutil"
28+
"k8s.io/kubernetes/pkg/kubelet/dockershim/network/metrics"
29+
)
30+
31+
func TestNetworkPluginManagerMetrics(t *testing.T) {
32+
metrics.Register()
33+
34+
operation := "test_operation"
35+
recordOperation(operation, time.Now())
36+
recordError(operation)
37+
38+
cases := []struct {
39+
metricName string
40+
want string
41+
}{
42+
{
43+
metricName: "kubelet_network_plugin_operations_total",
44+
want: `
45+
# HELP kubelet_network_plugin_operations_total [ALPHA] Cumulative number of network plugin operations by operation type.
46+
# TYPE kubelet_network_plugin_operations_total counter
47+
kubelet_network_plugin_operations_total{operation_type="test_operation"} 1
48+
`,
49+
},
50+
{
51+
metricName: "kubelet_network_plugin_operations_errors_total",
52+
want: `
53+
# HELP kubelet_network_plugin_operations_errors_total [ALPHA] Cumulative number of network plugin operation errors by operation type.
54+
# TYPE kubelet_network_plugin_operations_errors_total counter
55+
kubelet_network_plugin_operations_errors_total{operation_type="test_operation"} 1
56+
`,
57+
},
58+
}
59+
60+
for _, tc := range cases {
61+
t.Run(tc.metricName, func(t *testing.T) {
62+
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metricName); err != nil {
63+
t.Fatal(err)
64+
}
65+
})
66+
}
67+
}

0 commit comments

Comments
 (0)