Skip to content

Commit

Permalink
Travis-CI ands test updates (#423)
Browse files Browse the repository at this point in the history
Some initial work to get travis-ci running. This includes updates to the make the tests more reliable, and have fewer dependancies on on the initial container setup. Travis-ci is desirable for it's multiple-platforms.

* Added travis-ci
* Updated runtime_test to download osqueryd as part of a test setup step.
* moved downloadOsqueryInBinDir to test setup and removed race conditions

This does mean that we have duplicate CI systems
  • Loading branch information
directionless authored Feb 20, 2019
1 parent 7f395a2 commit 0c18bb8
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 24 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Use containers, not VMs
sudo: false

notifications:
email: false

language: go

go:
- 1.11.x

env:
global:
- SKIP_TEST_MDM=true
matrix:
- TRAVISTARGET=build
- TRAVISTARGET=test
- TRAVISTARGET=package-builder

os:
- linux
- osx

install:
- make deps

script:
- make $TRAVISTARGET

106 changes: 86 additions & 20 deletions pkg/osquery/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,75 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"syscall"
"testing"
"time"

"github.com/kolide/kit/fs"
"github.com/kolide/kit/testutil"
"github.com/kolide/launcher/pkg/packaging"
osquery "github.com/kolide/osquery-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testOsqueryBinaryDirectory string

// TestMain overrides the default test main function. This allows us to share setup/teardown.
func TestMain(m *testing.M) {
binDirectory, rmBinDirectory, err := osqueryTempDir()
if err != nil {
fmt.Println("Failed to make temp dir for test binaries")
os.Exit(1)
}
defer rmBinDirectory()
testOsqueryBinaryDirectory = filepath.Join(binDirectory, "osqueryd")

if err := downloadOsqueryInBinDir(binDirectory); err != nil {
fmt.Printf("Failed to download osquery: %v\n", err)
os.Exit(1)
}

// Build the osquerty extension once
binDir, err := getBinDir()
if err != nil {
fmt.Printf("Failed to get binDir: %v\n", err)
os.Exit(1)
}

if err := buildOsqueryExtensionInBinDir(binDir); err != nil {
fmt.Printf("Failed to build osquery extension: %v\n", err)
os.Exit(1)
}

// Run the tests!
retCode := m.Run()
os.Exit(retCode)
}

// getBinDir finds the directory of the currently running binary (where we will
// look for the osquery extension)
func getBinDir(t *testing.T) string {
func getBinDir() (string, error) {
binPath, err := os.Executable()
require.NoError(t, err)
if err != nil {
return "", err
}
binDir := filepath.Dir(binPath)
return binDir
return binDir, nil
}

func TestCalculateOsqueryPaths(t *testing.T) {
t.Parallel()
binDir := getBinDir(t)
fakeExtensionPath := filepath.Join(binDir, "osquery-extension.ext")
require.NoError(t, ioutil.WriteFile(fakeExtensionPath, []byte("#!/bin/bash\nsleep infinity"), 0755))
binDir, err := getBinDir()
require.NoError(t, err)

paths, err := calculateOsqueryPaths(binDir, "")
require.NoError(t, err)
Expand All @@ -58,15 +95,37 @@ func TestCreateOsqueryCommand(t *testing.T) {
extensionAutoloadPath: "/foo/bar/osquery.autoload",
}

osquerydPath, err := exec.LookPath("osqueryd")
require.NoError(t, err)
osquerydPath := testOsqueryBinaryDirectory

cmd, err := createOsquerydCommand(osquerydPath, paths, "config_plugin", "logger_plugin", "distributed_plugin", os.Stdout, os.Stderr)
require.NoError(t, err)
require.Equal(t, os.Stderr, cmd.Stderr)
require.Equal(t, os.Stdout, cmd.Stdout)
}

// downloadOsqueryInBinDir downloads osqueryd. This allows the test
// suite to run on hosts lacking osqueryd. We could consider moving this into a deps step.
func downloadOsqueryInBinDir(binDirectory string) error {
target := packaging.Target{}
if err := target.PlatformFromString(runtime.GOOS); err != nil {
return errors.Wrapf(err, "Error parsing platform: %s", runtime.GOOS)
}

outputFile := filepath.Join(binDirectory, "osqueryd") //, target.PlatformBinaryName("osqueryd"))
cacheDir := "/tmp"

path, err := packaging.FetchBinary(context.TODO(), cacheDir, "osqueryd", target.PlatformBinaryName("osqueryd"), "stable", target)
if err != nil {
return errors.Wrap(err, "An error occurred fetching the osqueryd binary")
}

if err := fs.CopyFile(path, outputFile); err != nil {
return errors.Wrapf(err, "Couldn't copy file to %s", outputFile)
}

return nil
}

// buildOsqueryExtensionInBinDir compiles the osquery extension and places it
// on disk in the same directory as the currently running executable (as
// expected when running an osquery process)
Expand Down Expand Up @@ -105,7 +164,6 @@ func TestBadBinaryPath(t *testing.T) {
require.NoError(t, err)
defer rmRootDirectory()

require.NoError(t, buildOsqueryExtensionInBinDir(getBinDir(t)))
runner, err := LaunchInstance(
WithRootDirectory(rootDirectory),
WithOsquerydBinary("/foobar"),
Expand All @@ -130,8 +188,10 @@ func TestSimplePath(t *testing.T) {
require.NoError(t, err)
defer rmRootDirectory()

require.NoError(t, buildOsqueryExtensionInBinDir(getBinDir(t)))
runner, err := LaunchInstance(WithRootDirectory(rootDirectory))
runner, err := LaunchInstance(
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
require.NoError(t, err)

waitHealthy(t, runner)
Expand All @@ -157,8 +217,10 @@ func TestOsqueryDies(t *testing.T) {
require.NoError(t, err)
defer rmRootDirectory()

require.NoError(t, buildOsqueryExtensionInBinDir(getBinDir(t)))
runner, err := LaunchInstance(WithRootDirectory(rootDirectory))
runner, err := LaunchInstance(
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
require.NoError(t, err)

waitHealthy(t, runner)
Expand All @@ -180,7 +242,6 @@ func TestNotStarted(t *testing.T) {
require.NoError(t, err)
defer rmRootDirectory()

require.NoError(t, buildOsqueryExtensionInBinDir(getBinDir(t)))
runner := newRunner(WithRootDirectory(rootDirectory))
require.NoError(t, err)

Expand Down Expand Up @@ -219,9 +280,12 @@ func TestExtensionSocketPath(t *testing.T) {
require.NoError(t, err)
defer rmRootDirectory()

require.NoError(t, buildOsqueryExtensionInBinDir(getBinDir(t)))
extensionSocketPath := filepath.Join(rootDirectory, "sock")
runner, err := LaunchInstance(WithRootDirectory(rootDirectory), WithExtensionSocketPath(extensionSocketPath))
runner, err := LaunchInstance(
WithRootDirectory(rootDirectory),
WithExtensionSocketPath(extensionSocketPath),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
require.NoError(t, err)

waitHealthy(t, runner)
Expand All @@ -244,8 +308,10 @@ func setupOsqueryInstanceForTests(t *testing.T) (runner *Runner, extensionPid in
rootDirectory, rmRootDirectory, err := osqueryTempDir()
require.NoError(t, err)

require.NoError(t, buildOsqueryExtensionInBinDir(getBinDir(t)))
runner, err = LaunchInstance(WithRootDirectory(rootDirectory))
runner, err = LaunchInstance(
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
require.NoError(t, err)
waitHealthy(t, runner)

Expand Down
5 changes: 5 additions & 0 deletions pkg/osquery/table/mdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ package table
import (
"testing"

"github.com/kolide/kit/env"
"github.com/stretchr/testify/require"
)

func TestMDMProfileStatus(t *testing.T) {
if env.Bool("SKIP_TEST_MDM", true) {
t.Skip("No docker")
}

_, err := getMDMProfileStatus()
require.Nil(t, err)
}
14 changes: 10 additions & 4 deletions pkg/service/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,20 @@ func TestSwappingCert(t *testing.T) {
client := New(conn, log.NewNopLogger())

_, _, err = client.RequestEnrollment(context.Background(), "", "", EnrollmentDetails{})
require.NotNil(t, err)
require.Error(t, err)

stop()

cert, err = tls.LoadX509KeyPair(goodCert, goodKey)
require.Nil(t, err)
stop = startServer(t, &tls.Config{Certificates: []tls.Certificate{cert}})
time.Sleep(1 * time.Second)

// Wait for amount of time
timer := time.NewTimer(time.Second * 2)
<-timer.C

_, _, err = client.RequestEnrollment(context.Background(), "", "", EnrollmentDetails{})
require.Nil(t, err)
require.NoError(t, err)

stop()
}
Expand All @@ -107,7 +110,10 @@ func TestCertRemainsBad(t *testing.T) {
cert, err := tls.LoadX509KeyPair(badCert, badKey)
require.Nil(t, err)
stop := startServer(t, &tls.Config{Certificates: []tls.Certificate{cert}})
time.Sleep(1 * time.Second)

// Wait for amount of time
timer := time.NewTimer(time.Second * 2)
<-timer.C

pem1, err := ioutil.ReadFile(badCert)
require.Nil(t, err)
Expand Down

0 comments on commit 0c18bb8

Please sign in to comment.