github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/metrics/prometheus_test.go (about) 1 package metrics 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 9 "core" 10 ) 11 12 const url = "http://localhost:9999" 13 const verySlow = 10000000 // Long duration so it never actually reports anything. 14 const timeout = 10000000 // Long timeout so it doesn't affect things 15 16 var label = core.BuildLabel{PackageName: "src/metrics", Name: "prometheus"} 17 18 func TestNoMetrics(t *testing.T) { 19 m := initMetrics(url, verySlow, timeout, nil, true) 20 assert.Equal(t, 0, m.errors) 21 assert.Equal(t, 0, m.pushes) 22 m.stop() 23 assert.Equal(t, 0, m.errors, "Stop should not push when there aren't metrics") 24 } 25 26 func TestSomeMetrics(t *testing.T) { 27 m := initMetrics(url, verySlow, timeout, nil, true) 28 assert.Equal(t, 0, m.errors) 29 assert.Equal(t, 0, m.pushes) 30 m.record(core.NewBuildTarget(label), time.Millisecond) 31 m.stop() 32 assert.Equal(t, 1, m.errors, "Stop should push once more when there are metrics") 33 } 34 35 func TestTargetStates(t *testing.T) { 36 m := initMetrics(url, verySlow, timeout, nil, true) 37 assert.Equal(t, 0, m.errors) 38 assert.Equal(t, 0, m.pushes) 39 target := core.NewBuildTarget(label) 40 m.record(target, time.Millisecond) 41 target.SetState(core.Cached) 42 m.record(target, time.Millisecond) 43 target.SetState(core.Built) 44 m.record(target, time.Millisecond) 45 target.Results.NumTests = 3 46 m.record(target, time.Millisecond) 47 target.Results.Failed = 1 48 m.record(target, time.Millisecond) 49 target.Results.Cached = true 50 m.record(target, time.Millisecond) 51 m.stop() 52 assert.Equal(t, 1, m.errors) 53 } 54 55 func TestPushAttempts(t *testing.T) { 56 m := initMetrics(url, 1, 1000, nil, true) // Fast push attempts 57 assert.Equal(t, 0, m.errors) 58 assert.Equal(t, 0, m.pushes) 59 m.record(core.NewBuildTarget(label), time.Millisecond) 60 time.Sleep(50 * time.Millisecond) // Not ideal but should be heaps of time for it to attempt pushes. 61 assert.Equal(t, maxErrors, m.errors) 62 assert.True(t, m.cancelled) 63 m.stop() 64 assert.Equal(t, maxErrors, m.errors, "Should not push again if it's hit the max errors") 65 } 66 67 func TestCustomLabels(t *testing.T) { 68 m := initMetrics(url, verySlow, timeout, map[string]string{ 69 "mylabel": "echo hello", 70 }, true) 71 // It's a little bit fiddly to observe that the const label has been set as expected. 72 c := m.cacheCounter.WithLabelValues("false") 73 assert.Contains(t, c.Desc().String(), `mylabel="hello"`) 74 } 75 76 func TestCustomLabelsShlex(t *testing.T) { 77 // Naive splitting will not produce good results here. 78 m := initMetrics(url, verySlow, timeout, map[string]string{ 79 "mylabel": "bash -c 'echo hello'", 80 }, false) 81 c := m.cacheCounter.WithLabelValues("false") 82 assert.Contains(t, c.Desc().String(), `mylabel="hello"`) 83 } 84 85 func TestCustomLabelsShlexInvalid(t *testing.T) { 86 assert.Panics(t, func() { 87 initMetrics(url, verySlow, timeout, map[string]string{ 88 "mylabel": "bash -c 'echo hello", // missing trailing quote 89 }, false) 90 }) 91 } 92 93 func TestCustomLabelsCommandFails(t *testing.T) { 94 assert.Panics(t, func() { 95 initMetrics(url, verySlow, timeout, map[string]string{ 96 "mylabel": "wibble", 97 }, false) 98 }) 99 } 100 101 func TestCustomLabelsCommandNewlines(t *testing.T) { 102 assert.Panics(t, func() { 103 initMetrics(url, verySlow, timeout, map[string]string{ 104 "mylabel": "echo 'hello\nworld\n'", 105 }, true) 106 }) 107 } 108 109 func TestExportedFunctions(t *testing.T) { 110 // For various reasons it's important that this is the only test that uses the global singleton. 111 config := core.DefaultConfiguration() 112 config.Metrics.PushGatewayURL = url 113 config.Metrics.PushFrequency = verySlow 114 InitFromConfig(config) 115 Record(core.NewBuildTarget(label), time.Millisecond) 116 Stop() 117 assert.Equal(t, 1, m.errors) 118 }