Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix change of behavior of StopService introduced in commit #0366e06 #505

Merged
merged 7 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- run: docker swarm init
- run: docker build -t sleep docker-images/sleep/
- run: docker build -t http-server docker-images/http-server/
- run: go test -timeout 180s -p 1 -tags=integration -coverprofile=coverage.txt ./...
- run: go test -v -timeout 300s -p 1 -tags=integration -coverprofile=coverage.txt ./...
- run: bash <(curl -s https://codecov.io/bash)

"publish_docker_dev":
Expand Down
10 changes: 1 addition & 9 deletions api/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package api
import (
"testing"

"github.com/docker/docker/api/types"
"github.com/mesg-foundation/core/container/dockertest"
"github.com/mesg-foundation/core/service"
"github.com/stvp/assert"
)
Expand Down Expand Up @@ -51,7 +49,7 @@ func TestCheckServiceNotRunning(t *testing.T) {
}

func TestCheckService(t *testing.T) {
a, dt, closer := newAPIAndDockerTest(t)
a, _, closer := newAPIAndDockerTest(t)
defer closer()
executor := newTaskExecutor(a)
s, _ := service.FromService(&service.Service{
Expand All @@ -66,10 +64,4 @@ func TestCheckService(t *testing.T) {
s.Start()
err := executor.checkServiceStatus(s)
assert.Nil(t, err)
// Mock DockerTest.
// Need 3 of them because the Docker API is called 3 times in s.Stop().
dt.ProvideContainerInspect(types.ContainerJSON{}, dockertest.NotFoundErr{})
dt.ProvideContainerInspect(types.ContainerJSON{}, dockertest.NotFoundErr{})
dt.ProvideContainerInspect(types.ContainerJSON{}, dockertest.NotFoundErr{})
s.Stop()
}
16 changes: 9 additions & 7 deletions container/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ func (c *Container) StopService(namespace []string) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout)
defer cancel()
container, err := c.FindContainer(namespace)
if err != nil {
if err != nil && !docker.IsErrNotFound(err) {
return err
}
if err := c.client.ServiceRemove(ctx, c.Namespace(namespace)); err != nil && !docker.IsErrNotFound(err) {
return err
}
timeout := 1 * time.Second
if err := c.client.ContainerStop(ctx, container.ID, &timeout); err != nil {
return err
}
if err := c.client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{}); err != nil && !docker.IsErrNotFound(err) {
return err
if container.ContainerJSONBase != nil {
timeout := 1 * time.Second
if err := c.client.ContainerStop(ctx, container.ID, &timeout); err != nil && !docker.IsErrNotFound(err) {
return err
}
if err := c.client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{}); err != nil && !docker.IsErrNotFound(err) {
return err
}
}
return c.waitForStatus(namespace, STOPPED)
}
Expand Down
20 changes: 6 additions & 14 deletions container/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,8 @@ func TestStopService(t *testing.T) {
ContainerJSONBase: &types.ContainerJSONBase{ID: containerID},
}, nil)

go func() {
<-dt.LastContainerList()
<-dt.LastContainerInspect()

dt.ProvideContainerList(nil, dockertest.NotFoundErr{})
dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, dockertest.NotFoundErr{})
}()
dt.ProvideContainerList(nil, dockertest.NotFoundErr{})
dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, dockertest.NotFoundErr{})

require.Nil(t, c.StopService(namespace))
require.Equal(t, c.Namespace(namespace), (<-dt.LastServiceRemove()).ServiceID)
Expand All @@ -72,16 +67,13 @@ func TestStopNotExistingService(t *testing.T) {
dt := dockertest.New()
c, _ := New(ClientOption(dt.Client()))

dt.ProvideContainerList([]types.Container{}, nil)
dt.ProvideContainerInspect(types.ContainerJSON{}, dockertest.NotFoundErr{})
dt.ProvideServiceRemove(dockertest.NotFoundErr{})
dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, dockertest.NotFoundErr{})
dt.ProvideContainerInspect(types.ContainerJSON{}, dockertest.NotFoundErr{})

require.Equal(t, dockertest.NotFoundErr{}, c.StopService(namespace))

select {
case <-dt.LastServiceRemove():
t.Error("should not remove non existent service")
default:
}
require.NoError(t, c.StopService(namespace))
}

func TestFindService(t *testing.T) {
Expand Down