|
| 1 | +package inputs_tail_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/influxdata/telegraf/config" |
| 13 | + _ "github.com/influxdata/telegraf/migrations/inputs_tail" // register migration |
| 14 | + "github.com/influxdata/telegraf/plugins/inputs/tail" // register plugin |
| 15 | + _ "github.com/influxdata/telegraf/plugins/parsers/influx" // register parser |
| 16 | +) |
| 17 | + |
| 18 | +func TestNoMigration(t *testing.T) { |
| 19 | + plugin := &tail.Tail{} |
| 20 | + defaultCfg := plugin.SampleConfig() |
| 21 | + |
| 22 | + // Migrate and check that nothing changed |
| 23 | + output, n, err := config.ApplyMigrations([]byte(defaultCfg)) |
| 24 | + require.NoError(t, err) |
| 25 | + require.NotEmpty(t, output) |
| 26 | + require.Zero(t, n) |
| 27 | + require.Equal(t, defaultCfg, string(output)) |
| 28 | +} |
| 29 | + |
| 30 | +func TestConflictWarning(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + conf string |
| 34 | + warn bool |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "conflicting values warn", |
| 38 | + conf: ` |
| 39 | +[[inputs.tail]] |
| 40 | + files = ["/var/mymetrics.out"] |
| 41 | + from_beginning = true |
| 42 | + initial_read_offset = "end" |
| 43 | + data_format = "influx" |
| 44 | +`, |
| 45 | + warn: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "agreeing values do not warn", |
| 49 | + conf: ` |
| 50 | +[[inputs.tail]] |
| 51 | + files = ["/var/mymetrics.out"] |
| 52 | + from_beginning = true |
| 53 | + initial_read_offset = "beginning" |
| 54 | + data_format = "influx" |
| 55 | +`, |
| 56 | + warn: false, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + var buf bytes.Buffer |
| 63 | + log.SetOutput(&buf) |
| 64 | + t.Cleanup(func() { log.SetOutput(os.Stderr) }) |
| 65 | + |
| 66 | + output, n, err := config.ApplyMigrations([]byte(tt.conf)) |
| 67 | + require.NoError(t, err) |
| 68 | + require.NotEmpty(t, output) |
| 69 | + require.Equal(t, uint64(1), n) |
| 70 | + |
| 71 | + if tt.warn { |
| 72 | + require.Contains(t, buf.String(), `conflicts with 'initial_read_offset = "end"'`) |
| 73 | + } else { |
| 74 | + require.NotContains(t, buf.String(), "conflicts with") |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestCases(t *testing.T) { |
| 81 | + // Get all directories in testcases |
| 82 | + folders, err := os.ReadDir("testcases") |
| 83 | + require.NoError(t, err) |
| 84 | + |
| 85 | + for _, f := range folders { |
| 86 | + // Only handle folders |
| 87 | + if !f.IsDir() { |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + t.Run(f.Name(), func(t *testing.T) { |
| 92 | + testcasePath := filepath.Join("testcases", f.Name()) |
| 93 | + inputFile := filepath.Join(testcasePath, "telegraf.conf") |
| 94 | + expectedFile := filepath.Join(testcasePath, "expected.conf") |
| 95 | + |
| 96 | + // Read the expected output |
| 97 | + expected := config.NewConfig() |
| 98 | + require.NoError(t, expected.LoadConfig(expectedFile)) |
| 99 | + require.NotEmpty(t, expected.Inputs) |
| 100 | + |
| 101 | + // Read the input data |
| 102 | + input, remote, err := config.LoadConfigFile(inputFile) |
| 103 | + require.NoError(t, err) |
| 104 | + require.False(t, remote) |
| 105 | + require.NotEmpty(t, input) |
| 106 | + |
| 107 | + // Migrate |
| 108 | + output, n, err := config.ApplyMigrations(input) |
| 109 | + require.NoError(t, err) |
| 110 | + require.NotEmpty(t, output) |
| 111 | + require.GreaterOrEqual(t, n, uint64(1)) |
| 112 | + actual := config.NewConfig() |
| 113 | + require.NoError(t, actual.LoadConfigData(output, config.EmptySourcePath)) |
| 114 | + |
| 115 | + // Test the output |
| 116 | + require.Len(t, actual.Inputs, len(expected.Inputs)) |
| 117 | + actualIDs := make([]string, 0, len(expected.Inputs)) |
| 118 | + expectedIDs := make([]string, 0, len(expected.Inputs)) |
| 119 | + for i := range actual.Inputs { |
| 120 | + actualIDs = append(actualIDs, actual.Inputs[i].ID()) |
| 121 | + expectedIDs = append(expectedIDs, expected.Inputs[i].ID()) |
| 122 | + } |
| 123 | + require.ElementsMatch(t, expectedIDs, actualIDs, string(output)) |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
0 commit comments