forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp_test.go
162 lines (151 loc) · 3.83 KB
/
amqp_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package amqp
import (
"testing"
"time"
"github.com/influxdata/telegraf/internal"
"github.com/streadway/amqp"
"github.com/stretchr/testify/require"
)
type MockClient struct {
PublishF func(key string, body []byte) error
CloseF func() error
PublishCallCount int
CloseCallCount int
t *testing.T
}
func (c *MockClient) Publish(key string, body []byte) error {
c.PublishCallCount++
return c.PublishF(key, body)
}
func (c *MockClient) Close() error {
c.CloseCallCount++
return c.CloseF()
}
func MockConnect(config *ClientConfig) (Client, error) {
return &MockClient{}, nil
}
func NewMockClient() Client {
return &MockClient{
PublishF: func(key string, body []byte) error {
return nil
},
CloseF: func() error {
return nil
},
}
}
func TestConnect(t *testing.T) {
tests := []struct {
name string
output *AMQP
errFunc func(t *testing.T, output *AMQP, err error)
}{
{
name: "defaults",
output: &AMQP{
Brokers: []string{DefaultURL},
ExchangeType: DefaultExchangeType,
ExchangeDurability: "durable",
AuthMethod: DefaultAuthMethod,
Database: DefaultDatabase,
RetentionPolicy: DefaultRetentionPolicy,
Timeout: internal.Duration{Duration: time.Second * 5},
connect: func(config *ClientConfig) (Client, error) {
return NewMockClient(), nil
},
},
errFunc: func(t *testing.T, output *AMQP, err error) {
config := output.config
require.Equal(t, []string{DefaultURL}, config.brokers)
require.Equal(t, "", config.exchange)
require.Equal(t, "topic", config.exchangeType)
require.Equal(t, false, config.exchangePassive)
require.Equal(t, true, config.exchangeDurable)
require.Equal(t, amqp.Table(nil), config.exchangeArguments)
require.Equal(t, amqp.Table{
"database": DefaultDatabase,
"retention_policy": DefaultRetentionPolicy,
}, config.headers)
require.Equal(t, amqp.Transient, config.deliveryMode)
require.NoError(t, err)
},
},
{
name: "headers overrides deprecated dbrp",
output: &AMQP{
Headers: map[string]string{
"foo": "bar",
},
connect: func(config *ClientConfig) (Client, error) {
return NewMockClient(), nil
},
},
errFunc: func(t *testing.T, output *AMQP, err error) {
config := output.config
require.Equal(t, amqp.Table{
"foo": "bar",
}, config.headers)
require.NoError(t, err)
},
},
{
name: "exchange args",
output: &AMQP{
ExchangeArguments: map[string]string{
"foo": "bar",
},
connect: func(config *ClientConfig) (Client, error) {
return NewMockClient(), nil
},
},
errFunc: func(t *testing.T, output *AMQP, err error) {
config := output.config
require.Equal(t, amqp.Table{
"foo": "bar",
}, config.exchangeArguments)
require.NoError(t, err)
},
},
{
name: "username password",
output: &AMQP{
URL: "amqp://foo:bar@localhost",
Username: "telegraf",
Password: "pa$$word",
connect: func(config *ClientConfig) (Client, error) {
return NewMockClient(), nil
},
},
errFunc: func(t *testing.T, output *AMQP, err error) {
config := output.config
require.Equal(t, []amqp.Authentication{
&amqp.PlainAuth{
Username: "telegraf",
Password: "pa$$word",
},
}, config.auth)
require.NoError(t, err)
},
},
{
name: "url support",
output: &AMQP{
URL: DefaultURL,
connect: func(config *ClientConfig) (Client, error) {
return NewMockClient(), nil
},
},
errFunc: func(t *testing.T, output *AMQP, err error) {
config := output.config
require.Equal(t, []string{DefaultURL}, config.brokers)
require.NoError(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.output.Connect()
tt.errFunc(t, tt.output, err)
})
}
}