Add the repository import and export with metadata for Subversion repositories (#1501)

* Add store exporter to collect the repository metadata
* Add EnvironmentInformationXmlGenerator
* Collect export data and put into compressed tar archive output stream
* Create full repository export endpoint.
* Add full repository export to ui
* Ignore irrelevant files from config store directory
* write metadata stores to file since a baos could teardown the server memory
* Migrate store name for git lfs files (#1504)

Changes the directory name for the git LFS blob store by
removing the repository id from the store name.

This is necessary for im- and exports of lfs blob stores,
because the original name had the repository id as a part
of it and therefore the old store would not be found when
the repository is imported with another id.

Existing blob files will be moved to the new store location
by an update step.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>

* Introduce util for migrations (#1505)

With this util it is more simple to rename
or delete stores.

* Rename files in export

Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
Eduard Heimbuch
2021-01-28 11:40:35 +01:00
committed by GitHub
parent a35c227a55
commit d91c71ace1
87 changed files with 4187 additions and 84 deletions

View File

@@ -44,6 +44,8 @@ import org.mockito.Mock;
import sonia.scm.PageResult;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.event.ScmEventBus;
import sonia.scm.importexport.FullScmRepositoryExporter;
import sonia.scm.importexport.FullScmRepositoryImporter;
import sonia.scm.repository.CustomNamespaceStrategy;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.NamespaceStrategy;
@@ -73,6 +75,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
@@ -147,6 +150,10 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
private Set<NamespaceStrategy> strategies;
@Mock
private ScmEventBus eventBus;
@Mock
private FullScmRepositoryExporter fullScmRepositoryExporter;
@Mock
private FullScmRepositoryImporter fullScmRepositoryImporter;
@Captor
private ArgumentCaptor<Predicate<Repository>> filterCaptor;
@@ -167,8 +174,8 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
super.manager = repositoryManager;
RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper = new RepositoryCollectionToDtoMapper(repositoryToDtoMapper, resourceLinks);
super.repositoryCollectionResource = new RepositoryCollectionResource(repositoryManager, repositoryCollectionToDtoMapper, dtoToRepositoryMapper, resourceLinks, repositoryInitializer);
super.repositoryImportResource = new RepositoryImportResource(repositoryManager, dtoToRepositoryMapper, serviceFactory, resourceLinks, eventBus);
super.repositoryExportResource = new RepositoryExportResource(repositoryManager, serviceFactory);
super.repositoryImportResource = new RepositoryImportResource(repositoryManager, dtoToRepositoryMapper, serviceFactory, resourceLinks, eventBus, fullScmRepositoryImporter);
super.repositoryExportResource = new RepositoryExportResource(repositoryManager, serviceFactory, fullScmRepositoryExporter);
dispatcher.addSingletonResource(getRepositoryRootResource());
when(serviceFactory.create(any(Repository.class))).thenReturn(service);
when(scmPathInfoStore.get()).thenReturn(uriInfo);
@@ -186,7 +193,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldFailForNotExistingRepository() throws URISyntaxException {
when(repositoryManager.get(any(NamespaceAndName.class))).thenReturn(null);
mockRepository("space", "repo");
createRepository("space", "repo");
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/other");
MockHttpResponse response = new MockHttpResponse();
@@ -198,7 +205,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldFindExistingRepository() throws URISyntaxException, UnsupportedEncodingException {
mockRepository("space", "repo");
createRepository("space", "repo");
when(configuration.getNamespaceStrategy()).thenReturn("CustomNamespaceStrategy");
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo");
@@ -212,7 +219,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldGetAll() throws URISyntaxException, UnsupportedEncodingException {
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
PageResult<Repository> singletonPageResult = createSingletonPageResult(createRepository("space", "repo"));
when(repositoryManager.getPage(any(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
when(configuration.getNamespaceStrategy()).thenReturn("CustomNamespaceStrategy");
@@ -227,7 +234,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldCreateFilterForSearch() throws URISyntaxException {
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
PageResult<Repository> singletonPageResult = createSingletonPageResult(createRepository("space", "repo"));
when(repositoryManager.getPage(filterCaptor.capture(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
when(configuration.getNamespaceStrategy()).thenReturn("CustomNamespaceStrategy");
@@ -244,7 +251,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldCreateFilterForNamespace() throws URISyntaxException {
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
PageResult<Repository> singletonPageResult = createSingletonPageResult(createRepository("space", "repo"));
when(repositoryManager.getPage(filterCaptor.capture(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
when(configuration.getNamespaceStrategy()).thenReturn("CustomNamespaceStrategy");
@@ -261,7 +268,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldCreateFilterForNamespaceWithQuery() throws URISyntaxException {
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
PageResult<Repository> singletonPageResult = createSingletonPageResult(createRepository("space", "repo"));
when(repositoryManager.getPage(filterCaptor.capture(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
when(configuration.getNamespaceStrategy()).thenReturn("CustomNamespaceStrategy");
@@ -295,7 +302,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldHandleUpdateForExistingRepository() throws Exception {
mockRepository("space", "repo");
createRepository("space", "repo");
URL url = Resources.getResource("sonia/scm/api/v2/repository-test-update.json");
byte[] repository = Resources.toByteArray(url);
@@ -314,7 +321,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldHandleUpdateForConcurrentlyChangedRepository() throws Exception {
mockRepository("space", "repo", 1337);
createRepository("space", "repo", 1337);
URL url = Resources.getResource("sonia/scm/api/v2/repository-test-update.json");
byte[] repository = Resources.toByteArray(url);
@@ -334,7 +341,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldHandleUpdateForExistingRepositoryForChangedNamespace() throws Exception {
mockRepository("wrong", "repo");
createRepository("wrong", "repo");
URL url = Resources.getResource("sonia/scm/api/v2/repository-test-update.json");
byte[] repository = Resources.toByteArray(url);
@@ -353,7 +360,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldHandleDeleteForExistingRepository() throws Exception {
mockRepository("space", "repo");
createRepository("space", "repo");
MockHttpRequest request = MockHttpRequest.delete("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo");
MockHttpResponse response = new MockHttpResponse();
@@ -444,7 +451,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
@Test
public void shouldCreateArrayOfProtocolUrls() throws Exception {
mockRepository("space", "repo");
createRepository("space", "repo");
when(service.getSupportedProtocols()).thenReturn(of(new MockScmProtocol("http", "http://"), new MockScmProtocol("ssh", "ssh://")));
when(configuration.getNamespaceStrategy()).thenReturn("CustomNamespaceStrategy");
@@ -461,7 +468,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
public void shouldRenameRepository() throws Exception {
String namespace = "space";
String name = "repo";
Repository repository1 = mockRepository(namespace, name);
Repository repository1 = createRepository(namespace, name);
when(manager.get(new NamespaceAndName(namespace, name))).thenReturn(repository1);
URL url = Resources.getResource("sonia/scm/api/v2/rename-repo.json");
@@ -679,7 +686,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
public void shouldMarkRepositoryAsArchived() throws Exception {
String namespace = "space";
String name = "repo";
Repository repository = mockRepository(namespace, name);
Repository repository = createRepository(namespace, name);
when(manager.get(new NamespaceAndName(namespace, name))).thenReturn(repository);
MockHttpRequest request = MockHttpRequest
@@ -697,7 +704,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
public void shouldRemoveArchiveMarkFromRepository() throws Exception {
String namespace = "space";
String name = "repo";
Repository repository = mockRepository(namespace, name);
Repository repository = createRepository(namespace, name);
repository.setArchived(true);
when(manager.get(new NamespaceAndName(namespace, name))).thenReturn(repository);
@@ -716,7 +723,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
public void shouldExportRepository() throws URISyntaxException {
String namespace = "space";
String name = "repo";
Repository repository = mockRepository(namespace, name);
Repository repository = createRepository(namespace, name, "svn");
when(manager.get(new NamespaceAndName(namespace, name))).thenReturn(repository);
mockRepositoryHandler(ImmutableSet.of(Command.BUNDLE));
@@ -738,7 +745,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
public void shouldExportRepositoryCompressed() throws URISyntaxException {
String namespace = "space";
String name = "repo";
Repository repository = mockRepository(namespace, name);
Repository repository = createRepository(namespace, name, "svn");
when(manager.get(new NamespaceAndName(namespace, name))).thenReturn(repository);
mockRepositoryHandler(ImmutableSet.of(Command.BUNDLE));
@@ -756,6 +763,28 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
verify(service).getBundleCommand();
}
@Test
public void shouldExportFullRepository() throws URISyntaxException {
String namespace = "space";
String name = "repo";
Repository repository = createRepository(namespace, name, "svn");
when(manager.get(new NamespaceAndName(namespace, name))).thenReturn(repository);
mockRepositoryHandler(ImmutableSet.of(Command.BUNDLE));
BundleCommandBuilder bundleCommandBuilder = mock(BundleCommandBuilder.class);
when(service.getBundleCommand()).thenReturn(bundleCommandBuilder);
MockHttpRequest request = MockHttpRequest
.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/export/svn/full");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(SC_OK, response.getStatus());
assertEquals("application/x-gzip", response.getOutputHeaders().get("Content-Type").get(0).toString());
verify(fullScmRepositoryExporter).export(eq(repository), any(OutputStream.class));
}
private void mockRepositoryHandler(Set<Command> cmds) {
RepositoryHandler repositoryHandler = mock(RepositoryHandler.class);
RepositoryType repositoryType = mock(RepositoryType.class);
@@ -769,11 +798,17 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
return new PageResult<>(singletonList(repository), 0);
}
private Repository mockRepository(String namespace, String name) {
return mockRepository(namespace, name, 0);
private Repository createRepository(String namespace, String name, String type) {
Repository repository = createRepository(namespace, name);
repository.setType(type);
return repository;
}
private Repository mockRepository(String namespace, String name, long lastModified) {
private Repository createRepository(String namespace, String name) {
return createRepository(namespace, name, 0);
}
private Repository createRepository(String namespace, String name, long lastModified) {
Repository repository = new Repository();
repository.setNamespace(namespace);
repository.setName(name);

View File

@@ -294,6 +294,16 @@ public class RepositoryToRepositoryDtoMapperTest {
dto.getLinks().getLinkBy("export").get().getHref());
}
@Test
public void shouldCreateFullExportLink() {
Repository repository = createTestRepository();
repository.setType("svn");
RepositoryDto dto = mapper.map(repository);
assertEquals(
"http://example.com/base/v2/repositories/testspace/test/export/svn/full",
dto.getLinks().getLinkBy("fullExport").get().getHref());
}
private ScmProtocol mockProtocol(String type, String protocol) {
return new MockScmProtocol(type, protocol);
}

View File

@@ -26,6 +26,7 @@ package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import de.otto.edison.hal.Link;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.junit.After;
@@ -39,6 +40,7 @@ import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.Command;
import java.net.URI;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -114,19 +116,25 @@ public class RepositoryTypeToRepositoryTypeDtoMapperTest {
}
@Test
public void shouldAppendImportFromBundleLink() {
public void shouldAppendImportFromBundleLinkAndFullImportLink() {
RepositoryType type = new RepositoryType("hk", "Hitchhiker", ImmutableSet.of(Command.UNBUNDLE));
when(subject.isPermitted("repository:create")).thenReturn(true);
RepositoryTypeDto dto = mapper.map(type);
List<Link> links = dto.getLinks().getLinksBy("import");
assertEquals(2, links.size());
assertEquals(
"https://scm-manager.org/scm/v2/repositories/import/hk/bundle",
dto.getLinks().getLinkBy("import").get().getHref()
links.get(0).getHref()
);
assertEquals(
"https://scm-manager.org/scm/v2/repositories/import/hk/full",
links.get(1).getHref()
);
}
@Test
public void shouldNotAppendImportFromBundleLinkIfCommandNotSupported() {
public void shouldNotAppendImportFromBundleLinkOrFullImportLinkIfCommandNotSupported() {
when(subject.isPermitted("repository:create")).thenReturn(true);
RepositoryTypeDto dto = mapper.map(type);
assertFalse(dto.getLinks().getLinkBy("import").isPresent());

View File

@@ -0,0 +1,80 @@
/*
* 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.importexport;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.plugin.InstalledPlugin;
import sonia.scm.plugin.InstalledPluginDescriptor;
import sonia.scm.plugin.PluginManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class EnvironmentInformationXmlGeneratorTest {
@Mock
SCMContextProvider contextProvider;
@Mock
PluginManager pluginManager;
@InjectMocks
EnvironmentInformationXmlGenerator generator;
@Test
void shouldGenerateXmlContent() {
InstalledPluginDescriptor descriptor = mock(InstalledPluginDescriptor.class, Answers.RETURNS_DEEP_STUBS);
when(descriptor.getInformation().getName()).thenReturn("scm-exporter-test-plugin");
when(descriptor.getInformation().getVersion()).thenReturn("42.0");
when(contextProvider.getVersion()).thenReturn("2.13.0");
InstalledPlugin installedPlugin = new InstalledPlugin(descriptor, null, null, null, false);
when(pluginManager.getInstalled()).thenReturn(ImmutableList.of(installedPlugin));
byte[] content = generator.generate();
String xmlContent = new String(content);
assertThat(xmlContent).contains(
"<scm-environment>",
" <plugins>\n" +
" <plugin>\n" +
" <name>scm-exporter-test-plugin</name>\n" +
" <version>42.0</version>\n" +
" </plugin>\n" +
" </plugins>",
"<coreVersion>2.13.0</coreVersion>",
"<arch>",
"<os>");
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.importexport;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.BundleCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.work.WorkdirProvider;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class FullScmRepositoryExporterTest {
private static final Repository REPOSITORY = RepositoryTestData.createHeartOfGold();
@Mock
private RepositoryServiceFactory serviceFactory;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private RepositoryService repositoryService;
@Mock
private EnvironmentInformationXmlGenerator environmentGenerator;
@Mock
private RepositoryMetadataXmlGenerator metadataGenerator;
@Mock
private TarArchiveRepositoryStoreExporter storeExporter;
@Mock
private WorkdirProvider workdirProvider;
@InjectMocks
private FullScmRepositoryExporter exporter;
private Collection<Path> workDirsCreated = new ArrayList<>();
@BeforeEach
void initRepoService() {
when(serviceFactory.create(REPOSITORY)).thenReturn(repositoryService);
when(environmentGenerator.generate()).thenReturn(new byte[0]);
when(metadataGenerator.generate(REPOSITORY)).thenReturn(new byte[0]);
}
@Test
void shouldExportEverythingAsTarArchive(@TempDir Path temp) throws IOException {
BundleCommandBuilder bundleCommandBuilder = mock(BundleCommandBuilder.class);
when(repositoryService.getBundleCommand()).thenReturn(bundleCommandBuilder);
when(workdirProvider.createNewWorkdir()).thenAnswer(invocation -> createWorkDir(temp));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.export(REPOSITORY, baos);
verify(storeExporter, times(1)).export(eq(REPOSITORY), any(OutputStream.class));
verify(environmentGenerator, times(1)).generate();
verify(metadataGenerator, times(1)).generate(REPOSITORY);
verify(bundleCommandBuilder, times(1)).bundle(any(OutputStream.class));
workDirsCreated.forEach(wd -> assertThat(wd).doesNotExist());
}
private File createWorkDir(Path temp) throws IOException {
Path newWorkDir = temp.resolve("workDir-" + workDirsCreated.size());
workDirsCreated.add(newWorkDir);
Files.createDirectories(newWorkDir);
return newWorkDir.toFile();
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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.importexport;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.api.UnbundleCommandBuilder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class FullScmRepositoryImporterTest {
private static final Repository REPOSITORY = RepositoryTestData.createHeartOfGold();
@Mock
private RepositoryServiceFactory serviceFactory;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private RepositoryService service;
@Mock
private UnbundleCommandBuilder unbundleCommandBuilder;
@Mock
private RepositoryManager repositoryManager;
@Mock
private ScmEnvironmentCompatibilityChecker compatibilityChecker;
@Mock
private TarArchiveRepositoryStoreImporter storeImporter;
@InjectMocks
private FullScmRepositoryImporter fullImporter;
@BeforeEach
void initRepositoryService() {
lenient().when(serviceFactory.create(REPOSITORY)).thenReturn(service);
lenient().when(service.getUnbundleCommand()).thenReturn(unbundleCommandBuilder);
}
@Test
void shouldNotImportRepositoryIfFileNotExists(@TempDir Path temp) throws IOException {
File emptyFile = new File(temp.resolve("empty").toString());
Files.touch(emptyFile);
assertThrows(ImportFailedException.class, () -> fullImporter.importFromStream(REPOSITORY, new FileInputStream(emptyFile)));
}
@Test
void shouldFailIfScmEnvironmentIsIncompatible() {
when(compatibilityChecker.check(any())).thenReturn(false);
assertThrows(
ImportFailedException.class,
() -> fullImporter.importFromStream(REPOSITORY, Resources.getResource("sonia/scm/repository/import/scm-import.tar.gz").openStream())
);
}
@Test
void shouldImportScmRepositoryArchive() throws IOException {
when(compatibilityChecker.check(any())).thenReturn(true);
when(repositoryManager.create(eq(REPOSITORY), any())).thenReturn(REPOSITORY);
Repository repository = fullImporter.importFromStream(REPOSITORY, Resources.getResource("sonia/scm/repository/import/scm-import.tar.gz").openStream());
assertThat(repository).isEqualTo(REPOSITORY);
verify(storeImporter).importFromTarArchive(eq(REPOSITORY), any(InputStream.class));
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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.importexport;
import org.junit.jupiter.api.Test;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryTestData;
import static org.assertj.core.api.Assertions.assertThat;
class RepositoryMetadataXmlGeneratorTest {
private final static Repository REPOSITORY = RepositoryTestData.createHeartOfGold("git");
private RepositoryMetadataXmlGenerator generator = new RepositoryMetadataXmlGenerator();
@Test
void shouldCreateMetadataWithRepositoryType() {
byte[] metadata = generator.generate(REPOSITORY);
assertThat(new String(metadata)).contains("<type>git</type>");
}
@Test
void shouldCreateMetadataWithRepositoryNamespaceAndName() {
byte[] metadata = generator.generate(REPOSITORY);
assertThat(new String(metadata)).contains("<namespace>hitchhiker</namespace>");
assertThat(new String(metadata)).contains("<name>HeartOfGold</name>");
}
@Test
void shouldCreateMetadataWithRepositoryContactAndDescription() {
byte[] metadata = generator.generate(REPOSITORY);
assertThat(new String(metadata)).contains("<contact>zaphod.beeblebrox@hitchhiker.com</contact>");
assertThat(new String(metadata)).contains("<description>Heart of Gold is the first prototype ship to successfully utilise the revolutionary Infinite Improbability Drive</description>");
}
@Test
void shouldCreateMetadataWithRepositoryPermissions() {
REPOSITORY.addPermission(new RepositoryPermission("arthur", "READ", false));
byte[] metadata = generator.generate(REPOSITORY);
assertThat(new String(metadata)).contains("<permissions>");
assertThat(new String(metadata)).contains("<name>arthur</name>");
assertThat(new String(metadata)).contains("<role>READ</role>");
}
}

View File

@@ -0,0 +1,128 @@
/*
* 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.importexport;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.plugin.InstalledPlugin;
import sonia.scm.plugin.PluginManager;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class ScmEnvironmentCompatibilityCheckerTest {
@Mock
private PluginManager pluginManager;
@Mock
private SCMContextProvider scmContextProvider;
@InjectMocks
private ScmEnvironmentCompatibilityChecker checker;
@BeforeEach
void preparePluginManager() {
InstalledPlugin first = mockPlugin("scm-first-plugin", "1.0.0");
InstalledPlugin second = mockPlugin("scm-second-plugin", "1.1.0");
lenient().when(pluginManager.getInstalled()).thenReturn(ImmutableList.of(first, second));
}
@Test
void shouldReturnTrueIfEnvironmentIsCompatible() {
when(scmContextProvider.getVersion()).thenReturn("2.0.0");
ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(
new EnvironmentPluginDescriptor("scm-first-plugin", "1.0.0"),
new EnvironmentPluginDescriptor("scm-second-plugin", "1.1.0")
);
ScmEnvironment env = createScmEnvironment("2.0.0", "linux", "64", plugins);
boolean compatible = checker.check(env);
assertThat(compatible).isTrue();
}
@Test
void shouldReturnFalseIfCoreVersionIncompatible() {
when(scmContextProvider.getVersion()).thenReturn("2.0.0");
ScmEnvironment env = createScmEnvironment("2.13.0", "linux", "64", Collections.emptyList());
boolean compatible = checker.check(env);
assertThat(compatible).isFalse();
}
@Test
void shouldReturnFalseIfPluginIsIncompatible() {
when(scmContextProvider.getVersion()).thenReturn("2.13.0");
ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(new EnvironmentPluginDescriptor("scm-second-plugin", "1.2.0"));
ScmEnvironment env = createScmEnvironment("2.13.0", "linux", "64", plugins);
boolean compatible = checker.check(env);
assertThat(compatible).isFalse();
}
@Test
void shouldReturnTrueIfPluginDoNotMatch() {
when(scmContextProvider.getVersion()).thenReturn("2.13.0");
ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(new EnvironmentPluginDescriptor("scm-third-plugin", "42.0.0"));
ScmEnvironment env = createScmEnvironment("2.13.0", "linux", "64", plugins);
boolean compatible = checker.check(env);
assertThat(compatible).isTrue();
}
private InstalledPlugin mockPlugin(String name, String version) {
InstalledPlugin plugin = mock(InstalledPlugin.class, Answers.RETURNS_DEEP_STUBS);
lenient().when(plugin.getDescriptor().getInformation().getName()).thenReturn(name);
lenient().when(plugin.getDescriptor().getInformation().getVersion()).thenReturn(version);
return plugin;
}
private ScmEnvironment createScmEnvironment(String coreVersion, String os, String arch, List<EnvironmentPluginDescriptor> pluginList) {
ScmEnvironment scmEnvironment = new ScmEnvironment();
scmEnvironment.setCoreVersion(coreVersion);
scmEnvironment.setOs(os);
scmEnvironment.setArch(arch);
EnvironmentPluginsDescriptor environmentPluginsDescriptor = new EnvironmentPluginsDescriptor();
environmentPluginsDescriptor.setPlugin(pluginList);
scmEnvironment.setPlugins(environmentPluginsDescriptor);
return scmEnvironment;
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.importexport;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.store.ExportableStore;
import sonia.scm.store.Exporter;
import sonia.scm.store.StoreEntryMetaData;
import sonia.scm.store.StoreExporter;
import sonia.scm.store.StoreType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TarArchiveRepositoryStoreExporterTest {
private static final Repository REPOSITORY = RepositoryTestData.create42Puzzle();
@Mock
private StoreExporter storeExporter;
@InjectMocks
private TarArchiveRepositoryStoreExporter tarArchiveRepositoryStoreExporter;
@Test
void shouldExportNothingIfNoStoresFound() throws IOException {
when(storeExporter.listExportableStores(REPOSITORY)).thenReturn(Collections.emptyList());
OutputStream outputStream = mock(OutputStream.class);
tarArchiveRepositoryStoreExporter.export(REPOSITORY, outputStream);
verify(outputStream, never()).write(any());
}
@Test
void shouldWriteDataIfRepoStoreFound() {
when(storeExporter.listExportableStores(REPOSITORY)).thenReturn(ImmutableList.of(new TestExportableStore()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tarArchiveRepositoryStoreExporter.export(REPOSITORY, outputStream);
String content = outputStream.toString();
assertThat(content).isNotBlank();
}
@Test
void shouldExportFromFoundRepoStore() throws IOException {
ExportableStore exportableStore = mock(ExportableStore.class);
when(storeExporter.listExportableStores(REPOSITORY)).thenReturn(ImmutableList.of(exportableStore));
OutputStream outputStream = mock(OutputStream.class);
tarArchiveRepositoryStoreExporter.export(REPOSITORY, outputStream);
verify(exportableStore).export(any(Exporter.class));
}
static class TestExportableStore implements ExportableStore {
@Override
public StoreEntryMetaData getMetaData() {
return new StoreEntryMetaData(StoreType.CONFIG, "puzzle42");
}
@Override
public void export(Exporter exporter) throws IOException {
try (OutputStream stream = exporter.put("testStore", 0)) {
stream.flush();
}
}
}
}

View File

@@ -0,0 +1,79 @@
/*
* 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.importexport;
import com.google.common.io.Resources;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.store.RepositoryStoreImporter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class TarArchiveRepositoryStoreImporterTest {
private final Repository repository = RepositoryTestData.createHeartOfGold();
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private RepositoryStoreImporter repositoryStoreImporter;
@InjectMocks
private TarArchiveRepositoryStoreImporter tarArchiveRepositoryStoreImporter;
@Test
void shouldDoNothingIfNoEntries() {
ByteArrayInputStream bais = new ByteArrayInputStream("".getBytes());
tarArchiveRepositoryStoreImporter.importFromTarArchive(repository, bais);
verify(repositoryStoreImporter, never()).doImport(any(Repository.class));
}
@Test
void shouldImportEachEntry() throws IOException {
InputStream inputStream = Resources.getResource("sonia/scm/repository/import/scm-metadata.tar").openStream();
tarArchiveRepositoryStoreImporter.importFromTarArchive(repository, inputStream);
verify(repositoryStoreImporter, times(2)).doImport(repository);
}
@Test
void shouldThrowImportFailedExceptionIfInvalidStorePath() throws IOException {
InputStream inputStream = Resources.getResource("sonia/scm/repository/import/scm-metadata_invalid.tar").openStream();
assertThrows(ImportFailedException.class, () -> tarArchiveRepositoryStoreImporter.importFromTarArchive(repository, inputStream));
}
}