Skip to content

Commit

Permalink
Improve runtime test reliability. (#185)
Browse files Browse the repository at this point in the history
Instead of waiting a predetermined time and then testing for instance health,
wait up to 30 seconds while constantly checking the health. This allows tests
to potentially run faster, and also not fail if on some runs it takes a bit
longer for the instance to become healthy.
  • Loading branch information
zwass authored and marpaia committed Oct 19, 2017
1 parent d3b635b commit 33674cf
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions osquery/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/kolide/kit/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -106,6 +107,16 @@ func TestBadBinaryPath(t *testing.T) {
assert.Nil(t, runner)
}

// waitHealthy expects the instance to be healthy within 30 seconds, or else
// fatals the test
func waitHealthy(t *testing.T, runner *Runner) {
testutil.FatalAfterFunc(t, 30*time.Second, func() {
for runner.Healthy() != nil {
time.Sleep(500 * time.Millisecond)
}
})
}

func TestSimplePath(t *testing.T) {
t.Parallel()
rootDirectory, rmRootDirectory, err := osqueryTempDir()
Expand All @@ -116,8 +127,7 @@ func TestSimplePath(t *testing.T) {
runner, err := LaunchInstance(WithRootDirectory(rootDirectory))
require.NoError(t, err)

err = runner.Healthy()
require.NoError(t, err)
waitHealthy(t, runner)

require.NoError(t, runner.Shutdown())
}
Expand All @@ -132,15 +142,13 @@ func TestRestart(t *testing.T) {
runner, err := LaunchInstance(WithRootDirectory(rootDirectory))
require.NoError(t, err)

require.NoError(t, runner.Healthy())
waitHealthy(t, runner)

require.NoError(t, runner.Restart())
time.Sleep(2 * time.Second)
require.NoError(t, runner.Healthy())
waitHealthy(t, runner)

require.NoError(t, runner.Restart())
time.Sleep(2 * time.Second)
require.NoError(t, runner.Healthy())
waitHealthy(t, runner)

require.NoError(t, runner.Shutdown())
}
Expand All @@ -155,15 +163,14 @@ func TestOsqueryDies(t *testing.T) {
runner, err := LaunchInstance(WithRootDirectory(rootDirectory))
require.NoError(t, err)

require.NoError(t, runner.Healthy())
waitHealthy(t, runner)

// Simulate the osquery process unexpectedly dying
runner.instanceLock.Lock()
require.NoError(t, runner.instance.cmd.Process.Kill())
runner.instanceLock.Unlock()

time.Sleep(2 * time.Second)
require.NoError(t, runner.Healthy())
waitHealthy(t, runner)

require.NoError(t, runner.Shutdown())
}
Expand Down

0 comments on commit 33674cf

Please sign in to comment.