Merge pull request #32 from pedronasser/runner-tests

Runner tests
This commit is contained in:
Travis Reeder
2016-08-01 14:08:29 -04:00
committed by GitHub
2 changed files with 93 additions and 16 deletions

View File

@@ -10,8 +10,8 @@ import (
"github.com/iron-io/functions/api/models"
"github.com/iron-io/titan/common"
"github.com/iron-io/titan/runner/agent"
"github.com/iron-io/titan/runner/configloader"
"github.com/iron-io/titan/runner/drivers"
driverscommon "github.com/iron-io/titan/runner/drivers/common"
"github.com/iron-io/titan/runner/drivers/docker"
"github.com/iron-io/titan/runner/drivers/mock"
)
@@ -19,7 +19,6 @@ import (
type Config struct {
Ctx context.Context
Route *models.Route
Endpoint string
Payload string
Timeout time.Duration
}
@@ -40,23 +39,19 @@ func New(cfg *Config) *Runner {
func (r *Runner) Run() error {
var err error
runnerConfig := configloader.RunnerConfiguration()
au := agent.ConfigAuth{runnerConfig.Registries}
// TODO: Is this really required for Titan's driver?
// Can we remove it?
env := common.NewEnvironment(func(e *common.Environment) {})
// TODO: Create a drivers.New(runnerConfig) in Titan
driver, err := selectDriver(env, runnerConfig)
driver, err := selectDriver("docker", env, &driverscommon.Config{})
if err != nil {
return err
}
ctask := &containerTask{
cfg: r.cfg,
auth: &au,
auth: &agent.ConfigAuth{},
stdout: &r.out,
stderr: &r.err,
}
@@ -83,13 +78,13 @@ func (r Runner) Status() string {
return r.status
}
func selectDriver(env *common.Environment, conf *agent.Config) (drivers.Driver, error) {
switch conf.Driver {
func selectDriver(driver string, env *common.Environment, conf *driverscommon.Config) (drivers.Driver, error) {
switch driver {
case "docker":
docker := docker.NewDocker(env, conf.DriverConfig)
docker := docker.NewDocker(env, conf)
return docker, nil
case "mock":
return mock.New(), nil
}
return nil, fmt.Errorf("driver %v not found", conf.Driver)
return nil, fmt.Errorf("driver %v not found", driver)
}

82
api/runner/runner_test.go Normal file
View File

@@ -0,0 +1,82 @@
package runner
import (
"bytes"
"testing"
"time"
"github.com/iron-io/functions/api/models"
"golang.org/x/net/context"
)
func TestRunnerHello(t *testing.T) {
for i, test := range []struct {
route *models.Route
payload string
expectedStatus string
expectedOut string
expectedErr string
}{
{&models.Route{Image: "iron/hello"}, ``, "success", "Hello World!", ""},
{&models.Route{Image: "iron/hello"}, `{"name": "test"}`, "success", "Hello test!", ""},
} {
runner := New(&Config{
Ctx: context.Background(),
Route: test.route,
Timeout: 5 * time.Second,
Payload: test.payload,
})
if err := runner.Run(); err != nil {
t.Fatalf("Test %d: error during Run() - %s", i, err)
}
if test.expectedStatus != runner.Status() {
t.Fatalf("Test %d: expected result status to be `%s` but it was `%s`", i, test.expectedStatus, runner.Status())
}
if !bytes.Contains(runner.ReadOut(), []byte(test.expectedOut)) {
t.Fatalf("Test %d: expected output log to contain `%s` in `%s`", i, test.expectedOut, runner.ReadOut())
}
if !bytes.Contains(runner.ReadErr(), []byte(test.expectedErr)) {
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, runner.ReadErr())
}
}
}
func TestRunnerError(t *testing.T) {
for i, test := range []struct {
route *models.Route
payload string
expectedStatus string
expectedOut string
expectedErr string
}{
{&models.Route{Image: "iron/error"}, ``, "error", "", "RuntimeError"},
{&models.Route{Image: "iron/error"}, `{"name": "test"}`, "error", "", "RuntimeError"},
} {
runner := New(&Config{
Ctx: context.Background(),
Route: test.route,
Timeout: 5 * time.Second,
Payload: test.payload,
})
if err := runner.Run(); err != nil {
t.Fatalf("Test %d: error during Run() - %s", i, err)
}
if test.expectedStatus != runner.Status() {
t.Fatalf("Test %d: expected result status to be `%s` but it was `%s`", i, test.expectedStatus, runner.Status())
}
if !bytes.Contains(runner.ReadOut(), []byte(test.expectedOut)) {
t.Fatalf("Test %d: expected output log to contain `%s` in `%s`", i, test.expectedOut, runner.ReadOut())
}
if !bytes.Contains(runner.ReadErr(), []byte(test.expectedErr)) {
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, runner.ReadErr())
}
}
}