Skip to content

Commit

Permalink
interface/grpc: refactor tests to use features from api package
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgooz committed Aug 24, 2018
1 parent 30de7fb commit 951ad87
Show file tree
Hide file tree
Showing 27 changed files with 445 additions and 213 deletions.
22 changes: 16 additions & 6 deletions interface/grpc/core/core_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package core

import (
"io"
"testing"

"github.com/docker/docker/pkg/archive"
"github.com/mesg-foundation/core/api"
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/container/dockertest"
Expand All @@ -11,13 +13,13 @@ import (

func newServer(t *testing.T) *Server {
container, err := container.New()
require.Nil(t, err)
require.NoError(t, err)

a, err := api.New(api.ContainerOption(container))
require.Nil(t, err)
require.NoError(t, err)

server, err := NewServer(APIOption(a))
require.Nil(t, err)
require.NoError(t, err)

return server
}
Expand All @@ -26,13 +28,21 @@ func newServerAndDockerTest(t *testing.T) (*Server, *dockertest.Testing) {
dt := dockertest.New()

container, err := container.New(container.ClientOption(dt.Client()))
require.Nil(t, err)
require.NoError(t, err)

a, err := api.New(api.ContainerOption(container))
require.Nil(t, err)
require.NoError(t, err)

server, err := NewServer(APIOption(a))
require.Nil(t, err)
require.NoError(t, err)

return server, dt
}

func serviceTar(t *testing.T, path string) io.Reader {
reader, err := archive.TarWithOptions(path, &archive.TarOptions{
Compression: archive.Gzip,
})
require.NoError(t, err)
return reader
}
26 changes: 12 additions & 14 deletions interface/grpc/core/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@ import (
"context"
"testing"

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

var serverdelete = new(Server)

func TestDeleteService(t *testing.T) {
emptyService := service.Service{}

url := "https://github.com/mesg-foundation/service-webhook"
var (
path = "./service-test-task"
server = newServer(t)
)

server := newServer(t)
stream := newTestDeployStream(url)
server.DeployService(stream)
s, validationErr, err := server.api.DeployService(serviceTar(t, path))
require.Zero(t, validationErr)
require.NoError(t, err)

reply, err := serverdelete.DeleteService(context.Background(), &DeleteServiceRequest{
ServiceID: stream.serviceID,
reply, err := server.DeleteService(context.Background(), &DeleteServiceRequest{
ServiceID: s.Id,
})
require.Nil(t, err)
require.NotNil(t, reply)
x, _ := services.Get(stream.serviceID)
require.Equal(t, emptyService, x)

_, err = server.api.GetService(s.Id)
require.Error(t, err)
}
4 changes: 2 additions & 2 deletions interface/grpc/core/deploy_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/cnf/structhash"
"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/database/services"
"github.com/stretchr/testify/require"
)

Expand All @@ -19,7 +18,8 @@ func TestIntegrationDeployService(t *testing.T) {
stream := newTestDeployStream(url)

require.Nil(t, server.DeployService(stream))
defer server.api.DeleteService(stream.serviceID)

require.Equal(t, 1, structhash.Version(stream.serviceID))
require.True(t, stringSliceContains(stream.statuses, fmt.Sprintf("%s Completed.", aurora.Green("✔"))))
services.Delete(stream.serviceID)
}
98 changes: 46 additions & 52 deletions interface/grpc/core/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,110 +5,104 @@ import (
"testing"

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

var serverexecute = new(Server)

func TestExecute(t *testing.T) {
var (
url = "https://github.com/mesg-foundation/service-webhook"
path = "./service-test-task"
taskKey = "call"
data = `{"url": "https://mesg.tech", "data": {}, "headers": {}}`
server = newServer(t)
)

server := newServer(t)
stream := newTestDeployStream(url)

server.DeployService(stream)
defer services.Delete(stream.serviceID)
s, validationErr, err := server.api.DeployService(serviceTar(t, path))
require.Zero(t, validationErr)
require.NoError(t, err)
defer server.api.DeleteService(s.Id)

serverexecute.StartService(context.Background(), &StartServiceRequest{
ServiceID: stream.serviceID,
})
defer serverexecute.StopService(context.Background(), &StopServiceRequest{
ServiceID: stream.serviceID,
})
require.NoError(t, server.api.StartService(s.Id))
defer server.api.StopService(s.Id)

reply, err := serverexecute.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: stream.serviceID,
reply, err := server.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: s.Id,
TaskKey: taskKey,
InputData: data,
})

require.Nil(t, err)
require.NotEqual(t, "", reply.ExecutionID)
}

func TestExecuteWithInvalidJSON(t *testing.T) {
url := "https://github.com/mesg-foundation/service-webhook"
var (
path = "./service-test-task"
server = newServer(t)
)

server := newServer(t)
stream := newTestDeployStream(url)
server.DeployService(stream)
s, validationErr, err := server.api.DeployService(serviceTar(t, path))
require.Zero(t, validationErr)
require.NoError(t, err)
defer server.api.DeleteService(s.Id)

_, err := serverexecute.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: stream.serviceID,
_, err = server.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: s.Id,
TaskKey: "test",
InputData: "",
})
require.NotNil(t, err)
require.Equal(t, err.Error(), "unexpected end of JSON input")
services.Delete(stream.serviceID)
}

func TestExecuteWithInvalidTask(t *testing.T) {
url := "https://github.com/mesg-foundation/service-webhook"

server := newServer(t)
stream := newTestDeployStream(url)
var (
path = "./service-test-task"
server = newServer(t)
)

server.DeployService(stream)
defer services.Delete(stream.serviceID)
s, validationErr, err := server.api.DeployService(serviceTar(t, path))
require.Zero(t, validationErr)
require.NoError(t, err)
defer server.api.DeleteService(s.Id)

serverexecute.StartService(context.Background(), &StartServiceRequest{
ServiceID: stream.serviceID,
})
defer serverexecute.StopService(context.Background(), &StopServiceRequest{
ServiceID: stream.serviceID,
})
require.NoError(t, server.api.StartService(s.Id))
defer server.api.StopService(s.Id)

_, err := serverexecute.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: stream.serviceID,
_, err = server.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: s.Id,
TaskKey: "error",
InputData: "{}",
})

