From f00e6aa967a1e11d1058720120124b90cd6fe3af Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 14 Jul 2015 14:20:17 -0700 Subject: [PATCH] add events to mesos and a few tests Signed-off-by: Victor Vieux --- cluster/mesos/cluster.go | 11 +++++ cluster/mesos/driver.go | 3 ++ test/integration/api/build.bats | 13 ------ test/integration/api/info.bats | 4 +- test/integration/mesos/api.bats | 37 ---------------- test/integration/mesos/api/build.bats | 25 +++++++++++ test/integration/mesos/api/commit.bats | 35 +++++++++++++++ test/integration/mesos/api/events.bats | 40 +++++++++++++++++ test/integration/mesos/api/info.bats | 18 ++++++++ test/integration/mesos/api/run.bats | 52 +++++++++++++++++++++++ test/integration/mesos/mesos_helpers.bash | 2 +- 11 files changed, 187 insertions(+), 53 deletions(-) delete mode 100644 test/integration/mesos/api.bats create mode 100644 test/integration/mesos/api/build.bats create mode 100644 test/integration/mesos/api/commit.bats create mode 100644 test/integration/mesos/api/events.bats create mode 100644 test/integration/mesos/api/info.bats create mode 100644 test/integration/mesos/api/run.bats diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index 7c28cbe4e4..43673156d9 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -137,6 +137,17 @@ func NewCluster(scheduler *scheduler.Scheduler, store *state.Store, TLSConfig *t return cluster, nil } +// Handle callbacks for the events +func (c *Cluster) Handle(e *cluster.Event) error { + if c.eventHandler == nil { + return nil + } + if err := c.eventHandler.Handle(e); err != nil { + log.Error(err) + } + return nil +} + // RegisterEventHandler registers an event handler. func (c *Cluster) RegisterEventHandler(h cluster.EventHandler) error { if c.eventHandler != nil { diff --git a/cluster/mesos/driver.go b/cluster/mesos/driver.go index 0c5e626056..94d41c0afa 100644 --- a/cluster/mesos/driver.go +++ b/cluster/mesos/driver.go @@ -49,6 +49,9 @@ func (c *Cluster) ResourceOffers(_ mesosscheduler.SchedulerDriver, offers []*mes } else { s = newSlave(slaveID, engine) c.slaves[slaveID] = s + if err := s.engine.RegisterEventHandler(c); err != nil { + log.Error(err) + } } } c.addOffer(offer) diff --git a/test/integration/api/build.bats b/test/integration/api/build.bats index 37e415066b..ad601438c6 100644 --- a/test/integration/api/build.bats +++ b/test/integration/api/build.bats @@ -7,17 +7,4 @@ function teardown() { stop_docker } -@test "docker build" { - start_docker 2 - swarm_manage - run docker_swarm images -q - [ "$status" -eq 0 ] - [ "${#lines[@]}" -eq 0 ] - - docker_swarm build -t test $TESTDATA/build - - run docker_swarm images -q - [ "$status" -eq 0 ] - [ "${#lines[@]}" -eq 1 ] -} diff --git a/test/integration/api/info.bats b/test/integration/api/info.bats index 5ab4d0f7a0..6171b6a402 100644 --- a/test/integration/api/info.bats +++ b/test/integration/api/info.bats @@ -8,10 +8,10 @@ function teardown() { } @test "docker info" { - start_docker 1 --label foo=bar + start_docker 2 --label foo=bar swarm_manage run docker_swarm info [ "$status" -eq 0 ] - [[ "${output}" == *"Nodes: 1"* ]] + [[ "${output}" == *"Nodes: 2"* ]] [[ "${output}" == *"└ Labels:"*"foo=bar"* ]] } diff --git a/test/integration/mesos/api.bats b/test/integration/mesos/api.bats deleted file mode 100644 index 3b2f72deac..0000000000 --- a/test/integration/mesos/api.bats +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bats - -load mesos_helpers - -function teardown() { - swarm_manage_cleanup - stop_mesos - stop_docker -} - -@test "docker info" { - start_docker 2 - start_mesos - swarm_manage_mesos - run docker_swarm info - [ "$status" -eq 0 ] - [[ "${output}" == *'Offers: 2'* ]] -} - -@test "docker run no resources" { - start_docker 1 - start_mesos - swarm_manage_mesos - run docker_swarm run -d busybox ls - [ "$status" -ne 0 ] - [[ "${output}" == *'resources constraints (-c and/or -m) are required by mesos'* ]] -} - -@test "docker run" { - start_docker_with_busybox 1 - start_mesos - swarm_manage_mesos - docker_swarm run -d -m 20m busybox ls - docker_swarm run -d -m 20m busybox ls -} - - diff --git a/test/integration/mesos/api/build.bats b/test/integration/mesos/api/build.bats new file mode 100644 index 0000000000..5d74334ea7 --- /dev/null +++ b/test/integration/mesos/api/build.bats @@ -0,0 +1,25 @@ +#!/usr/bin/env bats + +load ../mesos_helpers + +function teardown() { + swarm_manage_cleanup + stop_mesos + stop_docker +} + +@test "mesos - docker build" { + start_docker 2 + start_mesos + swarm_manage_mesos + + run docker_swarm images -q + [ "$status" -eq 0 ] + [ "${#lines[@]}" -eq 0 ] + + docker_swarm build -t test $TESTDATA/build + + run docker_swarm images -q + [ "$status" -eq 0 ] + [ "${#lines[@]}" -eq 1 ] +} \ No newline at end of file diff --git a/test/integration/mesos/api/commit.bats b/test/integration/mesos/api/commit.bats new file mode 100644 index 0000000000..7f4d61fb68 --- /dev/null +++ b/test/integration/mesos/api/commit.bats @@ -0,0 +1,35 @@ +#!/usr/bin/env bats + +load ../mesos_helpers + +function teardown() { + swarm_manage_cleanup + stop_mesos + stop_docker +} + +@test "mesos - docker commit" { + start_docker_with_busybox 2 + start_mesos + swarm_manage_mesos + + docker_swarm run -d -m 20m --name test_container busybox sleep 500 + + # make sure container exists + run docker_swarm ps -l + [ "${#lines[@]}" -eq 2 ] + [[ "${lines[1]}" == *"test_container"* ]] + + # no comming name before commit + run docker_swarm images + [ "$status" -eq 0 ] + [[ "${output}" != *"commit_image_busybox"* ]] + + # commit container + docker_swarm commit test_container commit_image_busybox + + # verify after commit + run docker_swarm images + [ "$status" -eq 0 ] + [[ "${output}" == *"commit_image_busybox"* ]] +} diff --git a/test/integration/mesos/api/events.bats b/test/integration/mesos/api/events.bats new file mode 100644 index 0000000000..3f2671a426 --- /dev/null +++ b/test/integration/mesos/api/events.bats @@ -0,0 +1,40 @@ +#!/usr/bin/env bats + +load ../mesos_helpers + +function teardown() { + swarm_manage_cleanup + stop_mesos + stop_docker +} + +@test "docker events" { + start_docker_with_busybox 2 + start_mesos + swarm_manage_mesos + + # start events, report real time events to $log_file + local log_file=$(mktemp) + docker_swarm events > "$log_file" & + local events_pid="$!" + + # This should emit 3 events: create, start, die. + docker_swarm run -m 20m --name test_container -e constraint:node==node-0 busybox true + + # events might take a little big to show up, wait until we get the last one. + retry 5 0.5 grep -q "die" "$log_file" + + # clean up `docker events` + kill "$events_pid" + + # verify + run cat "$log_file" + [ "$status" -eq 0 ] + [[ "${output}" == *"node:node-0"* ]] + [[ "${output}" == *"create"* ]] + [[ "${output}" == *"start"* ]] + [[ "${output}" == *"die"* ]] + + # after ok, remove the log file + rm -f "$log_file" +} diff --git a/test/integration/mesos/api/info.bats b/test/integration/mesos/api/info.bats new file mode 100644 index 0000000000..2395688b30 --- /dev/null +++ b/test/integration/mesos/api/info.bats @@ -0,0 +1,18 @@ +#!/usr/bin/env bats + +load ../mesos_helpers + +function teardown() { + swarm_manage_cleanup + stop_mesos + stop_docker +} + +@test "mesos - docker info" { + start_docker 2 + start_mesos + swarm_manage_mesos + run docker_swarm info + [ "$status" -eq 0 ] + [[ "${output}" == *'Offers: 2'* ]] +} \ No newline at end of file diff --git a/test/integration/mesos/api/run.bats b/test/integration/mesos/api/run.bats new file mode 100644 index 0000000000..36bb281565 --- /dev/null +++ b/test/integration/mesos/api/run.bats @@ -0,0 +1,52 @@ +#!/usr/bin/env bats + +load ../mesos_helpers + +function teardown() { + swarm_manage_cleanup + stop_mesos + stop_docker +} + +@test "mesos - docker run" { + start_docker_with_busybox 2 + start_mesos + swarm_manage_mesos + + # make sure no container exist + run docker_swarm ps -qa + [ "${#lines[@]}" -eq 0 ] + + # run + docker_swarm run -m 20m -d --name test_container busybox sleep 100 + + # verify, container is running + [ -n $(docker_swarm ps -q --filter=name=test_container --filter=status=running) ] + + # error check + run docker_swarm run -m 20m -d 4e8aa3148a132f19ec560952231c4d39522043994df7d2dc239942c0f9424ebd + [[ "${output}" == *"cannot specify 64-byte hexadecimal strings"* ]] +} + +@test "mesos - docker run no resources" { + start_docker 1 + start_mesos + swarm_manage_mesos + + run docker_swarm run -d busybox ls + [ "$status" -ne 0 ] + [[ "${output}" == *'resources constraints (-c and/or -m) are required by mesos'* ]] +} + +@test "mesos - docker run big" { + start_docker_with_busybox 3 + start_mesos + swarm_manage_mesos + + for i in `seq 1 100`; do + docker_swarm run -d -m 20m busybox echo $i + done + + run docker_swarm ps -aq + [ "${#lines[@]}" -eq 100 ] +} \ No newline at end of file diff --git a/test/integration/mesos/mesos_helpers.bash b/test/integration/mesos/mesos_helpers.bash index e7558bdc93..6d5cb11e55 100644 --- a/test/integration/mesos/mesos_helpers.bash +++ b/test/integration/mesos/mesos_helpers.bash @@ -1,6 +1,6 @@ #!/bin/bash -load ../helpers +load ../../helpers MESOS_IMAGE=jimenez/mesos-dev:clang MESOS_MASTER_PORT=$(( ( RANDOM % 1000 ) + 10000 ))