Skip to content

Commit

Permalink
worker: add file/base64 task reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Buchanan authored and adamstruck committed Jul 10, 2018
1 parent 662c792 commit aa85fe8
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,46 @@ func TestPersistentPreRun(t *testing.T) {
}
}
}

func TestTaskFileOption(t *testing.T) {
c, h := newCommandHooks()
h.Run = func(ctx context.Context, conf config.Config, log *logger.Logger, opts *Options) error {
if opts.TaskFile != "test.task.json" {
t.Fatal("unexpected task file option", opts.TaskFile)
}
return nil
}

c.SetArgs([]string{"run", "--taskFile", "test.task.json"})
c.Execute()

h.Run = func(ctx context.Context, conf config.Config, log *logger.Logger, opts *Options) error {
if opts.TaskFile != "test.task.json" {
t.Fatal("unexpected task file option", opts.TaskFile)
}
return nil
}

c.SetArgs([]string{"run", "-f", "test.task.json"})
c.Execute()

h.Run = func(ctx context.Context, conf config.Config, log *logger.Logger, opts *Options) error {
if opts.TaskBase64 != "abcd" {
t.Fatal("unexpected task base64 option", opts.TaskBase64)
}
return nil
}

c.SetArgs([]string{"run", "--taskBase64", "abcd"})
c.Execute()

h.Run = func(ctx context.Context, conf config.Config, log *logger.Logger, opts *Options) error {
if opts.TaskBase64 != "abcd" {
t.Fatal("unexpected task base64 option", opts.TaskBase64)
}
return nil
}

c.SetArgs([]string{"run", "-b", "abcd"})
c.Execute()
}
58 changes: 58 additions & 0 deletions tests/core/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,61 @@ func TestDockerContainerMetadata(t *testing.T) {
t.Error("didn't find container image hash metadata")
}
}

func TestWorkerRunFileTaskReader(t *testing.T) {
tests.SetLogOutput(log, t)
c := tests.DefaultConfig()
ctx := context.Background()

// Task builder collects events into a task view.
task := &tes.Task{}
b := events.TaskBuilder{Task: task}

opts := &workerCmd.Options{
TaskFile: "../../examples/hello-world.json",
}

worker, err := workerCmd.NewWorker(ctx, c, log, opts)
if err != nil {
t.Fatal("unexpected error", err)
}
worker.EventWriter = &events.MultiWriter{b, worker.EventWriter}

err = worker.Run(ctx)
if err != nil {
t.Fatal("unexpected error", err)
}

if task.State != tes.Complete {
t.Error("unexpected task state")
}
}

func TestWorkerRunBase64TaskReader(t *testing.T) {
tests.SetLogOutput(log, t)
c := tests.DefaultConfig()
ctx := context.Background()

// Task builder collects events into a task view.
task := &tes.Task{}
b := events.TaskBuilder{Task: task}

opts := &workerCmd.Options{
TaskBase64: "ewogICJuYW1lIjogIkhlbGxvIHdvcmxkIiwKICAiZGVzY3JpcHRpb24iOiAiRGVtb25zdHJhdGVzIHRoZSBtb3N0IGJhc2ljIGVjaG8gdGFzay4iLAogICJleGVjdXRvcnMiOiBbCiAgICB7CiAgICAgICJpbWFnZSI6ICJhbHBpbmUiLAogICAgICAiY29tbWFuZCI6IFsiZWNobyIsICJoZWxsbyB3b3JsZCJdCiAgICB9CiAgXQp9Cg==",
}

worker, err := workerCmd.NewWorker(ctx, c, log, opts)
if err != nil {
t.Fatal("unexpected error", err)
}
worker.EventWriter = &events.MultiWriter{b, worker.EventWriter}

err = worker.Run(ctx)
if err != nil {
t.Fatal("unexpected error", err)
}

if task.State != tes.Complete {
t.Error("unexpected task state")
}
}
2 changes: 2 additions & 0 deletions worker/file_taskreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type FileTaskReader struct {

// NewFileTaskReader creates a new FileTaskReader.
func NewFileTaskReader(path string) (*FileTaskReader, error) {
// TODO not sure if it's better to return an error immediately,
// or return an error from Task()
fh, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("opening task file: %v", err)
Expand Down
40 changes: 40 additions & 0 deletions worker/taskreader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package worker

import (
"context"
"testing"
)

func TestFileTaskReader(t *testing.T) {
r, err := NewFileTaskReader("../examples/hello-world.json")
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
task, err := r.Task(ctx)
if task.Name != "Hello world" {
t.Error("unexpected task content")
}

if task.Id == "" {
t.Error("unexpected empty task ID")
}
}

func TestBase64TaskReader(t *testing.T) {
r, err := NewBase64TaskReader("ewogICJuYW1lIjogIkhlbGxvIHdvcmxkIiwKICAiZGVzY3JpcHRpb24iOiAiRGVtb25zdHJhdGVzIHRoZSBtb3N0IGJhc2ljIGVjaG8gdGFzay4iLAogICJleGVjdXRvcnMiOiBbCiAgICB7CiAgICAgICJpbWFnZSI6ICJhbHBpbmUiLAogICAgICAiY29tbWFuZCI6IFsiZWNobyIsICJoZWxsbyB3b3JsZCJdCiAgICB9CiAgXQp9Cg==")
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
task, err := r.Task(ctx)
if task.Name != "Hello world" {
t.Error("unexpected task content")
}

if task.Id == "" {
t.Error("unexpected empty task ID")
}
}

0 comments on commit aa85fe8

Please sign in to comment.