forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
139 lines (117 loc) · 3.63 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver"
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"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/riakreceiver/internal/model"
)
const (
statsAPIResponseFile = "get_stats_response.json"
)
func TestNewClient(t *testing.T) {
testCase := []struct {
desc string
cfg *Config
expectError error
}{
{
desc: "Invalid HTTP config",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
},
},
},
expectError: errors.New("failed to create HTTP Client"),
},
{
desc: "Valid Configuration",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
TLSSetting: configtls.ClientConfig{},
Endpoint: defaultEndpoint,
},
},
expectError: nil,
},
}
for _, tc := range testCase {
t.Run(tc.desc, func(t *testing.T) {
ac, err := newClient(context.Background(), tc.cfg, componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), zap.NewNop())
if tc.expectError != nil {
require.Nil(t, ac)
require.Contains(t, err.Error(), tc.expectError.Error())
} else {
require.NoError(t, err)
actualClient, ok := ac.(*riakClient)
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, zap.NewNop(), actualClient.logger)
require.NotNil(t, actualClient.client)
}
})
}
}
func TestGetStatsDetails(t *testing.T) {
t.Run("Non-200 Response", 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.GetStats(context.Background())
require.Nil(t, clusters)
require.EqualError(t, err, "non 200 code returned 401")
})
t.Run("Successful call", func(t *testing.T) {
data := loadAPIResponseData(t, statsAPIResponseFile)
// Setup test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write(data)
require.NoError(t, err)
}))
defer ts.Close()
tc := createTestClient(t, ts.URL)
// Load the valid data into a struct to compare
var expected *model.Stats
err := json.Unmarshal(data, &expected)
require.NoError(t, err)
clusters, err := tc.GetStats(context.Background())
require.NoError(t, err)
require.Equal(t, expected, clusters)
})
}
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
}