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 432a1d6
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 172 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 (
url = "https://github.com/mesg-foundation/service-webhook"
server = newServer(t)
)

server := newServer(t)
stream := newTestDeployStream(url)
server.DeployService(stream)
s, validationErr, err := server.api.DeployServiceFromURL(url)
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)

s, err = server.api.GetService(s.Id)
require.Error(t, err)
}
96 changes: 45 additions & 51 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"
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.DeployServiceFromURL(url)
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 (
url = "https://github.com/mesg-foundation/service-webhook"
server = newServer(t)
)

server := newServer(t)
stream := newTestDeployStream(url)
server.DeployService(stream)
s, validationErr, err := server.api.DeployServiceFromURL(url)
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 (
url = "https://github.com/mesg-foundation/service-webhook"
server = newServer(t)
)

server.DeployService(stream)
defer services.Delete(stream.serviceID)
s, validationErr, err := server.api.DeployServiceFromURL(url)
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 (
url = "https://github.com/mesg-foundation/service-webhook"
server = newServer(t)
)

server.DeployService(stream)
defer services.Delete(stream.serviceID)
s, validationErr, err := server.api.DeployServiceFromURL(url)
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 (
url = "https://github.com/mesg-foundation/service-webhook"
server = newServer(t)
)

s, validationErr, err := server.api.DeployServiceFromURL(url)
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, "Webhook")
}
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"
}
}
36 changes: 21 additions & 15 deletions interface/grpc/core/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@ import (
"context"
"testing"

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

var serverstart = new(Server)

func TestStartService(t *testing.T) {
url := "https://github.com/mesg-foundation/service-webhook"
var (
// we use a test service without tasks definition here otherwise we need to
// spin up the gRPC server in order to prevent service exit with failer because
// it'll try to listen for tasks.
path = "./service-test-event"
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)

s, _ := services.Get(stream.serviceID)
reply, err := serverstart.StartService(context.Background(), &StartServiceRequest{
ServiceID: stream.serviceID,
reply, err := server.StartService(context.Background(), &StartServiceRequest{
ServiceID: s.Id,
})
require.Nil(t, err)
status, _ := s.Status()
s.Id = "" // TODO(ilgooz) remove this when Service type created by hand.

require.NoError(t, err)
require.NotZero(t, reply)
defer s.Stop()

status, err := s.Status()
require.NoError(t, err)
require.Equal(t, service.RUNNING, status)
require.NotNil(t, reply)
s.Stop()
services.Delete(stream.serviceID)
}
Loading

0 comments on commit 432a1d6

Please sign in to comment.