Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ian28223 committed Mar 28, 2022
1 parent 4d500b2 commit 92f6598
Showing 1 changed file with 161 additions and 3 deletions.
164 changes: 161 additions & 3 deletions pkg/logs/internal/tailers/journald/tailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,57 @@ func TestIdentifier(t *testing.T) {
}

func TestShouldDropEntry(t *testing.T) {
source := config.NewLogSource("", &config.LogsConfig{ExcludeUnits: []string{"foo", "bar"}})
tailer := NewTailer(source, nil)
err := tailer.setup()
// System-level service units do not have SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT
// User-level service units may have a common value for SD_JOURNAL_FIELD_SYSTEMD_UNIT
var tailer *Tailer
var source *config.LogSource

// expect all but the specified System-level service units to be dropped
source = config.NewLogSource("", &config.LogsConfig{IncludeSystemUnits: []string{"foo", "bar"}})
tailer = NewTailer(source, nil)
err = tailer.setup()
assert.Nil(t, err)

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo",
},
}))

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "bar",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

// expect all but the specified User-level service units to be dropped
source = config.NewLogSource("", &config.LogsConfig{IncludeUserUnits: []string{"foo", "bar"}})
tailer = NewTailer(source, nil)
err = tailer.setup()
assert.Nil(t, err)

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo",
},
}))

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "bar",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

// expect only the specified service units to be dropped
source = config.NewLogSource("", &config.LogsConfig{ExcludeSystemUnits: []string{"foo", "bar"}, ExcludeUserUnits: []string{"baz", "qux"}})
tailer = NewTailer(source, nil)
err = tailer.setup()
assert.Nil(t, err)

assert.True(t, tailer.shouldDrop(
Expand All @@ -61,6 +109,102 @@ func TestShouldDropEntry(t *testing.T) {
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "boo",
},
}))

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "bar",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "baz",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "qux",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

// expect all System-level service units to be dropped
source = config.NewLogSource("", &config.LogsConfig{ExcludeSystemUnits: []string{"*"}})
tailer = NewTailer(source, nil)
err = tailer.setup()
assert.Nil(t, err)

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo",
},
}))

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "bar",
},
}))

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "bar",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "baz",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

// expect all User-level service units to be dropped
source = config.NewLogSource("", &config.LogsConfig{ExcludeUserUnits: []string{"*"}})
tailer = NewTailer(source, nil)
err = tailer.setup()
assert.Nil(t, err)

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo",
},
}))

assert.False(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "bar",
},
}))

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "bar",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))

assert.True(t, tailer.shouldDrop(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "baz",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "[email protected]",
},
}))
}

func TestApplicationName(t *testing.T) {
Expand All @@ -71,6 +215,16 @@ func TestApplicationName(t *testing.T) {
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: "foo",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "foo-user.service",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo.service",
sdjournal.SD_JOURNAL_FIELD_COMM: "foo.sh",
},
}, []string{}))

assert.Equal(t, "foo-user.service", tailer.getApplicationName(
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "foo-user.service",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo.service",
sdjournal.SD_JOURNAL_FIELD_COMM: "foo.sh",
},
Expand Down Expand Up @@ -147,6 +301,7 @@ func TestApplicationNameShouldBeDockerForContainerEntries(t *testing.T) {
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: "foo",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "foo-user.service",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo.service",
sdjournal.SD_JOURNAL_FIELD_COMM: "foo.sh",
containerIDKey: "bar",
Expand All @@ -164,6 +319,7 @@ func TestApplicationNameShouldBeShortImageForContainerEntries(t *testing.T) {
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: "foo",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "foo-user.service",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo.service",
sdjournal.SD_JOURNAL_FIELD_COMM: "foo.sh",
containerIDKey: containerID,
Expand All @@ -185,6 +341,7 @@ func TestApplicationNameShouldBeDockerWhenTagNotFound(t *testing.T) {
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: "foo",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "foo-user.service",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo.service",
sdjournal.SD_JOURNAL_FIELD_COMM: "foo.sh",
containerIDKey: containerID,
Expand All @@ -209,6 +366,7 @@ func TestWrongTypeFromCache(t *testing.T) {
&sdjournal.JournalEntry{
Fields: map[string]string{
sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: "foo",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: "foo-user.service",
sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: "foo.service",
sdjournal.SD_JOURNAL_FIELD_COMM: "foo.sh",
containerIDKey: containerID,
Expand Down

0 comments on commit 92f6598

Please sign in to comment.