github.com/tiagovtristao/plz@v13.4.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 "github.com/thought-machine/please/src/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, 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, 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, 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.Cached = true 46 m.record(target, time.Millisecond) 47 m.stop() 48 assert.Equal(t, 1, m.errors) 49 } 50 51 func TestPushAttempts(t *testing.T) { 52 m := initMetrics(url, 1, 1000, nil, true, true) // Fast push attempts 53 assert.Equal(t, 0, m.errors) 54 assert.Equal(t, 0, m.pushes) 55 m.record(core.NewBuildTarget(label), time.Millisecond) 56 time.Sleep(50 * time.Millisecond) // Not ideal but should be heaps of time for it to attempt pushes. 57 assert.Equal(t, maxErrors, m.errors) 58 assert.True(t, m.cancelled) 59 m.stop() 60 assert.Equal(t, maxErrors, m.errors, "Should not push again if it's hit the max errors") 61 } 62 63 func TestCustomLabels(t *testing.T) { 64 m := initMetrics(url, verySlow, timeout, map[string]string{ 65 "mylabel": "echo hello", 66 }, true, true) 67 // It's a little bit fiddly to observe that the const label has been set as expected. 68 c := m.cacheCounter.WithLabelValues("false") 69 assert.Contains(t, c.Desc().String(), `mylabel="hello"`) 70 } 71 72 func TestCustomLabelsShlex(t *testing.T) { 73 // Naive splitting will not produce good results here. 74 m := initMetrics(url, verySlow, timeout, map[string]string{ 75 "mylabel": "bash -c 'echo hello'", 76 }, false, true) 77 c := m.cacheCounter.WithLabelValues("false") 78 assert.Contains(t, c.Desc().String(), `mylabel="hello"`) 79 } 80 81 func TestCustomLabelsShlexInvalid(t *testing.T) { 82 assert.Panics(t, func() { 83 initMetrics(url, verySlow, timeout, map[string]string{ 84 "mylabel": "bash -c 'echo hello", // missing trailing quote 85 }, false, true) 86 }) 87 } 88 89 func TestCustomLabelsCommandFails(t *testing.T) { 90 assert.Panics(t, func() { 91 initMetrics(url, verySlow, timeout, map[string]string{ 92 "mylabel": "wibble", 93 }, false, true) 94 }) 95 } 96 97 func TestCustomLabelsCommandNewlines(t *testing.T) { 98 assert.Panics(t, func() { 99 initMetrics(url, verySlow, timeout, map[string]string{ 100 "mylabel": "echo 'hello\nworld\n'", 101 }, true, true) 102 }) 103 } 104 105 func TestExportedFunctions(t *testing.T) { 106 // For various reasons it's important that this is the only test that uses the global singleton. 107 config := core.DefaultConfiguration() 108 config.Metrics.PushGatewayURL = url 109 config.Metrics.PushFrequency = verySlow 110 InitFromConfig(config) 111 Record(core.NewBuildTarget(label), time.Millisecond) 112 Stop() 113 assert.Equal(t, 1, m.errors) 114 }