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.

20 changes: 10 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 All @@ -12,6 +13,10 @@ import (
"github.com/mesg-foundation/core/config"
)

var (
errSwarmNotInit = errors.New(`docker swarm is not initialized. run "docker swarm init" to setup swarm and try again`)
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
)

// Container describes the API of container package.
type Container interface {
Build(path string) (tag string, err error)
Expand Down Expand Up @@ -66,7 +71,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 +97,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 errSwarmNotInit
}
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
17 changes: 4 additions & 13 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 All @@ -93,19 +91,12 @@ func TestNew(t *testing.T) {
}, ln.Options)
}

func TestNewWithExistingNode(t *testing.T) {
func TestNewSwarmError(t *testing.T) {
dt := dockertest.New()
dt.ProvideInfo(types.Info{Swarm: swarm.Info{NodeID: "1"}}, nil)

c, err := New(ClientOption(dt.Client()))
require.NoError(t, err)
require.NotNil(t, c)
dt.ProvideInfo(types.Info{Swarm: swarm.Info{NodeID: ""}}, nil)

select {
case <-dt.LastSwarmInit():
t.Fail()
default:
}
_, err := New(ClientOption(dt.Client()))
require.Error(t, err, errSwarmNotInit)
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}

func TestFindContainerNonExistent(t *testing.T) {
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