From 28a358d9b2ebed248808afbba63b174ca866930b Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 23 Apr 2015 15:43:30 -0700 Subject: [PATCH] use docker 1.6, add affinity tests and add doc Signed-off-by: Victor Vieux --- scheduler/filter/README.md | 29 ++++++++++++++++- scheduler/filter/expr_test.go | 8 ++--- test/integration/affinities.bats | 56 ++++++++++++++++++++++++++++++++ test/integration/helpers.bash | 4 +-- 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 test/integration/affinities.bats diff --git a/scheduler/filter/README.md b/scheduler/filter/README.md index dd98b0ef15..dc66b44324 100644 --- a/scheduler/filter/README.md +++ b/scheduler/filter/README.md @@ -101,7 +101,7 @@ without specifying them when starting the node. Those tags are sourced from #### Containers -You can schedule 2 containers and make the container #2 next to the container #1. +You can schedule 2 containers and make the container #2 next to the container #1 using it's name or ID. ```bash $ docker run -d -p 80:80 --name front nginx @@ -164,6 +164,33 @@ CONTAINER ID IMAGE COMMAND CREATED As you can see here, the containers were only scheduled on nodes with the `redis` image already pulled. +#### Labels + +You can schedule 2 containers and make the container #2 next to the container #1 using it's labels. + +```bash +$ docker run -d -p 80:80 --label com.example.type=front nginx + 87c4376856a8 + +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NODE NAMES +87c4376856a8 nginx:latest "nginx" Less than a second ago running 192.168.0.42:80->80/tcp node-1 trusting_yonath +``` + +Using `-e affinity:com.example.type==front` will schedule a container next to the container with label `front`. + +```bash +$ docker run -d -e affinity:com.example.type==front logger + 87c4376856a8 + +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NODE NAMES +87c4376856a8 nginx:latest "nginx" Less than a second ago running 192.168.0.42:80->80/tcp node-1 trusting_yonath +963841b138d8 logger:latest "logger" Less than a second ago running node-1 happy_hawking +``` + +The `logger` container ends up on `node-1` because its affinity with the container `front`. + #### Expression Syntax An affinity or a constraint expression consists of a `key` and a `value`. diff --git a/scheduler/filter/expr_test.go b/scheduler/filter/expr_test.go index 672909d858..87e960d9a1 100644 --- a/scheduler/filter/expr_test.go +++ b/scheduler/filter/expr_test.go @@ -15,14 +15,14 @@ func TestParseExprs(t *testing.T) { _, err = parseExprs("constraint", []string{"constraint:node ==node1"}) assert.Error(t, err) - // Cannot use dot in key - _, err = parseExprs("constraint", []string{"constraint:no.de==node1"}) - assert.Error(t, err) - // Cannot use * in key _, err = parseExprs("constraint", []string{"constraint:no*de==node1"}) assert.Error(t, err) + // Allow dot in key + _, err = parseExprs("constraint", []string{"constraint:no.de==node1"}) + assert.NoError(t, err) + // Allow leading underscore _, err = parseExprs("constraint", []string{"constraint:_node==_node1"}) assert.NoError(t, err) diff --git a/test/integration/affinities.bats b/test/integration/affinities.bats new file mode 100644 index 0000000000..864ad3a8d7 --- /dev/null +++ b/test/integration/affinities.bats @@ -0,0 +1,56 @@ +#!/usr/bin/env bats + +load helpers + +function teardown() { + swarm_manage_cleanup + stop_docker +} + +@test "container affinty" { + start_docker 2 + swarm_manage + + run docker_swarm run --name c1 -e constraint:node==node-0 -d busybox:latest sh + [ "$status" -eq 0 ] + run docker_swarm run --name c2 -e affinity:container==c1 -d busybox:latest sh + [ "$status" -eq 0 ] + run docker_swarm run --name c3 -e affinity:container!=c1 -d busybox:latest sh + [ "$status" -eq 0 ] + + run docker_swarm inspect c1 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-0"'* ]] + + run docker_swarm inspect c2 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-0"'* ]] + + run docker_swarm inspect c3 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-1"'* ]] +} + +@test "label affinity" { + start_docker 2 + swarm_manage + + run docker_swarm run --name c1 --label test.label=true -e constraint:node==node-0 -d busybox:latest sh + [ "$status" -eq 0 ] + run docker_swarm run --name c2 -e affinity:test.label==true -d busybox:latest sh + [ "$status" -eq 0 ] + run docker_swarm run --name c3 -e affinity:test.label!=true -d busybox:latest sh + [ "$status" -eq 0 ] + + run docker_swarm inspect c1 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-0"'* ]] + + run docker_swarm inspect c2 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-0"'* ]] + + run docker_swarm inspect c3 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-1"'* ]] +} diff --git a/test/integration/helpers.bash b/test/integration/helpers.bash index 9984afe13a..779181a092 100644 --- a/test/integration/helpers.bash +++ b/test/integration/helpers.bash @@ -4,8 +4,8 @@ SWARM_ROOT=${SWARM_ROOT:-${BATS_TEST_DIRNAME}/../..} # Docker image and version to use for integration tests. -DOCKER_IMAGE=${DOCKER_IMAGE:-aluzzardi/docker} -DOCKER_VERSION=${DOCKER_VERSION:-1.5} +DOCKER_IMAGE=${DOCKER_IMAGE:-dockerswarm/docker} +DOCKER_VERSION=${DOCKER_VERSION:-1.6} # Host on which the manager will listen to (random port between 6000 and 7000). SWARM_HOST=127.0.0.1:$(( ( RANDOM % 1000 ) + 6000 ))