diff --git a/test/integration/api.bats b/test/integration/api.bats index 618a0ded2c..85ed9ce876 100644 --- a/test/integration/api.bats +++ b/test/integration/api.bats @@ -9,7 +9,7 @@ function teardown() { @test "docker info should return the number of nodes" { start_docker 3 - start_manager + swarm_manage run docker_swarm info [ "$status" -eq 0 ] [[ "${lines[3]}" == *"Nodes: 3" ]] @@ -17,7 +17,7 @@ function teardown() { @test "docker ps -n 3 should return the 3 last containers, including non running one" { start_docker 1 - start_manager + swarm_manage run docker_swarm run -d busybox sleep 42 run docker_swarm run -d busybox false run docker_swarm ps -n 3 @@ -34,7 +34,7 @@ function teardown() { @test "docker ps -l should return the last container, including non running one" { start_docker 1 - start_manager + swarm_manage run docker_swarm run -d busybox sleep 42 sleep 1 #sleep so the 2 containers don't start at the same second run docker_swarm run -d busybox true diff --git a/test/integration/constraints.bats b/test/integration/constraints.bats index 72a89fa872..047efaa789 100644 --- a/test/integration/constraints.bats +++ b/test/integration/constraints.bats @@ -9,7 +9,7 @@ function teardown() { @test "node constraint" { start_docker 2 - start_manager + swarm_manage run docker_swarm run --name c1 -e constraint:node==node-0 -d busybox:latest sh [ "$status" -eq 0 ] @@ -34,7 +34,7 @@ function teardown() { @test "label constraints" { start_docker 1 --label foo=a start_docker 1 --label foo=b - start_manager + swarm_manage run docker_swarm run --name c1 -e constraint:foo==a -d busybox:latest sh [ "$status" -eq 0 ] diff --git a/test/integration/helpers.bash b/test/integration/helpers.bash index 65f9b33ba3..d9d684769b 100644 --- a/test/integration/helpers.bash +++ b/test/integration/helpers.bash @@ -37,17 +37,60 @@ function wait_until_reachable() { } # Start the swarm manager in background. -function start_manager() { - ${SWARM_ROOT}/swarm manage -H $SWARM_HOST "$@" `join , ${HOSTS[@]}` & +function swarm_manage() { + local discovery + if [ $# -ge 0 ]; then + discovery=`join , ${HOSTS[@]}` + else + discovery="$@" + fi + + ${SWARM_ROOT}/swarm manage -H $SWARM_HOST $discovery & SWARM_PID=$! wait_until_reachable $SWARM_HOST } +# Start swarm join for every engine with the discovery as parameter +function swarm_join() { + local i=0 + for h in ${HOSTS[@]}; do + echo "Swarm join #${i}: $h $@" + ${SWARM_ROOT}/swarm join --addr=$h "$@" & + SWARM_JOIN_PID[$i]=$! + ((++i)) + done + wait_until_swarm_joined $i +} + +# Wait until a swarm instance joins the cluster. +# Parameter $1 is number of nodes to check. +function wait_until_swarm_joined { + local attempts=0 + local max_attempts=10 + + until [ $attempts -ge $max_attempts ]; do + run docker -H $SWARM_HOST info + if [[ "${lines[3]}" == *"Nodes: $1"* ]]; then + break + fi + echo "Checking if joined successfully for the $((++attempts)) time" >&2 + sleep 1 + done + [[ $attempts -lt $max_attempts ]] +} + # Stops the manager. -function stop_manager() { +function swarm_manage_cleanup() { kill $SWARM_PID } +# Clean up Swarm join processes +function swarm_join_cleanup() { + for pid in ${SWARM_JOIN_PID[@]}; do + kill $pid + done +} + # Run the docker CLI against swarm. function docker_swarm() { docker -H $SWARM_HOST "$@" diff --git a/test/integration/main.bats b/test/integration/main.bats index 754ef6fa48..d5b9cb398d 100644 --- a/test/integration/main.bats +++ b/test/integration/main.bats @@ -3,7 +3,7 @@ load helpers @test "test that create doesn't accept any arugments" { - run swarm create derpderpderp + run swarm create derpderpderp [ "$status" -ne 0 ] } diff --git a/test/integration/zk-discovery.bats b/test/integration/zk-discovery.bats new file mode 100644 index 0000000000..103caa8f48 --- /dev/null +++ b/test/integration/zk-discovery.bats @@ -0,0 +1,35 @@ +#!/usr/bin/env bats + +load helpers + +# Address on which Zookeeper will listen (random port between 7000 and 8000). +ZK_HOST=127.0.0.1:$(( ( RANDOM % 1000 ) + 7000 )) + +# Container name for integration test +ZK_CONTAINER_NAME=swarm_integration_zk + +function start_zk() { + docker run --name $ZK_CONTAINER_NAME -p $ZK_HOST:2181 -d jplock/zookeeper +} + +function stop_zk() { + docker rm -f -v $ZK_CONTAINER_NAME > /dev/null +} + +function teardown() { + swarm_join_cleanup + swarm_manage_cleanup + stop_docker + stop_zk +} + +@test "zk discovery should be working correctly" { + start_zk + start_docker 1 + + swarm_manage zk://${ZK_HOST}/test + swarm_join zk://${ZK_HOST}/test + + run docker_swarm info + [[ "${lines[3]}" == *"Nodes: 1"* ]] +}