require.Error(t, err)
require.IsType(t, (*service.TaskNotFoundError)(nil), err)
}

func TestExecuteWithNonRunningService(t *testing.T) {
url := "https://github.com/mesg-foundation/service-webhook"

server := newServer(t)
stream := newTestDeployStream(url)
var (
path = "./service-test-task"
server = newServer(t)
)

server.DeployService(stream)
defer services.Delete(stream.serviceID)
s, validationErr, err := server.api.DeployService(serviceTar(t, path))
require.Zero(t, validationErr)
require.NoError(t, err)
defer server.api.DeleteService(s.Id)

_, err := serverexecute.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: stream.serviceID,
_, err = server.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: s.Id,
TaskKey: "test",
InputData: "{}",
})

require.Equal(t, &api.NotRunningServiceError{ServiceID: stream.serviceID}, err)
require.Equal(t, &api.NotRunningServiceError{ServiceID: s.Id}, err)
}

func TestExecuteWithNonExistingService(t *testing.T) {
_, err := serverexecute.ExecuteTask(context.Background(), &ExecuteTaskRequest{
server := newServer(t)

_, err := server.ExecuteTask(context.Background(), &ExecuteTaskRequest{
ServiceID: "service that doesnt exists",
TaskKey: "error",
InputData: "{}",
})

require.NotNil(t, err)
}
25 changes: 13 additions & 12 deletions interface/grpc/core/get_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import (
"context"
"testing"

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

var servergetservice = new(Server)

func TestGetService(t *testing.T) {
service := &service.Service{
Name: "TestGetService",
}
services.Save(service)
defer services.Delete(service.Id)
reply, err := servergetservice.GetService(context.Background(), &GetServiceRequest{
ServiceID: service.Id,
var (
path = "./service-test-task"
server = newServer(t)
)

s, validationErr, err := server.api.DeployService(serviceTar(t, path))
require.Zero(t, validationErr)
require.NoError(t, err)
defer server.api.DeleteService(s.Id)

reply, err := server.GetService(context.Background(), &GetServiceRequest{
ServiceID: s.Id,
})
require.Nil(t, err)
require.NotNil(t, reply)
require.Equal(t, reply.Service.Name, "TestGetService")
require.Equal(t, reply.Service.Name, "Task")
}
16 changes: 9 additions & 7 deletions interface/grpc/core/list_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"context"
"testing"

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

var serverlistservices = new(Server)

func TestListServices(t *testing.T) {
servicesFromAPI, err := serverlistservices.ListServices(context.Background(), &ListServicesRequest{})
servicesFromDB, _ := services.All()
require.Nil(t, err)
require.Equal(t, len(servicesFromAPI.Services), len(servicesFromDB))
server := newServer(t)

reply, err := server.ListServices(context.Background(), &ListServicesRequest{})
require.NoError(t, err)

services, err := server.api.ListServices()
require.NoError(t, err)

require.Equal(t, services, reply.Services)
}
6 changes: 6 additions & 0 deletions interface/grpc/core/service-test-event/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:10.6.0-alpine
WORKDIR /app
COPY ./package* ./
RUN npm install
COPY . .
CMD [ "node", "index.js" ]
4 changes: 4 additions & 0 deletions interface/grpc/core/service-test-event/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const MESG = require('mesg-js').service();
const app = require('express')();

app.listen(3000, () => console.log('Example app listening on port 3000!'));
7 changes: 7 additions & 0 deletions interface/grpc/core/service-test-event/mesg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: Event
description: "A service that only has events definition"
events:
request:
data:
data:
type: Object
12 changes: 12 additions & 0 deletions interface/grpc/core/service-test-event/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "webhook",
"version": "1.0.0",
"description": "Receive HTTP connections and emit events with the data",
"main": "index.js",
"author": "Anthony Estebe <anthony@mesg.tech>",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"mesg-js": "^1.2.1"
}
}
6 changes: 6 additions & 0 deletions interface/grpc/core/service-test-task/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:10.6.0-alpine
WORKDIR /app
COPY ./package* ./
RUN npm install
COPY . .
CMD [ "node", "index.js" ]
4 changes: 4 additions & 0 deletions interface/grpc/core/service-test-task/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const MESG = require('mesg-js').service();
const app = require('express')();

app.listen(3000, () => console.log('Example app listening on port 3000!'));
14 changes: 14 additions & 0 deletions interface/grpc/core/service-test-task/mesg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Task
description: "A service that only has tasks definition"
tasks:
call:
inputs:
url:
type: String
data:
type: Object
headers:
type: Object
outputs:
result:
data: {}
Loading

0 comments on commit 951ad87

Please sign in to comment.