diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2019-07-26 11:29:23 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-08-09 20:21:24 +0200 |
commit | ba3c8baa182f43e6e848ed2f503e4f260ce0aa1c (patch) | |
tree | 0cfbc5fbf1bdb55d3fbe8018ccbd3204081d0d97 /server/sonar-db-core/src | |
parent | b83c58503a60c532379a7797c3a5f630be77a1ff (diff) | |
download | sonarqube-ba3c8baa182f43e6e848ed2f503e4f260ce0aa1c.tar.gz sonarqube-ba3c8baa182f43e6e848ed2f503e4f260ce0aa1c.zip |
CreateDb initialize DB without starting full SQ
+ db-core & db-migration UTS are executed only on H2
Diffstat (limited to 'server/sonar-db-core/src')
-rw-r--r-- | server/sonar-db-core/src/test/java/org/sonar/db/CoreTestDb.java | 82 | ||||
-rw-r--r-- | server/sonar-db-core/src/test/java/org/sonar/db/CreateDb.java | 63 |
2 files changed, 20 insertions, 125 deletions
diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/CoreTestDb.java b/server/sonar-db-core/src/test/java/org/sonar/db/CoreTestDb.java index c7fdb601b1d..281c7fa5225 100644 --- a/server/sonar-db-core/src/test/java/org/sonar/db/CoreTestDb.java +++ b/server/sonar-db-core/src/test/java/org/sonar/db/CoreTestDb.java @@ -19,20 +19,11 @@ */ package org.sonar.db; -import java.io.File; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URI; -import java.util.Map; -import java.util.Properties; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import javax.annotation.Nullable; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.text.StrSubstitutor; import org.dbunit.DataSourceDatabaseTester; import org.dbunit.IDatabaseTester; import org.junit.AssumptionViolatedException; @@ -40,16 +31,20 @@ import org.sonar.api.config.Settings; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.db.dialect.H2; -import org.sonar.process.logging.LogbackHelper; import static java.util.Objects.requireNonNull; -import static org.apache.commons.lang.StringUtils.isEmpty; import static org.sonar.process.ProcessProperties.Property.JDBC_USERNAME; /** * This class should be call using @ClassRule in order to create the schema once (if @Rule is used * the schema will be recreated before each test). + * <p> + * <strong>Tests which rely on this class can only be run on H2</strong> because: + * <ul> + * <li>resetting the schema for each test on non-H2 database is assumed to expensive and slow</li> + * <li>when a specific schema is provided, this schema can't provide a syntax supported by all SGBDs and therefor only + * H2 is targeted</li> + * </ul> */ class CoreTestDb implements TestDb { @@ -60,6 +55,7 @@ class CoreTestDb implements TestDb { protected CoreTestDb() { // use static factory method } + protected CoreTestDb(Database db, DatabaseCommands commands, IDatabaseTester tester) { this.db = db; this.commands = commands; @@ -77,39 +73,39 @@ class CoreTestDb implements TestDb { } private CoreTestDb init(@Nullable String schemaPath) { + Consumer<Settings> noExtraSettingsLoaded = settings -> { + }; Function<Settings, Database> databaseCreator = settings -> { String dialect = settings.getString("sonar.jdbc.dialect"); + + // test relying on CoreTestDb can only run on H2 if (dialect != null && !"h2".equals(dialect)) { - return new DefaultDatabase(new LogbackHelper(), settings); + throw new AssumptionViolatedException("This test is intended to be run on H2 only"); } - return new CoreH2Database("h2Tests-" + (schemaPath == null ? "empty" : DigestUtils.md5Hex(schemaPath))); + + return new CoreH2Database("h2Tests-" + (schemaPath == null ? "empty" : DigestUtils.md5Hex(schemaPath))); }; Consumer<Database> databaseInitializer = database -> { if (schemaPath == null) { return; } - // scripts are assumed to be using H2 specific syntax, ignore the test if not on H2 - if (!database.getDialect().getId().equals("h2")) { - database.stop(); - throw new AssumptionViolatedException("This test is intended to be run on H2 only"); - } - ((CoreH2Database) database).executeScript(schemaPath); }; BiConsumer<Database, Boolean> noPostStartAction = (db, created) -> { }; - init(databaseCreator, databaseInitializer, noPostStartAction); + init(noExtraSettingsLoaded, databaseCreator, databaseInitializer, noPostStartAction); return this; } - protected void init(Function<Settings, Database> databaseCreator, + protected void init(Consumer<Settings> settingsLoader, + Function<Settings, Database> databaseCreator, Consumer<Database> databaseInitializer, BiConsumer<Database, Boolean> extendedStart) { if (db == null) { Settings settings = new MapSettings().addProperties(System.getProperties()); - loadOrchestratorSettings(settings); + settingsLoader.accept(settings); logJdbcSettings(settings); db = databaseCreator.apply(settings); db.start(); @@ -144,9 +140,7 @@ class CoreTestDb implements TestDb { @Override public void start() { - if (!H2.ID.equals(db.getDialect().getId())) { - throw new AssumptionViolatedException("Test disabled because it supports only H2"); - } + // everything is done in init } @Override @@ -161,40 +155,4 @@ class CoreTestDb implements TestDb { } } - private static void loadOrchestratorSettings(Settings settings) { - String url = settings.getString("orchestrator.configUrl"); - if (isEmpty(url)) { - return; - } - - InputStream input = null; - try { - URI uri = new URI(url); - - if (url.startsWith("file:")) { - File file = new File(uri); - input = FileUtils.openInputStream(file); - } else { - HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); - int responseCode = connection.getResponseCode(); - if (responseCode >= 400) { - throw new IllegalStateException("Fail to request: " + uri + ". Status code=" + responseCode); - } - - input = connection.getInputStream(); - } - - Properties props = new Properties(); - props.load(input); - settings.addProperties(props); - for (Map.Entry<String, String> entry : settings.getProperties().entrySet()) { - String interpolatedValue = StrSubstitutor.replace(entry.getValue(), System.getenv(), "${", "}"); - settings.setProperty(entry.getKey(), interpolatedValue); - } - } catch (Exception e) { - throw new IllegalStateException("Cannot load Orchestrator properties from:" + url, e); - } finally { - IOUtils.closeQuietly(input); - } - } } diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/CreateDb.java b/server/sonar-db-core/src/test/java/org/sonar/db/CreateDb.java deleted file mode 100644 index b81ea66aa01..00000000000 --- a/server/sonar-db-core/src/test/java/org/sonar/db/CreateDb.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.OrchestratorBuilder; -import com.sonar.orchestrator.locator.FileLocation; -import com.sonar.orchestrator.locator.Location; -import org.apache.commons.lang.StringUtils; - -import java.io.File; -import org.sonar.api.utils.log.Loggers; - -import static com.sonar.orchestrator.container.Edition.COMMUNITY; - -public class CreateDb { - - public static void main(String[] args) { - OrchestratorBuilder builder = Orchestrator.builderEnv(); - builder.setEdition(COMMUNITY); - String version = System.getProperty("sonar.runtimeVersion"); - if (StringUtils.isEmpty(version)) { - Location zip = FileLocation.byWildcardMavenFilename(new File("../../sonar-application/build/distributions"), "sonar-application-*.zip"); - builder.setZipLocation(zip); - } else { - builder.setSonarVersion(version); - } - builder.setOrchestratorProperty("orchestrator.workspaceDir", "build/it"); - builder.setServerProperty("sonar.es.bootstrap.checks.disable", areEsBootStrapChecksDisabled() ? "true" : "false"); - - Orchestrator orchestrator = builder.build(); - try { - orchestrator.start(); - } finally { - orchestrator.stop(); - } - } - - private static boolean areEsBootStrapChecksDisabled() { - boolean flag = "true".equalsIgnoreCase(System.getenv("CIRRUS_CI")); - if (flag) { - Loggers.get(CreateDb.class).info("Running on Cirrus: ES Bootstrap checks are disabled"); - } - return flag; - } -} |