| 69 | } |
| 70 | |
| 71 | func TestOneShot(t *testing.T) { |
| 72 | cstest.SkipOnWindows(t) |
| 73 | |
| 74 | ctx := t.Context() |
| 75 | |
| 76 | tests := []struct { |
| 77 | config string |
| 78 | wantErr string |
| 79 | wantLines int |
| 80 | wantLog []string |
| 81 | }{ |
| 82 | { |
| 83 | config: ` |
| 84 | source: journalctl |
| 85 | mode: cat |
| 86 | journalctl_filter: |
| 87 | - "-_UID=42"`, |
| 88 | wantErr: "exit status 1", |
| 89 | wantLog: []string{ |
| 90 | "Got stderr: journalctl: invalid option -- '_'", |
| 91 | }, |
| 92 | wantLines: 0, |
| 93 | }, |
| 94 | { |
| 95 | config: ` |
| 96 | source: journalctl |
| 97 | mode: cat |
| 98 | journalctl_filter: |
| 99 | - _SYSTEMD_UNIT=ssh.service`, |
| 100 | wantLines: 14, |
| 101 | }, |
| 102 | } |
| 103 | for _, ts := range tests { |
| 104 | t.Run(ts.config, func(t *testing.T) { |
| 105 | out := make(chan pipeline.Event, 100) |
| 106 | j := Source{} |
| 107 | |
| 108 | logger, hook := logtest.NewNullLogger() |
| 109 | |
| 110 | err := j.Configure(ctx, []byte(ts.config), logrus.NewEntry(logger), metrics.AcquisitionMetricsLevelNone) |
| 111 | require.NoError(t, err) |
| 112 | |
| 113 | err = j.OneShot(ctx, out) |
| 114 | cstest.RequireErrorContains(t, err, ts.wantErr) |
| 115 | |
| 116 | for _, wantMessage := range ts.wantLog { |
| 117 | cstest.RequireLogContains(t, hook, wantMessage) |
| 118 | } |
| 119 | |
| 120 | if ts.wantErr != "" { |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | assert.Len(t, out, ts.wantLines) |
| 125 | }) |
| 126 | } |
| 127 | } |
| 128 | |