2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2020-05-23 19:06:02 +02:00
|
|
|
|
2019-08-20 14:43:48 +02:00
|
|
|
package sonia.scm.plugin;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
2020-05-23 19:06:02 +02:00
|
|
|
import org.junit.jupiter.api.io.TempDir;
|
2019-08-20 14:43:48 +02:00
|
|
|
import org.mockito.Answers;
|
|
|
|
|
import org.mockito.InjectMocks;
|
|
|
|
|
import org.mockito.Mock;
|
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
import sonia.scm.SCMContextProvider;
|
|
|
|
|
import sonia.scm.net.ahc.AdvancedHttpClient;
|
2020-11-04 09:37:24 +01:00
|
|
|
import sonia.scm.net.ahc.AdvancedHttpResponse;
|
2019-08-20 14:43:48 +02:00
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.IOException;
|
2019-08-21 07:56:52 +02:00
|
|
|
import java.io.InputStream;
|
2019-08-20 14:43:48 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
2019-08-21 07:56:52 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
2019-08-21 07:44:50 +02:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
2020-05-23 19:06:02 +02:00
|
|
|
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
|
|
|
|
import static org.mockito.Mockito.anyInt;
|
|
|
|
|
import static org.mockito.Mockito.lenient;
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
2020-11-04 09:37:24 +01:00
|
|
|
import static sonia.scm.plugin.Tracing.SPAN_KIND;
|
2019-08-20 14:43:48 +02:00
|
|
|
|
2020-05-23 19:06:02 +02:00
|
|
|
@ExtendWith({MockitoExtension.class})
|
2019-08-20 14:43:48 +02:00
|
|
|
class PluginInstallerTest {
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private SCMContextProvider context;
|
|
|
|
|
|
|
|
|
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
|
|
|
|
private AdvancedHttpClient client;
|
|
|
|
|
|
2020-01-23 17:02:53 +01:00
|
|
|
@Mock
|
2020-01-24 12:01:21 +01:00
|
|
|
private SmpDescriptorExtractor extractor;
|
2020-01-23 17:02:53 +01:00
|
|
|
|
2019-08-20 14:43:48 +02:00
|
|
|
@InjectMocks
|
|
|
|
|
private PluginInstaller installer;
|
|
|
|
|
|
|
|
|
|
private Path directory;
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
2020-05-23 19:06:02 +02:00
|
|
|
void setUpContext(@TempDir Path directory) throws IOException {
|
2019-08-20 14:43:48 +02:00
|
|
|
this.directory = directory;
|
2019-08-21 07:44:50 +02:00
|
|
|
lenient().when(context.resolve(any())).then(ic -> {
|
|
|
|
|
Path arg = ic.getArgument(0);
|
|
|
|
|
return directory.resolve(arg);
|
|
|
|
|
});
|
2020-01-23 17:02:53 +01:00
|
|
|
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor(true);
|
|
|
|
|
lenient().when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
|
2019-08-20 14:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldDownloadPlugin() throws IOException {
|
|
|
|
|
mockContent("42");
|
|
|
|
|
|
2020-08-05 15:28:39 +02:00
|
|
|
installer.install(PluginInstallationContext.empty(), createGitPlugin());
|
2019-08-20 14:43:48 +02:00
|
|
|
|
|
|
|
|
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).hasContent("42");
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 16:38:29 +02:00
|
|
|
@Test
|
|
|
|
|
void shouldReturnPendingPluginInstallation() throws IOException {
|
|
|
|
|
mockContent("42");
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
|
2020-08-05 15:28:39 +02:00
|
|
|
PendingPluginInstallation pending = installer.install(PluginInstallationContext.empty(), gitPlugin);
|
2019-08-20 16:38:29 +02:00
|
|
|
|
|
|
|
|
assertThat(pending).isNotNull();
|
2019-08-21 12:49:15 +02:00
|
|
|
assertThat(pending.getPlugin().getDescriptor()).isEqualTo(gitPlugin.getDescriptor());
|
|
|
|
|
assertThat(pending.getPlugin().isPending()).isTrue();
|
2019-08-20 16:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-20 14:43:48 +02:00
|
|
|
private void mockContent(String content) throws IOException {
|
2020-11-04 09:37:24 +01:00
|
|
|
when(request("https://download.hitchhiker.com").contentAsStream())
|
2019-08-20 14:43:48 +02:00
|
|
|
.thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 09:37:24 +01:00
|
|
|
private AdvancedHttpResponse request(String url) throws IOException {
|
|
|
|
|
return client.get(url).spanKind(SPAN_KIND).request();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 14:43:48 +02:00
|
|
|
private AvailablePlugin createGitPlugin() {
|
|
|
|
|
return createPlugin(
|
|
|
|
|
"scm-git-plugin",
|
|
|
|
|
"https://download.hitchhiker.com",
|
|
|
|
|
"73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049" // 42
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldThrowPluginDownloadException() throws IOException {
|
2020-11-04 09:37:24 +01:00
|
|
|
when(request("https://download.hitchhiker.com")).thenThrow(new IOException("failed to download"));
|
2019-08-20 14:43:48 +02:00
|
|
|
|
2020-08-05 15:41:18 +02:00
|
|
|
PluginInstallationContext context = PluginInstallationContext.empty();
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
assertThrows(PluginDownloadException.class, () -> installer.install(context, gitPlugin));
|
2019-08-20 14:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldThrowPluginChecksumMismatchException() throws IOException {
|
|
|
|
|
mockContent("21");
|
|
|
|
|
|
2020-08-05 15:41:18 +02:00
|
|
|
PluginInstallationContext context = PluginInstallationContext.empty();
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
assertThrows(PluginChecksumMismatchException.class, () -> installer.install(context, gitPlugin));
|
2019-08-21 07:56:52 +02:00
|
|
|
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldThrowPluginDownloadExceptionAndCleanup() throws IOException {
|
|
|
|
|
InputStream stream = mock(InputStream.class);
|
|
|
|
|
when(stream.read(any(), anyInt(), anyInt())).thenThrow(new IOException("failed to read"));
|
2020-11-04 09:37:24 +01:00
|
|
|
when(request("https://download.hitchhiker.com").contentAsStream()).thenReturn(stream);
|
2019-08-21 07:56:52 +02:00
|
|
|
|
2020-08-05 15:41:18 +02:00
|
|
|
PluginInstallationContext context = PluginInstallationContext.empty();
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
assertThrows(PluginDownloadException.class, () -> installer.install(context, gitPlugin));
|
2019-08-21 07:56:52 +02:00
|
|
|
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
|
2019-08-20 14:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 17:02:53 +01:00
|
|
|
@Test
|
|
|
|
|
void shouldFailForUnsupportedPlugin() throws IOException {
|
|
|
|
|
mockContent("42");
|
|
|
|
|
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor(false);
|
|
|
|
|
when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
|
|
|
|
|
|
2020-08-05 15:41:18 +02:00
|
|
|
PluginInstallationContext context = PluginInstallationContext.empty();
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
assertThrows(PluginConditionFailedException.class, () -> installer.install(context, gitPlugin));
|
2020-01-23 17:02:53 +01:00
|
|
|
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
|
|
|
|
|
}
|
2019-08-20 14:43:48 +02:00
|
|
|
|
2020-08-05 16:54:48 +02:00
|
|
|
@Test
|
|
|
|
|
void shouldFailForNameMismatch() throws IOException {
|
|
|
|
|
mockContent("42");
|
|
|
|
|
|
|
|
|
|
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor("scm-svn-plugin", "1.0.0", true);
|
|
|
|
|
when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
|
|
|
|
|
|
|
|
|
|
PluginInstallationContext context = PluginInstallationContext.empty();
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
PluginInformationMismatchException exception = assertThrows(PluginInformationMismatchException.class, () -> installer.install(context, gitPlugin));
|
|
|
|
|
assertThat(exception.getApi().getName()).isEqualTo("scm-git-plugin");
|
|
|
|
|
assertThat(exception.getDownloaded().getName()).isEqualTo("scm-svn-plugin");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldFailForVersionMismatch() throws IOException {
|
|
|
|
|
mockContent("42");
|
|
|
|
|
|
|
|
|
|
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor("scm-git-plugin", "1.1.0", true);
|
|
|
|
|
when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
|
|
|
|
|
|
|
|
|
|
PluginInstallationContext context = PluginInstallationContext.empty();
|
|
|
|
|
AvailablePlugin gitPlugin = createGitPlugin();
|
|
|
|
|
PluginInformationMismatchException exception = assertThrows(PluginInformationMismatchException.class, () -> installer.install(context, gitPlugin));
|
|
|
|
|
assertThat(exception.getApi().getVersion()).isEqualTo("1.0.0");
|
|
|
|
|
assertThat(exception.getDownloaded().getVersion()).isEqualTo("1.1.0");
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 14:43:48 +02:00
|
|
|
private AvailablePlugin createPlugin(String name, String url, String checksum) {
|
|
|
|
|
PluginInformation information = new PluginInformation();
|
|
|
|
|
information.setName(name);
|
2020-08-05 16:54:48 +02:00
|
|
|
information.setVersion("1.0.0");
|
2019-08-20 14:43:48 +02:00
|
|
|
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(
|
|
|
|
|
information, null, Collections.emptySet(), url, checksum
|
|
|
|
|
);
|
|
|
|
|
return new AvailablePlugin(descriptor);
|
|
|
|
|
}
|
2020-01-23 17:02:53 +01:00
|
|
|
|
|
|
|
|
private InstalledPluginDescriptor createPluginDescriptor(boolean supported) {
|
2020-08-05 16:54:48 +02:00
|
|
|
return createPluginDescriptor("scm-git-plugin", "1.0.0", supported);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private InstalledPluginDescriptor createPluginDescriptor(String name, String version, boolean supported) {
|
2020-01-23 17:02:53 +01:00
|
|
|
InstalledPluginDescriptor installedPluginDescriptor = mock(InstalledPluginDescriptor.class, RETURNS_DEEP_STUBS);
|
2020-08-05 16:54:48 +02:00
|
|
|
lenient().when(installedPluginDescriptor.getInformation().getId()).thenReturn(name);
|
|
|
|
|
lenient().when(installedPluginDescriptor.getInformation().getName()).thenReturn(name);
|
|
|
|
|
lenient().when(installedPluginDescriptor.getInformation().getVersion()).thenReturn(version);
|
2020-01-23 17:02:53 +01:00
|
|
|
lenient().when(installedPluginDescriptor.getCondition().isSupported()).thenReturn(supported);
|
|
|
|
|
return installedPluginDescriptor;
|
|
|
|
|
}
|
2019-08-20 14:43:48 +02:00
|
|
|
}
|