Merge branch '1.3.x'

This commit is contained in:
Dave Syer
2017-06-26 14:50:37 +01:00
6 changed files with 311 additions and 316 deletions

View File

@@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>1.3.3.BUILD-SNAPSHOT</version>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<scm>

View File

@@ -75,23 +75,23 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
private static final String FILE_URI_PREFIX = "file:";
/**
* Timeout (in seconds) for obtaining HTTP or SSH connection (if applicable). Default
* 5 seconds.
* Timeout (in seconds) for obtaining HTTP or SSH connection (if
* applicable). Default 5 seconds.
*/
private int timeout = 5;
private boolean initialized;
/**
* Flag to indicate that the repository should be cloned on startup (not on demand).
* Generally leads to slower startup but faster first query.
* Flag to indicate that the repository should be cloned on startup (not on
* demand). Generally leads to slower startup but faster first query.
*/
private boolean cloneOnStart = false;
private JGitEnvironmentRepository.JGitFactory gitFactory = new JGitEnvironmentRepository.JGitFactory();
private String defaultLabel = DEFAULT_LABEL;
/**
* The credentials provider to use to connect to the Git repository.
*/
@@ -103,8 +103,8 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
private TransportConfigCallback transportConfigCallback;
/**
* Flag to indicate that the repository should force pull. If true discard any local
* changes and take from remote repository.
* Flag to indicate that the repository should force pull. If true discard
* any local changes and take from remote repository.
*/
private boolean forcePull;
@@ -161,8 +161,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
}
@Override
public synchronized Locations getLocations(String application, String profile,
String label) {
public synchronized Locations getLocations(String application, String profile, String label) {
if (label == null) {
label = this.defaultLabel;
}
@@ -173,8 +172,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
@Override
public void afterPropertiesSet() throws Exception {
Assert.state(getUri() != null,
"You need to configure a uri for the git repository");
Assert.state(getUri() != null, "You need to configure a uri for the git repository");
initialize();
if (this.cloneOnStart) {
initClonedRepository();
@@ -191,41 +189,35 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
git = createGitClient();
if (shouldPull(git)) {
fetch(git, label);
//checkout after fetch so we can get any new branches, tags, ect.
// checkout after fetch so we can get any new branches, tags,
// ect.
checkout(git, label);
if(isBranch(git, label)) {
//merge results from fetch
if (isBranch(git, label)) {
// merge results from fetch
merge(git, label);
if (!isClean(git)) {
logger.warn("The local repository is dirty. Resetting it to origin/"
+ label + ".");
logger.warn("The local repository is dirty. Resetting it to origin/" + label + ".");
resetHard(git, label, "refs/remotes/origin/" + label);
}
}
}
else{
//nothing to update so just checkout
} else {
// nothing to update so just checkout
checkout(git, label);
}
//always return what is currently HEAD as the version
return git.getRepository().getRef("HEAD").getObjectId().getName();
}
catch (RefNotFoundException e) {
// always return what is currently HEAD as the version
return git.getRepository().findRef("HEAD").getObjectId().getName();
} catch (RefNotFoundException e) {
throw new NoSuchLabelException("No such label: " + label, e);
}
catch (GitAPIException e) {
} catch (GitAPIException e) {
throw new IllegalStateException("Cannot clone or checkout repository", e);
}
catch (Exception e) {
} catch (Exception e) {
throw new IllegalStateException("Cannot load environment", e);
}
finally {
} finally {
try {
if (git != null) {
git.close();
}
}
catch (Exception e) {
} catch (Exception e) {
this.logger.warn("Could not close git repository", e);
}
}
@@ -233,6 +225,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
/**
* Clones the remote repository and then opens a connection to it.
*
* @throws GitAPIException
* @throws IOException
*/
@@ -255,41 +248,35 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
CheckoutCommand checkout = git.checkout();
if (shouldTrack(git, label)) {
trackBranch(git, checkout, label);
}
else {
} else {
// works for tags and local branches
checkout.setName(label);
}
return checkout.call();
}
protected boolean shouldPull(Git git) throws GitAPIException {
boolean shouldPull;
Status gitStatus = git.status().call();
boolean isWorkingTreeClean = gitStatus.isClean();
String originUrl = git.getRepository().getConfig().getString("remote", "origin",
"url");
String originUrl = git.getRepository().getConfig().getString("remote", "origin", "url");
if (this.forcePull && !isWorkingTreeClean) {
shouldPull = true;
logDirty(gitStatus);
}
else {
} else {
shouldPull = isWorkingTreeClean && originUrl != null;
}
if (!isWorkingTreeClean && !this.forcePull) {
this.logger.info("Cannot pull from remote " + originUrl
+ ", the working tree is not clean.");
this.logger.info("Cannot pull from remote " + originUrl + ", the working tree is not clean.");
}
return shouldPull;
}
@SuppressWarnings("unchecked")
private void logDirty(Status status) {
Set<String> dirties = dirties(status.getAdded(), status.getChanged(),
status.getRemoved(), status.getMissing(), status.getModified(),
status.getConflicting(), status.getUntracked());
Set<String> dirties = dirties(status.getAdded(), status.getChanged(), status.getRemoved(), status.getMissing(),
status.getModified(), status.getConflicting(), status.getUntracked());
this.logger.warn(String.format("Dirty files found: %s", dirties));
}
@@ -314,15 +301,14 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
configureCommand(fetch);
try {
FetchResult result = fetch.call();
if(result.getTrackingRefUpdates() != null && result.getTrackingRefUpdates().size() > 0) {
if (result.getTrackingRefUpdates() != null && result.getTrackingRefUpdates().size() > 0) {
logger.info("Fetched for remote " + label + " and found " + result.getTrackingRefUpdates().size()
+ " updates");
+ " updates");
}
return result;
}
catch (Exception ex) {
String message = "Could not fetch remote for " + label + " remote: " + git
.getRepository().getConfig().getString("remote", "origin", "url");
} catch (Exception ex) {
String message = "Could not fetch remote for " + label + " remote: "
+ git.getRepository().getConfig().getString("remote", "origin", "url");
warn(message, ex);
return null;
}
@@ -331,16 +317,15 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
private MergeResult merge(Git git, String label) {
try {
MergeCommand merge = git.merge();
merge.include(git.getRepository().getRef("origin/" + label));
merge.include(git.getRepository().findRef("origin/" + label));
MergeResult result = merge.call();
if(!result.getMergeStatus().isSuccessful()) {
if (!result.getMergeStatus().isSuccessful()) {
this.logger.warn("Merged from remote " + label + " with result " + result.getMergeStatus());
}
return result;
}
catch (Exception ex) {
String message = "Could not merge remote for " + label + " remote: " + git
.getRepository().getConfig().getString("remote", "origin", "url");
} catch (Exception ex) {
String message = "Could not merge remote for " + label + " remote: "
+ git.getRepository().getConfig().getString("remote", "origin", "url");
warn(message, ex);
return null;
}
@@ -352,31 +337,38 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
reset.setMode(ResetType.HARD);
try {
Ref resetRef = reset.call();
if(resetRef != null) {
if (resetRef != null) {
this.logger.info("Reset label " + label + " to version " + resetRef.getObjectId());
}
return resetRef;
}
catch (Exception ex) {
String message = "Could not reset to remote for " + label + " (current ref="
+ ref + "), remote: " + git.getRepository().getConfig()
.getString("remote", "origin", "url");
} catch (Exception ex) {
String message = "Could not reset to remote for " + label + " (current ref=" + ref + "), remote: "
+ git.getRepository().getConfig().getString("remote", "origin", "url");
warn(message, ex);
return null;
}
}
private Git createGitClient() throws IOException, GitAPIException {
if (new File(getBasedir(), ".git").exists()) {
return openGitRepository();
File lock = new File(getWorkingDirectory(), ".git/index.lock");
if (lock.exists()) {
// The only way this can happen is if another JVM (e.g. one that
// crashed earlier) created the lock. We can attempt to recover by
// wiping the slate clean.
logger.info("Deleting stale JGit lock file at " + lock);
lock.delete();
}
else {
if (new File(getWorkingDirectory(), ".git").exists()) {
return openGitRepository();
} else {
return copyRepository();
}
}
// Synchronize here so that multiple requests don't all try and delete the base dir
// together (this is a once only operation, so it only holds things up on the first
// Synchronize here so that multiple requests don't all try and delete the
// base dir
// together (this is a once only operation, so it only holds things up on
// the first
// request).
private synchronized Git copyRepository() throws IOException, GitAPIException {
deleteBaseDirIfExists();
@@ -384,8 +376,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
Assert.state(getBasedir().exists(), "Could not create basedir: " + getBasedir());
if (getUri().startsWith(FILE_URI_PREFIX)) {
return copyFromLocalRepository();
}
else {
} else {
return cloneToBasedir();
}
}
@@ -407,13 +398,12 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
}
private Git cloneToBasedir() throws GitAPIException {
CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository()
.setURI(getUri()).setDirectory(getBasedir());
CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository().setURI(getUri())
.setDirectory(getBasedir());
configureCommand(clone);
try {
return clone.call();
}
catch (GitAPIException e) {
} catch (GitAPIException e) {
deleteBaseDirIfExists();
throw e;
}
@@ -423,8 +413,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
if (getBasedir().exists()) {
try {
FileUtils.delete(getBasedir(), FileUtils.RECURSIVE);
}
catch (IOException e) {
} catch (IOException e) {
throw new IllegalStateException("Failed to initialize base directory", e);
}
}
@@ -473,8 +462,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
StatusCommand status = git.status();
try {
return status.call().isClean();
}
catch (Exception e) {
} catch (Exception e) {
String message = "Could not execute status command on local repository. Cause: ("
+ e.getClass().getSimpleName() + ") " + e.getMessage();
warn(message, e);
@@ -483,8 +471,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
}
private void trackBranch(Git git, CheckoutCommand checkout, String label) {
checkout.setCreateBranch(true).setName(label)
.setUpstreamMode(SetupUpstreamMode.TRACK)
checkout.setCreateBranch(true).setName(label).setUpstreamMode(SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + label);
}
@@ -496,8 +483,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
return containsBranch(git, label, null);
}
private boolean containsBranch(Git git, String label, ListMode listMode)
throws GitAPIException {
private boolean containsBranch(Git git, String label, ListMode listMode) throws GitAPIException {
ListBranchCommand command = git.branchList();
if (listMode != null) {
command.setListMode(listMode);
@@ -520,7 +506,8 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
/**
* Wraps the static method calls to {@link org.eclipse.jgit.api.Git} and
* {@link org.eclipse.jgit.api.CloneCommand} allowing for easier unit testing.
* {@link org.eclipse.jgit.api.CloneCommand} allowing for easier unit
* testing.
*/
static class JGitFactory {
@@ -543,7 +530,8 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
}
/**
* @param gitCredentialsProvider the gitCredentialsProvider to set
* @param gitCredentialsProvider
* the gitCredentialsProvider to set
*/
public void setGitCredentialsProvider(CredentialsProvider gitCredentialsProvider) {
this.gitCredentialsProvider = gitCredentialsProvider;

View File

@@ -27,7 +27,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -0,0 +1,25 @@
package org.springframework.cloud.config.server;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.cloud.config.server.environment.EnvironmentEncryptorEnvironmentRepositoryTests;
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryIntegrationTests;
import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepositoryIntegrationTests;
import org.springframework.cloud.config.server.environment.NativeEnvironmentRepositoryTests;
import org.springframework.cloud.config.server.environment.SVNKitEnvironmentRepositoryIntegrationTests;
/**
* A test suite for probing weird ordering problems in the tests.
*
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ MultipleJGitEnvironmentRepositoryIntegrationTests.class,
JGitEnvironmentRepositoryIntegrationTests.class, EnvironmentEncryptorEnvironmentRepositoryTests.class,
NativeEnvironmentRepositoryTests.class, SVNKitEnvironmentRepositoryIntegrationTests.class })
@Ignore
public class AdhocTestSuite {
}

View File

@@ -16,6 +16,14 @@
package org.springframework.cloud.config.server.environment;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -41,7 +49,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.config.environment.Environment;
@@ -52,17 +60,10 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StreamUtils;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
* @author Roy Clarkson
@@ -95,8 +96,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
String uri = ConfigServerTestUtils.prepareLocalRepo();
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.properties("spring.cloud.config.server.git.uri:" + uri).run();
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
@@ -111,12 +111,10 @@ public class JGitEnvironmentRepositoryIntegrationTests {
String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri);
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals("bar",
environment.getPropertySources().get(0).getSource().get("foo"));
assertEquals("bar", environment.getPropertySources().get(0).getSource().get("foo"));
Git git = Git.open(ResourceUtils.getFile(uri).getAbsoluteFile());
git.checkout().setName("master").call();
StreamUtils.copy("foo: foo", Charset.defaultCharset(),
@@ -124,15 +122,14 @@ public class JGitEnvironmentRepositoryIntegrationTests {
git.add().addFilepattern("bar.properties").call();
git.commit().setMessage("Updated for pull").call();
environment = repository.findOne("bar", "staging", "master");
assertEquals("foo",
environment.getPropertySources().get(0).getSource().get("foo"));
assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo"));
}
/**
* Tests a special use case where the remote repository has been updated with a forced
* push conflicting with the local repo of the Config Server. The Config Server has to
* reset hard on the new reference because a simple pull operation could result in a
* conflicting local repository.
* Tests a special use case where the remote repository has been updated
* with a forced push conflicting with the local repo of the Config Server.
* The Config Server has to reset hard on the new reference because a simple
* pull operation could result in a conflicting local repository.
*/
@Test
public void pullDirtyRepo() throws Exception {
@@ -144,23 +141,19 @@ public class JGitEnvironmentRepositoryIntegrationTests {
Git git = Git.open(ResourceUtils.getFile(uri).getAbsoluteFile());
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url",
remote.getDirectory().getAbsolutePath());
config.setString("remote", "origin", "fetch",
"+refs/heads/*:refs/remotes/origin/*");
config.setString("remote", "origin", "url", remote.getDirectory().getAbsolutePath());
config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
config.save();
// Pushes the raw branch to remote repository.
git.push().call();
String commitToRevertBeforePull = git.log().setMaxCount(1).call().iterator()
.next().getName();
String commitToRevertBeforePull = git.log().setMaxCount(1).call().iterator().next().getName();
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri);
JGitEnvironmentRepository repository = this.context
.getBean(JGitEnvironmentRepository.class);
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
// Fetches the repository for the first time.
SearchPathLocator.Locations locations = repository.getLocations("bar", "test", "raw");
@@ -170,17 +163,14 @@ public class JGitEnvironmentRepositoryIntegrationTests {
git.reset().setMode(ResetType.HARD).setRef("master").call();
// Generate a conflicting commit who will be forced on the origin.
Path applicationFilePath = Paths
.get(ResourceUtils.getFile(uri).getAbsoluteFile() + "/application.yml");
Path applicationFilePath = Paths.get(ResourceUtils.getFile(uri).getAbsoluteFile() + "/application.yml");
Files.write(applicationFilePath,
Arrays.asList("info:", " foo: bar", "raw: false"),
StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
Files.write(applicationFilePath, Arrays.asList("info:", " foo: bar", "raw: false"), StandardCharsets.UTF_8,
StandardOpenOption.TRUNCATE_EXISTING);
git.add().addFilepattern(".").call();
git.commit().setMessage("Conflicting commit.").call();
git.push().setForce(true).call();
String conflictingCommit = git.log().setMaxCount(1).call().iterator()
.next().getName();
String conflictingCommit = git.log().setMaxCount(1).call().iterator().next().getName();
// Reset to the raw branch.
git.reset().setMode(ResetType.HARD).setRef(commitToRevertBeforePull).call();
@@ -189,8 +179,17 @@ public class JGitEnvironmentRepositoryIntegrationTests {
locations = repository.getLocations("bar", "test", "raw");
assertEquals(locations.getVersion(), conflictingCommit);
assertTrue("Local repository is not cleaned after retrieving resources.",
git.status().call().isClean());
assertTrue("Local repository is not cleaned after retrieving resources.", git.status().call().isClean());
}
@Test
public void pullMissingRepo() throws Exception {
pull();
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
new File(repository.getUri().replaceAll("file:", ""), ".git/index.lock").createNewFile();
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo"));
}
@Test
@@ -198,10 +197,8 @@ public class JGitEnvironmentRepositoryIntegrationTests {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
// TODO: why didn't .properties() work for me?
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.searchPaths=sub");
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
.run("--spring.cloud.config.server.git.uri=" + uri, "--spring.cloud.config.server.git.searchPaths=sub");
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
@@ -214,8 +211,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
// TODO: why didn't .properties() work for me?
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.searchPaths={application}");
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("foo,bar", "staging", "master");
Environment environment = repository.findOne("foo,bar", "staging", "master");
assertEquals(3, environment.getPropertySources().size());
@@ -228,8 +224,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
// TODO: why didn't .properties() work for me?
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.searchPaths={profile}");
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("foo,bar", "staging", "master");
Environment environment = repository.findOne("staging", "foo,bar", "master");
assertEquals(3, environment.getPropertySources().size());
@@ -238,15 +233,13 @@ public class JGitEnvironmentRepositoryIntegrationTests {
@Test
public void singleElementArrayIndexSearchPath() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("nested-repo");
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.searchPaths[0]={application}");
JGitEnvironmentRepository repository = this.context
.getBean(JGitEnvironmentRepository.class);
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false).run(
"--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.searchPaths[0]={application}");
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
assertThat(repository.getSearchPaths(), Matchers.arrayContaining("{application}"));
assertFalse(Arrays.equals(repository.getSearchPaths(),
new JGitEnvironmentRepository(repository.getEnvironment())
.getSearchPaths()));
new JGitEnvironmentRepository(repository.getEnvironment()).getSearchPaths()));
}
@Test
@@ -254,8 +247,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
String uri = ConfigServerTestUtils.prepareLocalRepo();
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.properties("spring.cloud.config.server.git.uri:" + uri).run();
JGitEnvironmentRepository repository = this.context
.getBean(JGitEnvironmentRepository.class);
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
assertEquals("master", repository.getDefaultLabel());
}
@@ -264,8 +256,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
String uri = ConfigServerTestUtils.prepareLocalRepo();
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.properties("spring.cloud.config.server.git.uri:" + uri).run();
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "unknownlabel");
}
@@ -273,11 +264,9 @@ public class JGitEnvironmentRepositoryIntegrationTests {
public void findOne_CloneOnStartTrue_FindOneSuccess() throws Exception {
ConfigServerTestUtils.prepareLocalRepo();
String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.cloneOnStart=true");
EnvironmentRepository repository = this.context
.getBean(JGitEnvironmentRepository.class);
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false).run(
"--spring.cloud.config.server.git.uri=" + uri, "--spring.cloud.config.server.git.cloneOnStart=true");
EnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
assertTrue(((JGitEnvironmentRepository) repository).isCloneOnStart());
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
@@ -290,15 +279,12 @@ public class JGitEnvironmentRepositoryIntegrationTests {
public void findOne_FileAddedToRepo_FindOneSuccess() throws Exception {
ConfigServerTestUtils.prepareLocalRepo();
String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.cloneOnStart=true");
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false).run(
"--spring.cloud.config.server.git.uri=" + uri, "--spring.cloud.config.server.git.cloneOnStart=true");
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals("bar",
environment.getPropertySources().get(0).getSource().get("foo"));
assertEquals("bar", environment.getPropertySources().get(0).getSource().get("foo"));
Git git = Git.open(ResourceUtils.getFile(uri).getAbsoluteFile());
git.checkout().setName("master").call();
StreamUtils.copy("foo: foo", Charset.defaultCharset(),
@@ -306,8 +292,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
git.add().addFilepattern("bar.properties").call();
git.commit().setMessage("Updated for pull").call();
environment = repository.findOne("bar", "staging", "master");
assertEquals("foo",
environment.getPropertySources().get(0).getSource().get("foo"));
assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo"));
}
@Test
@@ -315,83 +300,80 @@ public class JGitEnvironmentRepositoryIntegrationTests {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
// TODO: why didn't .properties() work for me?
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.searchPaths=sub",
.run("--spring.cloud.config.server.git.uri=" + uri, "--spring.cloud.config.server.git.searchPaths=sub",
"--spring.cloud.config.server.git.cloneOnStart=true");
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
}
@Test(expected = NoSuchLabelException.class)
public void findOne_FindInvalidLabel_IllegalStateExceptionThrown()
throws IOException {
public void findOne_FindInvalidLabel_IllegalStateExceptionThrown() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo();
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.properties("spring.cloud.config.server.git.uri:" + uri,
"--spring.cloud.config.server.git.cloneOnStart=true")
.run();
EnvironmentRepository repository = this.context
.getBean(EnvironmentRepository.class);
EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "unknownlabel");
}
@Test
public void testVersionUpdate() throws Exception {
JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
JGitConfigServerTestData testData = JGitConfigServerTestData
.prepareClonedGitRepository(TestConfiguration.class);
//get our starting versions
// get our starting versions
String startingLocalVersion = getCommitID(testData.getClonedGit().getGit(), "master");
String startingRemoteVersion = getCommitID(testData.getServerGit().getGit(), "master");
//make sure we get the right version out of the gate
// make sure we get the right version out of the gate
Environment environment = testData.getRepository().findOne("bar", "staging", "master");
//make sure the environments version is the same as the remote repo version
// make sure the environments version is the same as the remote repo
// version
assertEquals(environment.getVersion(), startingRemoteVersion);
//update the remote repo
FileOutputStream out = new FileOutputStream(new File(testData.getServerGit().getGitWorkingDirectory(), "bar.properties"));
// update the remote repo
FileOutputStream out = new FileOutputStream(
new File(testData.getServerGit().getGitWorkingDirectory(), "bar.properties"));
StreamUtils.copy("foo: foo", Charset.defaultCharset(), out);
testData.getServerGit().getGit().add().addFilepattern("bar.properties").call();
testData.getServerGit().getGit().commit().setMessage("Updated for pull").call();
//pull the environment again which should update the local repo from the just updated remote repo
// pull the environment again which should update the local repo from
// the just updated remote repo
environment = testData.getRepository().findOne("bar", "staging", "master");
//do some more check outs to get updated version numbers
// do some more check outs to get updated version numbers
String updatedLocalVersion = getCommitID(testData.getClonedGit().getGit(), "master");
String updatedRemoteVersion = getCommitID(testData.getClonedGit().getGit(), "master");
//make sure our versions have been updated
// make sure our versions have been updated
assertEquals(updatedRemoteVersion, updatedLocalVersion);
assertNotEquals(updatedRemoteVersion, startingRemoteVersion);
assertNotEquals(updatedLocalVersion, startingLocalVersion);
//make sure our environment also reflects the updated version
//this used to have a bug
// make sure our environment also reflects the updated version
// this used to have a bug
assertEquals(environment.getVersion(), updatedRemoteVersion);
}
@Test
public void testNewRemoteBranch() throws Exception {
JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
JGitConfigServerTestData testData = JGitConfigServerTestData
.prepareClonedGitRepository(TestConfiguration.class);
Environment environment = testData.getRepository().findOne("bar", "staging", "master");
Object fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "bar");
testData.getServerGit().getGit().branchCreate()
.setName("testNewRemoteBranch")
.call();
testData.getServerGit().getGit().branchCreate().setName("testNewRemoteBranch").call();
testData.getServerGit().getGit().checkout()
.setName("testNewRemoteBranch")
.call();
testData.getServerGit().getGit().checkout().setName("testNewRemoteBranch").call();
//update the remote repo
// update the remote repo
FileOutputStream out = new FileOutputStream(
new File(testData.getServerGit().getGitWorkingDirectory(), "/bar.properties"));
StreamUtils.copy("foo: branchBar", Charset.defaultCharset(), out);
@@ -405,7 +387,8 @@ public class JGitEnvironmentRepositoryIntegrationTests {
@Test
public void testNewRemoteTag() throws Exception {
JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
JGitConfigServerTestData testData = JGitConfigServerTestData
.prepareClonedGitRepository(TestConfiguration.class);
Git serverGit = testData.getServerGit().getGit();
@@ -415,10 +398,10 @@ public class JGitEnvironmentRepositoryIntegrationTests {
serverGit.checkout().setName("master").call();
//create a new tag
// create a new tag
serverGit.tag().setName("testTag").setMessage("Testing a tag").call();
//update the remote repo
// update the remote repo
FileOutputStream out = new FileOutputStream(
new File(testData.getServerGit().getGitWorkingDirectory(), "/bar.properties"));
StreamUtils.copy("foo: testAfterTag", Charset.defaultCharset(), out);
@@ -433,50 +416,52 @@ public class JGitEnvironmentRepositoryIntegrationTests {
fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "bar");
//now move the tag and test again
serverGit.tag().setName("testTag").setForceUpdate(true).setMessage("Testing a moved tag").call();
// now move the tag and test again
serverGit.tag().setName("testTag").setForceUpdate(true).setMessage("Testing a moved tag").call();
environment = testData.getRepository().findOne("bar", "staging", "testTag");
fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "testAfterTag");
environment = testData.getRepository().findOne("bar", "staging", "testTag");
fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "testAfterTag");
}
@Test
@Test
public void testNewCommitID() throws Exception {
JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
JGitConfigServerTestData testData = JGitConfigServerTestData
.prepareClonedGitRepository(TestConfiguration.class);
//get our starting versions
// get our starting versions
String startingRemoteVersion = getCommitID(testData.getServerGit().getGit(), "master");
//make sure we get the right version out of the gate
// make sure we get the right version out of the gate
Environment environment = testData.getRepository().findOne("bar", "staging", "master");
assertEquals(environment.getVersion(), startingRemoteVersion);
//update the remote repo
FileOutputStream out = new FileOutputStream(new File(testData.getServerGit().getGitWorkingDirectory(), "bar.properties"));
// update the remote repo
FileOutputStream out = new FileOutputStream(
new File(testData.getServerGit().getGitWorkingDirectory(), "bar.properties"));
StreamUtils.copy("foo: barNewCommit", Charset.defaultCharset(), out);
testData.getServerGit().getGit().add().addFilepattern("bar.properties").call();
testData.getServerGit().getGit().commit().setMessage("Updated for pull").call();
String updatedRemoteVersion = getCommitID(testData.getServerGit().getGit(), "master");
//do a normal request and verify we get the new version
// do a normal request and verify we get the new version
environment = testData.getRepository().findOne("bar", "staging", "master");
assertEquals(environment.getVersion(), updatedRemoteVersion);
Object fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "barNewCommit");
//request the prior commit ID and make sure we get it
// request the prior commit ID and make sure we get it
environment = testData.getRepository().findOne("bar", "staging", startingRemoteVersion);
assertEquals(environment.getVersion(), startingRemoteVersion);
fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "bar");
}
@Test(expected = NoSuchLabelException.class)
public void testUnknownLabelWithRemote() throws Exception {
JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
JGitConfigServerTestData testData = JGitConfigServerTestData
.prepareClonedGitRepository(TestConfiguration.class);
testData.getRepository().findOne("bar", "staging", "BADLabel");
}
@@ -490,9 +475,9 @@ public class JGitEnvironmentRepositoryIntegrationTests {
public void passphrase() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("config-repo");
final String passphrase = "thisismypassphrase";
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.passphrase=" + passphrase);
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false).run(
"--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.passphrase=" + passphrase);
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
assertThat(repository.getPassphrase(), Matchers.containsString(passphrase));
}
@@ -501,9 +486,9 @@ public class JGitEnvironmentRepositoryIntegrationTests {
public void strictHostKeyChecking() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("config-repo");
final boolean strictHostKeyChecking = true;
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.run("--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.strict-host-key-checking=" + strictHostKeyChecking);
this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false).run(
"--spring.cloud.config.server.git.uri=" + uri,
"--spring.cloud.config.server.git.strict-host-key-checking=" + strictHostKeyChecking);
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
assertEquals(repository.isStrictHostKeyChecking(), strictHostKeyChecking);
}
@@ -511,10 +496,8 @@ public class JGitEnvironmentRepositoryIntegrationTests {
@Test
public void shouldSetTransportConfigCallback() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo();
this.context = new SpringApplicationBuilder(TestConfigurationWithTransportConfigCallback.class)
.web(false)
.properties("spring.cloud.config.server.git.uri:" + uri)
.run();
this.context = new SpringApplicationBuilder(TestConfigurationWithTransportConfigCallback.class).web(false)
.properties("spring.cloud.config.server.git.uri:" + uri).run();
JGitEnvironmentRepository repository = this.context.getBean(JGitEnvironmentRepository.class);
assertNotNull(repository.getTransportConfigCallback());
@@ -522,15 +505,13 @@ public class JGitEnvironmentRepositoryIntegrationTests {
@Configuration
@EnableConfigurationProperties(ConfigServerProperties.class)
@Import({ PropertyPlaceholderAutoConfiguration.class,
EnvironmentRepositoryConfiguration.class })
@Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class })
protected static class TestConfiguration {
}
@Configuration
@EnableConfigurationProperties(ConfigServerProperties.class)
@Import({ PropertyPlaceholderAutoConfiguration.class,
EnvironmentRepositoryConfiguration.class })
@Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class })
protected static class TestConfigurationWithTransportConfigCallback {
@Bean

View File

@@ -16,6 +16,21 @@
package org.springframework.cloud.config.server.environment;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
@@ -48,6 +63,7 @@ import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FS;
@@ -66,21 +82,6 @@ import org.springframework.core.env.StandardEnvironment;
import com.jcraft.jsch.Session;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Dave Syer
*
@@ -88,8 +89,7 @@ import static org.mockito.Mockito.when;
public class JGitEnvironmentRepositoryTests {
private StandardEnvironment environment = new StandardEnvironment();
private JGitEnvironmentRepository repository = new JGitEnvironmentRepository(
this.environment);
private JGitEnvironmentRepository repository = new JGitEnvironmentRepository(this.environment);
private File basedir = new File("target/config");
@@ -110,8 +110,7 @@ public class JGitEnvironmentRepositoryTests {
this.repository.findOne("bar", "staging", "master");
Environment environment = this.repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
assertEquals(this.repository.getUri() + "/bar.properties",
environment.getPropertySources().get(0).getName());
assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
assertVersion(environment);
}
@@ -119,7 +118,7 @@ public class JGitEnvironmentRepositoryTests {
public void nested() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.repository.setUri(uri);
this.repository.setSearchPaths(new String[] {"sub"});
this.repository.setSearchPaths(new String[] { "sub" });
this.repository.findOne("bar", "staging", "master");
Environment environment = this.repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
@@ -132,7 +131,7 @@ public class JGitEnvironmentRepositoryTests {
public void placeholderInSearchPath() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.repository.setUri(uri);
this.repository.setSearchPaths(new String[] {"{application}"});
this.repository.setSearchPaths(new String[] { "{application}" });
this.repository.findOne("sub", "staging", "master");
Environment environment = this.repository.findOne("sub", "staging", "master");
assertEquals(1, environment.getPropertySources().size());
@@ -151,7 +150,7 @@ public class JGitEnvironmentRepositoryTests {
public void nestedPattern() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
this.repository.setUri(uri);
this.repository.setSearchPaths(new String[] {"sub*"});
this.repository.setSearchPaths(new String[] { "sub*" });
this.repository.findOne("bar", "staging", "master");
Environment environment = this.repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
@@ -165,8 +164,7 @@ public class JGitEnvironmentRepositoryTests {
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "raw");
assertEquals(2, environment.getPropertySources().size());
assertEquals(this.repository.getUri() + "/bar.properties",
environment.getPropertySources().get(0).getName());
assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
assertVersion(environment);
}
@@ -175,8 +173,7 @@ public class JGitEnvironmentRepositoryTests {
this.repository.setBasedir(this.basedir);
Environment environment = this.repository.findOne("bar", "staging", "foo");
assertEquals(2, environment.getPropertySources().size());
assertEquals(this.repository.getUri() + "/bar.properties", environment
.getPropertySources().get(0).getName());
assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
assertVersion(environment);
}
@@ -186,8 +183,7 @@ public class JGitEnvironmentRepositoryTests {
this.repository.findOne("bar", "staging", "master");
Environment environment = this.repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
assertEquals(this.repository.getUri() + "/bar.properties",
environment.getPropertySources().get(0).getName());
assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
assertVersion(environment);
}
@@ -199,8 +195,7 @@ public class JGitEnvironmentRepositoryTests {
this.repository.findOne("bar", "staging", "master");
Environment environment = this.repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
assertEquals(this.repository.getUri() + "/bar.properties",
environment.getPropertySources().get(0).getName());
assertEquals(this.repository.getUri() + "/bar.properties", environment.getPropertySources().get(0).getName());
assertVersion(environment);
}
@@ -217,16 +212,14 @@ public class JGitEnvironmentRepositoryTests {
}
@Test
public void afterPropertiesSet_CloneOnStartTrue_CloneAndFetchCalled()
throws Exception {
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);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setCloneOnStart(true);
@@ -235,16 +228,14 @@ public class JGitEnvironmentRepositoryTests {
}
@Test
public void afterPropertiesSet_CloneOnStartFalse_CloneAndFetchNotCalled()
throws Exception {
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);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.afterPropertiesSet();
@@ -253,16 +244,14 @@ public class JGitEnvironmentRepositoryTests {
}
@Test
public void afterPropertiesSet_CloneOnStartTrueWithFileURL_CloneAndFetchNotCalled()
throws Exception {
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);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("file://somefilesystem/somegitrepo");
envRepository.setCloneOnStart(true);
@@ -286,8 +275,7 @@ public class JGitEnvironmentRepositoryTests {
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(false);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(
this.environment);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment);
repo.setForcePull(true);
boolean shouldPull = repo.shouldPull(git);
@@ -310,8 +298,7 @@ public class JGitEnvironmentRepositoryTests {
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(false);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(
this.environment);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment);
boolean shouldPull = repo.shouldPull(git);
@@ -333,8 +320,7 @@ public class JGitEnvironmentRepositoryTests {
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(
this.environment);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment);
boolean shouldPull = repo.shouldPull(git);
@@ -347,11 +333,9 @@ public class JGitEnvironmentRepositoryTests {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(
this.environment);
this.repository.setGitFactory(factory);
//refresh()->shouldPull
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
@@ -363,15 +347,21 @@ public class JGitEnvironmentRepositoryTests {
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
//refresh()->fetch
// refresh()->fetch
FetchCommand fetchCommand = mock(FetchCommand.class);
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote")); //here is our exception we are testing
when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote")); // here
// is
// our
// exception
// we
// are
// testing
//refresh()->checkout
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
//refresh()->checkout->containsBranch
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
@@ -381,20 +371,27 @@ public class JGitEnvironmentRepositoryTests {
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
//refresh()->merge
// 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
when(mergeCommand.call()).thenThrow(new NotMergedException()); // here
// is
// our
// exception
// we
// are
// testing
//refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName();
// refresh()->return
// git.getRepository().getRef("HEAD").getObjectId().getName();
Ref headRef = mock(Ref.class);
when(repository.getRef(anyString())).thenReturn(headRef);
when(repository.findRef(anyString())).thenReturn(headRef);
ObjectId newObjectId = ObjectId.fromRaw(new int[]{1,2,3,4,5});
ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
when(headRef.getObjectId()).thenReturn(newObjectId);
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", null);
assertEquals(locations.getVersion(),newObjectId.getName());
assertEquals(locations.getVersion(), newObjectId.getName());
}
@Test
@@ -403,8 +400,6 @@ public class JGitEnvironmentRepositoryTests {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(
this.environment);
this.repository.setGitFactory(factory);
//refresh()->shouldPull
@@ -425,7 +420,7 @@ public class JGitEnvironmentRepositoryTests {
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
when(fetchCommand.call()).thenReturn(fetchResult);
when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST);
when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.<TrackingRefUpdate>emptyList());
//refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
@@ -446,7 +441,7 @@ public class JGitEnvironmentRepositoryTests {
//refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName();
Ref headRef = mock(Ref.class);
when(repository.getRef(anyString())).thenReturn(headRef);
when(repository.findRef(anyString())).thenReturn(headRef);
ObjectId newObjectId = ObjectId.fromRaw(new int[]{1,2,3,4,5});
when(headRef.getObjectId()).thenReturn(newObjectId);
@@ -461,11 +456,9 @@ public class JGitEnvironmentRepositoryTests {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
JGitEnvironmentRepository repo = new JGitEnvironmentRepository(
this.environment);
this.repository.setGitFactory(factory);
//refresh()->shouldPull
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
@@ -477,17 +470,17 @@ public class JGitEnvironmentRepositoryTests {
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true).thenReturn(false);
//refresh()->fetch
// 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.EMPTY_LIST);
when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.<TrackingRefUpdate>emptyList());
//refresh()->checkout
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
//refresh()->checkout->containsBranch
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
@@ -497,29 +490,36 @@ public class JGitEnvironmentRepositoryTests {
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
//refresh()->merge
// 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
when(mergeCommand.call()).thenThrow(new NotMergedException()); // here
// is
// our
// exception
// we
// are
// testing
//refresh()->hardReset
// refresh()->hardReset
ResetCommand resetCommand = mock(ResetCommand.class);
when(git.reset()).thenReturn(resetCommand);
when(resetCommand.call()).thenReturn(ref);
//refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName();
// refresh()->return
// git.getRepository().getRef("HEAD").getObjectId().getName();
Ref headRef = mock(Ref.class);
when(repository.getRef(anyString())).thenReturn(headRef);
when(repository.findRef(anyString())).thenReturn(headRef);
ObjectId newObjectId = ObjectId.fromRaw(new int[]{1,2,3,4,5});
ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
when(headRef.getObjectId()).thenReturn(newObjectId);
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
assertEquals(locations.getVersion(),newObjectId.getName());
assertEquals(locations.getVersion(), newObjectId.getName());
}
@Test
public void shouldDeleteBaseDirWhenCloneFails() throws Exception {
public void shouldDeleteBaseDirWhenCloneFails() throws Exception {
Git mockGit = mock(Git.class);
CloneCommand mockCloneCommand = mock(CloneCommand.class);
@@ -527,19 +527,17 @@ public class JGitEnvironmentRepositoryTests {
when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);
when(mockCloneCommand.call()).thenThrow(new TransportException("failed to clone"));
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(
this.environment);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("http://somegitserver/somegitrepo");
envRepository.setBasedir(this.basedir);
try {
envRepository.findOne("bar", "staging", "master");
}
catch (Exception ex) {
} catch (Exception ex) {
// expected - ignore
}
assertFalse("baseDir should be deleted when clone fails", this.basedir.exists());
}
@@ -592,13 +590,14 @@ public class JGitEnvironmentRepositoryTests {
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
assertFalse(provider.isInteractive());
CredentialItem.StringType stringCredential = new CredentialItem.StringType(PassphraseCredentialsProvider.PROMPT, true);
CredentialItem.StringType stringCredential = new CredentialItem.StringType(PassphraseCredentialsProvider.PROMPT,
true);
assertTrue(provider.supports(stringCredential));
provider.get(new URIish(), stringCredential);
assertEquals(stringCredential.getValue(), passphrase);
}
@Test
public void gitCredentialsProviderFactoryCreatesPassphraseProvider() throws Exception {
final String passphrase = "mypassphrase";
@@ -620,7 +619,8 @@ public class JGitEnvironmentRepositoryTests {
CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
assertFalse(provider.isInteractive());
CredentialItem.StringType stringCredential = new CredentialItem.StringType(PassphraseCredentialsProvider.PROMPT, true);
CredentialItem.StringType stringCredential = new CredentialItem.StringType(PassphraseCredentialsProvider.PROMPT,
true);
assertTrue(provider.supports(stringCredential));
provider.get(new URIish(), stringCredential);
@@ -640,8 +640,8 @@ public class JGitEnvironmentRepositoryTests {
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
envRepository.setBasedir(new File("./mybasedir"));
envRepository.setGitCredentialsProvider(credentialsFactory.createFor(
envRepository.getUri(), username, password, null));
envRepository.setGitCredentialsProvider(
credentialsFactory.createFor(envRepository.getUri(), username, password, null));
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
@@ -665,12 +665,11 @@ public class JGitEnvironmentRepositoryTests {
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);
envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
envRepository.setUri(awsUri);
envRepository.setGitCredentialsProvider(credentialsFactory.createFor(
envRepository.getUri(), null, null, null));
envRepository.setGitCredentialsProvider(credentialsFactory.createFor(envRepository.getUri(), null, null, null));
envRepository.setCloneOnStart(true);
envRepository.afterPropertiesSet();
@@ -693,14 +692,17 @@ public class JGitEnvironmentRepositoryTests {
} catch (Exception e) {
final OpenSshConfig.Host hc = OpenSshConfig.get(FS.detect()).lookup("github.com");
JschConfigSessionFactory factory = (JschConfigSessionFactory) SshSessionFactory.getInstance();
// There's no public method that can be used to inspect the ssh configuration, so we'll reflect
// the configure method to allow us to check that the config property is set as expected.
Method configure = factory.getClass().getDeclaredMethod("configure", OpenSshConfig.Host.class, Session.class );
// There's no public method that can be used to inspect the ssh
// configuration, so we'll reflect
// the configure method to allow us to check that the config
// property is set as expected.
Method configure = factory.getClass().getDeclaredMethod("configure", OpenSshConfig.Host.class,
Session.class);
configure.setAccessible(true);
Session session = mock(Session.class);
ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
configure.invoke(factory, hc, session );
configure.invoke(factory, hc, session);
verify(session).setConfig(keyCaptor.capture(), valueCaptor.capture());
configure.setAccessible(false);
assertTrue("yes".equals(valueCaptor.getValue()));
@@ -710,7 +712,7 @@ public class JGitEnvironmentRepositoryTests {
@Test
public void shouldPrintStacktraceIfDebugEnabled() throws Exception {
final Log mockLogger = mock(Log.class);
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment){
JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment) {
@Override
public void afterPropertiesSet() throws Exception {
this.logger = mockLogger;
@@ -774,13 +776,12 @@ public class JGitEnvironmentRepositoryTests {
}
}
class MockGitFactory extends JGitEnvironmentRepository.JGitFactory {
private Git mockGit;
private CloneCommand mockCloneCommand;
public MockGitFactory (Git mockGit, CloneCommand mockCloneCommand) {
public MockGitFactory(Git mockGit, CloneCommand mockCloneCommand) {
this.mockGit = mockGit;
this.mockCloneCommand = mockCloneCommand;
}