-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #447 from mesg-foundation/feature/service-unit-tests
service: add unit tests
- Loading branch information
Showing
17 changed files
with
581 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.