-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
receiver_test.go
143 lines (130 loc) · 3.6 KB
/
receiver_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package mongodbatlasreceiver
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
)
func TestDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.Equal(t, 3*time.Minute, cfg.(*Config).ControllerConfig.CollectionInterval)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv, err := createMetricsReceiver(ctx, receivertest.NewNopSettings(), cfg, consumertest.NewNop())
require.NoError(t, err)
require.NotNil(t, recv, "receiver creation failed")
err = recv.Start(ctx, componenttest.NewNopHost())
require.NoError(t, err)
err = recv.Shutdown(ctx)
require.NoError(t, err)
}
func TestTimeConstraints(t *testing.T) {
tt := []struct {
name string
run func(t *testing.T)
}{
{
name: "initial lookback is now() - collection_interval",
run: func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
// lastRun is nil
recv := mongodbatlasreceiver{
cfg: cfg,
}
now := time.Now()
tc := recv.timeConstraints(now)
require.NotNil(t, tc)
require.Equal(t, tc.start, now.Add(cfg.CollectionInterval*-1).UTC().Format(time.RFC3339))
},
},
{
name: "lookback for subsequent runs is now() - lastRun",
run: func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
now := time.Now()
recv := mongodbatlasreceiver{
cfg: cfg,
// set last run to 1 collection ago
lastRun: now.Add(cfg.CollectionInterval * -1),
}
tc := recv.timeConstraints(now)
require.NotNil(t, tc)
require.Equal(t, tc.start, recv.lastRun.UTC().Format(time.RFC3339))
},
},
}
for _, testCase := range tt {
t.Run(testCase.name, testCase.run)
}
}
func TestShouldProcessCluster(t *testing.T) {
tests := []struct {
name string
projectCfg *ProjectConfig
clusterName string
want bool
}{
{
name: "included cluster should be processed",
projectCfg: &ProjectConfig{
IncludeClusters: []string{"Cluster1"},
},
clusterName: "Cluster1",
want: true,
},
{
name: "cluster not included should not be processed",
projectCfg: &ProjectConfig{
IncludeClusters: []string{"Cluster1"},
},
clusterName: "Cluster2",
want: false,
},
{
name: "excluded cluster should not be processed",
projectCfg: &ProjectConfig{
ExcludeClusters: []string{"Cluster2"},
},
clusterName: "Cluster2",
want: false,
},
{
name: "cluster not excluded should processed assuming it exists in the project",
projectCfg: &ProjectConfig{
ExcludeClusters: []string{"Cluster1"},
},
clusterName: "Cluster2",
want: true,
},
{
name: "cluster should be processed when no includes or excludes are set",
projectCfg: &ProjectConfig{},
clusterName: "Cluster1",
want: true,
},
{
name: "cluster should be processed when no includes or excludes are set and cluster name is empty",
projectCfg: nil,
clusterName: "Cluster1",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.projectCfg != nil {
tt.projectCfg.populateIncludesAndExcludes()
}
if got := shouldProcessCluster(tt.projectCfg, tt.clusterName); got != tt.want {
t.Errorf("shouldProcessCluster() = %v, want %v", got, tt.want)
}
})
}
}