Skip to content

Commit

Permalink
fix for #169
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstruck authored and buchanae committed Jul 18, 2017
1 parent 72258b2 commit c4d9134
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
2 changes: 1 addition & 1 deletion server/scheduler_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func updateAvailableResources(tx *bolt.Tx, worker *pbf.Worker) {
DiskGb: worker.GetResources().GetDiskGb(),
}
for _, taskID := range worker.TaskIds {
t := getTask(tx, taskID)
t, _ := getTaskView(tx, taskID, tes.TaskView_FULL)
res := t.GetResources()

// Cpus are represented by an unsigned int, and if we blindly
Expand Down
49 changes: 25 additions & 24 deletions server/task_boltdb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"errors"
"fmt"
"github.com/boltdb/bolt"
proto "github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (taskBolt *TaskBolt) ReadQueue(n int) []*tes.Task {
c := tx.Bucket(TasksQueued).Cursor()
for k, _ := c.First(); k != nil && len(tasks) < n; k, _ = c.Next() {
id := string(k)
task := getTask(tx, id)
task, _ := getTaskView(tx, id, tes.TaskView_FULL)
tasks = append(tasks, task)
}
return nil
Expand Down Expand Up @@ -194,18 +195,13 @@ func getTaskState(tx *bolt.Tx, id string) tes.State {
return tes.State(v)
}

type taskNotFoundError struct {
id string
}

func (e *taskNotFoundError) Error() string {
return fmt.Sprintf("no task found for id: %s", e.id)
}
// ErrTaskNotFound ...
var ErrTaskNotFound = errors.New("no task found for id")

func loadMinimalTaskView(tx *bolt.Tx, id string, task *tes.Task) error {
b := tx.Bucket(TaskBucket).Get([]byte(id))
if b == nil {
return &taskNotFoundError{id}
return ErrTaskNotFound
}
task.Id = id
task.State = getTaskState(tx, id)
Expand All @@ -215,9 +211,25 @@ func loadMinimalTaskView(tx *bolt.Tx, id string, task *tes.Task) error {
func loadBasicTaskView(tx *bolt.Tx, id string, task *tes.Task) error {
b := tx.Bucket(TaskBucket).Get([]byte(id))
if b == nil {
return &taskNotFoundError{id}
return ErrTaskNotFound
}
proto.Unmarshal(b, task)
inputs := []*tes.TaskParameter{}
for _, v := range task.Inputs {
v.Contents = ""
inputs = append(inputs, v)
}
task.Inputs = inputs
return loadMinimalTaskView(tx, id, task)
}

func loadFullTaskView(tx *bolt.Tx, id string, task *tes.Task) error {
b := tx.Bucket(TaskBucket).Get([]byte(id))
if b == nil {
return ErrTaskNotFound
}
proto.Unmarshal(b, task)
loadTaskLogs(tx, task)
return loadMinimalTaskView(tx, id, task)
}

Expand Down Expand Up @@ -251,23 +263,13 @@ func (taskBolt *TaskBolt) GetTask(ctx context.Context, req *tes.GetTaskRequest)

if err != nil {
log.Error("GetTask", "error", err, "taskID", req.Id)
if e, ok := err.(*taskNotFoundError); ok {
return nil, grpc.Errorf(codes.NotFound, e.Error())
if err == ErrTaskNotFound {
return nil, grpc.Errorf(codes.NotFound, fmt.Sprintf("%v: %s", err.Error(), req.Id))
}
}
return task, err
}

func getTask(tx *bolt.Tx, id string) *tes.Task {
// This is a thin wrapper around getTaskView in order to allow task views
// to be added with out changing existing code calling getTask().
task, err := getTaskView(tx, id, tes.TaskView_FULL)
if err != nil {
log.Error("Error in getTask", err)
}
return task
}

func getTaskView(tx *bolt.Tx, id string, view tes.TaskView) (*tes.Task, error) {
var err error
task := &tes.Task{}
Expand All @@ -278,8 +280,7 @@ func getTaskView(tx *bolt.Tx, id string, view tes.TaskView) (*tes.Task, error) {
case view == tes.TaskView_BASIC:
err = loadBasicTaskView(tx, id, task)
case view == tes.TaskView_FULL:
err = loadBasicTaskView(tx, id, task)
loadTaskLogs(tx, task)
err = loadFullTaskView(tx, id, task)
default:
err = fmt.Errorf("Unknown view: %s", view.String())
}
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestBasicAuthed(t *testing.T) {

conf := e2e.DefaultConfig()
conf.Server.Password = "abc123"
conf.Worker.ServerPassword = "abc123"
fun := e2e.NewFunnel(conf)
fun.WithLocalBackend()
fun.StartServer()
Expand Down
36 changes: 27 additions & 9 deletions tests/e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2e
import (
"context"
"github.com/ohsu-comp-bio/funnel/proto/tes"
"strings"
"testing"
"time"
)
Expand All @@ -18,24 +19,24 @@ func TestHelloWorld(t *testing.T) {
}
}

func TestGetUknownTask(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("GetTask returned a task for an unknown id")
}
}()
// this function panics on error
// we expect an error to occur in this case since the task does not exist
fun.GetView("nonexistent-task-id", tes.TaskView_MINIMAL)
func TestGetUnknownTask(t *testing.T) {
_, err := fun.HTTP.GetTask("nonexistent-task-id", "MINIMAL")
if err == nil || !strings.Contains(err.Error(), "STATUS CODE - 404") {
log.Debug("ERR", err)
t.Fatal("expected error")
}
}

func TestGetTaskView(t *testing.T) {
var err error
var task *tes.Task

fun.WriteFile("test_contents.txt", "hello world")

id := fun.Run(`
--cmd 'echo hello world'
--name 'foo'
--contents in={{ .storage }}/test_contents.txt
`)
fun.Wait(id)

Expand Down Expand Up @@ -64,12 +65,20 @@ func TestGetTaskView(t *testing.T) {
t.Fatal("unexpected task logs included in basic view")
}

if task.Inputs[0].Contents != "" {
t.Fatal("unexpected TaskParameter contents included in basic view")
}

task = fun.GetView(id, tes.TaskView_FULL)

if task.Logs[0].Logs[0].Stdout != "hello world\n" {
t.Fatal("Missing stdout in full view")
}

if task.Inputs[0].Contents != "hello world" {
t.Fatal("missing TaskParameter contents in full view")
}

// test http proxy
task, err = fun.HTTP.GetTask(id, "MINIMAL")
if err != nil {
Expand Down Expand Up @@ -99,6 +108,10 @@ func TestGetTaskView(t *testing.T) {
t.Fatal("unexpected task logs included in basic view")
}

if task.Inputs[0].Contents != "" {
t.Fatal("unexpected TaskParameter contents included in basic view")
}

task, err = fun.HTTP.GetTask(id, "FULL")
if err != nil {
t.Fatal(err)
Expand All @@ -107,6 +120,11 @@ func TestGetTaskView(t *testing.T) {
if task.Logs[0].Logs[0].Stdout != "hello world\n" {
t.Fatal("Missing stdout in full view")
}

if task.Inputs[0].Contents != "hello world" {
t.Fatal("missing TaskParameter contents in full view")
}

}

// TODO this is a bit hacky for now because we're reusing the same
Expand Down

0 comments on commit c4d9134

Please sign in to comment.