feat: Add constant 'algorithm' to the mock plugin (#11188)

This commit is contained in:
Sven Rebhan 2022-05-25 21:10:08 +02:00 committed by GitHub
parent ae98e7f1f6
commit 2a87947244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 0 deletions

View File

@ -28,6 +28,9 @@ Below is a sample config to generate one of each of the four types:
## One or more mock data fields *must* be defined.
##
## [[inputs.mock.constant]]
## name = "constant"
## value = value_of_any_type
## [[inputs.mock.random]]
## name = "rand"
## min = 1.0
@ -50,6 +53,7 @@ Below is a sample config to generate one of each of the four types:
The available algorithms for generating mock data include:
* Constant - generate a field with the given value of type string, float, int or bool
* Random Float - generate a random float, inclusive of min and max
* Sine Wave - produce a sine wave with a certain amplitude and period
* Step - always add the step value, negative values accepted

View File

@ -21,12 +21,18 @@ type Mock struct {
MetricName string `toml:"metric_name"`
Tags map[string]string `toml:"tags"`
Constant []*constant `toml:"constant"`
Random []*random `toml:"random"`
Step []*step `toml:"step"`
Stock []*stock `toml:"stock"`
SineWave []*sineWave `toml:"sine_wave"`
}
type constant struct {
Name string `toml:"name"`
Value interface{} `toml:"value"`
}
type random struct {
Name string `toml:"name"`
Min float64 `toml:"min"`
@ -71,6 +77,10 @@ func (m *Mock) Gather(acc telegraf.Accumulator) error {
m.generateSineWave(fields)
m.generateStep(fields)
for _, c := range m.Constant {
fields[c.Name] = c.Value
}
tags := make(map[string]string)
for key, value := range m.Tags {
tags[key] = value

View File

@ -8,6 +8,22 @@ import (
)
func TestGather(t *testing.T) {
testConstantString := &constant{
Name: "constant_string",
Value: "a string",
}
testConstantFloat := &constant{
Name: "constant_float",
Value: 3.1415,
}
testConstantInt := &constant{
Name: "constant_int",
Value: 42,
}
testConstantBool := &constant{
Name: "constant_bool",
Value: true,
}
testRandom := &random{
Name: "random",
Min: 1.0,
@ -38,6 +54,7 @@ func TestGather(t *testing.T) {
MetricName: "test",
Tags: tags,
Constant: []*constant{testConstantString, testConstantFloat, testConstantInt, testConstantBool},
Random: []*random{testRandom},
SineWave: []*sineWave{testSineWave},
Step: []*step{testStep},
@ -56,6 +73,14 @@ func TestGather(t *testing.T) {
switch k {
case "abc":
require.Equal(t, 50.0, v)
case "constant_string":
require.Equal(t, testConstantString.Value, v)
case "constant_float":
require.Equal(t, testConstantFloat.Value, v)
case "constant_int":
require.Equal(t, testConstantInt.Value, v)
case "constant_bool":
require.Equal(t, testConstantBool.Value, v)
case "random":
require.GreaterOrEqual(t, 6.0, v)
require.LessOrEqual(t, 1.0, v)

View File

@ -9,6 +9,9 @@
## One or more mock data fields *must* be defined.
##
## [[inputs.mock.constant]]
## name = "constant"
## value = value_of_any_type
## [[inputs.mock.random]]
## name = "rand"
## min = 1.0