github.com/influxdata/influxdb/v2@v2.7.6/telegraf/plugins/outputs/outputs_test.go (about) 1 package outputs 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 8 "github.com/influxdata/influxdb/v2/telegraf/plugins" 9 ) 10 11 // local plugin 12 type telegrafPluginConfig interface { 13 TOML() string 14 Type() plugins.Type 15 PluginName() string 16 UnmarshalTOML(data interface{}) error 17 } 18 19 func TestType(t *testing.T) { 20 b := baseOutput(0) 21 if b.Type() != plugins.Output { 22 t.Fatalf("output plugins type should be output, got %s", b.Type()) 23 } 24 } 25 26 func TestTOML(t *testing.T) { 27 cases := []struct { 28 name string 29 plugins map[telegrafPluginConfig]string 30 }{ 31 { 32 name: "test empty plugins", 33 plugins: map[telegrafPluginConfig]string{ 34 &File{}: `[[outputs.file]] 35 ## Files to write to, "stdout" is a specially handled file. 36 files = [] 37 38 ## Use batch serialization format instead of line based delimiting. The 39 ## batch format allows for the production of non line based output formats and 40 ## may more efficiently encode metric groups. 41 # use_batch_format = false 42 43 ## The file will be rotated after the time interval specified. When set 44 ## to 0 no time based rotation is performed. 45 # rotation_interval = "0d" 46 47 ## The logfile will be rotated when it becomes larger than the specified 48 ## size. When set to 0 no size based rotation is performed. 49 # rotation_max_size = "0MB" 50 51 ## Maximum number of rotated archives to keep, any older logs are deleted. 52 ## If set to -1, no archives are removed. 53 # rotation_max_archives = 5 54 55 ## Data format to output. 56 ## Each data format has its own unique set of configuration options, read 57 ## more about them here: 58 ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md 59 data_format = "influx" 60 `, 61 &InfluxDBV2{}: `[[outputs.influxdb_v2]] 62 ## The URLs of the InfluxDB cluster nodes. 63 ## 64 ## Multiple URLs can be specified for a single cluster, only ONE of the 65 ## urls will be written to each interval. 66 ## ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"] 67 urls = [] 68 69 ## Token for authentication. 70 token = "" 71 72 ## Organization is the name of the organization you wish to write to; must exist. 73 organization = "" 74 75 ## Destination bucket to write into. 76 bucket = "" 77 78 ## The value of this tag will be used to determine the bucket. If this 79 ## tag is not set the 'bucket' option is used as the default. 80 # bucket_tag = "" 81 82 ## If true, the bucket tag will not be added to the metric. 83 # exclude_bucket_tag = false 84 85 ## Timeout for HTTP messages. 86 # timeout = "5s" 87 88 ## Additional HTTP headers 89 # http_headers = {"X-Special-Header" = "Special-Value"} 90 91 ## HTTP Proxy override, if unset values the standard proxy environment 92 ## variables are consulted to determine which proxy, if any, should be used. 93 # http_proxy = "http://corporate.proxy:3128" 94 95 ## HTTP User-Agent 96 # user_agent = "telegraf" 97 98 ## Content-Encoding for write request body, can be set to "gzip" to 99 ## compress body or "identity" to apply no encoding. 100 # content_encoding = "gzip" 101 102 ## Enable or disable uint support for writing uints influxdb 2.0. 103 # influx_uint_support = false 104 105 ## Optional TLS Config for use on HTTP connections. 106 # tls_ca = "/etc/telegraf/ca.pem" 107 # tls_cert = "/etc/telegraf/cert.pem" 108 # tls_key = "/etc/telegraf/key.pem" 109 ## Use TLS but skip chain & host verification 110 # insecure_skip_verify = false 111 `, 112 }, 113 }, 114 { 115 name: "standard testing", 116 plugins: map[telegrafPluginConfig]string{ 117 &File{ 118 Files: []FileConfig{ 119 {Typ: "stdout"}, 120 {Path: "/tmp/out.txt"}, 121 }, 122 }: `[[outputs.file]] 123 ## Files to write to, "stdout" is a specially handled file. 124 files = ["stdout", "/tmp/out.txt"] 125 126 ## Use batch serialization format instead of line based delimiting. The 127 ## batch format allows for the production of non line based output formats and 128 ## may more efficiently encode metric groups. 129 # use_batch_format = false 130 131 ## The file will be rotated after the time interval specified. When set 132 ## to 0 no time based rotation is performed. 133 # rotation_interval = "0d" 134 135 ## The logfile will be rotated when it becomes larger than the specified 136 ## size. When set to 0 no size based rotation is performed. 137 # rotation_max_size = "0MB" 138 139 ## Maximum number of rotated archives to keep, any older logs are deleted. 140 ## If set to -1, no archives are removed. 141 # rotation_max_archives = 5 142 143 ## Data format to output. 144 ## Each data format has its own unique set of configuration options, read 145 ## more about them here: 146 ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md 147 data_format = "influx" 148 `, 149 &InfluxDBV2{ 150 URLs: []string{ 151 "http://192.168.1.10:9999", 152 "http://192.168.1.11:9999", 153 }, 154 Token: "tok1", 155 Organization: "org1", 156 Bucket: "bucket1", 157 }: `[[outputs.influxdb_v2]] 158 ## The URLs of the InfluxDB cluster nodes. 159 ## 160 ## Multiple URLs can be specified for a single cluster, only ONE of the 161 ## urls will be written to each interval. 162 ## ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"] 163 urls = ["http://192.168.1.10:9999", "http://192.168.1.11:9999"] 164 165 ## Token for authentication. 166 token = "tok1" 167 168 ## Organization is the name of the organization you wish to write to; must exist. 169 organization = "org1" 170 171 ## Destination bucket to write into. 172 bucket = "bucket1" 173 174 ## The value of this tag will be used to determine the bucket. If this 175 ## tag is not set the 'bucket' option is used as the default. 176 # bucket_tag = "" 177 178 ## If true, the bucket tag will not be added to the metric. 179 # exclude_bucket_tag = false 180 181 ## Timeout for HTTP messages. 182 # timeout = "5s" 183 184 ## Additional HTTP headers 185 # http_headers = {"X-Special-Header" = "Special-Value"} 186 187 ## HTTP Proxy override, if unset values the standard proxy environment 188 ## variables are consulted to determine which proxy, if any, should be used. 189 # http_proxy = "http://corporate.proxy:3128" 190 191 ## HTTP User-Agent 192 # user_agent = "telegraf" 193 194 ## Content-Encoding for write request body, can be set to "gzip" to 195 ## compress body or "identity" to apply no encoding. 196 # content_encoding = "gzip" 197 198 ## Enable or disable uint support for writing uints influxdb 2.0. 199 # influx_uint_support = false 200 201 ## Optional TLS Config for use on HTTP connections. 202 # tls_ca = "/etc/telegraf/ca.pem" 203 # tls_cert = "/etc/telegraf/cert.pem" 204 # tls_key = "/etc/telegraf/key.pem" 205 ## Use TLS but skip chain & host verification 206 # insecure_skip_verify = false 207 `, 208 }, 209 }, 210 } 211 for _, c := range cases { 212 for output, toml := range c.plugins { 213 if toml != output.TOML() { 214 t.Fatalf("%s failed want %s, got %v", c.name, toml, output.TOML()) 215 } 216 } 217 } 218 } 219 220 func TestDecodeTOML(t *testing.T) { 221 cases := []struct { 222 name string 223 want telegrafPluginConfig 224 wantErr error 225 output telegrafPluginConfig 226 data interface{} 227 }{ 228 { 229 name: "file empty", 230 want: &File{}, 231 wantErr: errors.New("bad files for file output plugin"), 232 output: &File{}, 233 }, 234 { 235 name: "file bad data not array", 236 want: &File{}, 237 wantErr: errors.New("not an array for file output plugin"), 238 output: &File{}, 239 data: map[string]interface{}{ 240 "files": "", 241 }, 242 }, 243 { 244 name: "file", 245 want: &File{ 246 Files: []FileConfig{ 247 {Path: "/tmp/out.txt"}, 248 {Typ: "stdout"}, 249 }, 250 }, 251 output: &File{}, 252 data: map[string]interface{}{ 253 "files": []interface{}{ 254 "/tmp/out.txt", 255 "stdout", 256 }, 257 }, 258 }, 259 { 260 name: "influxdb_v2 empty", 261 want: &InfluxDBV2{}, 262 wantErr: errors.New("bad urls for influxdb_v2 output plugin"), 263 output: &InfluxDBV2{}, 264 }, 265 { 266 name: "influxdb_v2 bad urls", 267 want: &InfluxDBV2{}, 268 wantErr: errors.New("urls is not an array for influxdb_v2 output plugin"), 269 output: &InfluxDBV2{}, 270 data: map[string]interface{}{ 271 "urls": "", 272 }, 273 }, 274 { 275 name: "influxdb_v2 missing token", 276 want: &InfluxDBV2{ 277 URLs: []string{ 278 "http://localhost:9999", 279 "http://192.168.0.1:9999", 280 }, 281 }, 282 wantErr: errors.New("token is missing for influxdb_v2 output plugin"), 283 output: &InfluxDBV2{}, 284 data: map[string]interface{}{ 285 "urls": []interface{}{ 286 "http://localhost:9999", 287 "http://192.168.0.1:9999", 288 }, 289 }, 290 }, 291 { 292 name: "influxdb_v2 missing org", 293 want: &InfluxDBV2{ 294 URLs: []string{ 295 "http://localhost:9999", 296 "http://192.168.0.1:9999", 297 }, 298 Token: "token1", 299 }, 300 wantErr: errors.New("organization is missing for influxdb_v2 output plugin"), 301 output: &InfluxDBV2{}, 302 data: map[string]interface{}{ 303 "urls": []interface{}{ 304 "http://localhost:9999", 305 "http://192.168.0.1:9999", 306 }, 307 "token": "token1", 308 }, 309 }, 310 { 311 name: "influxdb_v2 missing bucket", 312 want: &InfluxDBV2{ 313 URLs: []string{ 314 "http://localhost:9999", 315 "http://192.168.0.1:9999", 316 }, 317 Token: "token1", 318 Organization: "org1", 319 }, 320 wantErr: errors.New("bucket is missing for influxdb_v2 output plugin"), 321 output: &InfluxDBV2{}, 322 data: map[string]interface{}{ 323 "urls": []interface{}{ 324 "http://localhost:9999", 325 "http://192.168.0.1:9999", 326 }, 327 "token": "token1", 328 "organization": "org1", 329 }, 330 }, 331 { 332 name: "influxdb_v2", 333 want: &InfluxDBV2{ 334 URLs: []string{ 335 "http://localhost:9999", 336 "http://192.168.0.1:9999", 337 }, 338 Token: "token1", 339 Organization: "org1", 340 Bucket: "bucket1", 341 }, 342 output: &InfluxDBV2{}, 343 data: map[string]interface{}{ 344 "urls": []interface{}{ 345 "http://localhost:9999", 346 "http://192.168.0.1:9999", 347 }, 348 "token": "token1", 349 "organization": "org1", 350 "bucket": "bucket1", 351 }, 352 }, 353 } 354 for _, c := range cases { 355 err := c.output.UnmarshalTOML(c.data) 356 if c.wantErr != nil && (err == nil || err.Error() != c.wantErr.Error()) { 357 t.Fatalf("%s failed want err %s, got %v", c.name, c.wantErr.Error(), err) 358 } 359 if c.wantErr == nil && err != nil { 360 t.Fatalf("%s failed want err nil, got %v", c.name, err) 361 } 362 if !reflect.DeepEqual(c.output, c.want) { 363 t.Fatalf("%s failed want %v, got %v", c.name, c.want, c.output) 364 } 365 } 366 }