|
| 1 | +// Copyright 2022 The etcd Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package lease |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestLeaseGranted(t *testing.T) { |
| 26 | + // Test the leaseGranted metric |
| 27 | + expected := ` |
| 28 | +# HELP etcd_debugging_lease_granted_total The total number of granted leases. |
| 29 | +# TYPE etcd_debugging_lease_granted_total counter |
| 30 | +etcd_debugging_lease_granted_total 218 |
| 31 | +` |
| 32 | + err := testutil.CollectAndCompare(leaseGranted, strings.NewReader(expected)) |
| 33 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 34 | +} |
| 35 | + |
| 36 | +func TestNewLeaseRevoked(t *testing.T) { |
| 37 | + // Test the leaseRevoked metric |
| 38 | + expected := ` |
| 39 | +# HELP etcd_debugging_lease_revoked_total The total number of revoked leases. |
| 40 | +# TYPE etcd_debugging_lease_revoked_total counter |
| 41 | +etcd_debugging_lease_revoked_total 1 |
| 42 | +` |
| 43 | + err := testutil.CollectAndCompare(leaseRevoked, strings.NewReader(expected)) |
| 44 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 45 | +} |
| 46 | + |
| 47 | +func TestLeaseRenewed(t *testing.T) { |
| 48 | + // Test the leaseRenewed metric |
| 49 | + expected := ` |
| 50 | +# HELP etcd_debugging_lease_renewed_total The number of renewed leases seen by the leader. |
| 51 | +# TYPE etcd_debugging_lease_renewed_total counter |
| 52 | +etcd_debugging_lease_renewed_total 2 |
| 53 | +` |
| 54 | + err := testutil.CollectAndCompare(leaseRenewed, strings.NewReader(expected)) |
| 55 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 56 | +} |
| 57 | + |
| 58 | +func TestLeaseTotalTTLs(t *testing.T) { |
| 59 | + // Test the leaseTotalTTLs metric |
| 60 | + expected := ` |
| 61 | +# HELP etcd_debugging_lease_ttl_total Bucketed histogram of lease TTLs. |
| 62 | +# TYPE etcd_debugging_lease_ttl_total histogram |
| 63 | +etcd_debugging_lease_ttl_total_bucket{le="1"} 2 |
| 64 | +etcd_debugging_lease_ttl_total_bucket{le="2"} 3 |
| 65 | +etcd_debugging_lease_ttl_total_bucket{le="4"} 3 |
| 66 | +etcd_debugging_lease_ttl_total_bucket{le="8"} 7 |
| 67 | +etcd_debugging_lease_ttl_total_bucket{le="16"} 214 |
| 68 | +etcd_debugging_lease_ttl_total_bucket{le="32"} 215 |
| 69 | +etcd_debugging_lease_ttl_total_bucket{le="64"} 215 |
| 70 | +etcd_debugging_lease_ttl_total_bucket{le="128"} 218 |
| 71 | +etcd_debugging_lease_ttl_total_bucket{le="256"} 218 |
| 72 | +etcd_debugging_lease_ttl_total_bucket{le="512"} 218 |
| 73 | +etcd_debugging_lease_ttl_total_bucket{le="1024"} 218 |
| 74 | +etcd_debugging_lease_ttl_total_bucket{le="2048"} 218 |
| 75 | +etcd_debugging_lease_ttl_total_bucket{le="4096"} 218 |
| 76 | +etcd_debugging_lease_ttl_total_bucket{le="8192"} 218 |
| 77 | +etcd_debugging_lease_ttl_total_bucket{le="16384"} 218 |
| 78 | +etcd_debugging_lease_ttl_total_bucket{le="32768"} 218 |
| 79 | +etcd_debugging_lease_ttl_total_bucket{le="65536"} 218 |
| 80 | +etcd_debugging_lease_ttl_total_bucket{le="131072"} 218 |
| 81 | +etcd_debugging_lease_ttl_total_bucket{le="262144"} 218 |
| 82 | +etcd_debugging_lease_ttl_total_bucket{le="524288"} 218 |
| 83 | +etcd_debugging_lease_ttl_total_bucket{le="1.048576e+06"} 218 |
| 84 | +etcd_debugging_lease_ttl_total_bucket{le="2.097152e+06"} 218 |
| 85 | +etcd_debugging_lease_ttl_total_bucket{le="4.194304e+06"} 218 |
| 86 | +etcd_debugging_lease_ttl_total_bucket{le="8.388608e+06"} 218 |
| 87 | +etcd_debugging_lease_ttl_total_bucket{le="+Inf"} 218 |
| 88 | +etcd_debugging_lease_ttl_total_sum 2514 |
| 89 | +etcd_debugging_lease_ttl_total_count 218 |
| 90 | +` |
| 91 | + err := testutil.CollectAndCompare(leaseTotalTTLs, strings.NewReader(expected)) |
| 92 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 93 | +} |
| 94 | + |
| 95 | +func TestLeaseTotal(t *testing.T) { |
| 96 | + // Test the leaseActive metric |
| 97 | + expected := ` |
| 98 | +# HELP etcd_debugging_lease_active The current number of active leases. |
| 99 | +# TYPE etcd_debugging_lease_active gauge |
| 100 | +etcd_debugging_lease_active 1 |
| 101 | +` |
| 102 | + err := testutil.CollectAndCompare(leaseActive, strings.NewReader(expected)) |
| 103 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 104 | +} |
0 commit comments