JGitEnvironmentRepositoryTests.java
/*
* Copyright 2013-present the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.config.server.environment;
import java.io.File;
import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.assertj.core.api.Assertions;
import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.DeleteBranchCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.MergeCommand;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.StatusCommand;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.NotMergedException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.attributes.AttributesNodeProvider;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.lib.BaseRepositoryBuilder;
import org.eclipse.jgit.lib.ObjectDatabase;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.SystemReader;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.support.AwsCodeCommitCredentialProvider;
import org.springframework.cloud.config.server.support.GitSkipSslValidationCredentialsProvider;
import org.springframework.cloud.config.server.support.PassphraseCredentialsProvider;
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.UrlResource;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Dave Syer
* @author Gareth Clay
*/
public class JGitEnvironmentRepositoryTests {
RefDatabase database = Mockito.mock(RefDatabase.class);
private StandardEnvironment environment = new StandardEnvironment();
private JGitEnvironmentRepository repository;
private File basedir = new File("target/config");
private final ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
@BeforeAll
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@BeforeEach
public void init() throws Exception {
String uri = ConfigServerTestUtils.prepareLocalRepo();
this.repository = new JGitEnvironmentRepository(this.environment, new JGitEnvironmentProperties(),
ObservationRegistry.NOOP);
this.repository.setUri(uri);
if (this.basedir.exists()) {
FileUtils.delete(this.basedir, FileUtils.RECURSIVE | FileUtils.RETRY);
}
}
@Test
public void expireRefreshRateOnlyExpiresMatchingLocalRepository() {
long now = System.currentTimeMillis();
this.repository.setLastRefresh(now);
assertThat(this.repository.expireRefreshRate(this.repository.getUri() + "-other")).isFalse();
assertThat(this.repository.getLastRefresh()).isEqualTo(now);
assertThat(this.repository.expireRefreshRate(this.repository.getUri())).isTrue();
assertThat(this.repository.getLastRefresh()).isZero();
}
@ParameterizedTest
@CsvSource({ "https://github.com/team/config.git, https://github.com/team/config.git, true",
"https://user:password@github.com/team/config.git, https://github.com/team/config.git, true",
"https://github.com/team/config.git, https://github.com/team/config, true",
"https://github.com/team/config.git/, https://github.com/team/config.git, true",
"https://GITHUB.COM:443/team/config.git, https://github.com/team/config.git, true",
"http://git.example.com:80/team/config.git, http://git.example.com/team/config.git, true",
"ssh://git@github.com:22/team/config.git, ssh://git@github.com/team/config.git, true",
"git@git.example.com:/team/config.git, ssh://git@git.example.com/team/config.git, true",
"git@git.example.com:team/config.git, ssh://git@git.example.com/team/config.git, true",
"https://github.com/team/config.git, https://github.com/team/Config.git, true",
"https://github.com/Team/Config, git@github.com:team/config.git, true",
"https://github.com/team/config.git, http://github.com/team/config.git, true",
"ssh://git@git.example.com:2222/team/config.git, git@git.example.com:team/config.git, true",
"https://git.example.com:8443/team/config.git, https://git.example.com/team/config.git, true",
"alice@git.example.com:team/config.git, bob@git.example.com:team/config.git, true",
"git@git.example.com:/srv/config.git, git@git.example.com:/srv/config, true",
"file:/config/team.git, file:/config/team, true",
"https://github.com/team/config.git, https://github.com/team/other.git, false",
"https://github.com/team/config.git, https://github.com/other/config.git, false",
"https://github.com/team/config.git, https://gitlab.com/team/config.git, false" })
public void expireRefreshRateMatchesRepositoryUri(String configuredUri, String notificationUri, boolean matches) {
long now = System.currentTimeMillis();
this.repository.setUri(configuredUri);
this.repository.setLastRefresh(now);
assertThat(this.repository.expireRefreshRate(notificationUri)).isEqualTo(matches);
assertThat(this.repository.getLastRefresh()).isEqualTo(matches ? 0 : now);
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " ", "https://", "://invalid", "https://github.com", "https://github.com/" })
public void expireRefreshRateIgnoresMissingOrInvalidRepositoryUri(String notificationUri) {
long now = System.currentTimeMillis();
this.repository.setLastRefresh(now);
assertThat(this.repository.expireRefreshRate(notificationUri)).isFalse();
assertThat(this.repository.getLastRefresh()).isEqualTo(now);
}
@Test
public void expireRefreshRatePreservesNegativeRefreshRate() throws Exception {
this.repository.setRefreshRate(-1);
this.repository.setLastRefresh(System.currentTimeMillis());
this.repository.expireRefreshRate(this.repository.getUri());
assertThat(this.repository.getRefreshRate()).isEqualTo(-1);
assertThat(this.repository.shouldPull(mock(Git.class))).isFalse();
}
@Test
public void vanilla() {
Environment environment = this.repository.findOne("bar", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/bar.properties");
assertVersion(environment);
}
@Test
public void credentialsInUriAreMaskedInPropertySourceNames() {
// the clone/fetch is stubbed out, so this exercises everything that happens
// after an authenticated repository has been checked out locally
JGitEnvironmentRepository repository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP) {
@Override
public String refresh(String label) {
return "abcdef";
}
};
repository.setUri("https://ryan:secret@example.com/config-repo.git");
repository.setBasedir(new File(ConfigServerTestUtils.REPO_PREFIX + "config-repo"));
Environment environment = repository.findOne("bar", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources()).extracting(PropertySource::getName)
.allSatisfy(name -> assertThat(name).doesNotContain("ryan").doesNotContain("secret"));
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo("https://***:***@example.com/config-repo.git/bar.properties");
assertThat(environment.getVersion()).isEqualTo("abcdef");
}
@Test
public void fileUriRejectsRepositoryWhenResolvedPathUsesSymlinkFinalComponent() throws Exception {
String realUri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
File realDir = new UrlResource(StringUtils.cleanPath(realUri)).getFile();
Path realPath = realDir.toPath().toAbsolutePath().normalize();
Path parent = realPath.getParent();
Assumptions.assumeTrue(parent != null);
Path link = parent.resolve("another-config-repo-symlink-link");
try {
Files.deleteIfExists(link);
Files.createSymbolicLink(link, realPath);
}
catch (IOException | UnsupportedOperationException | SecurityException ex) {
Assumptions.abort("Cannot create symbolic link for test: " + ex.getMessage());
}
try {
this.repository.setUri(link.toUri().toString());
this.repository.setBasedir(this.basedir);
assertThatThrownBy(() -> this.repository.findOne("bar", "staging", "master"))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Cannot load environment")
.cause()
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("symbolic link");
}
finally {
Files.deleteIfExists(link);
}
}
@Test
public void fileUriRejectsDotGitAsSymbolicLink() throws Exception {
String realUri = ConfigServerTestUtils.prepareLocalRepo("test2-config-repo");
File realDir = new UrlResource(StringUtils.cleanPath(realUri)).getFile();
Path repoRoot = realDir.toPath().toAbsolutePath().normalize();
Path gitPath = repoRoot.resolve(".git");
Path backup = repoRoot.resolve(".git-backup-for-symlink-test");
Files.deleteIfExists(backup);
try {
try {
Files.move(gitPath, backup, StandardCopyOption.ATOMIC_MOVE);
}
catch (AtomicMoveNotSupportedException ex) {
Files.move(gitPath, backup, StandardCopyOption.REPLACE_EXISTING);
}
try {
Files.createSymbolicLink(gitPath, backup);
}
catch (IOException | UnsupportedOperationException | SecurityException ex) {
Assumptions.abort("Cannot create symbolic link for test: " + ex.getMessage());
}
this.repository.setUri(realUri);
this.repository.setBasedir(this.basedir);
assertThatThrownBy(() -> this.repository.findOne("bar", "staging", "master"))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Cannot load environment")
.cause()
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("symbolic link")
.hasMessageContaining(".git");
}
finally {
Files.deleteIfExists(gitPath);
if (Files.exists(backup)) {
try {
Files.move(backup, gitPath, StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException ex) {
// best-effort restore for other tests using the same fixture path
}
}
}
}
@Test
public void nested() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.repository.setUri(uri);
this.repository.setSearchPaths(new String[] { "sub" });
Environment environment = this.repository.findOne("bar", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/sub/application.yml");
assertVersion(environment);
}
@Test
public void placeholderInSearchPath() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.repository.setUri(uri);
this.repository.setSearchPaths(new String[] { "{application}" });
Environment environment = this.repository.findOne("sub", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(1);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/sub/application.yml");
assertVersion(environment);
}
private void assertVersion(Environment environment) {
String version = environment.getVersion();
assertThat(version).as("version was null").isNotNull();
assertThat(version.length() >= 40 && version.length() <= 64).isTrue();
}
@Test
@Disabled // see https://github.com/spring-projects/spring-framework/issues/29333
public void nestedPattern() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.repository.setUri(uri);
this.repository.setSearchPaths(new String[] { "sub*" });
Environment environment = this.repository.findOne("bar", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/sub/application.yml");
assertVersion(environment);
}
@Test
public void branch() {
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "raw");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/bar.properties");
assertVersion(environment);
}
@Test
public void tag() {
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "foo");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/bar.properties");
assertVersion(environment);
}
@Test
public void basedir() {
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/bar.properties");
assertVersion(environment);
}
@Test
public void multipleLabels() {
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "master,foo,raw");
assertThat(environment.getPropertySources()).hasSize(6);
}
@Test
public void multipleLabelsWithFailureButContinues() {
try {
this.repository.setBasedir(this.basedir);
this.repository.setContinueOnMultipleLabelFailure(true);
Environment environment = this.repository.findOne("bar", "staging", "master,doesnotexist,foo,raw");
assertThat(environment.getPropertySources()).hasSize(6);
}
finally {
this.repository.setContinueOnMultipleLabelFailure(false);
}
}
@Test
public void singleLabelWithFailureButContinues() {
try {
this.repository.setBasedir(this.basedir);
this.repository.setContinueOnMultipleLabelFailure(true);
assertThatThrownBy(() -> this.repository.findOne("bar", "staging", "doesnotexist"))
.isInstanceOf(NoSuchLabelException.class);
}
finally {
this.repository.setContinueOnMultipleLabelFailure(false);
}
}
@Test
public void multipleLabelsWithFailure() {
this.repository.setBasedir(this.basedir);
assertThatThrownBy(() -> this.repository.findOne("bar", "staging", "master,doesnotexist,foo,raw"))
.isInstanceOf(NoSuchLabelException.class);
}
@Test
public void basedirExists() throws Exception {
assertThat(this.basedir.mkdirs()).isTrue();
assertThat(new File(this.basedir, ".nothing").createNewFile()).isTrue();
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "master");
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getName())
.isEqualTo(this.repository.getUri() + "/bar.properties");
assertVersion(environment);
}
@Test
public void uriWithHostOnly() throws Exception {
this.repository.setUri("git://localhost");
assertThat(this.repository.getUri()).isEqualTo("git://localhost/");
}
@Test
public void uriWithHostAndPath() throws Exception {
this.repository.setUri("git://localhost/foo/");
assertThat(this.repository.getUri()).isEqualTo("git://localhost/foo");
}
@Test
public void testBranchEndsWithTag() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("branch-with-slash-repo");
this.repository.setUri(uri);
// exists branch "feature/foo"
Environment environment = this.repository.findOne("bar", "staging", "feature/foo");
assertVersion(environment);
// try tag "foo"
environment = this.repository.findOne("bar", "staging", "foo");
assertThat(environment.getPropertySources().get(0).getSource().get("key")).isEqualTo("value from tag");
}
@Test
public void afterPropertiesSet_CloneOnStartTrue_CloneAndFetchCalled() throws Exception {
Git mockGit = mock(Git.class);
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(1)).call();
}
@Test
public void afterPropertiesSet_CloneOnStartFalse_CloneAndFetchNotCalled() throws Exception {
Git mockGit = mock(Git.class);
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(0)).call();
verify(mockGit, times(0)).fetch();
}
@Test
public void afterPropertiesSet_CloneOnStartTrueWithFileURL_CloneAndFetchNotCalled() throws Exception {
Git mockGit = mock(Git.class);
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("file://somefilesystem/somegitrepo");
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(0)).call();
verify(mockGit, times(0)).fetch();
}
@Test
public void shouldPullForcepullNotClean() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(false);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
repo.setForcePull(true);
boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was false").isTrue();
}
@Test
public void shouldPullNotClean() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(false);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was true").isFalse();
}
@Test
public void shouldPullTruncatedIndexForcePull() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
ResetCommand resetCommand = mock(ResetCommand.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException("Short read of block.")).thenReturn(status);
when(status.isClean()).thenReturn(true);
when(git.reset()).thenReturn(resetCommand);
when(resetCommand.setMode(any())).thenReturn(resetCommand);
when(resetCommand.setRef(any())).thenReturn(resetCommand);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
repo.setUri("");
repo.setForcePull(true);
boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was false").isTrue();
}
@Test
public void shouldPullTruncatedIndexNotForcePull() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException("Short read of block."));
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
repo.setForcePull(false);
try {
final boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull did not fail").isFalse();
}
catch (JGitInternalException e) {
assertThat(e.getMessage()).as("shouldPull did not fail as expected").isEqualTo("Short read of block.");
}
}
@Test
public void shouldPullTruncatedIndexResetFail() throws Exception {
final String mockThrownMessage = "__mock_thrown__";
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
ResetCommand resetCommand = mock(ResetCommand.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException(mockThrownMessage)).thenReturn(status);
when(status.isClean()).thenReturn(true);
when(git.reset()).thenReturn(resetCommand);
when(resetCommand.call()).thenThrow(new GitAPIException("") {
});
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
try {
repo.shouldPull(git);
Assertions.fail("shouldPull did not fail");
}
catch (JGitInternalException e) {
assertThat(e.getMessage()).as("shouldPull did not fail as expected").isEqualTo(mockThrownMessage);
}
}
@Test
public void shouldPullStatusFail() throws Exception {
final String mockThrownMessage = "__mock_thrown__";
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenThrow(new JGitInternalException(mockThrownMessage));
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
try {
repo.shouldPull(git);
Assertions.fail("shouldPull did not fail");
}
catch (JGitInternalException e) {
assertThat(e.getMessage()).as("shouldPull did not fail as expected").isEqualTo(mockThrownMessage);
}
}
@Test
public void shouldPullClean() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was false").isTrue();
}
@Test
public void shouldNotRefresh() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setRefreshRate(2);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
repo.setLastRefresh(System.currentTimeMillis() - 5000);
boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was false").isTrue();
repo.setRefreshRate(30);
shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was true").isFalse();
}
@Test
public void shouldNotRefreshWhenNegativeValue() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setRefreshRate(-1);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
boolean shouldPull = repo.shouldPull(git);
assertThat(shouldPull).as("shouldPull was true").isFalse();
}
@Test
public void shouldUpdateLastRefresh() throws Exception {
Git git = mock(Git.class);
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
FetchCommand fetchCommand = mock(FetchCommand.class);
FetchResult fetchResult = mock(FetchResult.class);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(fetchCommand.call()).thenReturn(fetchResult);
when(git.fetch()).thenReturn(fetchCommand);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setRefreshRate(1000);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
repo.setLastRefresh(0);
repo.fetch(git, "master");
long timeDiff = System.currentTimeMillis() - repo.getLastRefresh();
assertThat(timeDiff < 1000L).as("time difference (" + timeDiff + ") was longer than 1 second").isTrue();
}
@Test
public void expireRefreshRateDuringFetchIsNotOverwritten() throws Exception {
Git git = mock(Git.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
FetchCommand fetchCommand = mock(FetchCommand.class);
FetchResult fetchResult = mock(FetchResult.class);
when(git.getRepository()).thenReturn(repository);
when(git.fetch()).thenReturn(fetchCommand);
when(repository.getConfig()).thenReturn(storedConfig);
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setRefreshRate(1000);
properties.setUri("https://github.com/team/config.git");
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
// A webhook for a push that the in-flight fetch did not see arrives mid-fetch.
when(fetchCommand.call()).thenAnswer(invocation -> {
repo.expireRefreshRate("https://github.com/team/config.git");
return fetchResult;
});
repo.fetch(git, "master");
// The refresh timestamp is taken before the fetch starts, so the expiry survives
// and the next request fetches again.
assertThat(repo.getLastRefresh()).isZero();
}
@Test
public void failedFetchKeepsRefreshRateBackoff() throws Exception {
Git git = mock(Git.class);
Repository repository = mock(Repository.class);
StoredConfig storedConfig = mock(StoredConfig.class);
FetchCommand fetchCommand = mock(FetchCommand.class);
when(git.getRepository()).thenReturn(repository);
when(git.fetch()).thenReturn(fetchCommand);
when(repository.getConfig()).thenReturn(storedConfig);
when(fetchCommand.call()).thenThrow(new TransportException("remote unavailable"));
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setRefreshRate(1000);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
repo.setLastRefresh(0);
assertThat(repo.fetch(git, "master")).isNull();
// An unreachable remote is not retried on every request.
assertThat(repo.getLastRefresh()).isPositive();
assertThat(repo.shouldPull(git)).isFalse();
}
@Test
public void testFetchException() throws Exception {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
this.repository.setGitFactory(factory);
this.repository.setDeleteUntrackedBranches(true);
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
Repository repository = stubbedRepo();
when(git.getRepository()).thenReturn(repository);
StoredConfig storedConfig = mock(StoredConfig.class);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
// refresh()->fetch
FetchCommand fetchCommand = mock(FetchCommand.class);
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
// here is our exception we are testing
when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote"));
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
List<Ref> refs = new ArrayList<>();
Ref ref = mock(Ref.class);
refs.add(ref);
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
// refresh()->merge
MergeCommand mergeCommand = mock(MergeCommand.class);
when(git.merge()).thenReturn(mergeCommand);
// here is our exception we are testing
when(mergeCommand.call()).thenThrow(new NotMergedException());
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", null);
assertThat(newObjectId.getName()).isEqualTo(locations.getVersion());
verify(git, times(0)).branchDelete();
}
private Repository stubbedRepo() {
return spy(new Repository(new BaseRepositoryBuilder()) {
private String id = UUID.randomUUID().toString();
@Override
public void create(boolean bare) throws IOException {
}
@Override
public String getIdentifier() {
return id;
}
@Override
public ObjectDatabase getObjectDatabase() {
return null;
}
@Override
public RefDatabase getRefDatabase() {
RefDatabase database = mock(RefDatabase.class);
Ref headRef = mock(Ref.class);
ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
when(headRef.getObjectId()).thenReturn(newObjectId);
try {
when(database.findRef(anyString())).thenReturn(headRef);
}
catch (IOException e) {
throw new RuntimeException(e);
}
return database;
}
@Override
public StoredConfig getConfig() {
return null;
}
@Override
public AttributesNodeProvider createAttributesNodeProvider() {
return null;
}
@Override
public void scanForRepoChanges() throws IOException {
}
@Override
public void notifyIndexChanged(boolean internal) {
}
@Override
public ReflogReader getReflogReader(String refName) throws IOException {
return null;
}
});
}
@Test
public void testMergeException() throws Exception {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
this.repository.setGitFactory(factory);
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
Repository repository = stubbedRepo();
when(git.getRepository()).thenReturn(repository);
StoredConfig storedConfig = mock(StoredConfig.class);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
// refresh()->fetch
FetchCommand fetchCommand = mock(FetchCommand.class);
FetchResult fetchResult = mock(FetchResult.class);
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
when(fetchCommand.call()).thenReturn(fetchResult);
when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.<TrackingRefUpdate>emptyList());
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
List<Ref> refs = new ArrayList<>();
Ref ref = mock(Ref.class);
refs.add(ref);
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
// refresh()->merge
MergeCommand mergeCommand = mock(MergeCommand.class);
when(git.merge()).thenReturn(mergeCommand);
when(mergeCommand.call()).thenThrow(new NotMergedException()); // here is our
// exception we
// are testing
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
assertThat(newObjectId.getName()).isEqualTo(locations.getVersion());
verify(git, times(0)).branchDelete();
}
@Test
public void testRefreshWithoutFetch() throws Exception {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
when(cloneCommand.setURI(anyString())).thenReturn(cloneCommand);
when(cloneCommand.setDirectory(any(File.class))).thenReturn(cloneCommand);
when(cloneCommand.call()).thenReturn(git);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
StatusCommand statusCommand = mock(StatusCommand.class);
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
Status status = mock(Status.class);
Repository repository = mock(Repository.class, Mockito.RETURNS_DEEP_STUBS);
StoredConfig storedConfig = mock(StoredConfig.class);
Ref ref = mock(Ref.class);
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
FetchCommand fetchCommand = mock(FetchCommand.class);
FetchResult fetchResult = mock(FetchResult.class);
Ref branch1Ref = mock(Ref.class);
when(git.branchList()).thenReturn(listBranchCommand);
when(git.status()).thenReturn(statusCommand);
when(git.getRepository()).thenReturn(repository);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.fetch()).thenReturn(fetchCommand);
when(git.merge()).thenReturn(mock(MergeCommand.class, Mockito.RETURNS_DEEP_STUBS));
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(checkoutCommand.call()).thenReturn(ref);
when(listBranchCommand.call()).thenReturn(Arrays.asList(branch1Ref));
when(fetchCommand.call()).thenReturn(fetchResult);
when(branch1Ref.getName()).thenReturn("refs/remotes/origin/master");
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
repo.setGitFactory(factory);
repo.setUri("http://somegitserver/somegitrepo");
repo.setBasedir(this.basedir);
// Set the refresh rate to 2 seconds and last update before 100ms. There should be
// no remote repo fetch.
repo.setLastRefresh(System.currentTimeMillis() - 100);
repo.setRefreshRate(2);
repo.refresh("master");
// Verify no fetch but merge only.
verify(git, times(0)).fetch();
verify(git).merge();
}
@Test
public void testResetHardException() throws Exception {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
this.repository.setGitFactory(factory);
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
Repository repository = stubbedRepo();
when(git.getRepository()).thenReturn(repository);
StoredConfig storedConfig = mock(StoredConfig.class);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true).thenReturn(false);
// refresh()->fetch
FetchCommand fetchCommand = mock(FetchCommand.class);
FetchResult fetchResult = mock(FetchResult.class);
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
when(fetchCommand.call()).thenReturn(fetchResult);
when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.<TrackingRefUpdate>emptyList());
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
List<Ref> refs = new ArrayList<>();
Ref ref = mock(Ref.class);
refs.add(ref);
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
// refresh()->merge
MergeCommand mergeCommand = mock(MergeCommand.class);
when(git.merge()).thenReturn(mergeCommand);
// here is our exception we are testing
when(mergeCommand.call()).thenThrow(new NotMergedException());
// refresh()->hardReset
ResetCommand resetCommand = mock(ResetCommand.class);
when(git.reset()).thenReturn(resetCommand);
when(resetCommand.call()).thenReturn(ref);
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
assertThat(newObjectId.getName()).isEqualTo(locations.getVersion());
verify(git, times(0)).branchDelete();
}
@Test
public void shouldDeleteBaseDirWhenCloneFails() throws Exception {
Git mockGit = mock(Git.class);
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
when(mockCloneCommand.call()).thenThrow(new TransportException("failed to clone"));
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setBasedir(this.basedir);
try {
envRepository.findOne("bar", "staging", "master");
}
catch (Exception ex) {
// expected - ignore
}
assertThat(this.basedir.listFiles().length > 0).as("baseDir should be deleted when clone fails").isFalse();
}
@Test
public void credentialsInUriAreMaskedInCloneFailureMessage() throws Exception {
// the message of this exception ends up in the /actuator/health details, so it
// must not carry the credentials the uri was configured with
Git mockGit = mock(Git.class);
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
when(mockCloneCommand.call()).thenThrow(new TransportException("failed to clone"));
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("https://ryan:secret@somegitserver/somegitrepo");
envRepository.setBasedir(this.basedir);
assertThatThrownBy(() -> envRepository.findOne("bar", "staging", "master"))
.isInstanceOf(NoSuchRepositoryException.class)
.hasMessage("Cannot clone or checkout repository: https://***:***@somegitserver/somegitrepo");
}
@Test
public void usernamePasswordShouldSetCredentials() throws Exception {
Git mockGit = mock(Git.class);
MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
envRepository.setBasedir(new File("./mybasedir"));
final String username = "someuser";
final String password = "mypassword";
envRepository.setUsername(username);
envRepository.setPassword(password);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
assertThat(mockCloneCommand.getCredentialsProvider()).isInstanceOf(UsernamePasswordCredentialsProvider.class);
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
CredentialItem.Username usernameCredential = new CredentialItem.Username();
CredentialItem.Password passwordCredential = new CredentialItem.Password();
assertThat(provider.supports(usernameCredential)).isTrue();
assertThat(provider.supports(passwordCredential)).isTrue();
provider.get(new URIish(), usernameCredential);
assertThat(username).isEqualTo(usernameCredential.getValue());
provider.get(new URIish(), passwordCredential);
assertThat(password).isEqualTo(String.valueOf(passwordCredential.getValue()));
}
@Test
public void passphraseShouldSetCredentials() throws Exception {
final String passphrase = "mypassphrase";
Git mockGit = mock(Git.class);
MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
envRepository.setBasedir(new File("./mybasedir"));
envRepository.setPassphrase(passphrase);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
assertThat(mockCloneCommand.hasPassphraseCredentialsProvider()).isTrue();
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
assertThat(provider.isInteractive()).isFalse();
CredentialItem.Password credential = new CredentialItem.Password(PassphraseCredentialsProvider.PROMPT);
assertThat(provider.supports(credential)).isTrue();
provider.get(new URIish(), credential);
assertThat(passphrase.toCharArray()).isEqualTo(credential.getValue());
}
@Test
public void gitCredentialsProviderFactoryCreatesPassphraseProvider() throws Exception {
final String passphrase = "mypassphrase";
final String gitUri = "git+ssh://git@somegitserver/somegitrepo";
Git mockGit = mock(Git.class);
MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri(gitUri);
envRepository.setBasedir(new File("./mybasedir"));
envRepository.setPassphrase(passphrase);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
assertThat(mockCloneCommand.hasPassphraseCredentialsProvider()).isTrue();
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
assertThat(provider.isInteractive()).isFalse();
CredentialItem.InformationalMessage informational = new CredentialItem.InformationalMessage(
"Passphrase required for ssh key");
CredentialItem.Password credential = new CredentialItem.Password(PassphraseCredentialsProvider.PROMPT);
assertThat(provider.supports(credential)).isTrue();
provider.get(new URIish(), informational, credential);
assertThat(passphrase.toCharArray()).isEqualTo(credential.getValue());
}
@Test
public void gitCredentialsProviderFactoryCreatesUsernamePasswordProvider() throws Exception {
Git mockGit = mock(Git.class);
MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
final String username = "someuser";
final String password = "mypassword";
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
envRepository.setBasedir(new File("./mybasedir"));
envRepository.setUsername(username);
envRepository.setPassword(password);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
assertThat(mockCloneCommand.getCredentialsProvider()).isInstanceOf(UsernamePasswordCredentialsProvider.class);
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
CredentialItem.Username usernameCredential = new CredentialItem.Username();
CredentialItem.Password passwordCredential = new CredentialItem.Password();
assertThat(provider.supports(usernameCredential)).isTrue();
assertThat(provider.supports(passwordCredential)).isTrue();
provider.get(new URIish(), usernameCredential);
assertThat(username).isEqualTo(usernameCredential.getValue());
provider.get(new URIish(), passwordCredential);
assertThat(password).isEqualTo(String.valueOf(passwordCredential.getValue()));
}
@Test
public void gitCredentialsProviderFactoryCreatesAwsCodeCommitProvider() throws Exception {
Git mockGit = mock(Git.class);
MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
final String awsUri = "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test";
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri(awsUri);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
assertThat(mockCloneCommand.getCredentialsProvider()).isInstanceOf(AwsCodeCommitCredentialProvider.class);
}
@Test
public void gitCredentialsProviderFactoryCreatesSkipSslValidationProvider() throws Exception {
Git mockGit = mock(Git.class);
MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
final String username = "someuser";
final String password = "mypassword";
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("https://somegitserver/somegitrepo");
envRepository.setBasedir(new File("./mybasedir"));
envRepository.setUsername(username);
envRepository.setPassword(password);
envRepository.setSkipSslValidation(true);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
assertThat(provider instanceof GitSkipSslValidationCredentialsProvider).isTrue();
CredentialItem.Username usernameCredential = new CredentialItem.Username();
CredentialItem.Password passwordCredential = new CredentialItem.Password();
assertThat(provider.supports(usernameCredential)).isTrue();
assertThat(provider.supports(passwordCredential)).isTrue();
provider.get(new URIish(), usernameCredential);
assertThat(username).isEqualTo(usernameCredential.getValue());
provider.get(new URIish(), passwordCredential);
assertThat(password).isEqualTo(String.valueOf(passwordCredential.getValue()));
}
@Test
public void shouldPrintStacktraceIfDebugEnabled() throws Exception {
final Log mockLogger = mock(Log.class);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP) {
@Override
public void afterPropertiesSet() throws Exception {
this.logger = mockLogger;
}
};
envRepository.afterPropertiesSet();
when(mockLogger.isDebugEnabled()).thenReturn(true);
envRepository.warn("", new RuntimeException());
verify(mockLogger).warn(eq(""));
verify(mockLogger).debug(eq("Stacktrace for: "), any(RuntimeException.class));
int numberOfInvocations = mockingDetails(mockLogger).getInvocations().size();
assertThat(numberOfInvocations).as("should call isDebugEnabled warn and debug").isEqualTo(3);
}
@Test
public void shouldSetTransportConfigCallbackOnCloneAndFetch() throws Exception {
Git mockGit = mock(Git.class);
FetchCommand fetchCommand = mock(FetchCommand.class);
when(mockGit.fetch()).thenReturn(fetchCommand);
when(fetchCommand.call()).thenReturn(mock(FetchResult.class));
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
TransportConfigCallback configCallback = mock(TransportConfigCallback.class);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setTransportConfigCallback(configCallback);
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(1)).setTransportConfigCallback(configCallback);
envRepository.fetch(mockGit, "master");
verify(fetchCommand, times(1)).setTransportConfigCallback(configCallback);
}
@Test
public void shouldSetRemoveBranchesFlagToFetchCommand() throws Exception {
Git mockGit = mock(Git.class);
FetchCommand fetchCommand = mock(FetchCommand.class);
when(mockGit.fetch()).thenReturn(fetchCommand);
when(fetchCommand.call()).thenReturn(mock(FetchResult.class));
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mock(CloneCommand.class)));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setDeleteUntrackedBranches(true);
envRepository.fetch(mockGit, "master");
verify(fetchCommand, times(1)).setRemoveDeletedRefs(true);
verify(fetchCommand, times(1)).call();
}
@Test
public void shouldHandleExceptionWhileRemovingBranches() throws Exception {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
this.repository.setGitFactory(factory);
this.repository.setDeleteUntrackedBranches(true);
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
Repository repository = stubbedRepo();
when(git.getRepository()).thenReturn(repository);
StoredConfig storedConfig = mock(StoredConfig.class);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
// refresh()->fetch
FetchCommand fetchCommand = mock(FetchCommand.class);
FetchResult fetchResult = mock(FetchResult.class);
TrackingRefUpdate trackingRefUpdate = mock(TrackingRefUpdate.class);
Collection<TrackingRefUpdate> trackingRefUpdates = Collections.singletonList(trackingRefUpdate);
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
when(fetchCommand.call()).thenReturn(fetchResult);
when(fetchResult.getTrackingRefUpdates()).thenReturn(trackingRefUpdates);
// refresh()->deleteBranch
ReceiveCommand receiveCommand = mock(ReceiveCommand.class);
when(trackingRefUpdate.asReceiveCommand()).thenReturn(receiveCommand);
when(receiveCommand.getType()).thenReturn(ReceiveCommand.Type.DELETE);
when(trackingRefUpdate.getLocalName()).thenReturn("refs/remotes/origin/feature/deletedBranchFromOrigin");
DeleteBranchCommand deleteBranchCommand = mock(DeleteBranchCommand.class);
when(git.branchDelete()).thenReturn(deleteBranchCommand);
when(deleteBranchCommand.setBranchNames(eq("feature/deletedBranchFromOrigin"))).thenReturn(deleteBranchCommand);
when(deleteBranchCommand.setForce(true)).thenReturn(deleteBranchCommand);
// here is our exception we are testing
when(deleteBranchCommand.call()).thenThrow(new NotMergedException());
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
List<Ref> refs = new ArrayList<>();
Ref ref = mock(Ref.class);
refs.add(ref);
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
// refresh()->merge
MergeResult mergeResult = mock(MergeResult.class);
MergeResult.MergeStatus mergeStatus = MergeResult.MergeStatus.MERGED;
MergeCommand mergeCommand = mock(MergeCommand.class);
when(git.merge()).thenReturn(mergeCommand);
when(mergeCommand.call()).thenReturn(mergeResult);
when(mergeResult.getMergeStatus()).thenReturn(mergeStatus);
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
assertThat(newObjectId.getName()).isEqualTo(locations.getVersion());
verify(deleteBranchCommand).setBranchNames(eq("feature/deletedBranchFromOrigin"));
verify(deleteBranchCommand).setForce(true);
verify(deleteBranchCommand).call();
}
/**
* Test case to set if the default-label is checked out.
* @throws Exception should throw any runtime exception.
*/
@Test
public void afterPropertiesSet_CloneOnStartTrue_DefaultLabelSet_CloneAndCheckoutCalled() throws Exception {
final String LABEL_TO_CHECKOUT = "release";
// Set the default branch of repository as master
Repository mockRepository = mock(Repository.class);
when(mockRepository.getBranch()).thenReturn("master");
Git mockGit = mock(Git.class);
when(mockGit.getRepository()).thenReturn(mockRepository);
// Mock the clone command
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
// Mocking the commands to checkout to label.
ListBranchCommand mockListBranchCommand = mock(ListBranchCommand.class);
CheckoutCommand mockCheckoutCommand = mock(CheckoutCommand.class);
// Return the mocked checkout and ListBranchCommand.
when(mockGit.checkout()).thenReturn(mockCheckoutCommand);
when(mockGit.branchList()).thenReturn(mockListBranchCommand);
// Add 2 branches on mock repo {master, release}
List<Ref> repositoryRefsList = new ArrayList<>();
// Mock master branch
Ref mockMasterRef = mock(Ref.class);
repositoryRefsList.add(mockMasterRef);
when(mockMasterRef.getName()).thenReturn("refs/remotes/origin/master");
// Mock release branch.
Ref mockReleaseRef = mock(Ref.class);
repositoryRefsList.add(mockReleaseRef);
when(mockReleaseRef.getName()).thenReturn("refs/remotes/origin/release");
// Mock calls on list and checkout commands
when(mockListBranchCommand.call()).thenReturn(repositoryRefsList);
when(mockCheckoutCommand.call()).thenReturn(mockReleaseRef);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setCloneOnStart(true);
// Set the label to checkout. should be different from master
envRepository.setDefaultLabel(LABEL_TO_CHECKOUT);
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(1)).call();
verify(mockCheckoutCommand, times(1)).call();
verify(mockListBranchCommand, times(2)).call();
verify(mockCheckoutCommand, times(1)).setName(anyString());
}
@Test
public void tryMasterBranch() throws Exception {
// Set the default branch of repository as master
Repository mockRepository = mock(Repository.class);
when(mockRepository.getBranch()).thenReturn("master");
Git mockGit = mock(Git.class);
when(mockGit.getRepository()).thenReturn(mockRepository);
// Mock the clone command
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
// Mocking the commands to checkout to label.
ListBranchCommand mockListBranchCommand = mock(ListBranchCommand.class);
CheckoutCommand mockCheckoutCommand = mock(CheckoutCommand.class);
// Return the mocked checkout and ListBranchCommand.
when(mockGit.checkout()).thenReturn(mockCheckoutCommand);
when(mockGit.branchList()).thenReturn(mockListBranchCommand);
// Add 2 branches on mock repo {master, main}
List<Ref> repositoryRefsList = new ArrayList<>();
// Mock master branch
Ref mockMasterRef = mock(Ref.class);
repositoryRefsList.add(mockMasterRef);
when(mockMasterRef.getName()).thenReturn("refs/remotes/origin/master");
// Mock calls on list and checkout commands
when(mockListBranchCommand.call()).thenReturn(repositoryRefsList);
when(mockCheckoutCommand.call()).thenThrow(new RefNotFoundException("Ref main cannot be resolved"))
.thenReturn(mockMasterRef);
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setTryMasterBranch(true);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setCloneOnStart(true);
// By default we will try and checkout main, make sure we fall back to trying
// master
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(1)).call();
verify(mockCheckoutCommand, times(2)).call();
verify(mockListBranchCommand, times(3)).call();
verify(mockCheckoutCommand, times(1)).setName(eq("master"));
}
@Test
public void doNotTryMasterBranch() throws Exception {
// Set the default branch of repository as master
Repository mockRepository = mock(Repository.class);
when(mockRepository.getBranch()).thenReturn("master");
Git mockGit = mock(Git.class);
when(mockGit.getRepository()).thenReturn(mockRepository);
// Mock the clone command
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
// Mocking the commands to checkout to label.
ListBranchCommand mockListBranchCommand = mock(ListBranchCommand.class);
CheckoutCommand mockCheckoutCommand = mock(CheckoutCommand.class);
// Return the mocked checkout and ListBranchCommand.
when(mockGit.checkout()).thenReturn(mockCheckoutCommand);
when(mockGit.branchList()).thenReturn(mockListBranchCommand);
// Add 2 branches on mock repo {master, main}
List<Ref> repositoryRefsList = new ArrayList<>();
// Mock master branch
Ref mockMasterRef = mock(Ref.class);
repositoryRefsList.add(mockMasterRef);
when(mockMasterRef.getName()).thenReturn("/master");
// Mock calls on list and checkout commands
when(mockListBranchCommand.call()).thenReturn(repositoryRefsList);
when(mockCheckoutCommand.call()).thenThrow(new RefNotFoundException("Ref main cannot be resolved"))
.thenReturn(mockMasterRef);
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
properties.setTryMasterBranch(false);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment, properties,
ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setCloneOnStart(true);
// By default we will try and checkout main, but do not fallback, expect exception
try {
envRepository.afterPropertiesSet();
}
catch (RefNotFoundException e) {
verify(mockCloneCommand, times(1)).call();
verify(mockCheckoutCommand, times(1)).call();
verify(mockListBranchCommand, times(1)).call();
verify(mockCheckoutCommand, times(1)).setName(eq("main"));
}
}
/**
* Test case to validate that check out is not called when the default branch for repo
* is same as default label.
* @throws Exception should throw any runtime exception.
*/
@Test
public void afterPropertiesSet_CloneOnStartTrue_DefaultLabelSameAsDefaultBranch_CheckoutNotCalled()
throws Exception {
final String LABEL_TO_CHECKOUT = "master";
// Set the default branch of repository as master
Repository mockRepository = mock(Repository.class);
when(mockRepository.getBranch()).thenReturn("master");
Git mockGit = mock(Git.class);
when(mockGit.getRepository()).thenReturn(mockRepository);
// Mock the clone command
CloneCommand mockCloneCommand = mock(CloneCommand.class);
when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
// Mocking the commands to checkout to label.
ListBranchCommand mockListBranchCommand = mock(ListBranchCommand.class);
CheckoutCommand mockCheckoutCommand = mock(CheckoutCommand.class);
// Return the mocked checkout and ListBranchCommand.
when(mockGit.checkout()).thenReturn(mockCheckoutCommand);
when(mockGit.branchList()).thenReturn(mockListBranchCommand);
// Add 2 branches on mock repo {master, release}
List<Ref> repositoryRefsList = new ArrayList<>();
// Mock master branch
Ref mockMasterRef = mock(Ref.class);
repositoryRefsList.add(mockMasterRef);
when(mockMasterRef.getName()).thenReturn("/master");
// Mock release branch.
Ref mockReleaseRef = mock(Ref.class);
repositoryRefsList.add(mockReleaseRef);
when(mockReleaseRef.getName()).thenReturn("/release");
// Mock calls on list and checkout commands
when(mockListBranchCommand.call()).thenReturn(repositoryRefsList);
when(mockCheckoutCommand.call()).thenReturn(mockReleaseRef);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment,
new JGitEnvironmentProperties(), ObservationRegistry.NOOP);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setCloneOnStart(true);
// Set the label to checkout. should be different from master
envRepository.setDefaultLabel(LABEL_TO_CHECKOUT);
envRepository.afterPropertiesSet();
verify(mockCloneCommand, times(1)).call();
// Checkout/List Branch/Checkout setName should not be called
verify(mockCheckoutCommand, times(0)).call();
verify(mockListBranchCommand, times(0)).call();
verify(mockCheckoutCommand, times(0)).setName(anyString());
}
class MockCloneCommand extends CloneCommand {
private Git mockGit;
MockCloneCommand(Git mockGit) {
this.mockGit = mockGit;
}
@Override
public Git call() throws GitAPIException, InvalidRemoteException {
return this.mockGit;
}
public boolean hasPassphraseCredentialsProvider() {
return this.credentialsProvider instanceof PassphraseCredentialsProvider;
}
public CredentialsProvider getCredentialsProvider() {
return this.credentialsProvider;
}
}
class MockGitFactory extends JGitEnvironmentRepository.JGitFactory {
private Git mockGit;
private CloneCommand mockCloneCommand;
MockGitFactory(Git mockGit, CloneCommand mockCloneCommand) {
this.mockGit = mockGit;
this.mockCloneCommand = mockCloneCommand;
}
@Override
public Git getGitByOpen(File file) throws IOException {
return this.mockGit;
}
@Override
public CloneCommand getCloneCommandByCloneRepository() {
return this.mockCloneCommand;
}
}
}