Permalink
Browse files

Uses TestContainers for MySQL tests (#1424)

This uses TestContainers for MySQL tests, setup by default in Travis.
  • Loading branch information...
1 parent ad8097a commit 30d77be54906142915165ce714150bfe4e31067d @bsideup bsideup committed with adriancole Dec 5, 2016
View
@@ -5,12 +5,6 @@
# https://docs.travis-ci.com/user/ci-environment/#Virtualization-environments
sudo: required
dist: trusty
-addons:
- apt:
- packages:
- - mysql-server-5.6
- - mysql-client-core-5.6
- - mysql-client-5.6
cache:
directories:
@@ -25,15 +19,10 @@ jdk:
services:
- docker
- - mysql
before_install:
# We don't install cassandra as it flakes too much. Instead we defer to circleci to test it
# See https://github.com/openzipkin/zipkin/issues/1356
- # install mysql schema
- - mysql -uroot -e 'SET GLOBAL innodb_file_format=Barracuda'
- - mysql -uroot -e 'create database if not exists zipkin'
- - mysql -uroot -Dzipkin < zipkin-storage/mysql/src/main/resources/mysql.sql
# parameters used during a release
- git config user.name "$GH_USER"
- git config user.email "$GH_USER_EMAIL"
View
@@ -12,6 +12,7 @@
# the License.
#
+# NOTE: we don't use Docker-based testing in CircleCI (as we do in TravisCI) to keep both ways for now.
machine:
services:
- mysql
View
@@ -349,6 +349,12 @@
</dependency>
<dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>mysql</artifactId>
+ <version>${testcontainers.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb-java-client.version}</version>
@@ -69,6 +69,19 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>mysql</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
@@ -0,0 +1,96 @@
+/**
+ * Copyright 2015-2016 The OpenZipkin Authors
+ *
+ * Licensed 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 zipkin.storage.mysql;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.mariadb.jdbc.MariaDbDataSource;
+import zipkin.internal.LazyCloseable;
+
+import static org.junit.Assume.assumeTrue;
+import static zipkin.internal.Util.envOr;
+
+public class LazyMySQLStorage extends LazyCloseable<MySQLStorage>
+ implements TestRule {
+
+ final String version;
+
+ ZipkinMySQLContainer container;
+
+ public LazyMySQLStorage(String version) {
+ this.version = version;
+ }
+
+ @Override protected MySQLStorage compute() {
+ try {
+ container = new ZipkinMySQLContainer(version);
+ container.start();
+ System.out.println("Will use TestContainers MySQL instance");
+ } catch (Exception e) {
+ // Ignored
+ }
+
+ // TODO call .check()
+ return computeStorageBuilder().build();
+ }
+
+ public MySQLStorage.Builder computeStorageBuilder() {
+ final MariaDbDataSource dataSource;
+
+ if (container != null && container.getDataSource() != null) {
+ dataSource = container.getDataSource();
+ } else {
+ dataSource = new MariaDbDataSource();
+
+ dataSource.setUser(System.getenv("MYSQL_USER"));
+ assumeTrue("Minimally, the environment variable MYSQL_USER must be set", dataSource.getUser() != null);
+
+ dataSource.setServerName(envOr("MYSQL_HOST", "localhost"));
+ dataSource.setPort(envOr("MYSQL_TCP_PORT", 3306));
+ dataSource.setDatabaseName(envOr("MYSQL_DB", "zipkin"));
+ dataSource.setPassword(envOr("MYSQL_PASS", ""));
+ }
+
+ dataSource.setProperties("autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8");
+
+ return new MySQLStorage.Builder()
+ .datasource(dataSource)
+ .executor(Runnable::run);
+ }
+
+ @Override public void close() {
+ try {
+ MySQLStorage storage = maybeNull();
+
+ if (storage != null) storage.close();
+ } finally {
+ if (container != null) container.stop();
+ }
+ }
+
+ @Override public Statement apply(Statement base, Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ get();
+ try {
+ base.evaluate();
+ } finally {
+ close();
+ }
+ }
+ };
+ }
+}
@@ -1,34 +0,0 @@
-/**
- * Copyright 2015-2016 The OpenZipkin Authors
- *
- * Licensed 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 zipkin.storage.mysql;
-
-import zipkin.storage.DependenciesTest;
-import zipkin.storage.StorageComponent;
-
-public class MySQLDependenciesTest extends DependenciesTest {
-
- private final MySQLStorage storage;
-
- public MySQLDependenciesTest() {
- this.storage = MySQLTestGraph.INSTANCE.storage.get();
- }
-
- @Override protected StorageComponent storage() {
- return storage;
- }
-
- @Override public void clear() {
- storage.clear();
- }
-}
@@ -1,35 +0,0 @@
-/**
- * Copyright 2015-2016 The OpenZipkin Authors
- *
- * Licensed 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 zipkin.storage.mysql;
-
-import zipkin.storage.SpanStoreTest;
-import zipkin.storage.StorageComponent;
-
-public class MySQLSpanStoreTest extends SpanStoreTest {
-
- private final MySQLStorage storage;
-
- public MySQLSpanStoreTest() {
- this.storage = MySQLTestGraph.INSTANCE.storage.get();
- }
-
- @Override protected StorageComponent storage() {
- return storage;
- }
-
- @Override
- public void clear() {
- storage.clear();
- }
-}
@@ -1,39 +0,0 @@
-/**
- * Copyright 2015-2016 The OpenZipkin Authors
- *
- * Licensed 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 zipkin.storage.mysql;
-
-import zipkin.storage.StrictTraceIdFalseTest;
-import zipkin.storage.StorageComponent;
-
-public class MySQLStrictTraceIdFalseTest extends StrictTraceIdFalseTest {
-
- private final MySQLStorage storage;
-
- public MySQLStrictTraceIdFalseTest() {
- MySQLStorage delegate = MySQLTestGraph.INSTANCE.storage.get();
- this.storage = new MySQLStorage.Builder()
- .strictTraceId(false)
- .datasource(delegate.datasource())
- .executor(Runnable::run).build();
- }
-
- @Override protected StorageComponent storage() {
- return storage;
- }
-
- @Override
- public void clear() {
- storage.clear();
- }
-}
@@ -0,0 +1,71 @@
+/**
+ * Copyright 2015-2016 The OpenZipkin Authors
+ *
+ * Licensed 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 zipkin.storage.mysql;
+
+import org.junit.ClassRule;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import zipkin.storage.StorageComponent;
+
+@RunWith(Enclosed.class)
+public class MySQLTest {
+
+ @ClassRule
+ public static LazyMySQLStorage storage = new LazyMySQLStorage("1.17.1");
+
+ public static class DependenciesTest extends zipkin.storage.DependenciesTest {
+
+ @Override protected StorageComponent storage() {
+ return storage.get();
+ }
+
+ @Override public void clear() {
+ storage.get().clear();
+ }
+ }
+
+ public static class SpanStoreTest extends zipkin.storage.SpanStoreTest {
+
+ @Override protected StorageComponent storage() {
+ return storage.get();
+ }
+
+ @Override
+ public void clear() {
+ storage.get().clear();
+ }
+ }
+
+ public static class StrictTraceIdFalseTest extends zipkin.storage.StrictTraceIdFalseTest {
+
+ private final MySQLStorage storage;
+
+ public StrictTraceIdFalseTest() {
+ this.storage = MySQLTest.storage.computeStorageBuilder()
+ .strictTraceId(false)
+ .build();
+ }
+
+ @Override protected StorageComponent storage() {
+ return storage;
+ }
+
+ @Override
+ public void clear() {
+ storage.clear();
+ }
+ }
+
+
+}
Oops, something went wrong.

0 comments on commit 30d77be

Please sign in to comment.