diff --git a/api/runner/runner.go b/api/runner/runner.go index 4775a9ae0..0b8b73873 100644 --- a/api/runner/runner.go +++ b/api/runner/runner.go @@ -10,18 +10,17 @@ 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" ) type Config struct { - Ctx context.Context - Route *models.Route - Endpoint string - Payload string - Timeout time.Duration + Ctx context.Context + Route *models.Route + Payload string + Timeout time.Duration } type Runner struct { @@ -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) } diff --git a/api/runner/runner_test.go b/api/runner/runner_test.go new file mode 100644 index 000000000..273f96766 --- /dev/null +++ b/api/runner/runner_test.go @@ -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()) + } + } +}