Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout + scope isolation in Otto interpreter #23

Merged
merged 2 commits into from
Aug 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simple/echowhale.wf.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"tasks": {
"whaleEcho": {
"inputs" : {
"type" : "ref",
"type" : "expr",
"value" : "$.invocation.inputs.default"
},
"name": "whalesay",
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/fortunewhale.wf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"generateFortune": {
"name": "fortune",
"inputs" : {
"type" : "ref",
"type" : "expr",
"value" : "$.invocation.inputs.default"
},
"dependencies": {
Expand All @@ -19,7 +19,7 @@
"name": "whalesay",
"inputs" : {
"default" : {
"type" : "ref",
"type" : "expr",
"value" : "$.tasks.generateFortune.output"
}
},
Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/query/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ go_library(
name = "go_default_library",
srcs = [
"expr.go",
"jsonpath.go",
"transformers.go",
"scope.go",
],
visibility = ["//visibility:public"],
deps = [
"//pkg/types:go_default_library",
"//pkg/types/typedvalues:go_default_library",
"//pkg/util:go_default_library",
"//vendor/github.com/robertkrimen/otto:go_default_library",
"//vendor/github.com/sirupsen/logrus:go_default_library",
],
)
41 changes: 28 additions & 13 deletions pkg/controller/query/expr.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package query

import (
"time"

"errors"

"github.com/fission/fission-workflow/pkg/types"
"github.com/fission/fission-workflow/pkg/types/typedvalues"
"github.com/fission/fission-workflow/pkg/util"
"github.com/robertkrimen/otto"
_ "github.com/robertkrimen/otto/underscore"
)

type ExpressionParser interface {
Resolve(rootScope interface{}, scope interface{}, expr *types.TypedValue) (*types.TypedValue, error)
}

// TODO measure performance
var RESOLVING_TIMEOUT = time.Duration(100) * time.Millisecond
var ErrTimeOut = errors.New("Expression resolve timeout.")

type JavascriptExpressionParser struct {
vm *otto.Otto // TODO limit functionality (might need a fork?)
vm *otto.Otto
parser typedvalues.Parser
}

/*
Helper functions
task().
dependency(id).

guid
*/
func NewJavascriptExpressionParser(parser typedvalues.Parser) *JavascriptExpressionParser {
// TODO inject helper functions
vm := otto.New()
err := vm.Set("uid", func(call otto.FunctionCall) otto.Value {
uid, _ := vm.ToValue(util.Uid())
Expand All @@ -45,10 +44,26 @@ func (oe *JavascriptExpressionParser) Resolve(rootScope interface{}, scope inter
return expr, nil
}

oe.vm.Set("$", rootScope)
oe.vm.Set("@", scope)
defer func() {
if caught := recover(); caught != nil {
if ErrTimeOut != caught {
panic(caught)
}
}
}()

scoped := oe.vm.Copy()
scoped.Set("$", rootScope)
scoped.Set("task", scope)

go func() {
<-time.After(RESOLVING_TIMEOUT)
scoped.Interrupt <- func() {
panic(ErrTimeOut)
}
}()

jsResult, err := oe.vm.Run(expr.Value)
jsResult, err := scoped.Run(expr.Value)
if err != nil {
return nil, err
}
Expand Down
104 changes: 104 additions & 0 deletions pkg/controller/query/expr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package query

import (
"testing"

"fmt"
"strings"

"github.com/fission/fission-workflow/pkg/types/typedvalues"
)

var pf = typedvalues.NewDefaultParserFormatter()

var scope = map[string]interface{}{
"bit": "bat",
}
var rootscope = map[string]interface{}{
"foo": "bar",
"currentScope": scope,
}

func TestResolveTestRootScopePath(t *testing.T) {

exprParser := NewJavascriptExpressionParser(pf)

resolved, err := exprParser.Resolve(rootscope, rootscope, typedvalues.Expr("$.currentScope.bit"))
if err != nil {
t.Error(err)
}

resolvedString, err := pf.Format(resolved)
if err != nil {
t.Error(err)
}

expected := scope["bit"]
if resolvedString != expected {
t.Errorf("Expected value '%v' does not match '%v'", expected, resolved)
}
}

func TestResolveTestScopePath(t *testing.T) {

exprParser := NewJavascriptExpressionParser(pf)

resolved, err := exprParser.Resolve(rootscope, scope, typedvalues.Expr("task.bit"))
if err != nil {
t.Error(err)
}

resolvedString, err := pf.Format(resolved)
if err != nil {
t.Error(err)
}

expected := scope["bit"]
if resolvedString != expected {
t.Errorf("Expected value '%v' does not match '%v'", expected, resolved)
}
}

func TestResolveLiteral(t *testing.T) {

exprParser := NewJavascriptExpressionParser(pf)

expected := "foobar"
resolved, _ := exprParser.Resolve(rootscope, scope, typedvalues.Expr(fmt.Sprintf("'%s'", expected)))

resolvedString, _ := pf.Format(resolved)
if resolvedString != expected {
t.Errorf("Expected value '%v' does not match '%v'", expected, resolved)
}
}

func TestResolveTransformation(t *testing.T) {

exprParser := NewJavascriptExpressionParser(pf)

src := "foobar"
resolved, _ := exprParser.Resolve(rootscope, scope, typedvalues.Expr(fmt.Sprintf("'%s'.toUpperCase()", src)))
expected := strings.ToUpper(src)

resolvedString, _ := pf.Format(resolved)
if resolvedString != expected {
t.Errorf("Expected value '%v' does not match '%v'", src, resolved)
}
}

func TestResolveInjectedFunction(t *testing.T) {

exprParser := NewJavascriptExpressionParser(pf)

resolved, err := exprParser.Resolve(rootscope, scope, typedvalues.Expr("uid()"))

if err != nil {
t.Error(err)
}

resolvedString, _ := pf.Format(resolved)

if resolvedString == "" {
t.Error("Uid returned empty string")
}
}