|
| 1 | +package inputs_zookeeper_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_zookeeper" // register migration |
| 12 | + "github.com/influxdata/telegraf/plugins/inputs/zookeeper" // register plugin |
| 13 | +) |
| 14 | + |
| 15 | +func TestNoMigration(t *testing.T) { |
| 16 | + plugin := &zookeeper.Zookeeper{} |
| 17 | + defaultCfg := plugin.SampleConfig() |
| 18 | + |
| 19 | + // Migrate and check that nothing changed |
| 20 | + output, n, err := config.ApplyMigrations([]byte(defaultCfg)) |
| 21 | + require.NoError(t, err) |
| 22 | + require.NotEmpty(t, output) |
| 23 | + require.Zero(t, n) |
| 24 | + require.Equal(t, defaultCfg, string(output)) |
| 25 | +} |
| 26 | + |
| 27 | +func TestEnableTLSConflict(t *testing.T) { |
| 28 | + cfg := []byte(` |
| 29 | +[[inputs.zookeeper]] |
| 30 | + servers = ["localhost:2181"] |
| 31 | + enable_tls = true |
| 32 | + tls_enable = false |
| 33 | + `) |
| 34 | + // Migrate and check that the contradicting settings are caught |
| 35 | + output, n, err := config.ApplyMigrations(cfg) |
| 36 | + require.ErrorContains(t, err, "contradicting setting for 'tls_enable' and 'enable_tls'") |
| 37 | + require.Empty(t, output) |
| 38 | + require.Zero(t, n) |
| 39 | +} |
| 40 | + |
| 41 | +func TestCases(t *testing.T) { |
| 42 | + // Get all directories in testcases |
| 43 | + folders, err := os.ReadDir("testcases") |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + for _, f := range folders { |
| 47 | + // Only handle folders |
| 48 | + if !f.IsDir() { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + t.Run(f.Name(), func(t *testing.T) { |
| 53 | + testcasePath := filepath.Join("testcases", f.Name()) |
| 54 | + inputFile := filepath.Join(testcasePath, "telegraf.conf") |
| 55 | + expectedFile := filepath.Join(testcasePath, "expected.conf") |
| 56 | + |
| 57 | + // Read the expected output |
| 58 | + expected := config.NewConfig() |
| 59 | + require.NoError(t, expected.LoadConfig(expectedFile)) |
| 60 | + require.NotEmpty(t, expected.Inputs) |
| 61 | + |
| 62 | + // Read the input data |
| 63 | + input, remote, err := config.LoadConfigFile(inputFile) |
| 64 | + require.NoError(t, err) |
| 65 | + require.False(t, remote) |
| 66 | + require.NotEmpty(t, input) |
| 67 | + |
| 68 | + // Migrate |
| 69 | + output, n, err := config.ApplyMigrations(input) |
| 70 | + require.NoError(t, err) |
| 71 | + require.NotEmpty(t, output) |
| 72 | + require.GreaterOrEqual(t, n, uint64(1)) |
| 73 | + actual := config.NewConfig() |
| 74 | + require.NoError(t, actual.LoadConfigData(output, config.EmptySourcePath)) |
| 75 | + |
| 76 | + // Test the output |
| 77 | + require.Len(t, actual.Inputs, len(expected.Inputs)) |
| 78 | + actualIDs := make([]string, 0, len(expected.Inputs)) |
| 79 | + expectedIDs := make([]string, 0, len(expected.Inputs)) |
| 80 | + for i := range actual.Inputs { |
| 81 | + actualIDs = append(actualIDs, actual.Inputs[i].ID()) |
| 82 | + expectedIDs = append(expectedIDs, expected.Inputs[i].ID()) |
| 83 | + } |
| 84 | + require.ElementsMatch(t, expectedIDs, actualIDs, string(output)) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments