|
| 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 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestLeaseGranted(t *testing.T) { |
| 25 | + // Test the leaseGranted metric |
| 26 | + // This is a counter metric, which means it can only be incremented |
| 27 | + // The metric is incremented by 10 and we check if it is 10 |
| 28 | + leaseGranted.Add(10) |
| 29 | + expected := ` |
| 30 | +# HELP etcd_debugging_lease_granted_total The total number of granted leases. |
| 31 | +# TYPE etcd_debugging_lease_granted_total counter |
| 32 | +etcd_debugging_lease_granted_total 10 |
| 33 | +` |
| 34 | + err := testutil.CollectAndCompare(leaseGranted, strings.NewReader(expected)) |
| 35 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 36 | +} |
| 37 | + |
| 38 | +func TestNewLeaseRevoked(t *testing.T) { |
| 39 | + // Test the leaseRevoked metric |
| 40 | + // This is a counter metric, which means it can only be incremented |
| 41 | + // The metric is incremented by 10 and we check if it is 10 |
| 42 | + leaseRevoked.Add(10) |
| 43 | + expected := ` |
| 44 | +# HELP etcd_debugging_lease_revoked_total The total number of revoked leases. |
| 45 | +# TYPE etcd_debugging_lease_revoked_total counter |
| 46 | +etcd_debugging_lease_revoked_total 10 |
| 47 | +` |
| 48 | + err := testutil.CollectAndCompare(leaseRevoked, strings.NewReader(expected)) |
| 49 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 50 | +} |
| 51 | + |
| 52 | +func TestLeaseRenewed(t *testing.T) { |
| 53 | + // Test the leaseRenewed metric |
| 54 | + // This is a counter metric, which means it can only be incremented |
| 55 | + // The metric is incremented by 10 and we check if it is 10 |
| 56 | + leaseRenewed.Add(10) |
| 57 | + expected := ` |
| 58 | +# HELP etcd_debugging_lease_renewed_total The number of renewed leases seen by the leader. |
| 59 | +# TYPE etcd_debugging_lease_renewed_total counter |
| 60 | +etcd_debugging_lease_renewed_total 10 |
| 61 | +` |
| 62 | + err := testutil.CollectAndCompare(leaseRenewed, strings.NewReader(expected)) |
| 63 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 64 | +} |
| 65 | + |
| 66 | +func TestLeaseTotalTTLs(t *testing.T) { |
| 67 | + // Test the leaseTotalTTLs metric |
| 68 | + // This is a histogram metric, which means it can be incremented and observed |
| 69 | + // The metric is incremented by 10 and we check if it is 10 |
| 70 | + leaseTotalTTLs.Observe(10) |
| 71 | + expected := ` |
| 72 | +# HELP etcd_debugging_lease_ttl_total Bucketed histogram of lease TTLs. |
| 73 | +# TYPE etcd_debugging_lease_ttl_total histogram |
| 74 | +etcd_debugging_lease_ttl_total_bucket{le="1"} 0 |
| 75 | +etcd_debugging_lease_ttl_total_bucket{le="2"} 0 |
| 76 | +etcd_debugging_lease_ttl_total_bucket{le="4"} 0 |
| 77 | +etcd_debugging_lease_ttl_total_bucket{le="8"} 0 |
| 78 | +etcd_debugging_lease_ttl_total_bucket{le="16"} 1 |
| 79 | +etcd_debugging_lease_ttl_total_bucket{le="32"} 1 |
| 80 | +etcd_debugging_lease_ttl_total_bucket{le="64"} 1 |
| 81 | +etcd_debugging_lease_ttl_total_bucket{le="128"} 1 |
| 82 | +etcd_debugging_lease_ttl_total_bucket{le="256"} 1 |
| 83 | +etcd_debugging_lease_ttl_total_bucket{le="512"} 1 |
| 84 | +etcd_debugging_lease_ttl_total_bucket{le="1024"} 1 |
| 85 | +etcd_debugging_lease_ttl_total_bucket{le="2048"} 1 |
| 86 | +etcd_debugging_lease_ttl_total_bucket{le="4096"} 1 |
| 87 | +etcd_debugging_lease_ttl_total_bucket{le="8192"} 1 |
| 88 | +etcd_debugging_lease_ttl_total_bucket{le="16384"} 1 |
| 89 | +etcd_debugging_lease_ttl_total_bucket{le="32768"} 1 |
| 90 | +etcd_debugging_lease_ttl_total_bucket{le="65536"} 1 |
| 91 | +etcd_debugging_lease_ttl_total_bucket{le="131072"} 1 |
| 92 | +etcd_debugging_lease_ttl_total_bucket{le="262144"} 1 |
| 93 | +etcd_debugging_lease_ttl_total_bucket{le="524288"} 1 |
| 94 | +etcd_debugging_lease_ttl_total_bucket{le="1.048576e+06"} 1 |
| 95 | +etcd_debugging_lease_ttl_total_bucket{le="2.097152e+06"} 1 |
| 96 | +etcd_debugging_lease_ttl_total_bucket{le="4.194304e+06"} 1 |
| 97 | +etcd_debugging_lease_ttl_total_bucket{le="8.388608e+06"} 1 |
| 98 | +etcd_debugging_lease_ttl_total_bucket{le="+Inf"} 1 |
| 99 | +etcd_debugging_lease_ttl_total_sum 10 |
| 100 | +etcd_debugging_lease_ttl_total_count 1 |
| 101 | +` |
| 102 | + err := testutil.CollectAndCompare(leaseTotalTTLs, strings.NewReader(expected)) |
| 103 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 104 | +} |
| 105 | + |
| 106 | +func TestLeaseTotal(t *testing.T) { |
| 107 | + // Test the leaseTotal metric |
| 108 | + // This is a gauge metric, which means it can be set to any value |
| 109 | + // The metric is set to 10 and we check if it is 10 |
| 110 | + leaseTotal.Set(10) |
| 111 | + expected := ` |
| 112 | +# HELP etcd_debugging_lease_total The total number of active leases. |
| 113 | +# TYPE etcd_debugging_lease_total gauge |
| 114 | +etcd_debugging_lease_total 10 |
| 115 | +` |
| 116 | + err := testutil.CollectAndCompare(leaseTotal, strings.NewReader(expected)) |
| 117 | + require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err) |
| 118 | +} |
0 commit comments