implemented restart strategy for windows services

This commit is contained in:
Sebastian Sdorra
2020-05-03 11:23:42 +02:00
parent 6e120cdd00
commit bac253d276
4 changed files with 240 additions and 151 deletions

View File

@@ -1,5 +1,166 @@
import static org.junit.jupiter.api.Assertions.*;
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.lifecycle;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.TempDirectory;
import sonia.scm.Platform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ExtendWith(TempDirectory.class)
class RestartStrategyFactoryTest {
private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@Test
void shouldReturnRestartStrategyFromSystemProperty() {
RestartStrategyFactory factory = builder().withStrategy(TestingRestartStrategy.class).create();
RestartStrategy restartStrategy = factory.fromClassLoader(classLoader);
assertThat(restartStrategy).isInstanceOf(TestingRestartStrategy.class);
}
@Test
void shouldReturnRestartStrategyFromSystemPropertyWithClassLoaderConstructor() {
RestartStrategyFactory factory = builder().withStrategy(ComplexRestartStrategy.class).create();
RestartStrategy restartStrategy = factory.fromClassLoader(classLoader);
assertThat(restartStrategy).isInstanceOf(ComplexRestartStrategy.class)
.extracting("classLoader")
.isSameAs(classLoader);
}
@Test
void shouldThrowExceptionForNonStrategyClass() {
RestartStrategyFactory factory = builder().withStrategy(RestartStrategyFactoryTest.class).create();
assertThrows(RestartNotSupportedException.class, () -> factory.fromClassLoader(classLoader));
}
@Test
void shouldReturnEmpty() {
RestartStrategyFactory factory = builder().withStrategy(RestartStrategyFactory.STRATEGY_NONE).create();
assertThat(factory.fromClassLoader(classLoader)).isNull();
}
@Test
void shouldReturnEmptyForUnknownOs() {
RestartStrategyFactory factory = builder().withOs("hitchhiker-os").create();
assertThat(factory.fromClassLoader(classLoader)).isNull();
}
@Test
void shouldReturnExitRestartStrategy() {
RestartStrategyFactory factory = builder().withStrategy(ExitRestartStrategy.NAME).create();
assertThat(factory.fromClassLoader(classLoader)).isInstanceOf(ExitRestartStrategy.class);
}
@ParameterizedTest
@ValueSource(strings = { "linux", "darwin", "solaris", "freebsd", "openbsd" })
void shouldReturnPosixRestartStrategyForPosixBased(String os) {
RestartStrategyFactory factory = builder().withOs(os).create();
assertThat(factory.fromClassLoader(classLoader)).isInstanceOf(PosixRestartStrategy.class);
}
@Test
void shouldReturnWinSWRestartStrategy(@TempDirectory.TempDir Path tempDir) throws IOException {
File exe = tempDir.resolve("winsw.exe").toFile();
exe.createNewFile();
RestartStrategyFactory factory = builder()
.withOs("windows")
.withEnvironment(WinSWRestartStrategy.ENV_EXECUTABLE, exe.getAbsolutePath())
.create();
assertThat(factory.fromClassLoader(classLoader)).isInstanceOf(WinSWRestartStrategy.class);
}
public static class TestingRestartStrategy extends RestartStrategy {
@Override
protected void executeRestart(InjectionContext context) {
}
}
public static class ComplexRestartStrategy extends RestartStrategy {
private final ClassLoader classLoader;
public ComplexRestartStrategy(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
protected void executeRestart(InjectionContext context) {
}
}
private static Builder builder() {
return new Builder();
}
private static class Builder {
private final Properties properties = new Properties();
private final Map<String, String> environment = new HashMap<>();
private Platform platform = new Platform("Linux", "64Bit", "x64");
public Builder withStrategy(Class<?> strategy) {
return withStrategy(strategy.getName());
}
public Builder withStrategy(String strategy) {
return withProperty(RestartStrategyFactory.PROPERTY_STRATEGY, strategy);
}
public Builder withProperty(String key, String value) {
properties.setProperty(key, value);
return this;
}
public Builder withEnvironment(String key, String value) {
environment.put(key, value);
return this;
}
public Builder withOs(String os) {
platform = new Platform(os, "64Bit", "x64");
return this;
}
public RestartStrategyFactory create() {
return new RestartStrategyFactory(platform, environment, properties);
}
}
}

View File

@@ -1,139 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.lifecycle;
import com.google.common.base.Strings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import sonia.scm.util.SystemUtil;
import java.util.Optional;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class RestartStrategyTest {
private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@Test
void shouldReturnRestartStrategyFromSystemProperty() {
withStrategy(TestingRestartStrategy.class.getName(), (rs) -> {
assertThat(rs).containsInstanceOf(TestingRestartStrategy.class);
});
}
@Test
void shouldReturnRestartStrategyFromSystemPropertyWithClassLoaderConstructor() {
withStrategy(ComplexRestartStrategy.class.getName(), (rs) -> {
assertThat(rs).containsInstanceOf(ComplexRestartStrategy.class)
.get()
.extracting("classLoader")
.isSameAs(classLoader);
});
}
@Test
void shouldThrowExceptionForNonStrategyClass() {
withStrategy(RestartStrategyTest.class.getName(), () -> {
assertThrows(RestartNotSupportedException.class, () -> RestartStrategy.get(classLoader));
});
}
@Test
void shouldReturnEmpty() {
withStrategy(RestartStrategyFactory.STRATEGY_NONE, (rs) -> {
assertThat(rs).isEmpty();
});
}
@Test
void shouldReturnEmptyForUnknownOs() {
withSystemProperty(SystemUtil.PROPERTY_OSNAME, "hitchhiker-os", () -> {
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(classLoader);
assertThat(restartStrategy).isEmpty();
});
}
@Test
void shouldReturnExitRestartStrategy() {
withStrategy(ExitRestartStrategy.NAME, (rs) -> {
assertThat(rs).containsInstanceOf(ExitRestartStrategy.class);
});
}
@ParameterizedTest
@ValueSource(strings = { "linux", "darwin", "solaris", "freebsd", "openbsd" })
void shouldReturnPosixRestartStrategyForPosixBased(String os) {
withSystemProperty(SystemUtil.PROPERTY_OSNAME, os, () -> {
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(classLoader);
assertThat(restartStrategy).containsInstanceOf(PosixRestartStrategy.class);
});
}
private void withStrategy(String strategy, Consumer<Optional<RestartStrategy>> consumer) {
withStrategy(strategy, () -> {
consumer.accept(RestartStrategy.get(classLoader));
});
}
private void withStrategy(String strategy, Runnable runnable) {
withSystemProperty(RestartStrategyFactory.PROPERTY_STRATEGY, strategy, runnable);
}
private void withSystemProperty(String key, String value, Runnable runnable) {
String oldValue = System.getProperty(key);
System.setProperty(key, value);
try {
runnable.run();
} finally {
if (Strings.isNullOrEmpty(oldValue)) {
System.clearProperty(key);
} else {
System.setProperty(key, oldValue);
}
}
}
public static class TestingRestartStrategy extends RestartStrategy {
@Override
protected void executeRestart(InjectionContext context) {
}
}
public static class ComplexRestartStrategy extends RestartStrategy {
private final ClassLoader classLoader;
public ComplexRestartStrategy(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
protected void executeRestart(InjectionContext context) {
}
}
}