@@ -12,8 +12,6 @@ import (
12
12
"github.com/stretchr/testify/require"
13
13
"go.opentelemetry.io/collector/component"
14
14
"go.opentelemetry.io/collector/confmap/confmaptest"
15
- "go.opentelemetry.io/collector/confmap/xconfmap"
16
- "k8s.io/apimachinery/pkg/runtime/schema"
17
15
apiWatch "k8s.io/apimachinery/pkg/watch"
18
16
19
17
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
@@ -40,29 +38,17 @@ func TestLoadConfig(t *testing.T) {
40
38
Interval : time .Hour ,
41
39
FieldSelector : "status.phase=Running" ,
42
40
LabelSelector : "environment in (production),tier in (frontend)" ,
43
- gvr : & schema.GroupVersionResource {
44
- Group : "" ,
45
- Version : "v1" ,
46
- Resource : "pods" ,
47
- },
48
41
},
49
42
{
50
- Name : "events" ,
51
- Mode : WatchMode ,
52
- Namespaces : []string {"default" },
53
- Group : "events.k8s.io" ,
54
- ResourceVersion : "" ,
43
+ Name : "events" ,
44
+ Mode : WatchMode ,
45
+ Namespaces : []string {"default" },
46
+ Group : "events.k8s.io" ,
55
47
ExcludeWatchType : []apiWatch.EventType {
56
48
apiWatch .Deleted ,
57
49
},
58
- gvr : & schema.GroupVersionResource {
59
- Group : "events.k8s.io" ,
60
- Version : "v1" ,
61
- Resource : "events" ,
62
- },
63
50
},
64
51
},
65
- makeDiscoveryClient : getMockDiscoveryClient ,
66
52
},
67
53
},
68
54
{
@@ -77,24 +63,13 @@ func TestLoadConfig(t *testing.T) {
77
63
Mode : PullMode ,
78
64
ResourceVersion : "1" ,
79
65
Interval : time .Hour ,
80
- gvr : & schema.GroupVersionResource {
81
- Group : "" ,
82
- Version : "v1" ,
83
- Resource : "pods" ,
84
- },
85
66
},
86
67
{
87
68
Name : "events" ,
88
69
Mode : PullMode ,
89
70
Interval : time .Hour ,
90
- gvr : & schema.GroupVersionResource {
91
- Group : "" ,
92
- Version : "v1" ,
93
- Resource : "events" ,
94
- },
95
71
},
96
72
},
97
- makeDiscoveryClient : getMockDiscoveryClient ,
98
73
},
99
74
},
100
75
{
@@ -110,34 +85,17 @@ func TestLoadConfig(t *testing.T) {
110
85
Namespaces : []string {"default" },
111
86
Group : "events.k8s.io" ,
112
87
ResourceVersion : "" ,
113
- gvr : & schema.GroupVersionResource {
114
- Group : "events.k8s.io" ,
115
- Version : "v1" ,
116
- Resource : "events" ,
117
- },
118
88
},
119
89
{
120
90
Name : "events" ,
121
91
Mode : WatchMode ,
122
92
Namespaces : []string {"default" },
123
93
Group : "events.k8s.io" ,
124
94
ResourceVersion : "2" ,
125
- gvr : & schema.GroupVersionResource {
126
- Group : "events.k8s.io" ,
127
- Version : "v1" ,
128
- Resource : "events" ,
129
- },
130
95
},
131
96
},
132
- makeDiscoveryClient : getMockDiscoveryClient ,
133
97
},
134
98
},
135
- {
136
- id : component .NewIDWithName (metadata .Type , "invalid_resource" ),
137
- },
138
- {
139
- id : component .NewIDWithName (metadata .Type , "exclude_deleted_with_pull" ),
140
- },
141
99
}
142
100
143
101
for _ , tt := range tests {
@@ -147,58 +105,81 @@ func TestLoadConfig(t *testing.T) {
147
105
148
106
factory := NewFactory ()
149
107
cfg := factory .CreateDefaultConfig ().(* Config )
150
- cfg .makeDiscoveryClient = getMockDiscoveryClient
151
108
152
109
sub , err := cm .Sub (tt .id .String ())
153
110
require .NoError (t , err )
154
111
require .NoError (t , sub .Unmarshal (cfg ))
155
112
156
- if tt .expected == nil {
157
- err = xconfmap .Validate (cfg )
158
- assert .Error (t , err )
159
- return
160
- }
161
- assert .NoError (t , xconfmap .Validate (cfg ))
162
113
assert .Equal (t , tt .expected .AuthType , cfg .AuthType )
163
114
assert .Equal (t , tt .expected .Objects , cfg .Objects )
164
115
})
165
116
}
166
117
}
167
118
168
- func TestValidateResourceConflict (t * testing.T ) {
169
- mockClient := newMockDynamicClient ()
170
- rCfg := createDefaultConfig ().(* Config )
171
- rCfg .makeDynamicClient = mockClient .getMockDynamicClient
172
- rCfg .makeDiscoveryClient = getMockDiscoveryClient
173
-
174
- // Validate it should choose first gvr if group is not specified
175
- rCfg .Objects = []* K8sObjectsConfig {
119
+ func TestValidate (t * testing.T ) {
120
+ tests := []struct {
121
+ desc string
122
+ cfg * Config
123
+ expectedErr string
124
+ }{
176
125
{
177
- Name : "myresources" ,
178
- Mode : PullMode ,
126
+ desc : "invalid mode" ,
127
+ cfg : & Config {
128
+ Objects : []* K8sObjectsConfig {
129
+ {
130
+ Name : "pods" ,
131
+ Mode : "invalid_mode" ,
132
+ },
133
+ },
134
+ },
135
+ expectedErr : "invalid mode: invalid_mode" ,
179
136
},
180
- }
181
-
182
- err := rCfg .Validate ()
183
- require .NoError (t , err )
184
- assert .Equal (t , "group1" , rCfg .Objects [0 ].gvr .Group )
185
-
186
- // Validate it should choose gvr for specified group
187
- rCfg .Objects = []* K8sObjectsConfig {
188
137
{
189
- Name : "myresources" ,
190
- Mode : PullMode ,
191
- Group : "group2" ,
138
+ desc : "exclude watch type with pull mode" ,
139
+ cfg : & Config {
140
+ Objects : []* K8sObjectsConfig {
141
+ {
142
+ Name : "pods" ,
143
+ Mode : PullMode ,
144
+ ExcludeWatchType : []apiWatch.EventType {
145
+ apiWatch .Deleted ,
146
+ },
147
+ },
148
+ },
149
+ },
150
+ expectedErr : "the Exclude config can only be used with watch mode" ,
151
+ },
152
+ {
153
+ desc : "default mode is set" ,
154
+ cfg : & Config {
155
+ Objects : []* K8sObjectsConfig {
156
+ {
157
+ Name : "pods" ,
158
+ },
159
+ },
160
+ },
161
+ },
162
+ {
163
+ desc : "default interval for pull mode" ,
164
+ cfg : & Config {
165
+ Objects : []* K8sObjectsConfig {
166
+ {
167
+ Name : "pods" ,
168
+ Mode : PullMode ,
169
+ },
170
+ },
171
+ },
192
172
},
193
173
}
194
174
195
- err = rCfg .Validate ()
196
- require .NoError (t , err )
197
- assert .Equal (t , "group2" , rCfg .Objects [0 ].gvr .Group )
198
- }
199
-
200
- func TestClientRequired (t * testing.T ) {
201
- rCfg := createDefaultConfig ().(* Config )
202
- err := rCfg .Validate ()
203
- require .Error (t , err )
175
+ for _ , tt := range tests {
176
+ t .Run (tt .desc , func (t * testing.T ) {
177
+ err := tt .cfg .Validate ()
178
+ if tt .expectedErr != "" {
179
+ assert .EqualError (t , err , tt .expectedErr )
180
+ return
181
+ }
182
+ assert .NoError (t , err )
183
+ })
184
+ }
204
185
}
0 commit comments