Skip to content

Commit

Permalink
Merge branch 'dev' into daemon-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert authored Aug 15, 2018
2 parents 3228394 + 2b8569d commit 38fa7c1
Show file tree
Hide file tree
Showing 116 changed files with 3,583 additions and 1,096 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- (#282) Branch support added. You can now specify your branches with a `#branch` fragment at the end of your git url. E.g.: https://github.com/mesg-foundation/service-ethereum-erc20#websocket
- (#299) Add more user friendly errors when failing to connect to the Core or Docker
- (#352) Use logrus logging package
- (#356) Use github.com/stretchr/testify package

#### Added
- (#242) Add more details in command `mesg-core service validate`
Expand All @@ -14,11 +15,14 @@
- (#303) Add command `service dev` that build and run the service with the logs
- (#303) Add command `service execute` that execute a task on a service
- (#316) Delete service when stoping the `service dev` command to avoid to keep all the versions of the services.
- (#317) Add errors when trying to execute a service that is not running (nothing was happening before)
- (#344) Add `service execute --data` flag to pass arguments as key=value.

#### Removed
- (#303) Deprecate command `service test` in favor of `service dev` and `service execute`

#### Fixed
- (#358) Fix goroutine leaks on api package handlers where gRPC streams are used. Handlers now doesn't block forever by exiting on context cancellation and stream.Send() errors.

## [v1.0.0]

Expand Down
12 changes: 6 additions & 6 deletions api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package client
import (
"testing"

"github.com/stvp/assert"
"github.com/stretchr/testify/require"
)

func TestAPI(t *testing.T) {
api, err := API()
assert.Nil(t, err)
assert.NotNil(t, api)
require.Nil(t, err)
require.NotNil(t, api)
}

func TestGetClient(t *testing.T) {
c, err := getClient()
assert.Nil(t, err)
assert.NotNil(t, c)
assert.NotNil(t, _client)
require.Nil(t, err)
require.NotNil(t, c)
require.NotNil(t, _client)
}
22 changes: 11 additions & 11 deletions api/client/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/stvp/assert"
"github.com/stretchr/testify/require"
)

func TestServices(t *testing.T) {
Expand All @@ -14,10 +14,10 @@ func TestServices(t *testing.T) {
Execute: &Task{ServiceID: "zzz"},
}
services := wf.services()
assert.Equal(t, len(services), 3)
assert.Equal(t, services[0], "xxx")
assert.Equal(t, services[1], "yyy")
assert.Equal(t, services[2], "zzz")
require.Equal(t, len(services), 3)
require.Equal(t, services[0], "xxx")
require.Equal(t, services[1], "yyy")
require.Equal(t, services[2], "zzz")
}

func TestServicesDuplicate(t *testing.T) {
Expand All @@ -27,9 +27,9 @@ func TestServicesDuplicate(t *testing.T) {
Execute: &Task{ServiceID: "xxx"},
}
services := wf.services()
assert.Equal(t, len(services), 2)
assert.Equal(t, services[0], "xxx")
assert.Equal(t, services[1], "yyy")
require.Equal(t, len(services), 2)
require.Equal(t, services[0], "xxx")
require.Equal(t, services[1], "yyy")
}

func TestIterateService(t *testing.T) {
Expand All @@ -43,8 +43,8 @@ func TestIterateService(t *testing.T) {
cpt++
return nil
})
assert.Nil(t, err)
assert.Equal(t, cpt, 3)
require.Nil(t, err)
require.Equal(t, cpt, 3)
}

func TestIterateServiceWithError(t *testing.T) {
Expand All @@ -56,5 +56,5 @@ func TestIterateServiceWithError(t *testing.T) {
err := iterateService(wf, func(ID string) error {
return errors.New("test error")
})
assert.NotNil(t, err)
require.NotNil(t, err)
}
18 changes: 9 additions & 9 deletions api/client/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/mesg-foundation/core/api/core"
"github.com/stvp/assert"
"github.com/stretchr/testify/require"
)

func TestProcessEventWithInvalidEventData(t *testing.T) {
Expand All @@ -19,7 +19,7 @@ func TestProcessEventWithInvalidEventData(t *testing.T) {
EventData: "",
}
err := wf.Execute.processEvent(wf, data)
assert.Equal(t, err.Error(), "unexpected end of JSON input")
require.Equal(t, err.Error(), "unexpected end of JSON input")
}

func TestProcessResulsWithInvalidEventData(t *testing.T) {
Expand All @@ -36,7 +36,7 @@ func TestProcessResulsWithInvalidEventData(t *testing.T) {
TaskKey: "taskx",
}
err := wf.Execute.processResult(wf, data)
assert.Equal(t, err.Error(), "unexpected end of JSON input")
require.Equal(t, err.Error(), "unexpected end of JSON input")
}

func TestConvertData(t *testing.T) {
Expand All @@ -46,8 +46,8 @@ func TestConvertData(t *testing.T) {
},
}
res, err := task.convertData("foo")
assert.Nil(t, err)
assert.Equal(t, res, "\"bar\"")
require.Nil(t, err)
require.Equal(t, res, "\"bar\"")
}

func TestConvertDataObject(t *testing.T) {
Expand All @@ -60,8 +60,8 @@ func TestConvertDataObject(t *testing.T) {
"foo": "bar",
"number": 42,
})
assert.Nil(t, err)
assert.Equal(t, res, "{\"foo\":\"bar\",\"number\":42}")
require.Nil(t, err)
require.Equal(t, res, "{\"foo\":\"bar\",\"number\":42}")
}

func TestConvertDataWithNull(t *testing.T) {
Expand All @@ -71,6 +71,6 @@ func TestConvertDataWithNull(t *testing.T) {
},
}
res, err := task.convertData("xxx")
assert.Nil(t, err)
assert.Equal(t, res, "null")
require.Nil(t, err)
require.Equal(t, res, "null")
}
34 changes: 17 additions & 17 deletions api/client/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,79 @@ import (
"testing"

"github.com/mesg-foundation/core/api/core"
"github.com/stvp/assert"
"github.com/stretchr/testify/require"
)

func TestValidEventFromAny(t *testing.T) {
wf := &Workflow{
OnEvent: &Event{Name: "*"},
}
assert.True(t, wf.validEvent(&core.EventData{EventKey: "xxx"}))
require.True(t, wf.validEvent(&core.EventData{EventKey: "xxx"}))
}

func TestValidEventFromValue(t *testing.T) {
wf := &Workflow{
OnEvent: &Event{Name: "xxx"},
}
assert.True(t, wf.validEvent(&core.EventData{EventKey: "xxx"}))
assert.False(t, wf.validEvent(&core.EventData{EventKey: "yyy"}))
require.True(t, wf.validEvent(&core.EventData{EventKey: "xxx"}))
require.False(t, wf.validEvent(&core.EventData{EventKey: "yyy"}))
}

func TestValidResultFromAnyNameAndAnyOutput(t *testing.T) {
wf := &Workflow{
OnResult: &Result{Name: "*", Output: "*"},
}
assert.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
require.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
}

func TestValidResultFromAnyNameAndNotAnyOutput(t *testing.T) {
wf := &Workflow{
OnResult: &Result{Name: "*", Output: "xxx"},
}
assert.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
assert.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "yyy"}))
require.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
require.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "yyy"}))
}

func TestValidResultFromNotAnyNameAndAnyOutput(t *testing.T) {
wf := &Workflow{
OnResult: &Result{Name: "xxx", Output: "*"},
}
assert.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
assert.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "yyy"}))
require.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
require.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "yyy"}))
}

