Skip to content

Commit

Permalink
Put back tests that were deleted because they use a remote url to dep…
Browse files Browse the repository at this point in the history
…loy a service
  • Loading branch information
NicolasMahe committed May 19, 2019
1 parent 0b54217 commit 70e82f0
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
39 changes: 39 additions & 0 deletions api/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,42 @@ func TestDeployInvalidService(t *testing.T) {
wg.Wait()
at.containerMock.AssertExpectations(t)
}

func TestDeployServiceFromURL(t *testing.T) {
var (
url = "git://github.com/mesg-foundation/service-webhook#single-outputs"
a, at = newTesting(t)
)
defer at.close()

at.containerMock.On("Build", mock.Anything).Once().Return("1", nil)

statuses := make(chan DeployStatus)
var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
_, validationError, err := a.DeployServiceFromURL(url, nil, DeployServiceStatusOption(statuses))
require.Nil(t, validationError)
require.NoError(t, err)
}()

require.Equal(t, DeployStatus{
Message: "Receiving service context...",
Type: Running,
}, <-statuses)

require.Equal(t, DeployStatus{
Message: "Building Docker image...",
Type: Running,
}, <-statuses)

require.Equal(t, DeployStatus{
Message: "Image built with success",
Type: DonePositive,
}, <-statuses)

wg.Wait()
at.containerMock.AssertExpectations(t)
}
21 changes: 21 additions & 0 deletions interface/grpc/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ func TestGetService(t *testing.T) {
require.Equal(t, reply.Service.Definition.Name, "Task")
}

func TestListServices(t *testing.T) {
url := "git://github.com/mesg-foundation/service-webhook#single-outputs"
server, closer := newServer(t)
defer closer()

stream := newTestDeployStream(url)
require.NoError(t, server.DeployService(stream))
defer server.api.DeleteService(stream.hash, false)

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

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

apiProtoServices := toProtoServices(services)

require.Len(t, apiProtoServices, 1)
require.Equal(t, reply.Services[0].Definition.Hash, apiProtoServices[0].Hash)
}

func TestStartService(t *testing.T) {
server, closer := newServer(t)
defer closer()
Expand Down
28 changes: 28 additions & 0 deletions interface/grpc/core/deploy_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build integration

package core

import (
"testing"

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

func TestIntegrationDeployService(t *testing.T) {
url := "git://github.com/mesg-foundation/service-webhook#single-outputs"

server, closer := newServer(t)
defer closer()
stream := newTestDeployStream(url)

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

require.Len(t, stream.sid, 7)
require.NotEmpty(t, stream.hash)
require.Contains(t, stream.statuses, api.DeployStatus{
Message: "Image built with success",
Type: api.DonePositive,
})
}
74 changes: 74 additions & 0 deletions interface/grpc/core/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package core

import (
"io/ioutil"
"strings"
"testing"

"github.com/mesg-foundation/core/api"
"github.com/mesg-foundation/core/protobuf/coreapi"
"github.com/stretchr/testify/require"
grpc "google.golang.org/grpc"
)

func TestDeployService(t *testing.T) {
url := "git://github.com/mesg-foundation/service-webhook#single-outputs"

server, dt, closer := newServerAndDockerTest(t)
defer closer()
dt.ProvideImageBuild(ioutil.NopCloser(strings.NewReader(`{"stream":"sha256:x"}`)), nil)

stream := newTestDeployStream(url)
require.Nil(t, server.DeployService(stream))
require.Len(t, stream.sid, 7)
require.NotEmpty(t, stream.hash)

require.Contains(t, stream.statuses, api.DeployStatus{
Message: "Image built with success",
Type: api.DonePositive,
})
}

// TODO(ilgooz) also add tests for receiving chunks.
type testDeployStream struct {
url string // Git repo url.
err error
sid string
hash string
statuses []api.DeployStatus
grpc.ServerStream
}

func newTestDeployStream(url string) *testDeployStream {
return &testDeployStream{url: url}
}

func (s *testDeployStream) Send(m *coreapi.DeployServiceReply) error {
s.sid = m.GetService().GetSid()
s.hash = m.GetService().GetHash()

status := m.GetStatus()
if status != nil {
var typ api.StatusType
switch status.Type {
case coreapi.DeployServiceReply_Status_RUNNING:
typ = api.Running
case coreapi.DeployServiceReply_Status_DONE_POSITIVE:
typ = api.DonePositive
case coreapi.DeployServiceReply_Status_DONE_NEGATIVE:
typ = api.DoneNegative
}
s.statuses = append(s.statuses, api.DeployStatus{
Message: status.Message,
Type: typ,
})
}

return nil
}

func (s *testDeployStream) Recv() (*coreapi.DeployServiceRequest, error) {
return &coreapi.DeployServiceRequest{
Value: &coreapi.DeployServiceRequest_Url{Url: s.url},
}, s.err
}
8 changes: 8 additions & 0 deletions interface/grpc/core/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
service "github.com/mesg-foundation/core/service"
)

func toProtoServices(ss []*service.Service) []*definition.Service {
services := make([]*definition.Service, len(ss))
for i, s := range ss {
services[i] = toProtoService(s)
}
return services
}

func toProtoService(s *service.Service) *definition.Service {
return &definition.Service{
Hash: s.Hash,
Expand Down
9 changes: 9 additions & 0 deletions interface/grpc/core/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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"
"github.com/mesg-foundation/core/database"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -53,6 +54,14 @@ func newServer(t *testing.T) (*Server, func()) {
return newServerWithContainer(t, c)
}

func newServerAndDockerTest(t *testing.T) (*Server, *dockertest.Testing, func()) {
dt := dockertest.New()
c, err := container.New(container.ClientOption(dt.Client()))
require.NoError(t, err)
s, closer := newServerWithContainer(t, c)
return s, dt, closer
}

func serviceTar(t *testing.T, path string) io.Reader {
reader, err := archive.TarWithOptions(path, &archive.TarOptions{
Compression: archive.Gzip,
Expand Down
38 changes: 38 additions & 0 deletions interface/grpc/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,41 @@ func TestSubmitWithNonExistentOutputKey(t *testing.T) {
require.Equal(t, outputKey, notFoundErr.TaskOutputKey)
require.Equal(t, s.Name, notFoundErr.ServiceName)
}

func TestSubmitWithInvalidTaskOutputs(t *testing.T) {
var (
taskKey = "call"
taskData = map[string]interface{}{
"url": "https://mesg.com",
"data": map[string]interface{}{},
"headers": map[string]interface{}{},
}
outputKey = "success"
outputData = `{"foo":1}`
server, closer = newServer(t)
)
defer closer()

s, validationErr, err := server.api.DeployService(serviceTar(t, taskServicePath), nil)
require.Zero(t, validationErr)
require.NoError(t, err)
defer server.api.DeleteService(s.Hash, false)

require.NoError(t, server.api.StartService(s.Hash))
defer server.api.StopService(s.Hash)

executionID, err := server.api.ExecuteTask(s.Hash, taskKey, taskData, nil)
require.NoError(t, err)

_, err = server.SubmitResult(context.Background(), &serviceapi.SubmitResultRequest{
ExecutionID: executionID,
OutputKey: outputKey,
OutputData: outputData,
})
require.Error(t, err)
invalidErr, ok := err.(*service.InvalidTaskOutputError)
require.True(t, ok)
require.Equal(t, taskKey, invalidErr.TaskKey)
require.Equal(t, outputKey, invalidErr.TaskOutputKey)
require.Equal(t, s.Name, invalidErr.ServiceName)
}

0 comments on commit 70e82f0

Please sign in to comment.