Add support for a shared directory
A shared directory is required by some future features, e.g. peer
discovery and websession sharing.
Change-Id: Ia0219a0a40d50f58e1412c4b4ab263bc3de244c3
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java
index e51324a..a9efadb 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java
@@ -14,8 +14,14 @@
package com.ericsson.gerrit.plugins.highavailability;
+import com.google.common.base.Strings;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.inject.AbstractModule;
+import com.google.inject.Provides;
+import com.google.inject.ProvisionException;
import com.google.inject.Scopes;
+import com.google.inject.Singleton;
import com.ericsson.gerrit.plugins.highavailability.cache.CacheModule;
import com.ericsson.gerrit.plugins.highavailability.event.EventModule;
@@ -23,6 +29,11 @@
import com.ericsson.gerrit.plugins.highavailability.index.IndexModule;
import com.ericsson.gerrit.plugins.highavailability.peers.PeerInfoModule;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
class Module extends AbstractModule {
@Override
@@ -34,4 +45,20 @@
install(new CacheModule());
install(new PeerInfoModule());
}
+
+ @Provides
+ @Singleton
+ @SharedDirectory
+ Path getSharedDirectory(PluginConfigFactory cfg,
+ @PluginName String pluginName)
+ throws IOException {
+ String sharedDirectory = Strings.emptyToNull(
+ cfg.getFromGerritConfig(pluginName, true).getString("sharedDirectory"));
+ if (sharedDirectory == null) {
+ throw new ProvisionException("sharedDirectory must be configured");
+ }
+ Path sharedDirectoryPath = Paths.get(sharedDirectory);
+ Files.createDirectories(sharedDirectoryPath);
+ return sharedDirectoryPath;
+ }
}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/SharedDirectory.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/SharedDirectory.java
new file mode 100644
index 0000000..211b0e1
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/SharedDirectory.java
@@ -0,0 +1,39 @@
+// Copyright (C) 2017 Ericsson
+//
+// 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 com.ericsson.gerrit.plugins.highavailability;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import com.google.inject.BindingAnnotation;
+
+import java.lang.annotation.Retention;
+
+/**
+ * {@link java.nio.file.Path} to a directory accessible from both master
+ * instances.
+ * <p>
+ * Example of usage:
+ *
+ * <pre>
+ * {@literal @Inject}
+ * MyType(@SharedDirectory {@link java.nio.file.Path} dir) {
+ * Path someSharedFilePath = dir.resolve("someSharedFile.txt");
+ * }
+ * </pre>
+ */
+@Retention(RUNTIME)
+@BindingAnnotation
+public @interface SharedDirectory {
+}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index e3865fd..26af4ee 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -11,6 +11,7 @@
: url = target_instance_url
: user = username
: password = password
+: sharedDirectory = /directory/accessible/from/both/instances
plugin.@PLUGIN@.url
: Specify the URL for the secondary (target) instance.
@@ -22,6 +23,9 @@
: Password to connect to the secondary (target) instance. This value can
also be defined in secure.config.
+plugin.@PLUGIN@.sharedDirectory
+: Path to a directory accessible from both master instances.
+
@PLUGIN@ plugin uses REST API calls to keep the target instance in-sync. It
is possible to customize the parameters of the underlying http client doing these
calls by specifying the following fields:
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/ModuleTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/ModuleTest.java
new file mode 100644
index 0000000..2e6a768
--- /dev/null
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/ModuleTest.java
@@ -0,0 +1,99 @@
+// Copyright (C) 2017 Ericsson
+//
+// 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 com.ericsson.gerrit.plugins.highavailability;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.when;
+
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.inject.ProvisionException;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ModuleTest {
+
+ private static String PLUGIN_NAME = "somePluginName";
+
+ @Mock
+ private PluginConfigFactory pluginConfigFactoryMock;
+
+ @Mock
+ private PluginConfig pluginConfigMock;
+
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ private Module module;
+
+ @Before
+ public void setUp() {
+ when(pluginConfigFactoryMock.getFromGerritConfig(PLUGIN_NAME, true))
+ .thenReturn(pluginConfigMock);
+ module = new Module();
+ }
+
+ @Test(expected = ProvisionException.class)
+ public void shouldThrowExceptionIfSharedDirectoryNotConfigured()
+ throws IOException {
+ module.getSharedDirectory(pluginConfigFactoryMock, PLUGIN_NAME);
+ }
+
+ @Test
+ public void shouldReturnConfiguredSharedDirectory() throws IOException {
+ File configuredDirectory = tempFolder.newFolder();
+ when(pluginConfigMock.getString("sharedDirectory"))
+ .thenReturn(configuredDirectory.getAbsolutePath());
+
+ Path sharedDirectory =
+ module.getSharedDirectory(pluginConfigFactoryMock, PLUGIN_NAME);
+ assertThat(sharedDirectory.toString())
+ .isEqualTo(configuredDirectory.toString());
+ }
+
+ @Test
+ public void shouldCreateSharedDirectoryIfItDoesNotExist() throws IOException {
+ File configuredDirectory = tempFolder.newFolder();
+ assertThat(configuredDirectory.delete()).isTrue();
+ when(pluginConfigMock.getString("sharedDirectory"))
+ .thenReturn(configuredDirectory.getAbsolutePath());
+
+ Path sharedDirectory =
+ module.getSharedDirectory(pluginConfigFactoryMock, PLUGIN_NAME);
+ assertThat(sharedDirectory.toFile().exists()).isTrue();
+ }
+
+ @Test(expected = IOException.class)
+ public void shouldThrowAnExceptionIfAnErrorOccurCreatingSharedDirectory()
+ throws IOException {
+ File configuredDirectory = tempFolder.newFile();
+ when(pluginConfigMock.getString("sharedDirectory"))
+ .thenReturn(configuredDirectory.getAbsolutePath());
+
+ module.getSharedDirectory(pluginConfigFactoryMock, PLUGIN_NAME);
+ }
+}