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

Change namespace from string slice to string #1151

Merged
merged 5 commits into from
Jul 2, 2019
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
6 changes: 3 additions & 3 deletions container/build_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
)

func TestIntegrationBuild(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
tag, err := c.Build("test/")
require.NoError(t, err)
require.NotEqual(t, "", tag)
}

func TestIntegrationBuildNotWorking(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
tag, err := c.Build("test-not-valid/")
require.Error(t, err)
require.Equal(t, "", tag)
}

func TestIntegrationBuildWrongPath(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
_, err = c.Build("testss/")
require.Error(t, err)
Expand Down
40 changes: 18 additions & 22 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
docker "github.com/docker/docker/client"
"github.com/mesg-foundation/engine/config"
)

var (
Expand All @@ -19,19 +18,19 @@ var (
// Container describes the API of container package.
type Container interface {
Build(path string) (tag string, err error)
CreateNetwork(namespace []string) (id string, err error)
DeleteNetwork(namespace []string) error
FindContainer(namespace []string) (types.ContainerJSON, error)
FindNetwork(namespace []string) (types.NetworkResource, error)
FindService(namespace []string) (swarm.Service, error)
CreateNetwork(namespace string) (id string, err error)
DeleteNetwork(namespace string) error
FindContainer(namespace string) (types.ContainerJSON, error)
FindNetwork(namespace string) (types.NetworkResource, error)
FindService(namespace string) (swarm.Service, error)
ListServices(labels ...string) ([]swarm.Service, error)
ListTasks(namespace []string) ([]swarm.Task, error)
Namespace(ss []string) string
ListTasks(namespace string) ([]swarm.Task, error)
Namespace(namespace string) string
SharedNetworkID() (networkID string, err error)
StartService(options ServiceOptions) (serviceID string, err error)
Status(namespace []string) (StatusType, error)
StopService(namespace []string) (err error)
TasksError(namespace []string) ([]string, error)
Status(namespace string) (StatusType, error)
StopService(namespace string) (err error)
TasksError(namespace string) ([]string, error)
DeleteVolume(name string) error
}

Expand All @@ -46,27 +45,24 @@ type DockerContainer struct {
// defaultStopGracePeriod is the timeout value between stopping a container and killing it.
defaultStopGracePeriod time.Duration

config *config.Config
// namespace prefix.
nsprefix string
}

// Option is a configuration func for Container.
type Option func(*DockerContainer)

// New creates a new Container with given options.
func New(options ...Option) (*DockerContainer, error) {
func New(nsprefix string, options ...Option) (*DockerContainer, error) {
c := &DockerContainer{
callTimeout: 60 * time.Second,
defaultStopGracePeriod: 10 * time.Second, // default docker value
nsprefix: nsprefix,
}
for _, option := range options {
option(c)
}
var err error
cfg, err := config.Global()
if err != nil {
return nil, err
}
c.config = cfg
if c.client == nil {
c.client, err = docker.NewClientWithOpts(docker.FromEnv)
if err != nil {
Expand Down Expand Up @@ -114,7 +110,7 @@ func (c *DockerContainer) isSwarmInit() error {
}

// FindContainer returns a docker container.
func (c *DockerContainer) FindContainer(namespace []string) (types.ContainerJSON, error) {
func (c *DockerContainer) FindContainer(namespace string) (types.ContainerJSON, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout)
defer cancel()
containers, err := c.client.ContainerList(ctx, types.ContainerListOptions{
Expand Down Expand Up @@ -142,7 +138,7 @@ func (c *DockerContainer) FindContainer(namespace []string) (types.ContainerJSON
// - RUNNING: when the container is running in docker regardless of the status of the service.
// - STARTING: when the service is running but the container is not yet started.
// - STOPPED: when the container and the service is not running in docker.
func (c *DockerContainer) Status(namespace []string) (StatusType, error) {
func (c *DockerContainer) Status(namespace string) (StatusType, error) {
container, err := c.containerExists(namespace)
if err != nil {
return UNKNOWN, err
Expand Down Expand Up @@ -172,13 +168,13 @@ func (c *DockerContainer) Status(namespace []string) (StatusType, error) {
}

// containerExists checks if container with namespace can be found.
func (c *DockerContainer) containerExists(namespace []string) (bool, error) {
func (c *DockerContainer) containerExists(namespace string) (bool, error) {
_, err := c.FindContainer(namespace)
return presenceHandling(err)
}

// serviceExists checks if corresponding container for service namespace can be found.
func (c *DockerContainer) serviceExists(namespace []string) (bool, error) {
func (c *DockerContainer) serviceExists(namespace string) (bool, error) {
_, err := c.FindService(namespace)
return presenceHandling(err)
}
Expand Down
24 changes: 12 additions & 12 deletions container/container_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
)

func TestIntegrationFindContainerNotExisting(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
_, err = c.FindContainer([]string{"TestFindContainerNotExisting"})
_, err = c.FindContainer("TestFindContainerNotExisting")
require.Error(t, err)
}

func TestIntegrationFindContainer(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
namespace := []string{"TestFindContainer"}
namespace := "TestFindContainer"
startTestService(namespace)
defer c.StopService(namespace)
c.waitForStatus(namespace, RUNNING)
Expand All @@ -28,28 +28,28 @@ func TestIntegrationFindContainer(t *testing.T) {
}

func TestIntegrationFindContainerStopped(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
namespace := []string{"TestFindContainerStopped"}
namespace := "TestFindContainerStopped"
startTestService(namespace)
c.StopService(namespace)
_, err = c.FindContainer(namespace)
require.Error(t, err)
}

func TestIntegrationContainerStatusNeverStarted(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
namespace := []string{"TestContainerStatusNeverStarted"}
namespace := "TestContainerStatusNeverStarted"
status, err := c.Status(namespace)
require.NoError(t, err)
require.Equal(t, status, STOPPED)
}

func TestIntegrationContainerStatusRunning(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
namespace := []string{"TestContainerStatusRunning"}
namespace := "TestContainerStatusRunning"
startTestService(namespace)
defer c.StopService(namespace)
c.waitForStatus(namespace, RUNNING)
Expand All @@ -59,9 +59,9 @@ func TestIntegrationContainerStatusRunning(t *testing.T) {
}

func TestIntegrationContainerStatusStopped(t *testing.T) {
c, err := New()
c, err := New(nstestprefix)
require.NoError(t, err)
namespace := []string{"TestContainerStatusStopped"}
namespace := "TestContainerStatusStopped"
startTestService(namespace)
c.waitForStatus(namespace, RUNNING)
c.StopService(namespace)
Expand Down
34 changes: 17 additions & 17 deletions container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"

"github.com/mesg-foundation/engine/config"
"github.com/mesg-foundation/engine/container/dockertest"
"github.com/mesg-foundation/engine/utils/docker/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

const nstestprefix = "enginetest"

func newTesting(t *testing.T) (*DockerContainer, *mocks.CommonAPIClient) {
m := &mocks.CommonAPIClient{}
mockNew(m)

c, err := New(ClientOption(m))
c, err := New(nstestprefix, ClientOption(m))
require.NoError(t, err)
require.NotZero(t, c)

Expand All @@ -35,7 +36,7 @@ func mockNew(m *mocks.CommonAPIClient) {
}
networkInspectArguments = []interface{}{
mock.Anything,
"engine",
nstestprefix,
types.NetworkInspectOptions{},
}
networkInspectResponse = types.NetworkResource{
Expand Down Expand Up @@ -99,8 +100,7 @@ func mockStatus(t *testing.T, m *mocks.CommonAPIClient, namespace string, wanted

func TestNew(t *testing.T) {
dt := dockertest.New()
c, err := New(ClientOption(dt.Client()))
cfg, _ := config.Global()
c, err := New(nstestprefix, ClientOption(dt.Client()))
require.NoError(t, err)
require.NotNil(t, c)

Expand All @@ -117,7 +117,7 @@ func TestNew(t *testing.T) {
}

ln := <-dt.LastNetworkCreate()
require.Equal(t, cfg.Name, ln.Name)
require.Equal(t, nstestprefix, ln.Name)
require.Equal(t, types.NetworkCreate{
CheckDuplicate: true,
Driver: "overlay",
Expand All @@ -131,15 +131,15 @@ func TestNewSwarmError(t *testing.T) {
dt := dockertest.New()
dt.ProvideInfo(types.Info{Swarm: swarm.Info{NodeID: ""}}, nil)

_, err := New(ClientOption(dt.Client()))
_, err := New(nstestprefix, ClientOption(dt.Client()))
require.Equal(t, err, errSwarmNotInit)
}

func TestFindContainerNonExistent(t *testing.T) {
namespace := []string{"namespace"}
namespace := "namespace"

dt := dockertest.New()
c, _ := New(ClientOption(dt.Client()))
c, _ := New(nstestprefix, ClientOption(dt.Client()))

dt.ProvideContainerList(nil, dockertest.NotFoundErr{})

Expand All @@ -156,7 +156,7 @@ func TestFindContainerNonExistent(t *testing.T) {
}

func TestFindContainer(t *testing.T) {
namespace := []string{"TestFindContainer"}
namespace := "namespace"
containerID := "1"
containerData := []types.Container{
{ID: containerID},
Expand All @@ -168,7 +168,7 @@ func TestFindContainer(t *testing.T) {
}

dt := dockertest.New()
c, _ := New(ClientOption(dt.Client()))
c, _ := New(nstestprefix, ClientOption(dt.Client()))

dt.ProvideContainerList(containerData, nil)
dt.ProvideContainerInspect(containerJSONData, nil)
Expand All @@ -189,10 +189,10 @@ func TestFindContainer(t *testing.T) {
}

func TestNonExistentContainerStatus(t *testing.T) {
namespace := []string{"namespace"}
namespace := "namespace"

dt := dockertest.New()
c, _ := New(ClientOption(dt.Client()))
c, _ := New(nstestprefix, ClientOption(dt.Client()))

dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, dockertest.NotFoundErr{})
dt.ProvideContainerInspect(types.ContainerJSON{}, dockertest.NotFoundErr{})
Expand All @@ -207,7 +207,7 @@ func TestNonExistentContainerStatus(t *testing.T) {
}

func TestExistentContainerStatus(t *testing.T) {
namespace := []string{"namespace"}
namespace := "namespace"
containerID := "1"
containerData := []types.Container{
{ID: containerID},
Expand All @@ -220,7 +220,7 @@ func TestExistentContainerStatus(t *testing.T) {
}

dt := dockertest.New()
c, _ := New(ClientOption(dt.Client()))
c, _ := New(nstestprefix, ClientOption(dt.Client()))

dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, nil)
dt.ProvideContainerList(containerData, nil)
Expand All @@ -232,7 +232,7 @@ func TestExistentContainerStatus(t *testing.T) {
}

func TestExistentContainerRunningStatus(t *testing.T) {
namespace := []string{"namespace"}
namespace := "namespace"
containerID := "1"
containerData := []types.Container{
{ID: containerID},
Expand All @@ -245,7 +245,7 @@ func TestExistentContainerRunningStatus(t *testing.T) {
}

dt := dockertest.New()
c, _ := New(ClientOption(dt.Client()))
c, _ := New(nstestprefix, ClientOption(dt.Client()))

dt.ProvideContainerList(containerData, nil)
dt.ProvideContainerInspect(containerJSONData, nil)
Expand Down
Loading