Prevent JGit from loading local configuration in tests (#1272)

Without any preparations JGit defaults to read system / user / project
configuration from local file system. Users running tests locally
might see test failures due to their config diverging from the test
author config.

For me JGitEnvironmentRepositoryIntegrationTests.pullDirtyRepo() was
constantly failing, because JGit calculated a different hash for the
created commit than on the build server.
This commit is contained in:
Eduard Wirch
2019-04-22 17:43:02 +02:00
committed by Ryan Baxter
parent aa91914020
commit b4563ed7f6
15 changed files with 127 additions and 14 deletions

View File

@@ -2,6 +2,8 @@ package org.springframework.cloud.config.server;
import java.io.IOException;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -35,6 +37,9 @@ public class BootstrapConfigServerIntegrationTests {
@BeforeClass
public static void init() throws IOException {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo("encrypt-repo");
}

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.cloud.config.server;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,6 +52,9 @@ public class CompositeIntegrationTests {
@BeforeClass
public static void init() throws Exception {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo",
"target/repos/svn-config-repo");
@@ -100,6 +105,9 @@ public class CompositeIntegrationTests {
@BeforeClass
public static void init() throws Exception {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo",
"target/repos/svn-config-repo");

View File

@@ -2,6 +2,8 @@ package org.springframework.cloud.config.server;
import java.io.IOException;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -44,6 +46,9 @@ public class ConfigClientOffIntegrationTests {
@BeforeClass
public static void init() throws IOException {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
}

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.config.server;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -66,6 +68,9 @@ public class ConfigClientOnIntegrationTests {
@BeforeClass
public static void init() throws IOException {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
localRepo = ConfigServerTestUtils.prepareLocalRepo();
}

View File

@@ -2,6 +2,8 @@ package org.springframework.cloud.config.server;
import java.io.IOException;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -26,12 +28,15 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
webEnvironment = RANDOM_PORT)
@ActiveProfiles({ "test", "native" })
public class NativeConfigServerIntegrationTests {
@LocalServerPort
private int port;
@BeforeClass
public static void init() throws IOException{
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
}

View File

@@ -3,6 +3,8 @@ package org.springframework.cloud.config.server;
import java.io.IOException;
import java.util.Arrays;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -36,6 +38,9 @@ public class VanillaConfigServerIntegrationTests {
@BeforeClass
public static void init() throws IOException {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
}

View File

@@ -18,6 +18,8 @@ package org.springframework.cloud.config.server.config;
import java.util.HashMap;
import java.util.List;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -65,6 +67,9 @@ public class CustomCompositeEnvironmentRepositoryTests {
@BeforeClass
public static void init() throws Exception {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
}
@@ -113,6 +118,9 @@ public class CustomCompositeEnvironmentRepositoryTests {
@BeforeClass
public static void init() throws Exception {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
ConfigServerTestUtils.prepareLocalRepo();
}

View File

@@ -32,12 +32,15 @@ import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.*;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.SystemReader;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
@@ -65,6 +68,12 @@ public class JGitEnvironmentRepositoryConcurrencyTests {
private File basedir = new File("target/config");
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
if (this.basedir.exists()) {

View File

@@ -33,13 +33,16 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.SystemReader;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
@@ -79,6 +82,12 @@ public class JGitEnvironmentRepositoryIntegrationTests {
private File basedir = new File("target/config");
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
if (this.basedir.exists()) {
@@ -466,19 +475,19 @@ public class JGitEnvironmentRepositoryIntegrationTests {
fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
assertEquals(fooProperty, "bar");
}
@Test
/**
* In this scenario there is set the refresh rate so the remote repository is not fetched for every configuration read.
*
*
* There is more than one label queried - test and master - but only one such branch exists - master.
*
*
* There is a new commit to master branch but when client loads new configuration, the "test" label is queried first.
*/
public void testNewCommitIDWithRefreshRate() throws Exception {
JGitConfigServerTestData testData = JGitConfigServerTestData
.prepareClonedGitRepository(TestConfiguration.class);
// get our starting versions
String startingRemoteVersion = getCommitID(testData.getServerGit().getGit(), "master");
@@ -489,7 +498,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
} catch (NoSuchLabelException ex) {
// OK
}
// make sure we get the right version out of the gate
Environment environment = testData.getRepository().findOne("bar", "staging", "master");
assertEquals(environment.getVersion(), startingRemoteVersion);
@@ -504,7 +513,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
// Set refresh rate to 60 seconds (now it will fetch the remote repo only once)
testData.getRepository().setRefreshRate(60);
//Ask test label configuration first
try {
testData.getRepository().findOne("bar", "staging", "test");
@@ -512,7 +521,7 @@ public class JGitEnvironmentRepositoryIntegrationTests {
} catch (NoSuchLabelException ex) {
// OK
}
// do a normal request and verify we get the new version
environment = testData.getRepository().findOne("bar", "staging", "master");
assertEquals(environment.getVersion(), updatedRemoteVersion);

View File

@@ -41,6 +41,7 @@ import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.NotMergedException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
@@ -53,7 +54,9 @@ 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.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -94,6 +97,12 @@ public class JGitEnvironmentRepositoryTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
String uri = ConfigServerTestUtils.prepareLocalRepo();
@@ -523,18 +532,18 @@ public class JGitEnvironmentRepositoryTests {
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);
@@ -571,7 +580,7 @@ public class JGitEnvironmentRepositoryTests {
repo.setRefreshRate(2);
repo.refresh("master");
// Verify no fetch but merge only.
verify(git, times(0)).fetch();
verify(git).merge();

View File

@@ -18,7 +18,10 @@ package org.springframework.cloud.config.server.environment;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
@@ -42,6 +45,12 @@ public class MultipleJGitEnvironmentApplicationPlaceholderRepositoryTests {
private MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(this.environment,
new MultipleJGitEnvironmentProperties());
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
String defaultUri = ConfigServerTestUtils.prepareLocalRepo("config-repo");

View File

@@ -16,7 +16,10 @@
*/
package org.springframework.cloud.config.server.environment;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cloud.config.environment.Environment;
@@ -39,6 +42,12 @@ public class MultipleJGitEnvironmentLabelPlaceholderRepositoryTests {
new MultipleJGitEnvironmentProperties());
private String defaultUri;
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
this.defaultUri = ConfigServerTestUtils.prepareLocalRepo("master-labeltest-config-repo");

View File

@@ -19,7 +19,10 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cloud.config.environment.Environment;
@@ -47,6 +50,12 @@ public class MultipleJGitEnvironmentProfilePlaceholderRepositoryTests {
private MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(this.environment,
new MultipleJGitEnvironmentProperties());
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
String defaultUri = ConfigServerTestUtils.prepareLocalRepo("config-repo");

View File

@@ -25,9 +25,12 @@ import java.util.LinkedHashMap;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.SystemReader;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.matchers.ThrowableMessageMatcher;
@@ -59,6 +62,12 @@ public class MultipleJGitEnvironmentRepositoryIntegrationTests {
@Rule
public ExpectedException expected = ExpectedException.none();
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
if (this.basedir.exists()) {

View File

@@ -23,7 +23,10 @@ import java.util.HashMap;
import java.util.Map;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -59,6 +62,12 @@ public class MultipleJGitEnvironmentRepositoryTests {
private MultipleJGitEnvironmentRepository repository;
@BeforeClass
public static void initClass() {
// mock Git configuration to make tests independent of local Git configuration
SystemReader.setInstance(new MockSystemReader());
}
@Before
public void init() throws Exception {
String defaultUri = ConfigServerTestUtils.prepareLocalRepo("config-repo");