interface_emit_test.go 2.7 KB

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