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

Remove initialization of swarm but display useful error #615

Merged
merged 14 commits into from
Dec 4, 2018
25 changes: 0 additions & 25 deletions container/client_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package container

import (
"context"
"errors"
"io"
"time"

Expand Down Expand Up @@ -66,7 +67,7 @@ func New(options ...Option) (*DockerContainer, error) {
}
}
c.negotiateAPIVersion()
if err := c.createSwarmIfNeeded(); err != nil {
if err := c.checkSwarm(); err != nil {
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
return c, err
}
return c, c.createSharedNetworkIfNeeded()
Expand All @@ -92,22 +93,17 @@ func (c *DockerContainer) negotiateAPIVersion() {
c.client.NegotiateAPIVersion(ctx)
}

func (c *DockerContainer) createSwarmIfNeeded() error {
func (c *DockerContainer) checkSwarm() error {
ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout)
defer cancel()
info, err := c.client.Info(ctx)
if err != nil {
return err
}
if info.Swarm.NodeID != "" {
return nil
if info.Swarm.NodeID == "" {
return errors.New("docker swarm is not initialized. Execute \"docker swarm init\" and try again")
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}
ctx, cancel = context.WithTimeout(context.Background(), c.callTimeout)
defer cancel()
_, err = c.client.SwarmInit(ctx, swarm.InitRequest{
ListenAddr: "0.0.0.0:2377", // https://docs.docker.com/engine/reference/commandline/swarm_init/#usage
})
return err
return nil
}

// FindContainer returns a docker container.
Expand Down
6 changes: 0 additions & 6 deletions container/container_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestIntegrationCreateSwarmIfNeeded(t *testing.T) {
c, err := New()
require.NoError(t, err)
require.Nil(t, c.createSwarmIfNeeded())
}

func TestIntegrationFindContainerNotExisting(t *testing.T) {
c, err := New()
require.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ func TestNew(t *testing.T) {
t.Error("should fetch info")
}

require.Equal(t, "0.0.0.0:2377", (<-dt.LastSwarmInit()).Request.ListenAddr)

ln := <-dt.LastNetworkCreate()
require.Equal(t, cfg.Core.Name, ln.Name)
require.Equal(t, types.NetworkCreate{
Expand Down
2 changes: 1 addition & 1 deletion container/dockertest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (c *Client) Info(context.Context) (types.Info, error) {
case resp := <-c.responses.info:
return resp.info, resp.err
default:
return types.Info{}, nil
return types.Info{Swarm: swarm.Info{NodeID: "1"}}, nil
}
}

Expand Down