forked from Syncano/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 197
/
integration_test.go
134 lines (102 loc) · 4.02 KB
/
integration_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//go:build integration
// +build integration
package main
import (
"fmt"
"os"
"testing"
"time"
"regexp"
"strings"
"github.com/kbudde/rabbitmq_exporter/testenv"
)
func TestQueueCount(t *testing.T) {
var env testenv.TestEnvironment
var exporterURL string
var rabbitManagementURL string
t.Run("Setup test environment", func(t *testing.T) {
env = testenv.NewEnvironment(t, testenv.RabbitMQ3Latest)
})
defer env.CleanUp() // do not panic or exit fatally or the container will stay up
t.Run("Preparation", func(t *testing.T) {
exporterURL = fmt.Sprintf("http://localhost:%s/metrics", defaultConfig.PublishPort)
rabbitManagementURL = env.ManagementURL()
os.Setenv("RABBIT_URL", rabbitManagementURL)
os.Setenv("RABBIT_CAPABILITIES", "bert") // no_sort not supported in rabbitmq 3.13+
defer os.Unsetenv("RABBIT_URL")
defer os.Unsetenv("RABBIT_CAPABILITIES")
go main()
time.Sleep(2 * time.Second)
})
t.Run("Ensure there are no queues", func(t *testing.T) {
body := testenv.GetOrDie(exporterURL, 5*time.Second)
r := regexp.MustCompile(`rabbitmq_queues{cluster="rabbit@localtest"} 0`)
if s := r.FindString(body); s == "" {
t.Fatalf("QueueCount 0 not found in body: %v", body)
}
})
t.Run("Add one queue and check again", func(t *testing.T) {
env.Rabbit.DeclareQueue("QueueForCheckCount", false)
body := testenv.GetOrDie(exporterURL, 5*time.Second)
r := regexp.MustCompile(`rabbitmq_queues{cluster="rabbit@localtest"} 1`)
if s := r.FindString(body); s == "" {
// t.Logf("body: %s", body)
t.Fatalf("QueueCount 1 not found ")
}
})
t.Run("Add message with timestamp", func(t *testing.T) {
queue := "timestamp"
env.Rabbit.DeclareQueue(queue, true)
timestamp := time.Date(2017, 11, 27, 8, 25, 23, 0, time.UTC)
env.Rabbit.SendMessageToQ("Test timestamp", queue, ×tamp)
time.Sleep(10 * time.Second) // give rabbitmq management plugin a bit of time
// log.Println(testenv.GetOrDie(env.ManagementURL()+"/api/queues", 5*time.Second))
body := testenv.GetOrDie(exporterURL, 5*time.Second)
search := fmt.Sprintf(`rabbitmq_queue_head_message_timestamp{cluster="rabbit@localtest",durable="true",policy="",queue="%s",self="1",vhost="/"} %1.9e`, queue, float64(timestamp.Unix()))
i := strings.Index(body, search)
if i == -1 {
t.Log(body, search)
t.Fatalf("Timestamp not found")
}
})
t.Run("Queue durable true", func(t *testing.T) {
queue := "dur-true"
env.Rabbit.DeclareQueue("dur-true", true)
time.Sleep(5 * time.Second) // give rabbitmq management plugin a bit of time
body := testenv.GetOrDie(exporterURL, 5*time.Second)
search := fmt.Sprintf(`rabbitmq_queue_messages{cluster="rabbit@localtest",durable="true",policy="",queue="%s",self="1",vhost="/"} 0`, queue)
i := strings.Index(body, search)
if i == -1 {
t.Log(body, search)
t.Fatalf("Queue dur-true not found")
}
})
t.Run("Queue durable false", func(t *testing.T) {
queue := "dur-false"
env.Rabbit.DeclareQueue("dur-false", false)
time.Sleep(5 * time.Second) // give rabbitmq management plugin a bit of time
body := testenv.GetOrDie(exporterURL, 5*time.Second)
search := fmt.Sprintf(`rabbitmq_queue_messages{cluster="rabbit@localtest",durable="false",policy="",queue="%s",self="1",vhost="/"} 0`, queue)
i := strings.Index(body, search)
if i == -1 {
t.Log(body, search)
t.Fatalf("Queue dur-false not found")
}
})
t.Run("Queue policy", func(t *testing.T) {
queue := "QueueWithPol"
env.Rabbit.DeclareQueue(queue, false)
policy := "QueuePolicy"
env.MustSetPolicy(policy, "^.*$")
time.Sleep(10 * time.Second) // give rabbitmq management plugin a bit of time
body := testenv.GetOrDie(exporterURL, 5*time.Second)
search := fmt.Sprintf(`rabbitmq_queue_messages{cluster="rabbit@localtest",durable="false",policy="%s",queue="%s",self="1",vhost="/"} 0`, policy, queue)
i := strings.Index(body, search)
if i == -1 {
// t.Log(env.ManagementURL())
// t.Log(testenv.GetOrDie(env.ManagementURL()+"/api/queues", 5*time.Second))
t.Log(body, search)
t.Fatalf("Queue with policy not found")
}
})
}