Merge branch 'stable-2.16'
* stable-2.16:
Upgrade bazlets to latest stable-2.16 to build with 2.16.6 API
Add the standard LICENSE file
Documentation: Fix the in-tree bazel test command
Start dockerized postgres server in junit test using testcontainers
PostgresQueueTest#testList: Tolerate up to 1s skew in queued time stamp
Upgrade bazlets to latest stable-2.15 to build with 2.15.11 API
Change-Id: I7829c03bc605de0b5fd0c954dd74b13c3f08197b
diff --git a/BUILD b/BUILD
index 64383ff..1dac6b9 100644
--- a/BUILD
+++ b/BUILD
@@ -22,6 +22,7 @@
"@javax_inject//jar",
"@aopalliance//jar",
"@slf4j-api//jar",
+ "@slf4j-ext//jar",
"@log4j-slf4j-impl//jar",
"@log4j-api//jar",
"@log4j-core//jar",
@@ -73,7 +74,10 @@
name = "gc_conductor_tests",
srcs = glob(["src/test/java/**/*.java"]),
resources = glob(["src/test/resources/**/*"]),
- tags = ["gc-conductor"],
+ tags = [
+ "docker",
+ "gc-conductor",
+ ],
deps = [
":gc-conductor__plugin_test_deps",
],
@@ -87,7 +91,14 @@
":gc-conductor__plugin",
":gc-executor_lib",
"@byte-buddy//jar",
+ "@duct_tape//jar",
+ "@jna//jar",
"@mockito//jar",
"@objenesis//jar",
+ "@testcontainers-database-commons//jar",
+ "@testcontainers-jdbc//jar",
+ "@testcontainers-postgres//jar",
+ "@testcontainers//jar",
+ "@visible_assertions//jar",
],
)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..11069ed
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+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.
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl
index a89f9b9..5bfbd30 100644
--- a/external_plugin_deps.bzl
+++ b/external_plugin_deps.bzl
@@ -27,17 +27,31 @@
)
maven_jar(
+ name = "duct_tape",
+ artifact = "org.rnorth.duct-tape:duct-tape:1.0.7",
+ sha1 = "a26b5d90d88c91321dc7a3734ea72d2fc019ebb6",
+ )
+
+ maven_jar(
name = "objenesis",
artifact = "org.objenesis:objenesis:2.6",
sha1 = "639033469776fd37c08358c6b92a4761feb2af4b",
)
+ SLF4J_VERS = "1.7.25"
+
maven_jar(
name = "slf4j-api",
- artifact = "org.slf4j:slf4j-api:1.7.25",
+ artifact = "org.slf4j:slf4j-api:" + SLF4J_VERS,
sha1 = "da76ca59f6a57ee3102f8f9bd9cee742973efa8a",
)
+ maven_jar(
+ name = "slf4j-ext",
+ artifact = "org.slf4j:slf4j-ext:" + SLF4J_VERS,
+ sha1 = "d1685f86e2becda32ad8bb823b8cc4990eb45f69",
+ )
+
LOG4J2_VERS = "2.11.1"
maven_jar(
@@ -132,3 +146,41 @@
artifact = "tech.huffman.re-retrying:re-retrying:3.0.0",
sha1 = "bd3ce1aaafc0f357354e76890f0a8199a0f42f3a",
)
+
+ maven_jar(
+ name = "visible_assertions",
+ artifact = "org.rnorth.visible-assertions:visible-assertions:2.1.0",
+ sha1 = "f2fcff2862860828ac38a5e1f14d941787c06b13",
+ )
+
+ maven_jar(
+ name = "jna",
+ artifact = "net.java.dev.jna:jna:4.5.1",
+ sha1 = "65bd0cacc9c79a21c6ed8e9f588577cd3c2f85b9",
+ )
+
+ TEST_CONTAINERS_VERS = "1.10.6"
+
+ maven_jar(
+ name = "testcontainers",
+ artifact = "org.testcontainers:testcontainers:" + TEST_CONTAINERS_VERS,
+ sha1 = "435c94ec099e229e62295cec4feec91c192aa764",
+ )
+
+ maven_jar(
+ name = "testcontainers-database-commons",
+ artifact = "org.testcontainers:database-commons:" + TEST_CONTAINERS_VERS,
+ sha1 = "36b1a0f88358b433c1d10d74829d4180ca9cb2ec",
+ )
+
+ maven_jar(
+ name = "testcontainers-jdbc",
+ artifact = "org.testcontainers:jdbc:" + TEST_CONTAINERS_VERS,
+ sha1 = "449d18d09dc8d04f9c90b0bdc1930afdf4558a7e",
+ )
+
+ maven_jar(
+ name = "testcontainers-postgres",
+ artifact = "org.testcontainers:postgresql:" + TEST_CONTAINERS_VERS,
+ sha1 = "a73d73b200b60da821745acca2785874ae65e5b9",
+ )
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModule.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModule.java
index 24ceba9..f240315 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModule.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModule.java
@@ -15,7 +15,6 @@
package com.ericsson.gerrit.plugins.gcconductor.postgresqueue;
import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.DRIVER;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.INITIAL_DATABASE;
import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.createDatabase;
import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.databaseExists;
import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.executeStatement;
@@ -64,7 +63,7 @@
@Provides
@Singleton
BasicDataSource provideGcDatabaseAccess(CommonConfig cfg) throws SQLException {
- BasicDataSource adminDataSource = createDataSource(cfg, INITIAL_DATABASE);
+ BasicDataSource adminDataSource = createDataSource(cfg);
try (Connection conn = adminDataSource.getConnection();
Statement stat = conn.createStatement();
ResultSet resultSet = stat.executeQuery(databaseExists(cfg.getDatabaseName()))) {
@@ -74,13 +73,13 @@
} finally {
adminDataSource.close();
}
- return createDataSource(cfg, cfg.getDatabaseName());
+ return createDataSource(cfg);
}
- private static BasicDataSource createDataSource(CommonConfig cfg, String database) {
+ private static BasicDataSource createDataSource(CommonConfig cfg) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(DRIVER);
- ds.setUrl(cfg.getDatabaseUrl() + database + cfg.getDatabaseUrlOptions());
+ ds.setUrl(cfg.getDatabaseUrl() + cfg.getDatabaseName() + cfg.getDatabaseUrlOptions());
ds.setUsername(cfg.getUsername());
ds.setPassword(cfg.getPassword());
ds.setMinEvictableIdleTimeMillis(EVICT_IDLE_TIME_MS);
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index e6e9ec1..896cc60 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -9,6 +9,12 @@
Standalone build mode is recommended, as this mode doesn't require local Gerrit
tree to exist.
+Successfully running some of the tests requires Docker,
+which are skipped if Docker is not available.
+
+Bazel currently does not show
+link:https://github.com/bazelbuild/bazel/issues/3476[skipped tests].
+
## Build standalone
To build the plugin, issue the following command:
@@ -91,7 +97,7 @@
To execute the tests run:
```
- bazel test --test_tag_filters=@PLUGIN@
+ bazel test --test_tag_filters=@PLUGIN@ //...
```
or filtering using the comma separated tags:
diff --git a/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModuleTest.java b/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModuleTest.java
index fa2593f..cb177c2 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModuleTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresModuleTest.java
@@ -15,7 +15,7 @@
package com.ericsson.gerrit.plugins.gcconductor.postgresqueue;
import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.TestUtil.configMockFor;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.TestUtil.deleteDatabase;
+import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.TestUtil.invalidConfigMockFor;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
@@ -26,18 +26,27 @@
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.After;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.testcontainers.containers.PostgreSQLContainer;
public class PostgresModuleTest {
- private static final String TEST_DATABASE_NAME = "gc_test_module";
private EvaluatorConfig configMock;
private PostgresModule module;
private BasicDataSource dataSource;
+ private static PostgreSQLContainer<?> container;
+
+ @BeforeClass
+ public static void startPostgres() {
+ container = new PostgreSQLContainer<>();
+ container.start();
+ }
+
@Before
public void setUp() throws SQLException {
- configMock = configMockFor(TEST_DATABASE_NAME);
+ configMock = configMockFor(container);
module = new PostgresModule(null);
dataSource = module.provideGcDatabaseAccess(configMock);
}
@@ -47,7 +56,6 @@
if (dataSource != null) {
dataSource.close();
}
- deleteDatabase(TEST_DATABASE_NAME);
}
@Test
@@ -66,7 +74,7 @@
@Test(expected = SQLException.class)
public void shouldFailIfDatabaseNameIsInvalid() throws SQLException {
- module.provideGcDatabaseAccess(configMockFor("invalid!@#$@$%^-name"));
+ module.provideGcDatabaseAccess(invalidConfigMockFor(container));
}
@Test
diff --git a/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresQueueTest.java b/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresQueueTest.java
index 2b60076..50e769c 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresQueueTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/PostgresQueueTest.java
@@ -15,8 +15,8 @@
package com.ericsson.gerrit.plugins.gcconductor.postgresqueue;
import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.TestUtil.configMockFor;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.TestUtil.deleteDatabase;
import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -33,19 +33,28 @@
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.After;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.testcontainers.containers.PostgreSQLContainer;
public class PostgresQueueTest {
- private static final String TEST_DATABASE_NAME = "gc_test_queue";
private BasicDataSource dataSource;
private PostgresQueue queue;
+ private static PostgreSQLContainer<?> container;
+
+ @BeforeClass
+ public static void startPostgres() {
+ container = new PostgreSQLContainer<>();
+ container.start();
+ }
+
@Before
- public void setUp() throws SQLException {
- dataSource =
- new PostgresModule(null).provideGcDatabaseAccess(configMockFor(TEST_DATABASE_NAME));
+ public void setUp() throws SQLException, GcQueueException {
+ dataSource = new PostgresModule(null).provideGcDatabaseAccess(configMockFor(container));
queue = new PostgresQueue(dataSource);
+ emptyQueue();
}
@After
@@ -53,7 +62,6 @@
if (dataSource != null) {
dataSource.close();
}
- deleteDatabase(TEST_DATABASE_NAME);
}
@Test
@@ -184,18 +192,21 @@
assertThat(queue.list().get(0).getPath()).isEqualTo(repoPath);
assertThat(queue.list().get(0).getExecutor()).isEqualTo(executor);
assertThat(queue.list().get(0).getQueuedAt()).isAtLeast(before);
- assertThat(queue.list().get(0).getQueuedAt())
- .isAtMost(new Timestamp(System.currentTimeMillis()));
+ assertTimestampDiff(queue.list().get(0).getQueuedAt());
assertThat(queue.list().get(0).getQueuedFrom()).isEqualTo(hostname);
assertThat(queue.list().get(1).getPath()).isEqualTo(repoPath2);
assertThat(queue.list().get(1).getExecutor()).isNull();
assertThat(queue.list().get(1).getQueuedAt()).isAtLeast(queue.list().get(0).getQueuedAt());
- assertThat(queue.list().get(1).getQueuedAt())
- .isAtMost(new Timestamp(System.currentTimeMillis()));
+ assertTimestampDiff(queue.list().get(1).getQueuedAt());
assertThat(queue.list().get(1).getQueuedFrom()).isEqualTo(hostname);
}
+ private void assertTimestampDiff(Timestamp actual) {
+ long timestampDiff = Math.abs(actual.getTime() - System.currentTimeMillis());
+ assertThat(timestampDiff).isAtMost(TimeUnit.SECONDS.toMillis(1));
+ }
+
@Test(expected = GcQueueException.class)
public void testListThatFailsWhenGettingConnection() throws Exception {
queue = new PostgresQueue(createDataSourceThatFailsWhenGettingConnection());
@@ -493,4 +504,20 @@
return dataSouceMock;
}
+
+ private void emptyQueue() throws GcQueueException {
+ queue
+ .list()
+ .stream()
+ .map(RepositoryInfo::getPath)
+ .forEach(
+ repository -> {
+ try {
+ queue.remove(repository);
+ } catch (GcQueueException e) {
+ fail();
+ }
+ });
+ assertThat(queue.list()).isEmpty();
+ }
}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/TestUtil.java b/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/TestUtil.java
index d45237a..a90f0f4 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/TestUtil.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/gcconductor/postgresqueue/TestUtil.java
@@ -14,44 +14,34 @@
package com.ericsson.gerrit.plugins.gcconductor.postgresqueue;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.DRIVER;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.INITIAL_DATABASE;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.dropDatabase;
-import static com.ericsson.gerrit.plugins.gcconductor.postgresqueue.DatabaseConstants.executeStatement;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.ericsson.gerrit.plugins.gcconductor.evaluator.EvaluatorConfig;
-import java.sql.SQLException;
-import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Ignore;
+import org.testcontainers.containers.PostgreSQLContainer;
@Ignore
public class TestUtil {
- private static final String DATABASE_SERVER_URL = "jdbc:postgresql://localhost:5432/";
- private static final String DEFAULT_USER_AND_PASSWORD = "gc";
-
- static EvaluatorConfig configMockFor(String databaseName) {
+ static EvaluatorConfig configMockFor(PostgreSQLContainer<?> container) {
EvaluatorConfig configMock = mock(EvaluatorConfig.class);
- when(configMock.getDatabaseUrl()).thenReturn(DATABASE_SERVER_URL);
+ when(configMock.getDatabaseUrl()).thenReturn(urlWithoutDatabase(container));
when(configMock.getDatabaseUrlOptions()).thenReturn("");
- when(configMock.getDatabaseName()).thenReturn(databaseName);
- when(configMock.getUsername()).thenReturn(DEFAULT_USER_AND_PASSWORD);
- when(configMock.getPassword()).thenReturn(DEFAULT_USER_AND_PASSWORD);
+ when(configMock.getDatabaseName()).thenReturn(container.getDatabaseName());
+ when(configMock.getUsername()).thenReturn(container.getUsername());
+ when(configMock.getPassword()).thenReturn(container.getPassword());
return configMock;
}
- static void deleteDatabase(String databaseName) throws SQLException {
- BasicDataSource ds = new BasicDataSource();
- try {
- ds.setDriverClassName(DRIVER);
- ds.setUrl(DATABASE_SERVER_URL + INITIAL_DATABASE);
- ds.setUsername(DEFAULT_USER_AND_PASSWORD);
- ds.setPassword(DEFAULT_USER_AND_PASSWORD);
- executeStatement(ds, dropDatabase(databaseName));
- } finally {
- ds.close();
- }
+ static EvaluatorConfig invalidConfigMockFor(PostgreSQLContainer<?> container) {
+ EvaluatorConfig configMock = configMockFor(container);
+ when(configMock.getDatabaseName()).thenReturn(container.getDatabaseName() + "not");
+ return configMock;
+ }
+
+ private static String urlWithoutDatabase(PostgreSQLContainer<?> container) {
+ String urlWithDatabase = container.getJdbcUrl();
+ return urlWithDatabase.substring(0, urlWithDatabase.lastIndexOf('/') + 1);
}
}
diff --git a/src/test/resources/log4j2.xml b/src/test/resources/log4j2.xml
new file mode 100644
index 0000000..7ee8d38
--- /dev/null
+++ b/src/test/resources/log4j2.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN" shutdownHook="disable">
+ <Properties>
+ <Property name="log.path">opt/gerrit/review_site/logs/gc</Property>
+ </Properties>
+ <Appenders>
+ <RollingFile name="RollingFile" fileName="${log.path}/gc.log"
+ filePattern="${log.path}/gc.log.%d{yyyy-MM-dd}.gz"
+ ignoreExceptions="false">
+ <PatternLayout
+ pattern="%d{yyy-MM-dd HH:mm:ss.SSS} %-5level %logger{1} - %t - %msg%n" />
+ <Policies>
+ <TimeBasedTriggeringPolicy interval="1" modulate="true" />
+ <SizeBasedTriggeringPolicy size="30 MB" />
+ </Policies>
+ </RollingFile>
+ </Appenders>
+ <Loggers>
+ <Logger name="com.ericsson.gerrit.plugins.gcconductor" level="info" />
+ <Root level="error">
+ <AppenderRef ref="RollingFile" />
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file