Skip to content

Commit

Permalink
service: enable using Container for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgooz committed Sep 13, 2018
1 parent 49b9005 commit 9fc91dc
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 34 deletions.
4 changes: 2 additions & 2 deletions service/logs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestLogs(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
readers, err := service.Logs()
Expand All @@ -42,7 +42,7 @@ func TestLogsOnlyOneDependency(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
readers, err := service.Logs("test2")
Expand Down
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
}
4 changes: 2 additions & 2 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,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
32 changes: 18 additions & 14 deletions service/start_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestStartService(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
dockerServices, err := service.Start()
defer service.Stop()
require.Nil(t, err)
Expand All @@ -28,6 +28,7 @@ func TestStartService(t *testing.T) {
}

func TestStartWith2Dependencies(t *testing.T) {
c := newContainer(t)
service, _ := FromService(&Service{
Name: "TestStartWith2Dependencies",
Dependencies: []*Dependency{
Expand All @@ -40,14 +41,14 @@ func TestStartWith2Dependencies(t *testing.T) {
Image: "sleep:latest",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(c))
servicesID, err := service.Start()
defer service.Stop()
require.Nil(t, err)
require.Equal(t, 2, len(servicesID))
deps := service.Dependencies
container1, err1 := defaultContainer.FindContainer(deps[0].namespace())
container2, err2 := defaultContainer.FindContainer(deps[1].namespace())
container1, err1 := c.FindContainer(deps[0].namespace())
container2, err2 := c.FindContainer(deps[1].namespace())
require.Nil(t, err1)
require.Nil(t, err2)
require.Equal(t, "http-server:latest", container1.Config.Image)
Expand All @@ -63,7 +64,7 @@ func TestStartAgainService(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
dockerServices, err := service.Start()
Expand All @@ -87,7 +88,7 @@ func TestStartAgainService(t *testing.T) {
// Image: "http-server",
// },
// },
// }, ContainerOption(defaultContainer))
// }, ContainerOption(newContainer(t)))
// service.Start()
// defer service.Stop()
// service.Dependencies[0].Stop()
Expand All @@ -101,6 +102,7 @@ func TestStartAgainService(t *testing.T) {
// }

func TestStartDependency(t *testing.T) {
c := newContainer(t)
service, _ := FromService(&Service{
Name: "TestStartDependency",
Dependencies: []*Dependency{
Expand All @@ -109,9 +111,9 @@ func TestStartDependency(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
networkID, err := defaultContainer.CreateNetwork(service.namespace())
defer defaultContainer.DeleteNetwork(service.namespace())
}, ContainerOption(c))
networkID, err := c.CreateNetwork(service.namespace())
defer c.DeleteNetwork(service.namespace())
dep := service.Dependencies[0]
serviceID, err := dep.Start(networkID)
defer dep.Stop()
Expand All @@ -122,6 +124,7 @@ func TestStartDependency(t *testing.T) {
}

func TestNetworkCreated(t *testing.T) {
c := newContainer(t)
service, _ := FromService(&Service{
Name: "TestNetworkCreated",
Dependencies: []*Dependency{
Expand All @@ -130,10 +133,10 @@ func TestNetworkCreated(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(c))
service.Start()
defer service.Stop()
network, err := defaultContainer.FindNetwork(service.namespace())
network, err := c.FindNetwork(service.namespace())
require.Nil(t, err)
require.NotEqual(t, "", network.ID)
}
Expand All @@ -148,7 +151,7 @@ func TestStartStopStart(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
service.Stop()
dockerServices, err := service.Start()
Expand All @@ -160,6 +163,7 @@ func TestStartStopStart(t *testing.T) {
}

func TestServiceDependenciesListensFromSamePort(t *testing.T) {
c := newContainer(t)
var (
service, _ = FromService(&Service{
Name: "TestServiceDependenciesListensFromSamePort",
Expand All @@ -170,7 +174,7 @@ func TestServiceDependenciesListensFromSamePort(t *testing.T) {
Ports: []string{"80"},
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(c))

service1, _ = FromService(&Service{
Name: "TestServiceDependenciesListensFromSamePort1",
Expand All @@ -181,7 +185,7 @@ func TestServiceDependenciesListensFromSamePort(t *testing.T) {
Ports: []string{"80"},
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(c))
)
_, err := service.Start()
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions service/status_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestStatusService(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
status, err := service.Status()
require.Nil(t, err)
require.Equal(t, STOPPED, status)
Expand All @@ -40,7 +40,7 @@ func TestStatusDependency(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
dep := service.Dependencies[0]
status, err := dep.Status()
require.Nil(t, err)
Expand All @@ -63,7 +63,7 @@ func TestList(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
list, err := ListRunning()
Expand All @@ -85,7 +85,7 @@ func TestListMultipleDependencies(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
defer service.Stop()
list, err := ListRunning()
Expand Down
16 changes: 9 additions & 7 deletions service/stop_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestStopRunningService(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
service.Start()
err := service.Stop()
require.Nil(t, err)
Expand All @@ -35,14 +35,15 @@ func TestStopNonRunningService(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(newContainer(t)))
err := service.Stop()
require.Nil(t, err)
status, _ := service.Status()
require.Equal(t, STOPPED, status)
}

func TestStopDependency(t *testing.T) {
c := newContainer(t)
service, _ := FromService(&Service{
Name: "TestStartDependency",
Dependencies: []*Dependency{
Expand All @@ -51,9 +52,9 @@ func TestStopDependency(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
networkID, err := defaultContainer.CreateNetwork(service.namespace())
defer defaultContainer.DeleteNetwork(service.namespace())
}, ContainerOption(c))
networkID, err := c.CreateNetwork(service.namespace())
defer c.DeleteNetwork(service.namespace())
dep := service.Dependencies[0]
dep.Start(networkID)
err = dep.Stop()
Expand All @@ -63,6 +64,7 @@ func TestStopDependency(t *testing.T) {
}

func TestNetworkDeleted(t *testing.T) {
c := newContainer(t)
service, _ := FromService(&Service{
Name: "TestNetworkDeleted",
Dependencies: []*Dependency{
Expand All @@ -71,10 +73,10 @@ func TestNetworkDeleted(t *testing.T) {
Image: "http-server",
},
},
}, ContainerOption(defaultContainer))
}, ContainerOption(c))
service.Start()
service.Stop()
n, err := defaultContainer.FindNetwork(service.namespace())
n, err := c.FindNetwork(service.namespace())
require.Empty(t, n)
require.NotNil(t, err)
}

0 comments on commit 9fc91dc

Please sign in to comment.