forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolver_aws_cloudmap_test.go
116 lines (101 loc) · 2.82 KB
/
resolver_aws_cloudmap_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package loadbalancingexporter
import (
"context"
"fmt"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/service/servicediscovery"
"github.com/aws/aws-sdk-go-v2/service/servicediscovery/types"
"github.com/aws/smithy-go/middleware"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
var instanceID = "test"
var namespaceName = "cloudmap"
var statusFilterHealthy = types.HealthStatusFilterHealthy
var port uint16 = 1234
func TestInitialCloudMapResolution(t *testing.T) {
// prepare
_, tb := getTelemetryAssets(t)
res := &cloudMapResolver{
logger: zap.NewNop(),
namespaceName: &namespaceName,
serviceName: &instanceID,
healthStatus: &statusFilterHealthy,
resInterval: 5 * time.Second,
resTimeout: 1 * time.Second,
stopCh: make(chan struct{}),
discoveryFn: mockDiscovery,
telemetry: tb,
}
// test
var resolved []string
res.onChange(func(endpoints []string) {
resolved = endpoints
})
require.NoError(t, res.start(context.Background()))
defer func() {
require.NoError(t, res.shutdown(context.Background()))
}()
// verify
assert.Len(t, resolved, 3)
for i, value := range []string{"127.0.0.1:8080", "127.0.0.2:8080", "127.0.0.3:8080"} {
assert.Equal(t, value, resolved[i])
}
}
func TestInitialCloudMapResolutionWithPort(t *testing.T) {
// prepare
_, tb := getTelemetryAssets(t)
res := &cloudMapResolver{
logger: zap.NewNop(),
namespaceName: &namespaceName,
serviceName: &instanceID,
port: &port,
healthStatus: &statusFilterHealthy,
resInterval: 5 * time.Second,
resTimeout: 1 * time.Second,
stopCh: make(chan struct{}),
discoveryFn: mockDiscovery,
telemetry: tb,
}
// test
var resolved []string
res.onChange(func(endpoints []string) {
resolved = endpoints
})
require.NoError(t, res.start(context.Background()))
defer func() {
require.NoError(t, res.shutdown(context.Background()))
}()
// verify
assert.Len(t, resolved, 3)
for i, value := range []string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"} {
assert.Equal(t, value, resolved[i])
}
}
func makeSummary(i int) types.HttpInstanceSummary {
return types.HttpInstanceSummary{
Attributes: map[string]string{
"AWS_INSTANCE_IPV4": fmt.Sprintf("127.0.0.%d", i),
"AWS_INSTANCE_PORT": "8080",
},
HealthStatus: types.HealthStatusHealthy,
InstanceId: &instanceID,
NamespaceName: nil,
ServiceName: nil,
}
}
func mockDiscovery(*servicediscovery.DiscoverInstancesInput) (*servicediscovery.DiscoverInstancesOutput, error) {
s := &servicediscovery.DiscoverInstancesOutput{
Instances: []types.HttpInstanceSummary{
makeSummary(1),
makeSummary(2),
makeSummary(3),
},
ResultMetadata: middleware.Metadata{},
}
return s, nil
}