|
| 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