Skip to content

Commit

Permalink
Merge pull request #870 from mesg-foundation/feature/simplify-service…
Browse files Browse the repository at this point in the history
…-tests

service: simplify tests
  • Loading branch information
antho1404 authored Apr 12, 2019
2 parents 5598fcf + 189de1a commit 3888a85
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 35 deletions.
6 changes: 4 additions & 2 deletions service/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/docker/pkg/stdcopy"
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/container/mocks"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -34,12 +35,13 @@ func testDependencyLogs(t *testing.T, do func(s *Service, c container.Container,
go wstd.Write(stdData)
go werr.Write(errData)

s, mc := newFromServiceAndContainerMocks(t, &Service{
s := &Service{
Hash: "1",
Dependencies: []*Dependency{
{Key: dependencyKey},
},
})
}
mc := &mocks.Container{}

d, _ := s.getDependency(dependencyKey)
mc.On("ServiceLogs", d.namespace(s.namespace())).Once().Return(rp, nil)
Expand Down
5 changes: 0 additions & 5 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
"github.com/stretchr/testify/require"
)

func newFromServiceAndContainerMocks(t *testing.T, s *Service) (*Service, *mocks.Container) {
_ = t
return s, &mocks.Container{}
}

func TestNew(t *testing.T) {
var (
path = "../service-test/task"
Expand Down
30 changes: 18 additions & 12 deletions service/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestStartService(t *testing.T) {
Sid = "SidStartService"
networkID = "3"
sharedNetworkID = "4"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: serviceName,
Sid: Sid,
Expand All @@ -96,7 +96,8 @@ func TestStartService(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestStartWith2Dependencies(t *testing.T) {
networkID = "7"
sharedNetworkID = "8"
serviceName = "TestStartWith2Dependencies"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: serviceName,
Dependencies: []*Dependency{
Expand All @@ -137,7 +138,8 @@ func TestStartWith2Dependencies(t *testing.T) {
Image: dependencyImage2,
},
},
})
}
mc = &mocks.Container{}
)

var (
Expand Down Expand Up @@ -169,15 +171,16 @@ func TestStartWith2Dependencies(t *testing.T) {
func TestStartServiceRunning(t *testing.T) {
var (
dependencyKey = "1"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Dependencies: []*Dependency{
{
Key: dependencyKey,
Image: "2",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand All @@ -197,7 +200,7 @@ func TestPartiallyRunningService(t *testing.T) {
networkID = "3"
sharedNetworkID = "4"
containerServiceIDs = []string{"5", "6"}
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestPartiallyRunningService",
Dependencies: []*Dependency{
Expand All @@ -210,7 +213,8 @@ func TestPartiallyRunningService(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

var (
Expand Down Expand Up @@ -248,7 +252,7 @@ func TestStartDependency(t *testing.T) {
networkID = "3"
sharedNetworkID = "4"
containerServiceID = "5"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestStartDependency",
Dependencies: []*Dependency{
Expand All @@ -257,7 +261,8 @@ func TestStartDependency(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand All @@ -278,7 +283,7 @@ func TestServiceStartError(t *testing.T) {
networkID = "3"
sharedNetworkID = "4"
startErr = errors.New("ops")
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestNetworkCreated",
Dependencies: []*Dependency{
Expand All @@ -287,7 +292,8 @@ func TestServiceStartError(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand Down
26 changes: 16 additions & 10 deletions service/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"testing"

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

func TestUnknownServiceStatus(t *testing.T) {
var (
dependencyKey = "1"
statusErr = errors.New("ops")
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestUnknownServiceStatus",
Dependencies: []*Dependency{
Expand All @@ -21,7 +22,8 @@ func TestUnknownServiceStatus(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand All @@ -38,7 +40,7 @@ func TestUnknownServiceStatus(t *testing.T) {
func TestStoppedServiceStatus(t *testing.T) {
var (
dependencyKey = "1"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestStoppedServiceStatus",
Dependencies: []*Dependency{
Expand All @@ -47,7 +49,8 @@ func TestStoppedServiceStatus(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand All @@ -64,7 +67,7 @@ func TestStoppedServiceStatus(t *testing.T) {
func TestRunningServiceStatus(t *testing.T) {
var (
dependencyKey = "1"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestRunningServiceStatus",
Dependencies: []*Dependency{
Expand All @@ -73,7 +76,8 @@ func TestRunningServiceStatus(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand All @@ -91,7 +95,7 @@ func TestPartialServiceStatus(t *testing.T) {
var (
dependencyKey = "1"
dependencyKey2 = "2"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestPartialServiceStatus",
Dependencies: []*Dependency{
Expand All @@ -104,7 +108,8 @@ func TestPartialServiceStatus(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

var (
Expand All @@ -125,7 +130,7 @@ func TestPartialServiceStatus(t *testing.T) {
func TestDependencyStatus(t *testing.T) {
var (
dependencyKey = "1"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestDependencyStatus",
Dependencies: []*Dependency{
Expand All @@ -134,7 +139,8 @@ func TestDependencyStatus(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand Down
11 changes: 7 additions & 4 deletions service/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"testing"

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

func TestStopRunningService(t *testing.T) {
var (
dependencyKey = "1"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestStopRunningService",
Dependencies: []*Dependency{
Expand All @@ -19,7 +20,8 @@ func TestStopRunningService(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand All @@ -37,7 +39,7 @@ func TestStopRunningService(t *testing.T) {
func TestStopDependency(t *testing.T) {
var (
dependencyKey = "1"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Hash: "1",
Name: "TestStopService",
Dependencies: []*Dependency{
Expand All @@ -46,7 +48,8 @@ func TestStopDependency(t *testing.T) {
Image: "http-server",
},
},
})
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
Expand Down
6 changes: 4 additions & 2 deletions service/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"testing"

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

Expand All @@ -12,7 +13,7 @@ func TestDeleteVolumes(t *testing.T) {
dependencyKey2 = "2"
volumeA = "a"
volumeB = "b"
s, mc = newFromServiceAndContainerMocks(t, &Service{
s = &Service{
Name: "TestCreateVolumes",
Dependencies: []*Dependency{
{
Expand All @@ -26,7 +27,8 @@ func TestDeleteVolumes(t *testing.T) {
VolumesFrom: []string{dependencyKey1},
},
},
})
}
mc = &mocks.Container{}
)

var (
Expand Down

0 comments on commit 3888a85

Please sign in to comment.