func TestValidResultFromNotAnyNameAndNotAnyOutput(t *testing.T) {
wf := &Workflow{
OnResult: &Result{Name: "xxx", Output: "yyy"},
}
assert.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "yyy"}))
assert.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "yyy"}))
assert.False(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
assert.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "xxx"}))
require.True(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "yyy"}))
require.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "yyy"}))
require.False(t, wf.validResult(&core.ResultData{TaskKey: "xxx", OutputKey: "xxx"}))
require.False(t, wf.validResult(&core.ResultData{TaskKey: "yyy", OutputKey: "xxx"}))
}

func TestInvalidListenResult(t *testing.T) {
wf := &Workflow{
OnResult: &Result{},
}
assert.NotNil(t, listenResults(wf))
require.NotNil(t, listenResults(wf))
}

func TestInvalidListenEvent(t *testing.T) {
wf := &Workflow{
OnEvent: &Event{},
}
assert.NotNil(t, listenEvents(wf))
require.NotNil(t, listenEvents(wf))
}

func TestInvalidWorkflowWithNoExecute(t *testing.T) {
wf := Workflow{OnEvent: &Event{}}
err := wf.Start()
assert.Equal(t, err.Error(), "A workflow needs a task")
require.Equal(t, err.Error(), "A workflow needs a task")
}

