-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
integration_test.go
140 lines (119 loc) · 4.64 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
135
136
137
138
139
140
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build integration
package bigipreceiver
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)
func TestIntegration(t *testing.T) {
mockServer := setupMockIControlServer(t)
defer mockServer.Close()
scraperinttest.NewIntegrationTest(
NewFactory(),
scraperinttest.WithCustomConfig(
func(_ *testing.T, cfg component.Config, _ *scraperinttest.ContainerInfo) {
rCfg := cfg.(*Config)
rCfg.CollectionInterval = 100 * time.Millisecond
rCfg.Endpoint = mockServer.URL
// Any username/password will work with mocked server right now
rCfg.Username = "otelu"
rCfg.Password = "otelp"
}),
scraperinttest.WithCompareOptions(
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricValues(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp()),
).Run(t)
}
const (
authHeader = "X-F5-Auth-Token"
loginURISuffix = "/authn/login"
getVirtualServersURISuffix = "/ltm/virtual"
getVirtualServersStatsURISuffix = "/ltm/virtual/stats"
getPoolsStatsURISuffix = "/ltm/pool/stats"
getPoolMembersStatsURISuffix = "/members/stats"
getNodesStatsURISuffix = "/ltm/node/stats"
mockLoginResponseFile = "login_response.json"
mockVirtualServersResponseFile = "virtual_servers_response.json"
mockVirtualServersStatsResponseFile = "virtual_servers_stats_response.json"
mockPoolsStatsResponseFile = "pools_stats_response.json"
mockNodesStatsResponseFile = "nodes_stats_response.json"
poolMembersStatsResponseFileSuffix = "_pool_members_stats_response.json"
)
func setupMockIControlServer(t *testing.T) *httptest.Server {
t.Helper()
// Setup responses before creating the server
mockLoginResponse := createMockServerResponseData(t, mockLoginResponseFile)
mockVirtualServersResponse := createMockServerResponseData(t, mockVirtualServersResponseFile)
mockVirtualServersStatsResponse := createMockServerResponseData(t, mockVirtualServersStatsResponseFile)
mockPoolsStatsResponse := createMockServerResponseData(t, mockPoolsStatsResponseFile)
mockNodesStatsResponse := createMockServerResponseData(t, mockNodesStatsResponseFile)
type loginBody struct {
Username string `json:"username"`
Password string `json:"password"`
}
// Setup mock iControl REST API server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Mock login responses
var err error
if strings.HasSuffix(r.RequestURI, loginURISuffix) {
var body loginBody
err = json.NewDecoder(r.Body).Decode(&body)
assert.NoError(t, err)
if body.Username == "" || body.Password == "" || r.Method != http.MethodPost {
w.WriteHeader(http.StatusUnauthorized)
} else {
_, err = w.Write(mockLoginResponse)
assert.NoError(t, err)
}
return
}
// Mock response if no auth token header
if r.Header.Get(authHeader) == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch {
case strings.HasSuffix(r.RequestURI, getVirtualServersURISuffix):
_, err = w.Write(mockVirtualServersResponse)
case strings.HasSuffix(r.RequestURI, getVirtualServersStatsURISuffix):
_, err = w.Write(mockVirtualServersStatsResponse)
case strings.HasSuffix(r.RequestURI, getPoolsStatsURISuffix):
_, err = w.Write(mockPoolsStatsResponse)
case strings.HasSuffix(r.RequestURI, getNodesStatsURISuffix):
_, err = w.Write(mockNodesStatsResponse)
case strings.HasSuffix(r.RequestURI, getPoolMembersStatsURISuffix):
// Assume pool member response files follow a specific file pattern based of pool name
poolURI := strings.TrimSuffix(r.RequestURI, getPoolMembersStatsURISuffix)
poolURIParts := strings.Split(poolURI, "/")
poolName := strings.ReplaceAll(poolURIParts[len(poolURIParts)-1], "~", "_")
poolMembersStatsData := createMockServerResponseData(t, poolName+poolMembersStatsResponseFileSuffix)
_, err = w.Write(poolMembersStatsData)
default:
w.WriteHeader(http.StatusBadRequest)
err = nil
}
assert.NoError(t, err)
}))
return server
}
func createMockServerResponseData(t *testing.T, fileName string) []byte {
t.Helper()
fullPath := filepath.Join("testdata", "integration", "mock_server", fileName)
data, err := os.ReadFile(fullPath)
require.NoError(t, err)
return data
}