/*
 * Copyright (c) 2014 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.spotify.helios.system;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.ContainerExit;
import com.spotify.docker.client.messages.HostConfig;
import com.spotify.helios.client.HeliosClient;

import org.junit.Test;

import java.security.SecureRandom;

import static com.spotify.helios.common.descriptors.HostStatus.Status.UP;
import static java.lang.Integer.toHexString;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class ReapingTest extends SystemTestBase {

  private final DockerClient docker;

  public ReapingTest() throws Exception {
    this.docker = getNewDockerClient();
  }

  @Test
  public void test() throws Exception {
    startDefaultMaster();

    final String id = "test-" + toHexString(new SecureRandom().nextInt());
    final String namespace = "helios-" + id;
    final String intruder1 = intruder(namespace);
    final String intruder2 = intruder(namespace);

    // Start a container in the agent namespace
    startContainer(intruder1);

    // Start agent
    final HeliosClient client = defaultClient();
    startDefaultAgent(testHost(), "--id=" + id);
    awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

    int expectedExitCode = -1;
    if (docker.info().executionDriver().startsWith("lxc-")) {
      // with LXC, killing a container results in exit code 0
      expectedExitCode = 0;
    }

    // Wait for the agent to kill the container
    final ContainerExit exit1 = docker.waitContainer(intruder1);
    assertThat(exit1.statusCode(), is(expectedExitCode));

    // Start another container in the agent namespace
    startContainer(intruder2);

    // Wait for the agent to kill the second container as well
    final ContainerExit exit2 = docker.waitContainer(intruder2);
    assertThat(exit2.statusCode(), is(expectedExitCode));
  }

  private String intruder(final String namespace) {
    return namespace + "-unwanted-" + toHexString(new SecureRandom().nextInt()) + "-" + testTag;
  }

  private void startContainer(final String name)
      throws DockerException, InterruptedException {
    docker.pull(BUSYBOX);
    final ContainerConfig config = ContainerConfig.builder()
        .image(BUSYBOX)
        .cmd(IDLE_COMMAND)
        .build();
    final HostConfig hostConfig = HostConfig.builder().build();
    final ContainerCreation creation = docker.createContainer(config, name);
    final String containerId = creation.id();
    docker.startContainer(containerId, hostConfig);
  }
}

@@ -1309,16 +1309,39 @@
 eRandom;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1453,16 +1453,55 @@
 String;%0A
+import static java.util.Arrays.asList;%0A
 import s
@@ -1576,18 +1576,21 @@
 est.
-core.I
+Matcher
 s.is
+In
 ;%0Aim
@@ -2455,43 +2455,206 @@
     
-int expectedExitC
+// With LXC, killing a container results in exit code 0.%0A    // In docker 1.5 killing a container results in exit c
 ode 
-= -1;
+137, in previous versions it's -1.
 %0A    
-if (
+final List%3CInteger%3E expectedExitCodes = 
 dock
@@ -2703,107 +2703,83 @@
 c-%22)
-) %7B
+ ?
 %0A      
-// with LXC, killing a container results in exit code 0%0A      expectedExitCode = 0;%0A    %7D
+                                      asList(0) : asList(-1, 137);
 %0A%0A  
@@ -2918,32 +2918,34 @@
 statusCode(), is
+In
 (expectedExitCod
@@ -2941,24 +2941,25 @@
 ctedExitCode
+s
 ));%0A%0A    // 
@@ -3202,16 +3202,18 @@
 de(), is
+In
 (expecte
@@ -3221,16 +3221,17 @@
 ExitCode
+s
 ));%0A  %7D%0A

Make ReapingTest work with docker 1.5