-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
148 lines (121 loc) · 4.15 KB
/
main_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// MockSQSClient is a mock of the SQS client
type MockSQSClient struct {
mock.Mock
}
// GetQueueAttributes mocks the GetQueueAttributes method
func (m *MockSQSClient) GetQueueAttributes(ctx context.Context, params *sqs.GetQueueAttributesInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error) {
args := m.Called(ctx, params, optFns)
return args.Get(0).(*sqs.GetQueueAttributesOutput), args.Error(1)
}
func TestGetMonitorInterval(t *testing.T) {
tests := []struct {
name string
envValue string
expectedResult time.Duration
}{
{"Default value", "", 30 * time.Second},
{"Custom value", "60", 60 * time.Second},
{"Invalid value", "invalid", 30 * time.Second},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("SQS_MONITOR_INTERVAL_SECONDS", tt.envValue)
defer os.Unsetenv("SQS_MONITOR_INTERVAL_SECONDS")
result := getMonitorInterval()
assert.Equal(t, tt.expectedResult, result)
})
}
}
func TestMonitorQueue(t *testing.T) {
mockClient := new(MockSQSClient)
originalSvc := svc
svc = mockClient
defer func() { svc = originalSvc }()
queueURL := "https://sqs.us-west-2.amazonaws.com/123456789012/MyQueue"
expectedOutput := &sqs.GetQueueAttributesOutput{
Attributes: map[string]string{
"ApproximateNumberOfMessages": "10",
"ApproximateNumberOfMessagesDelayed": "5",
"ApproximateNumberOfMessagesNotVisible": "2",
},
}
mockClient.On("GetQueueAttributes", mock.Anything, mock.Anything, mock.Anything).Return(expectedOutput, nil)
c := make(chan queueResult)
go monitorQueue(queueURL, c)
result := <-c
assert.Equal(t, queueURL, result.QueueURL)
assert.Equal(t, "MyQueue", result.QueueName)
assert.Equal(t, expectedOutput, result.QueueResults)
mockClient.AssertExpectations(t)
}
func TestHealthcheck(t *testing.T) {
assert.NotPanics(t, func() { healthcheck(nil, nil) })
}
func TestSQSMetrics(t *testing.T) {
// Set up mock SQS client
mockClient := new(MockSQSClient)
originalSvc := svc
svc = mockClient
defer func() { svc = originalSvc }()
// Set up test queue URL and expected metrics
queueURL := "https://sqs.us-west-2.amazonaws.com/123456789012/TestQueue"
expectedOutput := &sqs.GetQueueAttributesOutput{
Attributes: map[string]string{
"ApproximateNumberOfMessages": "10",
"ApproximateNumberOfMessagesDelayed": "5",
"ApproximateNumberOfMessagesNotVisible": "2",
},
}
// Configure mock client to return the expected output
mockClient.On("GetQueueAttributes", mock.Anything, mock.Anything, mock.Anything).Return(expectedOutput, nil)
// Set environment variables
os.Setenv("SQS_QUEUE_URLS", queueURL)
os.Setenv("SQS_MONITOR_INTERVAL_SECONDS", "1")
defer os.Unsetenv("SQS_QUEUE_URLS")
defer os.Unsetenv("SQS_MONITOR_INTERVAL_SECONDS")
// Start the monitor in a goroutine
go monitorQueues([]string{queueURL})
// Wait for metrics to be collected
time.Sleep(2 * time.Second)
// Set up test HTTP server
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
testServer := httptest.NewServer(mux)
defer testServer.Close()
// Send request to /metrics endpoint
resp, err := http.Get(testServer.URL + "/metrics")
assert.NoError(t, err)
defer resp.Body.Close()
// Read response body
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
// Check for expected metrics
bodyString := string(body)
expectedMetrics := []string{
`sqs_approximatenumberofmessages{queue="TestQueue"} 10`,
`sqs_approximatenumberofmessagesdelayed{queue="TestQueue"} 5`,
`sqs_approximatenumberofmessagesnotvisible{queue="TestQueue"} 2`,
}
for _, metric := range expectedMetrics {
assert.True(t, strings.Contains(bodyString, metric), fmt.Sprintf("Metric not found: %s", metric))
}
// Clear registered metrics to avoid conflicts in other tests
prometheus.DefaultRegisterer = prometheus.NewRegistry()
}