package brooklyn.entity.database.rubyrep;


import brooklyn.config.BrooklynProperties;
import brooklyn.entity.database.DatabaseNode;
import brooklyn.entity.database.postgresql.PostgreSqlIntegrationTest;
import brooklyn.entity.database.postgresql.PostgreSqlNode;
import brooklyn.entity.proxying.BasicEntitySpec;
import brooklyn.location.basic.LocationRegistry;
import brooklyn.location.basic.SshMachineLocation;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.Test;

import static java.util.Arrays.asList;

/**
 * The RubyRepRackspaceLiveTest installs RubyRep on various operating systems like Ubuntu, CentOS, Red Hat etc. To make sure that
 * RubyRep and PostgreSql works like expected on these Operating Systems.
 */
public class RubyRepRackspaceLiveTest extends RubyRepIntegrationTest {
    
    @Test(groups = "Live")
    public void test_Debian_6() throws Exception {
        test("Debian 6");
    }

    @Test(groups = "Live")
    public void test_Ubuntu_10_0() throws Exception {
        test("Ubuntu 10.0");
    }

    @Test(groups = "Live")
    public void test_Ubuntu_11_0() throws Exception {
        test("Ubuntu 11.0");
    }

    @Test(groups = "Live")
    public void test_Ubuntu_12_0() throws Exception {
        test("Ubuntu 12.0");
    }

    @Test(groups = "Live")
    public void test_CentOS_6_0() throws Exception {
        test("CentOS 6.0");
    }

    @Test(groups = "Live")
    public void test_CentOS_5_6() throws Exception {
        test("CentOS 5.6");
    }

    @Test(groups = "Live")
    public void test_Fedora_17() throws Exception {
        test("Fedora 17");
    }

    @Test(groups = "Live")
    public void test_Red_Hat_Enterprise_Linux_6() throws Exception {
        test("Red Hat Enterprise Linux 6");
    }

    public void test(String osRegex) throws Exception {
        PostgreSqlNode db1 = tapp.createAndManageChild(BasicEntitySpec.newInstance(PostgreSqlNode.class)
                .configure("creationScriptContents", PostgreSqlIntegrationTest.CREATION_SCRIPT)
                .configure("port", 9111));
        PostgreSqlNode db2 = tapp.createAndManageChild(BasicEntitySpec.newInstance(PostgreSqlNode.class)
                .configure("creationScriptContents", PostgreSqlIntegrationTest.CREATION_SCRIPT)
                .configure("port", 9111));

        BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newDefault();
        brooklynProperties.put("brooklyn.jclouds.cloudservers-uk.image-name-regex", osRegex);
        brooklynProperties.remove("brooklyn.jclouds.cloudservers-uk.image-id");
        brooklynProperties.put("inboundPorts", new int[] {22, 9111});
        LocationRegistry locationRegistry = new LocationRegistry(brooklynProperties);
        
        startInLocation(tapp, db1, db2, locationRegistry.resolve("cloudservers-uk"));

        //hack to get the port for mysql open; is the inbounds property not respected on rackspace??
        for (DatabaseNode node : ImmutableSet.of(db1, db2)) {
            SshMachineLocation l = (SshMachineLocation) node.getLocations().iterator().next();
            l.exec(asList("iptables -I INPUT -p tcp --dport 9111 -j ACCEPT"));
        }
        
        testReplication(db1, db2);
    }
}

@@ -37,16 +37,93 @@
 yrep;%0A%0A%0A
+import static java.util.Arrays.asList;%0A%0Aimport org.testng.annotations.Test;%0A%0A
 import b
@@ -481,16 +481,17 @@
 cation;%0A
+%0A
 import c
@@ -532,84 +532,8 @@
 Set;
-%0Aimport org.testng.annotations.Test;%0A%0Aimport static java.util.Arrays.asList;
 %0A%0A/*
@@ -3227,10 +3227,620 @@
 ;%0A    %7D%0A
+    %0A    // disable inherited non-live tests%0A    @Test(enabled = false, groups = %22Integration%22)%0A    public void test_localhost_mysql() throws Exception %7B%0A        super.test_localhost_mysql();%0A    %7D%0A%0A    // disable inherited non-live tests%0A    @Test(enabled = false, groups = %22Integration%22)%0A    public void test_localhost_postgres() throws Exception %7B%0A        super.test_localhost_postgres();%0A    %7D%0A%0A    // disable inherited non-live tests%0A    @Test(enabled = false, groups = %22Integration%22)%0A    public void test_localhost_postgres_mysql() throws Exception %7B%0A        super.test_localhost_postgres_mysql();%0A    %7D%0A
 %7D%0A

disable inherited integration tests