-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
client_test.go
182 lines (156 loc) · 4.83 KB
/
client_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package rabbitmqreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver"
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver/internal/models"
)
const (
queuesAPIResponseFile = "get_queues_response.json"
)
func TestNewClient(t *testing.T) {
clientConfigNonExistandCA := confighttp.NewDefaultClientConfig()
clientConfigNonExistandCA.Endpoint = defaultEndpoint
clientConfigNonExistandCA.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint
testCase := []struct {
desc string
cfg *Config
host component.Host
settings component.TelemetrySettings
logger *zap.Logger
expectError error
}{
{
desc: "Invalid HTTP config",
cfg: &Config{
ClientConfig: clientConfigNonExistandCA,
},
host: componenttest.NewNopHost(),
settings: componenttest.NewNopTelemetrySettings(),
logger: zap.NewNop(),
expectError: errors.New("failed to create HTTP Client"),
},
{
desc: "Valid Configuration",
cfg: &Config{
ClientConfig: clientConfig,
},
host: componenttest.NewNopHost(),
settings: componenttest.NewNopTelemetrySettings(),
logger: zap.NewNop(),
expectError: nil,
},
}
for _, tc := range testCase {
t.Run(tc.desc, func(t *testing.T) {
ac, err := newClient(context.Background(), tc.cfg, tc.host, tc.settings, tc.logger)
if tc.expectError != nil {
require.Nil(t, ac)
require.ErrorContains(t, err, tc.expectError.Error())
} else {
require.NoError(t, err)
actualClient, ok := ac.(*rabbitmqClient)
require.True(t, ok)
require.Equal(t, tc.cfg.Username, actualClient.creds.username)
require.Equal(t, string(tc.cfg.Password), actualClient.creds.password)
require.Equal(t, tc.cfg.Endpoint, actualClient.hostEndpoint)
require.Equal(t, tc.logger, actualClient.logger)
require.NotNil(t, actualClient.client)
}
})
}
}
func TestGetQueuesDetails(t *testing.T) {
testCases := []struct {
desc string
testFunc func(*testing.T)
}{
{
desc: "Non-200 Response",
testFunc: func(t *testing.T) {
// Setup test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
}))
defer ts.Close()
tc := createTestClient(t, ts.URL)
clusters, err := tc.GetQueues(context.Background())
require.Nil(t, clusters)
require.EqualError(t, err, "non 200 code returned 401")
},
},
{
desc: "Bad payload returned",
testFunc: func(t *testing.T) {
// Setup test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("{}"))
assert.NoError(t, err)
}))
defer ts.Close()
tc := createTestClient(t, ts.URL)
clusters, err := tc.GetQueues(context.Background())
require.Nil(t, clusters)
require.ErrorContains(t, err, "failed to decode response payload")
},
},
{
desc: "Successful call",
testFunc: func(t *testing.T) {
data := loadAPIResponseData(t, queuesAPIResponseFile)
// Setup test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write(data)
assert.NoError(t, err)
}))
defer ts.Close()
tc := createTestClient(t, ts.URL)
// Load the valid data into a struct to compare
var expected []*models.Queue
err := json.Unmarshal(data, &expected)
require.NoError(t, err)
clusters, err := tc.GetQueues(context.Background())
require.NoError(t, err)
require.Equal(t, expected, clusters)
},
},
}
for _, tc := range testCases {
t.Run(tc.desc, tc.testFunc)
}
}
func createTestClient(t *testing.T, baseEndpoint string) client {
t.Helper()
cfg := createDefaultConfig().(*Config)
cfg.Endpoint = baseEndpoint
testClient, err := newClient(context.Background(), cfg, componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), zap.NewNop())
require.NoError(t, err)
return testClient
}
func loadAPIResponseData(t *testing.T, fileName string) []byte {
t.Helper()
fullPath := filepath.Join("testdata", "apiresponses", fileName)
data, err := os.ReadFile(fullPath)
require.NoError(t, err)
return data
}