| 1 |
/* |
| 2 |
Copyright 2022 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package testing |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/prometheus/client_golang/prometheus" |
| 24 |
promtest "github.com/prometheus/client_golang/prometheus/testutil" |
| 25 |
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/metrics" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
clientDialFailureHeader = ` |
| 30 |
# HELP konnectivity_network_proxy_client_dial_failure_total Number of dial failures observed, by reason (example: remote endpoint error) |
| 31 |
# TYPE konnectivity_network_proxy_client_dial_failure_total counter` |
| 32 |
clientDialFailureSample = `konnectivity_network_proxy_client_dial_failure_total{reason="%s"} %d` |
| 33 |
|
| 34 |
clientConnsHeader = ` |
| 35 |
# HELP konnectivity_network_proxy_client_client_connections Number of open client connections, by status (Example: dialing) |
| 36 |
# TYPE konnectivity_network_proxy_client_client_connections gauge` |
| 37 |
clientConnsSample = `konnectivity_network_proxy_client_client_connections{status="%s"} %d` |
| 38 |
) |
| 39 |
|
| 40 |
func ExpectClientDialFailures(expected map[metrics.DialFailureReason]int) error { |
| 41 |
expect := clientDialFailureHeader + "\n" |
| 42 |
for r, v := range expected { |
| 43 |
expect += fmt.Sprintf(clientDialFailureSample+"\n", r, v) |
| 44 |
} |
| 45 |
return ExpectMetric(metrics.Namespace, metrics.Subsystem, "dial_failure_total", expect) |
| 46 |
} |
| 47 |
|
| 48 |
func ExpectClientDialFailure(reason metrics.DialFailureReason, count int) error { |
| 49 |
return ExpectClientDialFailures(map[metrics.DialFailureReason]int{reason: count}) |
| 50 |
} |
| 51 |
|
| 52 |
func ExpectClientConnections(expected map[metrics.ClientConnectionStatus]int) error { |
| 53 |
expect := clientConnsHeader + "\n" |
| 54 |
for r, v := range expected { |
| 55 |
expect += fmt.Sprintf(clientConnsSample+"\n", r, v) |
| 56 |
} |
| 57 |
return ExpectMetric(metrics.Namespace, metrics.Subsystem, "client_connections", expect) |
| 58 |
} |
| 59 |
|
| 60 |
func ExpectClientConnection(status metrics.ClientConnectionStatus, count int) error { |
| 61 |
return ExpectClientConnections(map[metrics.ClientConnectionStatus]int{status: count}) |
| 62 |
} |
| 63 |
|
| 64 |
func ExpectMetric(namespace, subsystem, name, expected string) error { |
| 65 |
fqName := prometheus.BuildFQName(namespace, subsystem, name) |
| 66 |
return promtest.GatherAndCompare(prometheus.DefaultGatherer, strings.NewReader(expected), fqName) |
| 67 |
} |