MySqlCluster - tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/937bc2ea Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/937bc2ea Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/937bc2ea Branch: refs/heads/master Commit: 937bc2eaedd8c7634e5c45be5ceb27132be3026f Parents: 51deeba Author: Svetoslav Neykov <[email protected]> Authored: Fri Jul 31 10:37:17 2015 +0300 Committer: Svetoslav Neykov <[email protected]> Committed: Wed Aug 5 15:23:07 2015 +0300 ---------------------------------------------------------------------- .../entity/database/VogellaExampleAccess.java | 1 + .../mysql/MySqlClusterIntegrationTest.java | 44 +++++++ .../database/mysql/MySqlClusterLiveEc2Test.java | 42 +++++++ .../mysql/MySqlClusterLiveSoftlayerTest.java | 38 ++++++ .../database/mysql/MySqlClusterTestHelper.java | 115 +++++++++++++++++++ 5 files changed, 240 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/937bc2ea/software/database/src/test/java/brooklyn/entity/database/VogellaExampleAccess.java ---------------------------------------------------------------------- diff --git a/software/database/src/test/java/brooklyn/entity/database/VogellaExampleAccess.java b/software/database/src/test/java/brooklyn/entity/database/VogellaExampleAccess.java index 962a294..4916c7c 100644 --- a/software/database/src/test/java/brooklyn/entity/database/VogellaExampleAccess.java +++ b/software/database/src/test/java/brooklyn/entity/database/VogellaExampleAccess.java @@ -105,6 +105,7 @@ public class VogellaExampleAccess { preparedStatement.executeUpdate(); writeResultSet(readDataBase()); + preparedStatement.close(); } // Remove again the insert comment added by modifyDataBase() http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/937bc2ea/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterIntegrationTest.java ---------------------------------------------------------------------- diff --git a/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterIntegrationTest.java b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterIntegrationTest.java new file mode 100644 index 0000000..037d93a --- /dev/null +++ b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterIntegrationTest.java @@ -0,0 +1,44 @@ +/* + * 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 brooklyn.entity.database.mysql; + +import org.testng.annotations.Test; + +import com.google.common.collect.Iterables; + +import brooklyn.entity.BrooklynAppLiveTestSupport; +import brooklyn.entity.Entity; +import brooklyn.util.os.Os; + +public class MySqlClusterIntegrationTest extends BrooklynAppLiveTestSupport { + + @Test(groups = {"Integration"}) + public void test_localhost() throws Exception { + try { + MySqlClusterTestHelper.test(app, mgmt.getLocationRegistry().resolve("localhost")); + } finally { + for (Entity member : Iterables.getOnlyElement(app.getChildren()).getChildren()) { + String runDir = member.getAttribute(MySqlNode.RUN_DIR); + if (runDir != null) { + Os.deleteRecursively(runDir); + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/937bc2ea/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveEc2Test.java ---------------------------------------------------------------------- diff --git a/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveEc2Test.java b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveEc2Test.java new file mode 100644 index 0000000..b2adfbd --- /dev/null +++ b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveEc2Test.java @@ -0,0 +1,42 @@ +/* + * 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 brooklyn.entity.database.mysql; + +import org.testng.annotations.Test; + +import brooklyn.entity.AbstractEc2LiveTest; +import brooklyn.location.Location; + +@Test(groups = { "Live" }) +public class MySqlClusterLiveEc2Test extends AbstractEc2LiveTest { + + @Override + protected void doTest(Location loc) throws Exception { + MySqlClusterTestHelper.test(app, loc); + } + + @Override + @Test(enabled=false, groups = "Live") + public void test_Debian_7_2() throws Exception { } // Disabled because MySQl not available + + @Test(enabled=false) + public void testDummy() {} // Convince testng IDE integration that this really does have test methods + +} + http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/937bc2ea/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveSoftlayerTest.java ---------------------------------------------------------------------- diff --git a/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveSoftlayerTest.java b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveSoftlayerTest.java new file mode 100644 index 0000000..cb26f1e --- /dev/null +++ b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterLiveSoftlayerTest.java @@ -0,0 +1,38 @@ +/* + * 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 brooklyn.entity.database.mysql; + +import org.testng.annotations.Test; + +import brooklyn.entity.AbstractSoftlayerLiveTest; +import brooklyn.location.Location; + +@Test(groups = { "Live" }) +public class MySqlClusterLiveSoftlayerTest extends AbstractSoftlayerLiveTest { + + @Override + protected void doTest(Location loc) throws Exception { + MySqlClusterTestHelper.test(app, loc); + } + + @Test(enabled=false) + public void testDummy() {} // Convince testng IDE integration that this really does have test methods + +} + http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/937bc2ea/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterTestHelper.java ---------------------------------------------------------------------- diff --git a/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterTestHelper.java b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterTestHelper.java new file mode 100644 index 0000000..511d601 --- /dev/null +++ b/software/database/src/test/java/brooklyn/entity/database/mysql/MySqlClusterTestHelper.java @@ -0,0 +1,115 @@ +/* + * 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 brooklyn.entity.database.mysql; + +import static org.testng.Assert.assertEquals; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; + +import brooklyn.entity.database.VogellaExampleAccess; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.location.Location; +import brooklyn.test.Asserts; +import brooklyn.test.entity.TestApplication; +import brooklyn.util.collections.MutableMap; +import brooklyn.util.exceptions.Exceptions; + +/** + * Runs a slightly modified version of the popular Vogella MySQL tutorial, + * from + * http://www.vogella.de/articles/MySQLJava/article.html + */ +public class MySqlClusterTestHelper { + public static final Logger log = LoggerFactory.getLogger(MySqlClusterTestHelper.class); + + // From http://www.vogella.de/articles/MySQLJava/article.html + public static final String CREATION_SCRIPT = Joiner.on("\n").join(ImmutableList.of( + "CREATE DATABASE feedback;", + "CREATE USER 'sqluser'@'localhost' IDENTIFIED BY 'sqluserpw';", + "GRANT USAGE ON *.* TO 'sqluser'@'localhost';", + "GRANT ALL PRIVILEGES ON feedback.* TO 'sqluser'@'localhost';", + "CREATE USER 'sqluser'@'%' IDENTIFIED BY 'sqluserpw';", + "GRANT USAGE ON *.* TO 'sqluser'@'%';", + "GRANT ALL PRIVILEGES ON feedback.* TO 'sqluser'@'%';", + "FLUSH PRIVILEGES;", + "USE feedback;", + "CREATE TABLE COMMENTS (", + " id INT NOT NULL AUTO_INCREMENT,", + " MYUSER VARCHAR(30) NOT NULL,", + " EMAIL VARCHAR(30), ", + " WEBPAGE VARCHAR(100) NOT NULL,", + " DATUM DATE NOT NULL, ", + " SUMMARY VARCHAR(40) NOT NULL,", + " COMMENTS VARCHAR(400) NOT NULL,", + " PRIMARY KEY (ID)", + " );", + "", + "INSERT INTO COMMENTS values (default, 'lars', '[email protected]','http://www.vogella.de', '2009-09-14 10:33:11', 'Summary','My first comment' );" + )); + + public static void test(TestApplication app, Location location) throws Exception { + MySqlCluster mysql = app.createAndManageChild(EntitySpec.create(MySqlCluster.class) + .configure(MySqlCluster.INITIAL_SIZE, 2) + .configure(MySqlNode.MYSQL_SERVER_CONF, MutableMap.<String, Object>of("skip-name-resolve",""))); + + app.start(ImmutableList.of(location)); + log.info("MySQL started"); + MySqlNode masterEntity = (MySqlNode) mysql.getAttribute(MySqlCluster.FIRST); + masterEntity.invoke(MySqlNode.EXECUTE_SCRIPT, ImmutableMap.of("commands", CREATION_SCRIPT)).asTask().getUnchecked(); + + VogellaExampleAccess masterDb = new VogellaExampleAccess("com.mysql.jdbc.Driver", mysql.getAttribute(MySqlNode.DATASTORE_URL)); + VogellaExampleAccess slaveDb = new VogellaExampleAccess("com.mysql.jdbc.Driver", Iterables.getOnlyElement(mysql.getAttribute(MySqlCluster.SLAVE_DATASTORE_URL_LIST))); + masterDb.connect(); + slaveDb.connect(); + + assertSlave(masterDb, slaveDb, 1); + masterDb.modifyDataBase(); + assertSlave(masterDb, slaveDb, 2); + masterDb.revertDatabase(); + assertSlave(masterDb, slaveDb, 1); + + masterDb.close(); + slaveDb.close(); + + log.info("Ran vogella MySQL example -- SUCCESS"); + } + + private static void assertSlave(final VogellaExampleAccess masterDb, final VogellaExampleAccess slaveDb, final int recordCnt) throws Exception { + Asserts.succeedsEventually(new Runnable() { + @Override + public void run() { + try { + List<List<String>> masterResult = masterDb.readDataBase(); + assertEquals(masterResult.size(), recordCnt); + assertEquals(masterResult, slaveDb.readDataBase()); + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + }); + } +}
