-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.go
61 lines (46 loc) · 1.44 KB
/
init_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package victoriaMetricsInit
import (
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"os"
"runtime"
"testing"
"time"
)
func TestInitMetrics(t *testing.T) {
t.Run("request", func(t *testing.T) {
testHost := "testHost:8428"
_ = os.Setenv("VICTORIA_METRICS_HOST", testHost)
_ = os.Setenv("VICTORIA_METRICS_INTERVAL", "1s")
gock.New("http://" + testHost).Get("/api/v1/import/prometheus").Reply(200)
InitMetrics("testInstance")
time.Sleep(time.Second * 2)
assert.True(t, gock.IsDone())
})
t.Run("defaults", func(t *testing.T) {
defaultTimeInterval = 2 * time.Second
_ = os.Setenv("VICTORIA_METRICS_HOST", "")
_ = os.Setenv("VICTORIA_METRICS_INTERVAL", "")
gock.New("http://" + defaultHost).Get("/api/v1/import/prometheus").Reply(200)
InitMetrics("testInstance")
runtime.Gosched()
time.Sleep(defaultTimeInterval + time.Millisecond*500)
assert.True(t, gock.IsDone())
assert.Equal(t, "testInstance", LastInstance)
})
t.Run("errors", func(t *testing.T) {
stdErrFile, _ := os.CreateTemp("", "stderr.txt")
origStderr := os.Stderr
os.Stderr = stdErrFile
defer (func() {
_ = stdErrFile.Close()
_ = os.Remove(stdErrFile.Name())
os.Stderr = origStderr
})()
_ = os.Setenv("VICTORIA_METRICS_HOST", "\n")
_ = os.Setenv("VICTORIA_METRICS_INTERVAL", "")
InitMetrics("testInstance")
content, _ := os.ReadFile(stdErrFile.Name())
assert.Contains(t, string(content), "Failed to init metrics: ")
})
}