|
| 1 | +package inputs_kafka_consumer_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/influxdata/telegraf/config" |
| 11 | + _ "github.com/influxdata/telegraf/migrations/inputs_kafka_consumer" // register migration |
| 12 | + "github.com/influxdata/telegraf/plugins/inputs/kafka_consumer" // register plugin |
| 13 | + _ "github.com/influxdata/telegraf/plugins/parsers/influx" // register parser |
| 14 | +) |
| 15 | + |
| 16 | +func TestNoMigration(t *testing.T) { |
| 17 | + plugin := &kafka_consumer.KafkaConsumer{} |
| 18 | + defaultCfg := plugin.SampleConfig() |
| 19 | + |
| 20 | + // Migrate and check that nothing changed |
| 21 | + output, n, err := config.ApplyMigrations([]byte(defaultCfg)) |
| 22 | + require.NoError(t, err) |
| 23 | + require.NotEmpty(t, output) |
| 24 | + require.Zero(t, n) |
| 25 | + require.Equal(t, defaultCfg, string(output)) |
| 26 | +} |
| 27 | + |
| 28 | +func TestStartupErrorBehaviorConflict(t *testing.T) { |
| 29 | + cfg := []byte(` |
| 30 | +[[inputs.kafka_consumer]] |
| 31 | + brokers = ["localhost:9092"] |
| 32 | + topics = ["telegraf"] |
| 33 | + connection_strategy = "defer" |
| 34 | + startup_error_behavior = "ignore" |
| 35 | + `) |
| 36 | + // Migrate and check that the contradicting settings are caught |
| 37 | + output, n, err := config.ApplyMigrations(cfg) |
| 38 | + require.ErrorContains(t, err, "contradicting setting for 'startup_error_behavior' and 'connection_strategy'") |
| 39 | + require.Empty(t, output) |
| 40 | + require.Zero(t, n) |
| 41 | +} |
| 42 | + |
| 43 | +func TestInvalidConnectionStrategy(t *testing.T) { |
| 44 | + cfg := []byte(` |
| 45 | +[[inputs.kafka_consumer]] |
| 46 | + brokers = ["localhost:9092"] |
| 47 | + topics = ["telegraf"] |
| 48 | + connection_strategy = "invalid" |
| 49 | + `) |
| 50 | + // Migrate and check that the invalid value is caught |
| 51 | + output, n, err := config.ApplyMigrations(cfg) |
| 52 | + require.ErrorContains(t, err, `invalid connection strategy "invalid"`) |
| 53 | + require.Empty(t, output) |
| 54 | + require.Zero(t, n) |
| 55 | +} |
| 56 | + |
| 57 | +func TestCases(t *testing.T) { |
| 58 | + // Get all directories in testcases |
| 59 | + folders, err := os.ReadDir("testcases") |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + for _, f := range folders { |
| 63 | + // Only handle folders |
| 64 | + if !f.IsDir() { |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + t.Run(f.Name(), func(t *testing.T) { |
| 69 | + testcasePath := filepath.Join("testcases", f.Name()) |
| 70 | + inputFile := filepath.Join(testcasePath, "telegraf.conf") |
| 71 | + expectedFile := filepath.Join(testcasePath, "expected.conf") |
| 72 | + |
| 73 | + // Read the expected output |
| 74 | + expected := config.NewConfig() |
| 75 | + require.NoError(t, expected.LoadConfig(expectedFile)) |
| 76 | + require.NotEmpty(t, expected.Inputs) |
| 77 | + |
| 78 | + // Read the input data |
| 79 | + input, remote, err := config.LoadConfigFile(inputFile) |
| 80 | + require.NoError(t, err) |
| 81 | + require.False(t, remote) |
| 82 | + require.NotEmpty(t, input) |
| 83 | + |
| 84 | + // Migrate |
| 85 | + output, n, err := config.ApplyMigrations(input) |
| 86 | + require.NoError(t, err) |
| 87 | + require.NotEmpty(t, output) |
| 88 | + require.GreaterOrEqual(t, n, uint64(1)) |
| 89 | + actual := config.NewConfig() |
| 90 | + require.NoError(t, actual.LoadConfigData(output, config.EmptySourcePath)) |
| 91 | + |
| 92 | + // Test the output |
| 93 | + require.Len(t, actual.Inputs, len(expected.Inputs)) |
| 94 | + actualIDs := make([]string, 0, len(expected.Inputs)) |
| 95 | + expectedIDs := make([]string, 0, len(expected.Inputs)) |
| 96 | + for i := range actual.Inputs { |
| 97 | + actualIDs = append(actualIDs, actual.Inputs[i].ID()) |
| 98 | + expectedIDs = append(expectedIDs, expected.Inputs[i].ID()) |
| 99 | + } |
| 100 | + require.ElementsMatch(t, expectedIDs, actualIDs, string(output)) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments