...

Source file src/github.com/coreos/go-systemd/v22/journal/journal_test.go

Documentation: github.com/coreos/go-systemd/v22/journal

     1  // Copyright 2018 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package journal
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"runtime"
    21  	"strconv"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestJournalEnabled(t *testing.T) {
    27  	enabled := Enabled()
    28  
    29  	if !enabled {
    30  		t.Fatalf("journald socket not detected")
    31  	}
    32  }
    33  
    34  func TestValidVarName(t *testing.T) {
    35  	validTestCases := []string{
    36  		"TEST",
    37  		"TE_ST",
    38  		"TEST_",
    39  		"0TEST0",
    40  	}
    41  	invalidTestCases := []string{
    42  		"test",
    43  		"_TEST",
    44  		"",
    45  	}
    46  
    47  	for _, tt := range validTestCases {
    48  		if err := validVarName(tt); err != nil {
    49  			t.Fatalf("\"%s\" should be a valid variable", tt)
    50  		}
    51  	}
    52  	for _, tt := range invalidTestCases {
    53  		if err := validVarName(tt); err == nil {
    54  			t.Fatalf("\"%s\" should be an invalid variable", tt)
    55  		}
    56  	}
    57  
    58  }
    59  
    60  func TestJournalSend(t *testing.T) {
    61  	if !Enabled() {
    62  		t.Skip("systemd journal not available locally")
    63  	}
    64  
    65  	// an always-too-big value (hopefully)
    66  	hugeValue := 1234567890
    67  
    68  	// a value slightly larger than default limit,
    69  	// see `SO_SNDBUF` in socket(7)
    70  	largeValue := hugeValue
    71  	if wmem, err := ioutil.ReadFile("/proc/sys/net/core/wmem_default"); err == nil {
    72  		wmemStr := strings.TrimSpace(string(wmem))
    73  		if v, err := strconv.Atoi(wmemStr); err == nil {
    74  			largeValue = v + 1
    75  		}
    76  	}
    77  
    78  	// small messages should go over normal data,
    79  	// larger ones over temporary file with fd in ancillary data
    80  	testValues := []struct {
    81  		label string
    82  		len   int
    83  	}{
    84  		{
    85  			"empty message",
    86  			0,
    87  		},
    88  		{
    89  			"small message",
    90  			5,
    91  		},
    92  		{
    93  			"large message",
    94  			largeValue,
    95  		},
    96  		{
    97  			"huge message",
    98  			hugeValue,
    99  		},
   100  	}
   101  
   102  	// This is memory intensive, so we manually trigger GC before and after each test.
   103  	for i, tt := range testValues {
   104  		t.Logf("journal send test #%v - %s (len=%d)", i, tt.label, tt.len)
   105  		runtime.GC()
   106  		err := SendAlloc(i, tt.label, tt.len)
   107  		if err != nil {
   108  			t.Fatalf("#%v: failed sending %s: %s", i, tt.label, err)
   109  		}
   110  		runtime.GC()
   111  	}
   112  }
   113  
   114  func SendAlloc(run int, label string, len int) error {
   115  	largeVars := map[string]string{
   116  		"KEY": string(make([]byte, len)),
   117  	}
   118  
   119  	msg := fmt.Sprintf("go-systemd test #%v - %s", run, label)
   120  	return Send(msg, PriCrit, largeVars)
   121  }
   122  

View as plain text