package org.apache.mesos.elasticsearch.systemtest.base;

import com.containersol.minimesos.MesosCluster;
import com.containersol.minimesos.mesos.ClusterArchitecture;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.mesos.elasticsearch.systemtest.Configuration;
import org.apache.mesos.elasticsearch.systemtest.containers.AlpineContainer;
import org.apache.mesos.elasticsearch.systemtest.util.DockerUtil;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
 * Base test class which launches Mesos CLUSTER
 */
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public abstract class TestBase {

    protected static final Configuration TEST_CONFIG = new Configuration();

    protected static final ClusterArchitecture CLUSTER_ARCHITECTURE = new ClusterArchitecture.Builder()
            .withZooKeeper()
            .withMaster()
            .withSlave(TEST_CONFIG.getPortRanges()[0])
            .withSlave(TEST_CONFIG.getPortRanges()[1])
            .withSlave(TEST_CONFIG.getPortRanges()[2])
            .build();

    @ClassRule
    public static final MesosCluster CLUSTER = new MesosCluster(CLUSTER_ARCHITECTURE);

    @ClassRule
    public static final TestWatcher WATCHER = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            CLUSTER.stop();
        }
    };

    @BeforeClass
    public static void prepareCleanDockerEnvironment() {
        new DockerUtil(CLUSTER_ARCHITECTURE.dockerClient).killAllSchedulers();
        new DockerUtil(CLUSTER_ARCHITECTURE.dockerClient).killAllExecutors();

        // Completely wipe out old data dir, just in case there is any old data in there.
        String dataDir = org.apache.mesos.elasticsearch.scheduler.Configuration.DEFAULT_HOST_DATA_DIR;
        AlpineContainer alpineContainer = new AlpineContainer(CLUSTER_ARCHITECTURE.dockerClient, dataDir, dataDir, new String[]{"sh", "-c", "rm -rf " + dataDir + "/* ; sleep 9999 ;"});
        alpineContainer.start(TEST_CONFIG.getClusterTimeout());
        alpineContainer.remove();
    }

    @AfterClass
    public static void killAllContainers() {
        CLUSTER.stop();
        new DockerUtil(CLUSTER_ARCHITECTURE.dockerClient).killAllExecutors();
    }

    public static Configuration getTestConfig() {
        return TEST_CONFIG;
    }
}

@@ -662,16 +662,17 @@
 arnings(
+%7B
 %22URF_UNR
@@ -701,16 +701,34 @@
 D_FIELD%22
+, %22MS_PKGPROTECT%22%7D
 )%0Apublic
@@ -853,22 +853,16 @@
  static 
-final 
 ClusterA
@@ -872,16 +872,370 @@
 itecture
+ CLUSTER_ARCHITECTURE;%0A%0A    protected static MesosCluster CLUSTER;%0A%0A    @ClassRule%0A    public static final TestWatcher WATCHER = new TestWatcher() %7B%0A        @Override%0A        protected void failed(Throwable e, Description description) %7B%0A            CLUSTER.stop();%0A        %7D%0A    %7D;%0A%0A    @BeforeClass%0A    public static void reinitialiseCluster() %7B%0A       
  CLUSTER
@@ -1280,24 +1280,28 @@
 e.Builder()%0A
+    
             
@@ -1325,24 +1325,28 @@
             
+    
 .withMaster(
@@ -1343,24 +1343,28 @@
 ithMaster()%0A
+    
             
@@ -1410,32 +1410,36 @@
 0%5D)%0A            
+ 
+   
 .withSlave(TEST_
@@ -1465,16 +1465,20 @@
 s()%5B1%5D)%0A
+    
         
@@ -1536,16 +1536,20 @@
         
+   
+ 
 .build()
@@ -1554,60 +1554,15 @@
 ();%0A
-%0A
     
-@ClassRule%0A    public static final MesosCluster
+   
  CLU
@@ -1612,182 +1612,8 @@
 E);%0A
-%0A
-    @ClassRule%0A    public static final TestWatcher WATCHER = new TestWatcher() %7B%0A        @Override%0A        protected void failed(Throwable e, Description description) %7B%0A    
     
@@ -1626,38 +1626,30 @@
 USTER.st
-op(
+art(60
 );%0A    
-  
-  %7D%0A    %7D;
+%7D
 %0A%0A    @B

Fix static cluster reuse with a hack to force initialize the cluster on each test.