Skip to content

Commit

Permalink
logmon: Add windows compatibility test
Browse files Browse the repository at this point in the history
  • Loading branch information
endocrimes committed Jun 28, 2019
1 parent 02c529c commit 36bf4df
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions client/logmon/logmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ func TestLogmon_Start_rotate(t *testing.T) {
require.NoError(lm.Stop())
}

// asserts that calling Start twice restarts the log rotator
func TestLogmon_Start_restart(t *testing.T) {
// asserts that calling Start twice restarts the log rotator and that any logs
// published while the listener was unavailable are recieved.
func TestLogmon_Start_restart_flusheslogs(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("windows does not support pushing data to a pipe with no servers")
}

require := require.New(t)

stdoutLog := "stdout"
Expand Down Expand Up @@ -183,3 +188,92 @@ func TestLogmon_Start_restart(t *testing.T) {
require.NoError(err)
})
}

// asserts that calling Start twice restarts the log rotator
func TestLogmon_Start_restart(t *testing.T) {
require := require.New(t)

stdoutLog := "stdout"
stderrLog := "stderr"

var stdoutFifoPath, stderrFifoPath string

dir, err := ioutil.TempDir("", "nomadtest")
require.NoError(err)
defer os.RemoveAll(dir)

if runtime.GOOS == "windows" {
stdoutFifoPath = "//./pipe/test-restart.stdout"
stderrFifoPath = "//./pipe/test-restart.stderr"
} else {
stdoutFifoPath = filepath.Join(dir, "stdout.fifo")
stderrFifoPath = filepath.Join(dir, "stderr.fifo")
}

cfg := &LogConfig{
LogDir: dir,
StdoutLogFile: stdoutLog,
StdoutFifo: stdoutFifoPath,
StderrLogFile: stderrLog,
StderrFifo: stderrFifoPath,
MaxFiles: 2,
MaxFileSizeMB: 1,
}

lm := NewLogMon(testlog.HCLogger(t))
impl, ok := lm.(*logmonImpl)
require.True(ok)
require.NoError(lm.Start(cfg))

stdout, err := fifo.OpenWriter(stdoutFifoPath)
require.NoError(err)
stderr, err := fifo.OpenWriter(stderrFifoPath)
require.NoError(err)

// Write a string and assert it was written to the file
_, err = stdout.Write([]byte("test\n"))
require.NoError(err)

testutil.WaitForResult(func() (bool, error) {
raw, err := ioutil.ReadFile(filepath.Join(dir, "stdout.0"))
if err != nil {
return false, err
}
return "test\n" == string(raw), fmt.Errorf("unexpected stdout %q", string(raw))
}, func(err error) {
require.NoError(err)
})
require.True(impl.tl.IsRunning())

// Close stdout and assert that logmon no longer writes to the file
require.NoError(stdout.Close())
require.NoError(stderr.Close())

testutil.WaitForResult(func() (bool, error) {
return !impl.tl.IsRunning(), fmt.Errorf("logmon is still running")
}, func(err error) {
require.NoError(err)
})

// Start logmon again and assert that it can recieve logs again
require.NoError(lm.Start(cfg))

stdout, err = fifo.OpenWriter(stdoutFifoPath)
require.NoError(err)
stderr, err = fifo.OpenWriter(stderrFifoPath)
require.NoError(err)

_, err = stdout.Write([]byte("test\n"))
require.NoError(err)
testutil.WaitForResult(func() (bool, error) {
raw, err := ioutil.ReadFile(filepath.Join(dir, "stdout.0"))
if err != nil {
return false, err
}

expected := "test\ntest\n" == string(raw)
return expected, fmt.Errorf("unexpected stdout %q", string(raw))
}, func(err error) {
require.NoError(err)
})
}

0 comments on commit 36bf4df

Please sign in to comment.