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-dao | |
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-dao')
4 files changed, 158 insertions, 1 deletions
diff --git a/server/sonar-db-dao/build.gradle b/server/sonar-db-dao/build.gradle index b78b2b6fada..a5d3fb7698d 100644 --- a/server/sonar-db-dao/build.gradle +++ b/server/sonar-db-dao/build.gradle @@ -29,6 +29,7 @@ dependencies { testCompile 'org.assertj:assertj-guava' testCompile 'org.dbunit:dbunit' testCompile 'org.mockito:mockito-core' + testCompile 'org.sonarsource.orchestrator:sonar-orchestrator' testCompile project(':sonar-testing-harness') testCompile project(':server:sonar-db-core').sourceSets.test.output testCompile project(':sonar-plugin-api-impl') @@ -51,6 +52,15 @@ task dumpSchema(type:JavaExec) { } tasks.check.dependsOn dumpSchema +task createDB(type:JavaExec) { + main = 'org.sonar.db.createdb.CreateDb' + classpath = sourceSets.test.runtimeClasspath + systemProperty 'orchestrator.configUrl', System.getProperty('orchestrator.configUrl') + if (!project.version.endsWith("-SNAPSHOT")) { + systemProperty 'sonar.runtimeVersion', project.version + } +} + task testJar(type: Jar) { classifier = 'tests' from sourceSets.test.output diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/OrchestratorSettingsUtils.java b/server/sonar-db-dao/src/test/java/org/sonar/db/OrchestratorSettingsUtils.java new file mode 100644 index 00000000000..b3f5e8f02ab --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/OrchestratorSettingsUtils.java @@ -0,0 +1,76 @@ +/* + * 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 java.io.File; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URI; +import java.util.Map; +import java.util.Properties; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.text.StrSubstitutor; +import org.sonar.api.config.Settings; + +import static org.apache.commons.lang.StringUtils.isEmpty; + +public class OrchestratorSettingsUtils { + private OrchestratorSettingsUtils() { + // prevents instantiation + } + + public 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-dao/src/test/java/org/sonar/db/TestDbImpl.java b/server/sonar-db-dao/src/test/java/org/sonar/db/TestDbImpl.java index b54eafd45a9..03d7b755562 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/TestDbImpl.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/TestDbImpl.java @@ -53,6 +53,9 @@ class TestDbImpl extends CoreTestDb { } private void init(@Nullable String schemaPath, MyBatisConfExtension[] confExtensions) { + Consumer<Settings> loadOrchestratorSettings = settings -> { + OrchestratorSettingsUtils.loadOrchestratorSettings(settings); + }; Function<Settings, Database> databaseCreator = settings -> { String dialect = settings.getString("sonar.jdbc.dialect"); if (dialect != null && !"h2".equals(dialect)) { @@ -73,7 +76,7 @@ class TestDbImpl extends CoreTestDb { ((SQDatabase) database).executeScript(schemaPath); }; BiConsumer<Database, Boolean> createMyBatis = (db, created) -> myBatis = newMyBatis(db, confExtensions); - init(databaseCreator, schemaPathExecutor, createMyBatis); + init(loadOrchestratorSettings, databaseCreator, schemaPathExecutor, createMyBatis); } private static MyBatis newMyBatis(Database db, MyBatisConfExtension[] extensions) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java new file mode 100644 index 00000000000..bb8d1236cef --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java @@ -0,0 +1,68 @@ +/* + * 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.createdb; + +import com.sonar.orchestrator.config.Configuration; +import com.sonar.orchestrator.db.DatabaseClient; +import com.sonar.orchestrator.db.DatabaseFactory; +import com.sonar.orchestrator.db.DefaultDatabase; +import java.util.function.Consumer; +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.SQDatabase; + +public class CreateDb { + + public static void main(String[] args) { + createDb(configuration -> { + Settings settings = new MapSettings(); + configuration.asMap().forEach(settings::setProperty); + logJdbcSettings(settings); + SQDatabase.newDatabase(settings, true).start(); + }); + } + + private static void createDb(Consumer<Configuration> execute) { + Configuration configuration = Configuration.builder() + .addSystemProperties() + .setProperty("orchestrator.keepDatabase", "false") + .build(); + + DatabaseClient databaseClient = DatabaseFactory.create(configuration, configuration.locators()); + DefaultDatabase defaultDatabase = new DefaultDatabase(databaseClient); + defaultDatabase.killOtherConnections(); + try { + defaultDatabase.start(); + + execute.accept(configuration); + } finally { + defaultDatabase.stop(); + } + } + + private static void logJdbcSettings(Settings settings) { + Logger logger = Loggers.get(CreateDb.class); + for (String key : settings.getKeysStartingWith("sonar.jdbc")) { + logger.info(key + ": " + settings.getString(key)); + } + } +} |