@@ -21,6 +21,8 @@ import (
21
21
"testing"
22
22
"time"
23
23
24
+ "github.com/stretchr/testify/require"
25
+
24
26
"github.com/elastic/beats/libbeat/logp"
25
27
26
28
"github.com/stretchr/testify/assert"
@@ -38,39 +40,42 @@ func TestAPIKeyConfig_IsEnabled(t *testing.T) {
38
40
39
41
func TestAPIKeyConfig_ESConfig (t * testing.T ) {
40
42
for name , tc := range map [string ]struct {
41
- cfg * APIKeyConfig
43
+ cfg * common. Config
42
44
esCfg * common.Config
43
45
44
46
expectedConfig * APIKeyConfig
45
47
expectedErr error
46
48
}{
47
49
"default" : {
48
- cfg : defaultAPIKeyConfig (),
50
+ cfg : common . NewConfig (),
49
51
expectedConfig : defaultAPIKeyConfig (),
50
52
},
51
53
"ES config missing" : {
52
- cfg : & APIKeyConfig { Enabled : true , LimitPerMin : apiKeyLimit } ,
54
+ cfg : common . MustNewConfigFrom ( `{"enabled" : true}` ) ,
53
55
expectedConfig : & APIKeyConfig {
54
56
Enabled : true ,
55
57
LimitPerMin : apiKeyLimit ,
56
58
ESConfig : elasticsearch .DefaultConfig ()},
57
59
},
58
60
"ES configured" : {
59
- cfg : & APIKeyConfig {
60
- Enabled : true ,
61
- ESConfig : & elasticsearch.Config {Hosts : elasticsearch.Hosts {"192.0.0.1:9200" }}},
61
+ cfg : common .MustNewConfigFrom (`{"enabled": true, "elasticsearch.timeout":"7s"}` ),
62
62
esCfg : common .MustNewConfigFrom (`{"hosts":["186.0.0.168:9200"]}` ),
63
63
expectedConfig : & APIKeyConfig {
64
- Enabled : true ,
65
- ESConfig : & elasticsearch.Config {Hosts : elasticsearch.Hosts {"192.0.0.1:9200" }}},
64
+ Enabled : true ,
65
+ LimitPerMin : apiKeyLimit ,
66
+ ESConfig : & elasticsearch.Config {
67
+ Hosts : elasticsearch.Hosts {"localhost:9200" },
68
+ Protocol : "http" ,
69
+ Timeout : 7 * time .Second },
70
+ esConfigured : true },
66
71
},
67
72
"disabled with ES from output" : {
68
- cfg : defaultAPIKeyConfig (),
73
+ cfg : common . NewConfig (),
69
74
esCfg : common .MustNewConfigFrom (`{"hosts":["192.0.0.168:9200"]}` ),
70
75
expectedConfig : defaultAPIKeyConfig (),
71
76
},
72
77
"ES from output" : {
73
- cfg : & APIKeyConfig { Enabled : true , LimitPerMin : 20 },
78
+ cfg : common . MustNewConfigFrom ( `{"enabled" : true, "limit" : 20}` ) ,
74
79
esCfg : common .MustNewConfigFrom (`{"hosts":["192.0.0.168:9200"],"username":"foo","password":"bar"}` ),
75
80
expectedConfig : & APIKeyConfig {
76
81
Enabled : true ,
@@ -84,13 +89,15 @@ func TestAPIKeyConfig_ESConfig(t *testing.T) {
84
89
},
85
90
} {
86
91
t .Run (name , func (t * testing.T ) {
87
- err := tc .cfg .setup (logp .NewLogger ("api_key" ), tc .esCfg )
92
+ var apiKeyConfig APIKeyConfig
93
+ require .NoError (t , tc .cfg .Unpack (& apiKeyConfig ))
94
+ err := apiKeyConfig .setup (logp .NewLogger ("api_key" ), tc .esCfg )
88
95
if tc .expectedErr == nil {
89
96
assert .NoError (t , err )
90
97
} else {
91
98
assert .Error (t , err )
92
99
}
93
- assert .Equal (t , tc .expectedConfig , tc . cfg )
100
+ assert .Equal (t , tc .expectedConfig , & apiKeyConfig )
94
101
95
102
})
96
103
}
0 commit comments