From 2847fc8a8011be00a502f7e6ba9f875f858e2d6a Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Wed, 1 Feb 2017 17:31:42 +0000 Subject: [PATCH] journal: add some send tests This commit introduces tests exercising sending logic for empty/small/large messages. --- journal/journal_test.go | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/journal/journal_test.go b/journal/journal_test.go index 49e914f2..5574fadf 100644 --- a/journal/journal_test.go +++ b/journal/journal_test.go @@ -15,6 +15,10 @@ package journal import ( + "fmt" + "io/ioutil" + "strconv" + "strings" "testing" ) @@ -40,3 +44,58 @@ func TestValidaVarName(t *testing.T) { } } } + +func TestJournalSend(t *testing.T) { + // an always-too-big value (hopefully) + hugeValue := 1234567890 + + // a value slightly larger than default limit, + // see `SO_SNDBUF` in socket(7) + largeValue := hugeValue + if wmem, err := ioutil.ReadFile("/proc/sys/net/core/wmem_default"); err == nil { + wmemStr := strings.TrimSpace(string(wmem)) + if v, err := strconv.Atoi(wmemStr); err == nil { + largeValue = v + 1 + } + } + // See https://github.com/coreos/go-systemd/pull/221#issuecomment-276727718 + _ = largeValue + + // small messages should go over normal data, + // larger ones over temporary file with fd in ancillary data + testValues := []struct { + label string + len int + }{ + { + "empty message", + 0, + }, + { + "small message", + 5, + }, + /* See https://github.com/coreos/go-systemd/pull/221#issuecomment-276727718 + { + "large message", + largeValue, + }, + { + "huge message", + hugeValue, + }, + */ + } + + for i, tt := range testValues { + t.Logf("journal send test #%v - %s (len=%d)", i, tt.label, tt.len) + largeVars := map[string]string{ + "KEY": string(make([]byte, tt.len)), + } + + err := Send(fmt.Sprintf("go-systemd test #%v - %s", i, tt.label), PriCrit, largeVars) + if err != nil { + t.Fatalf("#%v: failed sending %s: %s", i, tt.label, err) + } + } +}