func TestInvalidWorkflowWithNoEvent(t *testing.T) {
wf := Workflow{Execute: &Task{}}
err := wf.Start()
assert.Equal(t, err.Error(), "A workflow needs an event OnEvent or OnResult")
require.Equal(t, err.Error(), "A workflow needs an event OnEvent or OnResult")
}
8 changes: 4 additions & 4 deletions api/core/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/mesg-foundation/core/database/services"
"github.com/mesg-foundation/core/service"
"github.com/stvp/assert"
"github.com/stretchr/testify/require"
)

var serverdelete = new(Server)
Expand All @@ -27,8 +27,8 @@ func TestDeleteService(t *testing.T) {
reply, err := serverdelete.DeleteService(context.Background(), &DeleteServiceRequest{
ServiceID: deployment.ServiceID,
})
assert.Nil(t, err)
assert.NotNil(t, reply)
require.Nil(t, err)
require.NotNil(t, reply)
x, _ := services.Get(deployment.ServiceID)
assert.Equal(t, x, emptyService)
require.Equal(t, x, emptyService)
}
8 changes: 4 additions & 4 deletions api/core/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/mesg-foundation/core/database/services"
"github.com/mesg-foundation/core/service"
"github.com/stvp/assert"
"github.com/stretchr/testify/require"
)

var serverdeploy = new(Server)
Expand All @@ -23,8 +23,8 @@ func TestDeployService(t *testing.T) {
deployment, err := serverdeploy.DeployService(context.Background(), &DeployServiceRequest{
Service: &service,
})
assert.Nil(t, err)
assert.NotNil(t, deployment)
assert.NotEqual(t, "", deployment.ServiceID)
require.Nil(t, err)
require.NotNil(t, deployment)
require.NotEqual(t, "", deployment.ServiceID)
services.Delete(deployment.ServiceID)
}
10 changes: 10 additions & 0 deletions api/core/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core

// NotRunningServiceError is an error when a service is not running.
type NotRunningServiceError struct {
ServiceID string
}

func (e *NotRunningServiceError) Error() string {
return "Service " + e.ServiceID + " is not running"
}
12 changes: 12 additions & 0 deletions api/core/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core

import (
"testing"

"github.com/stvp/assert"
)

func TestNotRunningServiceError(t *testing.T) {
e := NotRunningServiceError{ServiceID: "test"}
assert.Equal(t, "Service test is not running", e.Error())
}
42 changes: 33 additions & 9 deletions api/core/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,49 @@ import (

"github.com/mesg-foundation/core/database/services"
"github.com/mesg-foundation/core/execution"
"github.com/mesg-foundation/core/service"
)

// ExecuteTask executes a task for a given service.
func (s *Server) ExecuteTask(ctx context.Context, request *ExecuteTaskRequest) (*ExecuteTaskReply, error) {
service, err := services.Get(request.ServiceID)
srv, err := services.Get(request.ServiceID)
if err != nil {
return nil, err
}

var inputs map[string]interface{}
if err := json.Unmarshal([]byte(request.InputData), &inputs); err != nil {
inputs, err := getData(request)
if err != nil {
return nil, err
}
execution, err := execution.Create(&service, request.TaskKey, inputs)
if err != nil {
if err := checkServiceStatus(&srv); err != nil {
return nil, err
}

executionID, err := execute(&srv, request.TaskKey, inputs)
return &ExecuteTaskReply{
ExecutionID: execution.ID,
}, execution.Execute()
ExecutionID: executionID,
}, err
}

func checkServiceStatus(srv *service.Service) error {
status, err := srv.Status()
if err != nil {
return err
}
if status != service.RUNNING {
return &NotRunningServiceError{ServiceID: srv.Hash()}
}
return nil
}

func getData(request *ExecuteTaskRequest) (map[string]interface{}, error) {
var inputs map[string]interface{}
err := json.Unmarshal([]byte(request.InputData), &inputs)
return inputs, err
}

func execute(srv *service.Service, key string, inputs map[string]interface{}) (executionID string, err error) {
exc, err := execution.Create(srv, key, inputs)
if err != nil {
return "", err
}
return exc.ID, exc.Execute()
}
Loading

0 comments on commit 38fa7c1

Please sign in to comment.