| 1 |
//go:build linux || darwin |
| 2 |
|
| 3 |
package nebula |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"net/netip" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/rcrowley/go-metrics" |
| 12 |
"github.com/slackhq/nebula/cert" |
| 13 |
"github.com/slackhq/nebula/firewall" |
| 14 |
"github.com/slackhq/nebula/overlay/overlaytest" |
| 15 |
"github.com/slackhq/nebula/test" |
| 16 |
"github.com/slackhq/nebula/udp" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
) |
| 20 |
|
| 21 |
// Test_emitStats_primesGauges covers issue #907: a Prometheus scrape that |
| 22 |
// landed before the first ticker fire used to read 0 for the cert gauges. |
| 23 |
// emitStats now primes the gauges before entering the ticker loop. We assert |
| 24 |
// the gauge is zero before the first call and non-zero after. |
| 25 |
func Test_emitStats_primesGauges(t *testing.T) { |
| 26 |
defer metrics.DefaultRegistry.UnregisterAll() |
| 27 |
|
| 28 |
l := test.NewLogger() |
| 29 |
hostMap := newHostMap(l) |
| 30 |
preferredRanges := []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")} |
| 31 |
hostMap.preferredRanges.Store(&preferredRanges) |
| 32 |
|
| 33 |
notAfter := time.Now().Add(time.Hour) |
| 34 |
cs := &CertState{ |
| 35 |
initiatingVersion: cert.Version1, |
| 36 |
privateKey: []byte{}, |
| 37 |
v1Cert: &dummyCert{version: cert.Version1, notAfter: notAfter}, |
| 38 |
v1Credential: nil, |
| 39 |
} |
| 40 |
|
| 41 |
lh := newTestLighthouse() |
| 42 |
ifce := &Interface{ |
| 43 |
hostMap: hostMap, |
| 44 |
inside: &overlaytest.NoopTun{}, |
| 45 |
outside: &udp.NoopConn{}, |
| 46 |
firewall: &Firewall{Conntrack: &FirewallConntrack{Conns: map[firewall.Packet]*conn{}}}, |
| 47 |
lightHouse: lh, |
| 48 |
pki: &PKI{}, |
| 49 |
handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig), |
| 50 |
l: l, |
| 51 |
// On linux, udp.NewUDPStatsEmitter indexes writers[0] and asserts to |
| 52 |
// *udp.StdConn. A zero value works: getMemInfo sees a nil rawConn, |
| 53 |
// returns an error, and the emitter falls through to a no-op. |
| 54 |
writers: []udp.Conn{&udp.StdConn{}}, |
| 55 |
} |
| 56 |
ifce.pki.cs.Store(cs) |
| 57 |
|
| 58 |
ttlGauge := metrics.GetOrRegisterGauge("certificate.ttl_seconds", nil) |
| 59 |
require.Zero(t, ttlGauge.Value(), "gauge should be zero before emitStats runs") |
| 60 |
|
| 61 |
// Pre-cancel the context so emitStats returns after priming the gauges |
| 62 |
// without ever reading from ticker.C. The one hour interval is just a |
| 63 |
// belt-and-suspenders, the test does not expect the ticker to fire. |
| 64 |
ctx, cancel := context.WithCancel(context.Background()) |
| 65 |
cancel() |
| 66 |
ifce.emitStats(ctx, time.Hour) |
| 67 |
|
| 68 |
ttl := ttlGauge.Value() |
| 69 |
assert.Positive(t, ttl, "ttl gauge should be primed by emitStats before its first tick") |
| 70 |
assert.LessOrEqual(t, ttl, int64(3600)) |
| 71 |
assert.Equal(t, int64(cert.Version1), metrics.GetOrRegisterGauge("certificate.initiating_version", nil).Value()) |
| 72 |
assert.Equal(t, int64(cert.Version1), metrics.GetOrRegisterGauge("certificate.max_version", nil).Value()) |
| 73 |
} |