From 2a3c60feb4ddee4349bf9d3a7ed630ddb6043e70 Mon Sep 17 00:00:00 2001 From: rlynch2 Date: Mon, 24 Oct 2016 17:41:05 -0700 Subject: [PATCH] Fixed bug which returns incorrect version number on first request after remote repo is updated. --- .../JGitEnvironmentRepository.java | 1 + ...EnvironmentRepositoryIntegrationTests.java | 95 +++++++++++++++++-- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java index fefdc8b1..1da604d7 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java @@ -171,6 +171,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository Ref ref = checkout(git, label); if (shouldPull(git, ref)) { pull(git, label, ref); + ref = git.getRepository().getRef(ref.getName()); if (!isClean(git)) { logger.warn("The local repository is dirty. Reseting it to origin/" diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java index e6a6b940..86ca92ad 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java @@ -16,12 +16,6 @@ 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.assertThat; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -33,8 +27,10 @@ import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; +import org.eclipse.jgit.api.CheckoutCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.ResetCommand.ResetType; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.util.FileUtils; @@ -51,9 +47,12 @@ import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; 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.*; + /** * @author Dave Syer * @author Roy Clarkson @@ -324,6 +323,90 @@ public class JGitEnvironmentRepositoryIntegrationTests { repository.findOne("bar", "staging", "unknownlabel"); } + @Test + public void testVersionUpdate() throws Exception { + //setup local repository + ConfigServerTestUtils.prepareLocalRepo(); + String uri = ConfigServerTestUtils.copyLocalRepo("config-copy"); + File localRepoFile = ResourceUtils.getFile(uri); + Git localGit = Git.open(localRepoFile.getAbsoluteFile()); + + //setup remote repository + File remoteDir = new File("target/repos/clone"); + if(remoteDir.exists()) { + FileSystemUtils.deleteRecursively(remoteDir); + }else{ + remoteDir.mkdirs(); + } + Git remoteGit = Git.cloneRepository() + .setURI( "file://" + localRepoFile.getAbsolutePath() ) + .setDirectory( remoteDir ) + .setBranch("master") + .setCloneAllBranches(true) + .call(); + StoredConfig config = localGit.getRepository().getConfig(); + config.setString("remote", "origin", "url", + remoteDir.getAbsolutePath()); + config.setString("remote", "origin", "fetch", + "+refs/heads/*:refs/remotes/origin/*"); + config.save(); + + //get commit ids + CheckoutCommand checkout = localGit.checkout(); + checkout.setName("master"); + Ref localRef = checkout.call(); + String localVersion = localRef.getObjectId().getName(); + + checkout = remoteGit.checkout(); + checkout.setName("master"); + Ref remoteRef = checkout.call(); + String remoteVersion = remoteRef.getObjectId().getName(); + + //verify the remote and local repo have the same commit ID + assertEquals(remoteVersion, localVersion); + + //setup our test spring application pointing to the local repo + this.context = new SpringApplicationBuilder(TestConfiguration.class).web(false) + .properties("spring.cloud.config.server.git.uri:" + "file://" + localRepoFile.getAbsolutePath()).run(); + EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class); + Environment environment = repository.findOne("bar", "staging", "master"); + + //make sure the environments version is the same as the remote repo version + assertEquals(environment.getVersion(), remoteVersion); + + //update the remote repo + FileOutputStream out = new FileOutputStream(remoteDir.getAbsolutePath() + "/bar.properties"); + StreamUtils.copy("foo: foo", Charset.defaultCharset(), out); + remoteGit.add().addFilepattern("bar.properties").call(); + remoteGit.commit().setMessage("Updated for pull").call(); + + //pull the environment again which should update the local repo from the just updated remote repo + environment = repository.findOne("bar", "staging", "master"); + + //do some more check outs to get updated version numbers + checkout = localGit.checkout(); + checkout.setName("master"); + localRef = checkout.call(); + String updatedLocalVersion = localRef.getObjectId().getName(); + + checkout = remoteGit.checkout(); + checkout.setName("master"); + remoteRef = checkout.call(); + String updatedRemoteVersion = remoteRef.getObjectId().getName(); + + //make sure our versions have been updated + assertEquals(updatedRemoteVersion, updatedLocalVersion); + assertNotEquals(updatedRemoteVersion, remoteVersion); + assertNotEquals(updatedLocalVersion, localVersion); + + //make sure our environment also reflects the updated version + //this used to have a bug + assertEquals(environment.getVersion(), updatedRemoteVersion); + + + + } + @Configuration @Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class })