mirror of
https://github.com/slackhq/nebula.git
synced 2026-05-15 20:37:36 +02:00
Fix bugs
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
//go:build linux || darwin
|
||||||
|
|
||||||
package nebula
|
package nebula
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -13,12 +15,13 @@ import (
|
|||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Test_emitStats_primesGauges verifies issue #907: certificate gauges should
|
// Test_emitStats_primesGauges covers issue #907: a Prometheus scrape that
|
||||||
// not read 0 between goroutine launch and the first ticker fire. The ticker
|
// landed before the first ticker fire used to read 0 for the cert gauges.
|
||||||
// interval here is set far longer than the test runtime so that any non-zero
|
// emitStats now primes the gauges before entering the ticker loop. We assert
|
||||||
// reading must come from the synchronous prime call, not a tick.
|
// the gauge is zero before the first call and non-zero after.
|
||||||
func Test_emitStats_primesGauges(t *testing.T) {
|
func Test_emitStats_primesGauges(t *testing.T) {
|
||||||
defer metrics.DefaultRegistry.UnregisterAll()
|
defer metrics.DefaultRegistry.UnregisterAll()
|
||||||
|
|
||||||
@@ -45,29 +48,26 @@ func Test_emitStats_primesGauges(t *testing.T) {
|
|||||||
pki: &PKI{},
|
pki: &PKI{},
|
||||||
handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
|
handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
|
||||||
l: l,
|
l: l,
|
||||||
|
// On linux, udp.NewUDPStatsEmitter indexes writers[0] and asserts to
|
||||||
|
// *udp.StdConn. A zero value works: getMemInfo sees a nil rawConn,
|
||||||
|
// returns an error, and the emitter falls through to a no-op.
|
||||||
|
writers: []udp.Conn{&udp.StdConn{}},
|
||||||
}
|
}
|
||||||
ifce.pki.cs.Store(cs)
|
ifce.pki.cs.Store(cs)
|
||||||
|
|
||||||
|
ttlGauge := metrics.GetOrRegisterGauge("certificate.ttl_seconds", nil)
|
||||||
|
require.Zero(t, ttlGauge.Value(), "gauge should be zero before emitStats runs")
|
||||||
|
|
||||||
|
// Pre-cancel the context so emitStats returns after priming the gauges
|
||||||
|
// without ever reading from ticker.C. The one hour interval is just a
|
||||||
|
// belt-and-suspenders, the test does not expect the ticker to fire.
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
done := make(chan struct{})
|
cancel()
|
||||||
go func() {
|
ifce.emitStats(ctx, time.Hour)
|
||||||
ifce.emitStats(ctx, time.Hour) // ticker interval that will never fire
|
|
||||||
close(done)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Give the goroutine a beat to run the synchronous prime call. This is
|
ttl := ttlGauge.Value()
|
||||||
// generous: emit() is microseconds of work in practice.
|
assert.Positive(t, ttl, "ttl gauge should be primed by emitStats before its first tick")
|
||||||
assert.Eventually(t, func() bool {
|
|
||||||
return metrics.GetOrRegisterGauge("certificate.ttl_seconds", nil).Value() > 0
|
|
||||||
}, time.Second, 10*time.Millisecond, "certificate.ttl_seconds should be primed before first tick")
|
|
||||||
|
|
||||||
ttl := metrics.GetOrRegisterGauge("certificate.ttl_seconds", nil).Value()
|
|
||||||
assert.Positive(t, ttl, int64(0))
|
|
||||||
assert.LessOrEqual(t, ttl, int64(3600))
|
assert.LessOrEqual(t, ttl, int64(3600))
|
||||||
|
|
||||||
assert.Equal(t, int64(cert.Version1), metrics.GetOrRegisterGauge("certificate.initiating_version", nil).Value())
|
assert.Equal(t, int64(cert.Version1), metrics.GetOrRegisterGauge("certificate.initiating_version", nil).Value())
|
||||||
assert.Equal(t, int64(cert.Version1), metrics.GetOrRegisterGauge("certificate.max_version", nil).Value())
|
assert.Equal(t, int64(cert.Version1), metrics.GetOrRegisterGauge("certificate.max_version", nil).Value())
|
||||||
|
|
||||||
cancel()
|
|
||||||
<-done
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user