Skip to content

Commit

Permalink
Improve winlogbeat checkpoint test (#4371)
Browse files Browse the repository at this point in the history
Check for errors and exit to stop panics from occurring when the test fails.
Increase the sleep to 750ms to give the checkpoint more time to flush states to disk.
  • Loading branch information
andrewkroh authored and ruflin committed May 22, 2017
1 parent 2c96a9c commit 5b7ce03
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions winlogbeat/checkpoint/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func TestWriteMaxUpdates(t *testing.T) {
}()

file := filepath.Join(dir, "some", "new", "dir", ".winlogbeat.yml")
assert.False(t, fileExists(file), "%s should not exist", file)
if !assert.False(t, fileExists(file), "%s should not exist", file) {
return
}

cp, err := NewCheckpoint(file, 2, time.Hour)
if err != nil {
t.Fatal(err)
Expand All @@ -39,18 +42,24 @@ func TestWriteMaxUpdates(t *testing.T) {
time.Sleep(500 * time.Millisecond)
_, found := cp.States()["App"]
assert.True(t, found)

ps, err := cp.read()
assert.NoError(t, err)
if err != nil {
t.Fatal("read failed", err)
}
assert.Len(t, ps.States, 0)

// Send update - it is written to disk.
cp.Persist("App", 2, time.Now())
time.Sleep(500 * time.Millisecond)
time.Sleep(750 * time.Millisecond)
ps, err = cp.read()
assert.NoError(t, err)
assert.Len(t, ps.States, 1)
assert.Equal(t, "App", ps.States[0].Name)
assert.Equal(t, uint64(2), ps.States[0].RecordNumber)
if err != nil {
t.Fatal("read failed", err)
}
if assert.Len(t, ps.States, 1, "state not written, could be a flush timing issue, retry") {
assert.Equal(t, "App", ps.States[0].Name)
assert.Equal(t, uint64(2), ps.States[0].RecordNumber)
}
}

// Test that a write is triggered when the maximum time period since the last
Expand All @@ -68,7 +77,10 @@ func TestWriteTimedFlush(t *testing.T) {
}()

file := filepath.Join(dir, ".winlogbeat.yml")
assert.False(t, fileExists(file), "%s should not exist", file)
if !assert.False(t, fileExists(file), "%s should not exist", file) {
return
}

cp, err := NewCheckpoint(file, 100, time.Second)
if err != nil {
t.Fatal(err)
Expand All @@ -80,10 +92,13 @@ func TestWriteTimedFlush(t *testing.T) {
cp.Persist("App", 1, time.Now())
time.Sleep(1500 * time.Millisecond)
ps, err := cp.read()
assert.NoError(t, err)
assert.Len(t, ps.States, 1)
assert.Equal(t, "App", ps.States[0].Name)
assert.Equal(t, uint64(1), ps.States[0].RecordNumber)
if err != nil {
t.Fatal("read failed", err)
}
if assert.Len(t, ps.States, 1) {
assert.Equal(t, "App", ps.States[0].Name)
assert.Equal(t, uint64(1), ps.States[0].RecordNumber)
}
}

// Test that createDir creates the directory with 0750 permissions.
Expand All @@ -103,17 +118,24 @@ func TestCreateDir(t *testing.T) {
file := filepath.Join(stateDir, ".winlogbeat.yml")
cp := &Checkpoint{file: file}

assert.False(t, fileExists(stateDir), "%s should not exist", file)
assert.NoError(t, cp.createDir())
assert.True(t, fileExists(stateDir), "%s should exist", file)
if !assert.False(t, fileExists(file), "%s should not exist", file) {
return
}
if err = cp.createDir(); err != nil {
t.Fatal("createDir", err)
}
if !assert.True(t, fileExists(stateDir), "%s should exist", file) {
return
}

// mkdir on Windows does not pass the POSIX mode to the CreateDirectory
// syscall so doesn't test the mode.
if runtime.GOOS != "windows" {
fileInfo, err := os.Stat(stateDir)
assert.NoError(t, err)
assert.Equal(t, true, fileInfo.IsDir())
assert.Equal(t, os.FileMode(0750), fileInfo.Mode().Perm())
if assert.NoError(t, err) {
assert.Equal(t, true, fileInfo.IsDir())
assert.Equal(t, os.FileMode(0750), fileInfo.Mode().Perm())
}
}
}

Expand All @@ -134,7 +156,9 @@ func TestCreateDirAlreadyExists(t *testing.T) {
file := filepath.Join(dir, ".winlogbeat.yml")
cp := &Checkpoint{file: file}

assert.True(t, fileExists(dir), "%s should exist", file)
if !assert.True(t, fileExists(dir), "%s should exist", file) {
return
}
assert.NoError(t, cp.createDir())
}

Expand Down

0 comments on commit 5b7ce03

Please sign in to comment.