@@ -22,19 +22,39 @@ import (
22
22
"github.com/stretchr/testify/assert"
23
23
)
24
24
25
+ func TestEmptyConfig (t * testing.T ) {
26
+ assert := assert .New (t )
27
+
28
+ emptyConfig := `
29
+ {
30
+ "endpoint" : "http://localhost:8000/nginx_status",
31
+ "metrics_config" : [
32
+ ]
33
+ }
34
+ `
35
+
36
+ //Create a temporary config file 'temp.json' with invalid json format
37
+ assert .NoError (ioutil .WriteFile ("temp.json" , []byte (emptyConfig ), 0777 ))
38
+
39
+ _ , err := NewCollector ("tempCollector" , "temp.json" )
40
+ assert .Error (err )
41
+
42
+ assert .NoError (os .Remove ("temp.json" ))
43
+ }
44
+
25
45
func TestConfigWithErrors (t * testing.T ) {
26
46
assert := assert .New (t )
27
47
28
- //Syntax error: Missed '"' after active connections
48
+ //Syntax error: Missed '"' after activeConnections
29
49
invalid := `
30
50
{
31
- "endpoint" : "host:port /nginx_status",
32
- "metricsConfig " : [
51
+ "endpoint" : "http://localhost:8000 /nginx_status",
52
+ "metrics_config " : [
33
53
{
34
54
"name" : "activeConnections,
35
- "metricType " : "gauge",
55
+ "metric_type " : "gauge",
36
56
"units" : "integer",
37
- "pollingFrequency " : "10s" ,
57
+ "polling_frequency " : 10 ,
38
58
"regex" : "Active connections: ([0-9]+)"
39
59
}
40
60
]
@@ -50,6 +70,41 @@ func TestConfigWithErrors(t *testing.T) {
50
70
assert .NoError (os .Remove ("temp.json" ))
51
71
}
52
72
73
+ func TestConfigWithRegexErrors (t * testing.T ) {
74
+ assert := assert .New (t )
75
+
76
+ //Error: Missed operand for '+' in activeConnections regex
77
+ invalid := `
78
+ {
79
+ "endpoint" : "host:port/nginx_status",
80
+ "metrics_config" : [
81
+ {
82
+ "name" : "activeConnections",
83
+ "metric_type" : "gauge",
84
+ "units" : "integer",
85
+ "polling_frequency" : 10,
86
+ "regex" : "Active connections: (+)"
87
+ },
88
+ {
89
+ "name" : "reading",
90
+ "metric_type" : "gauge",
91
+ "units" : "integer",
92
+ "polling_frequency" : 10,
93
+ "regex" : "Reading: ([0-9]+) .*"
94
+ }
95
+ ]
96
+ }
97
+ `
98
+
99
+ //Create a temporary config file 'temp.json'
100
+ assert .NoError (ioutil .WriteFile ("temp.json" , []byte (invalid ), 0777 ))
101
+
102
+ _ , err := NewCollector ("tempCollector" , "temp.json" )
103
+ assert .Error (err )
104
+
105
+ assert .NoError (os .Remove ("temp.json" ))
106
+ }
107
+
53
108
func TestConfig (t * testing.T ) {
54
109
assert := assert .New (t )
55
110
0 commit comments