github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/systemd/journal_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package systemd_test
    21  
    22  import (
    23  	"log/syslog"
    24  	"net"
    25  	"path"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	. "github.com/snapcore/snapd/systemd"
    30  )
    31  
    32  type journalTestSuite struct{}
    33  
    34  var _ = Suite(&journalTestSuite{})
    35  
    36  func (j *journalTestSuite) TestStreamFileErrorNoPath(c *C) {
    37  	restore := MockJournalStdoutPath(path.Join(c.MkDir(), "fake-journal"))
    38  	defer restore()
    39  
    40  	jout, err := NewJournalStreamFile("foobar", syslog.LOG_INFO, false)
    41  	c.Assert(err, ErrorMatches, ".*no such file or directory")
    42  	c.Assert(jout, IsNil)
    43  }
    44  
    45  func (j *journalTestSuite) TestStreamFileHeader(c *C) {
    46  	fakePath := path.Join(c.MkDir(), "fake-journal")
    47  	restore := MockJournalStdoutPath(fakePath)
    48  	defer restore()
    49  
    50  	listener, err := net.ListenUnix("unix", &net.UnixAddr{Name: fakePath})
    51  	c.Assert(err, IsNil)
    52  	defer listener.Close()
    53  
    54  	doneCh := make(chan struct{}, 1)
    55  
    56  	go func() {
    57  		defer func() { close(doneCh) }()
    58  
    59  		// see https://github.com/systemd/systemd/blob/97a33b126c845327a3a19d6e66f05684823868fb/src/journal/journal-send.c#L424
    60  		conn, err := listener.AcceptUnix()
    61  		c.Assert(err, IsNil)
    62  		defer conn.Close()
    63  
    64  		expectedHdrLen := len("foobar") + 1 + 1 + 2 + 2 + 2 + 2 + 2
    65  		hdrBuf := make([]byte, expectedHdrLen)
    66  		hdrLen, err := conn.Read(hdrBuf)
    67  		c.Assert(err, IsNil)
    68  		c.Assert(hdrLen, Equals, expectedHdrLen)
    69  		c.Check(hdrBuf, DeepEquals, []byte("foobar\n\n6\n0\n0\n0\n0\n"))
    70  
    71  		data := make([]byte, 4096)
    72  		sz, err := conn.Read(data)
    73  		c.Assert(err, IsNil)
    74  		c.Assert(sz > 0, Equals, true)
    75  		c.Check(data[0:sz], DeepEquals, []byte("hello from unit tests"))
    76  
    77  		doneCh <- struct{}{}
    78  	}()
    79  
    80  	jout, err := NewJournalStreamFile("foobar", syslog.LOG_INFO, false)
    81  	c.Assert(err, IsNil)
    82  	c.Assert(jout, NotNil)
    83  
    84  	_, err = jout.WriteString("hello from unit tests")
    85  	c.Assert(err, IsNil)
    86  	defer jout.Close()
    87  
    88  	<-doneCh
    89  }