Skip to content

Commit

Permalink
Merge pull request #447 from mesg-foundation/feature/service-unit-tests
Browse files Browse the repository at this point in the history
service: add unit tests
  • Loading branch information
NicolasMahe authored Sep 24, 2018
2 parents 97c2599 + c84b366 commit c06cb82
Show file tree
Hide file tree
Showing 17 changed files with 581 additions and 268 deletions.
2 changes: 1 addition & 1 deletion api/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCheckServiceNotRunning(t *testing.T) {
}

func TestCheckService(t *testing.T) {
a, _ := newAPIAndDockerTest(t)
a, _ := New()
executor := newTaskExecutor(a)
s, _ := service.FromService(&service.Service{
Name: "TestCheckService",
Expand Down
2 changes: 1 addition & 1 deletion service/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Dependency struct {
// error logs.
func (d *Dependency) Logs() (rstd, rerr io.ReadCloser, err error) {
var reader io.ReadCloser
reader, err = defaultContainer.ServiceLogs(d.namespace())
reader, err = d.service.docker.ServiceLogs(d.namespace())
if err != nil {
return nil, nil, err
}
Expand Down
66 changes: 66 additions & 0 deletions service/dependency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package service

import (
"io"
"sync"
"testing"

"github.com/docker/docker/pkg/stdcopy"
"github.com/stretchr/testify/require"
)

func TestDependencyLogs(t *testing.T) {
testDependencyLogs(t, func(s *Service, dependencyKey string) (rstd, rerr io.ReadCloser,
err error) {
dep, err := s.getDependency(dependencyKey)
require.NoError(t, err)
return dep.Logs()
})
}

func testDependencyLogs(t *testing.T, do func(s *Service, dependencyKey string) (rstd, rerr io.ReadCloser,
err error)) {
var (
dependencyKey = "1"
stdData = []byte{1, 2}
errData = []byte{3, 4}
)

rp, wp := io.Pipe()
wstd := stdcopy.NewStdWriter(wp, stdcopy.Stdout)
werr := stdcopy.NewStdWriter(wp, stdcopy.Stderr)

go wstd.Write(stdData)
go werr.Write(errData)

s, dt := newFromServiceAndDockerTest(t, &Service{
Dependencies: []*Dependency{
{Key: dependencyKey},
},
})
dt.ProvideServiceLogs(rp, nil)

rstd, rerr, err := do(s, dependencyKey)
require.NoError(t, err)

var wg sync.WaitGroup
wg.Add(2)

go func() {
defer wg.Done()
buf := make([]byte, 2)
_, err := rstd.Read(buf)
require.NoError(t, err)
require.Equal(t, stdData, buf)
}()

go func() {
defer wg.Done()
buf := make([]byte, 2)
_, err = rerr.Read(buf)
require.NoError(t, err)
require.Equal(t, errData, buf)
}()

wg.Wait()
}
87 changes: 87 additions & 0 deletions service/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package service

import (
"testing"

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

func TestGetEvent(t *testing.T) {
var (
eventKey = "1"
s, _ = FromService(&Service{
Events: []*Event{
{Key: eventKey},
},
})
)
e, err := s.GetEvent(eventKey)
require.NoError(t, err)
require.Equal(t, eventKey, e.Key)
}

func TestGetEventNonExistent(t *testing.T) {
var (
serviceName = "1"
eventKey = "2"
s, _ = FromService(&Service{
Name: serviceName,
Events: []*Event{
{Key: "3"},
},
})
)
e, err := s.GetEvent(eventKey)
require.Zero(t, e)
require.Equal(t, &EventNotFoundError{
EventKey: eventKey,
ServiceName: serviceName,
}, err)
}

func TestEventValidateAndRequireData(t *testing.T) {
var (
serviceName = "1"
eventKey = "2"
paramKey = "3"
validEventData = map[string]interface{}{
paramKey: "4",
}
inValidEventData = map[string]interface{}{
paramKey: 4,
}
s, _ = FromService(&Service{
Name: serviceName,
Events: []*Event{
{
Key: eventKey,
Data: []*Parameter{
{
Key: paramKey,
Type: "String",
},
},
},
},
})
)

e, _ := s.GetEvent(eventKey)

warnings := e.ValidateData(validEventData)
require.Len(t, warnings, 0)

err := e.RequireData(validEventData)
require.NoError(t, err)

warnings = e.ValidateData(inValidEventData)
require.Len(t, warnings, 1)
require.Equal(t, paramKey, warnings[0].Key)

err = e.RequireData(inValidEventData)
require.Equal(t, &InvalidEventDataError{
EventKey: eventKey,
ServiceName: serviceName,
Warnings: warnings,
}, err)
}
18 changes: 0 additions & 18 deletions service/init.go

This file was deleted.

51 changes: 51 additions & 0 deletions service/logs_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// +build integration

package service

import (
"testing"

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

func TestLogs(t *testing.T) {
service, _ := FromService(&Service{
Name: "TestLogs",
Dependencies: []*Dependency{
{
Key: "test",
Image: "http-server",
},
{
Key: "test2",
Image: "http-server",
},
},
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
readers, err := service.Logs()
require.Nil(t, err)
require.Equal(t, 2, len(readers))
}

func TestLogsOnlyOneDependency(t *testing.T) {
service, _ := FromService(&Service{
Name: "TestLogsOnlyOneDependency",
Dependencies: []*Dependency{
{
Key: "test",
Image: "http-server",
},
{
Key: "test2",
Image: "http-server",
},
},
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
readers, err := service.Logs("test2")
require.Nil(t, err)
require.Equal(t, 1, len(readers))
}
49 changes: 9 additions & 40 deletions service/logs_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
package service

import (
"io"
"testing"

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

func TestLogs(t *testing.T) {
service, _ := FromService(&Service{
Name: "TestLogs",
Dependencies: []*Dependency{
{
Key: "test",
Image: "http-server",
},
{
Key: "test2",
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
service.Start()
defer service.Stop()
readers, err := service.Logs()
require.Nil(t, err)
require.Equal(t, 2, len(readers))
}

func TestLogsOnlyOneDependency(t *testing.T) {
service, _ := FromService(&Service{
Name: "TestLogsOnlyOneDependency",
Dependencies: []*Dependency{
{
Key: "test",
Image: "http-server",
},
{
Key: "test2",
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
service.Start()
defer service.Stop()
readers, err := service.Logs("test2")
require.Nil(t, err)
require.Equal(t, 1, len(readers))
func TestServiceLogs(t *testing.T) {
testDependencyLogs(t, func(s *Service, dependencyKey string) (rstd, rerr io.ReadCloser,
err error) {
l, err := s.Logs(dependencyKey)
require.NoError(t, err)
require.Len(t, l, 1)
return l[0].Standard, l[0].Error, nil
})
}
10 changes: 5 additions & 5 deletions service/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestServiceNamespace(t *testing.T) {
service, _ := FromService(&Service{Name: "TestServiceNamespace"}, ContainerOption(defaultContainer))
service, _ := FromService(&Service{Name: "TestServiceNamespace"})
namespace := service.namespace()
require.Equal(t, namespace, []string{service.ID})
}
Expand All @@ -22,29 +22,29 @@ func TestDependencyNamespace(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
})
dep := service.Dependencies[0]
require.Equal(t, dep.namespace(), []string{service.ID, "test"})
}

func TestEventSubscriptionChannel(t *testing.T) {
service, _ := FromService(&Service{Name: "TestEventSubscriptionChannel"}, ContainerOption(defaultContainer))
service, _ := FromService(&Service{Name: "TestEventSubscriptionChannel"})
require.Equal(t, service.EventSubscriptionChannel(), hash.Calculate(append(
service.namespace(),
eventChannel,
)))
}

func TestTaskSubscriptionChannel(t *testing.T) {
service, _ := FromService(&Service{Name: "TaskSubscriptionChannel"}, ContainerOption(defaultContainer))
service, _ := FromService(&Service{Name: "TaskSubscriptionChannel"})
require.Equal(t, service.TaskSubscriptionChannel(), hash.Calculate(append(
service.namespace(),
taskChannel,
)))
}

func TestResultSubscriptionChannel(t *testing.T) {
service, _ := FromService(&Service{Name: "ResultSubscriptionChannel"}, ContainerOption(defaultContainer))
service, _ := FromService(&Service{Name: "ResultSubscriptionChannel"})
require.Equal(t, service.ResultSubscriptionChannel(), hash.Calculate(append(
service.namespace(),
resultChannel,
Expand Down
16 changes: 16 additions & 0 deletions service/service_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build integration

package service

import (
"testing"

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

func newContainer(t *testing.T) *container.Container {
c, err := container.New()
require.NoError(t, err)
return c
}
11 changes: 9 additions & 2 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func newContainerAndDockerTest(t *testing.T) (*container.Container, *dockertest.
return container, dt
}

func newFromServiceAndDockerTest(t *testing.T, s *Service) (*Service, *dockertest.Testing) {
c, dt := newContainerAndDockerTest(t)
s, err := FromService(s, ContainerOption(c))
require.NoError(t, err)
return s, dt
}

func TestGenerateId(t *testing.T) {
service, _ := FromService(&Service{
Name: "TestGenerateId",
Expand All @@ -33,11 +40,11 @@ func TestGenerateId(t *testing.T) {
func TestNoCollision(t *testing.T) {
service1, _ := FromService(&Service{
Name: "TestNoCollision",
}, ContainerOption(defaultContainer))
})

service2, _ := FromService(&Service{
Name: "TestNoCollision2",
}, ContainerOption(defaultContainer))
})

require.NotEqual(t, service1.ID, service2.ID)
}
Expand Down
Loading

0 comments on commit c06cb82

Please sign in to comment.