Skip to content

Commit

Permalink
Allows specifying a custom image name for nodes (#7)
Browse files Browse the repository at this point in the history
* Add a new flag 'image' to the command create

Signed-off-by: Damien DUPORTAL <[email protected]>

* Move default image spec out of CLI + tests

Signed-off-by: Damien DUPORTAL <[email protected]>

* fix: semaphore build use ./toolbox

* use go1.12

* attempt to fix CI
  • Loading branch information
dduportal authored and jlevesy committed Mar 13, 2019
1 parent 0c9721f commit 6396f2c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
23 changes: 17 additions & 6 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,40 @@ agent:
blocks:
- name: Install dependencies
task:
secrets:
- sind-gh-token
prologue:
commands:
- sudo curl -sfL https://raw.githubusercontent.com/mmatur/checkout-semaphoreci2/master/godownloader.sh | sudo bash -s -- -b $GOPATH/bin v0.0.3
- checkout-semaphoreci2 --owner=jlevesy --repo=go-sind --required.pr && CHECKOUT_EXIT=$?
- if [ ${CHECKOUT_EXIT} -eq 0 ]; then echo OK; else exit 0; fi
- cache restore go-mod-$SEMAPHORE_GIT_BRANCH-$(checksum go.sum),go-mod-$SEMAPHORE_GIT_BRANCH,go-mod-master
- make toolbox
jobs:
- name: make vendor and cache
commands:
- checkout
- make toolbox
- cache restore go-mod-$SEMAPHORE_GIT_BRANCH-$(checksum go.sum),go-mod-$SEMAPHORE_GIT_BRANCH,go-mod-master
- ./tool/box make vendor
- ./toolbox make vendor
- cache store go-mod-$SEMAPHORE_GIT_BRANCH-$(checksum go.sum) ./.gocache

- name: Test
task:
secrets:
- sind-gh-token
prologue:
commands:
- checkout
- sudo curl -sfL https://raw.githubusercontent.com/mmatur/checkout-semaphoreci2/master/godownloader.sh | sudo bash -s -- -b $GOPATH/bin v0.0.3
- checkout-semaphoreci2 --owner=jlevesy --repo=go-sind --required.pr && CHECKOUT_EXIT=$?
- if [ ${CHECKOUT_EXIT} -eq 0 ]; then echo OK; else exit 0; fi
- cache restore go-mod-$SEMAPHORE_GIT_BRANCH-$(checksum go.sum),go-mod-$SEMAPHORE_GIT_BRANCH,go-mod-master
- make toolbox
jobs:
- name: Lint
commands:
- tool/box make lint
- ./toolbox make lint
- name: Unit
commands:
- tool/box make unit_test
- ./toolbox make unit_test
- name: Integration
commands:
- make integration_test
2 changes: 1 addition & 1 deletion Dockerfile.toolbox
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11.5-stretch
FROM golang:1.12-stretch

ARG UID
ARG GID
Expand Down
13 changes: 8 additions & 5 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"

"github.com/jlevesy/go-sind/sind"
)

var (
managers int
workers int
networkName string
portsMapping []string
managers int
workers int
networkName string
portsMapping []string
nodeImageName string

createCmd = &cobra.Command{
Use: "create",
Expand All @@ -29,6 +31,7 @@ func init() {
createCmd.Flags().IntVarP(&workers, "workers", "w", 0, "Amount of workers in the created cluster.")
createCmd.Flags().StringVarP(&networkName, "network_name", "n", "sind_default", "Name of the network to create.")
createCmd.Flags().StringSliceVarP(&portsMapping, "ports", "p", []string{}, "Ingress network port binding.")
createCmd.Flags().StringVarP(&nodeImageName, "image", "i", "docker:18.09-dind", "Name of the image to use for the nodes.")
}

func runCreate(cmd *cobra.Command, args []string) {
Expand Down
36 changes: 33 additions & 3 deletions integration/sind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/jlevesy/go-sind/sind"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)

func TestSindCanCreateACluster(t *testing.T) {
t.Parallel()
ctx := context.Background()
t.Log("Creating cluster")
params := sind.CreateClusterParams{ClusterName: "test_swarm", NetworkName: "test_swarm", Managers: 3, Workers: 4}
Expand Down Expand Up @@ -53,8 +53,39 @@ func TestSindCanCreateACluster(t *testing.T) {
}
}

func TestSindCanCreateAClusterWithCustomImage(t *testing.T) {
ctx := context.Background()
t.Log("Creating cluster with custom image name")

params := sind.CreateClusterParams{ClusterName: "test_swarm", NetworkName: "test_swarm", Managers: 3, Workers: 4, ImageName: "docker:dind"}
cluster, err := sind.CreateCluster(ctx, params)
if err != nil {
t.Fatalf("unable to create cluster: %v", err)
}

defer func() {
if err = cluster.Delete(ctx); err != nil {
t.Fatalf("unable to delete cluster: %v", err)
}
}()

dockerCli, err := cluster.Host.Client()
if err != nil {
t.Fatalf("unable to get a docker client to the host: %v", err)
}

listFilters := filters.NewArgs(filters.Arg("ancestor", params.ImageName), filters.Arg("status", "running"))
runningContainers, err := dockerCli.ContainerList(ctx, types.ContainerListOptions{Filters: listFilters})
if err != nil {
t.Fatalf("unable to retrieve a list of running containers on the host: %v", err)
}

if len(runningContainers) != params.Managers+params.Workers {
t.Errorf("invalid amount of running containers based on the image %s. Expected: %d, Got: %d.", params.ImageName, params.Managers+params.Workers, len(runningContainers))
}
}

func TestSindCanCreateMultipleClusters(t *testing.T) {
t.Parallel()
ctx := context.Background()
for i := 0; i < 3; i++ {
t.Log("Creating cluster n°", i)
Expand All @@ -78,7 +109,6 @@ func TestSindCanCreateMultipleClusters(t *testing.T) {
}

func TestSindCanPushAnImageToCluster(t *testing.T) {
t.Parallel()
ctx := context.Background()
tag := "alpine:latest"

Expand Down
8 changes: 6 additions & 2 deletions sind/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ const (
)

const (
dockerDINDimage = "docker:dind"
defaultSwarmListenAddr = "0.0.0.0:2377"
)

const (
// DefaultNodeImageName is the default image name to use for creating swarm nodes.
DefaultNodeImageName = "docker:18.09-dind"
)

// CreateClusterParams are args to pass to CreateCluster.
type CreateClusterParams struct {
ClusterName string
Expand Down Expand Up @@ -74,7 +78,7 @@ func (n *CreateClusterParams) imageName() string {
return n.ImageName
}

return dockerDINDimage
return DefaultNodeImageName
}

// CreateCluster creates a new swarm cluster.
Expand Down

0 comments on commit 6396f2c

Please sign in to comment.