Make server tests work offline

This commit is contained in:
Dave Syer
2014-10-25 18:20:19 +01:00
parent 19cd6be001
commit 09bdb001d7
20 changed files with 121 additions and 35 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2013-2014 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
*
* http://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;
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.util.FileUtils;
import com.jcraft.jsch.Session;
/**
* @author Dave Syer
*
*/
public class ConfigServerTestUtils {
public static String prepareLocalRepo() throws IOException {
return prepareLocalRepo("target/test-classes", "config-repo", "target/config");
}
public static String prepareLocalRepo(String buildDir, String repoPath, String checkoutDir) throws IOException {
if (!repoPath.startsWith("/")) {
repoPath = "/" + repoPath;
}
if (!repoPath.endsWith("/")) {
repoPath = repoPath + "/";
}
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
@Override
protected void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
});
File dotGit = new File(buildDir + repoPath + ".git");
File git = new File(buildDir + repoPath + "git");
if (git.exists()) {
if (dotGit.exists()) {
FileUtils.delete(dotGit, FileUtils.RECURSIVE);
}
}
git.renameTo(dotGit);
File local = new File(checkoutDir);
if (local.exists()) {
FileUtils.delete(local, FileUtils.RECURSIVE);
}
if (!buildDir.startsWith("/")) {
buildDir = "./" + buildDir;
}
return "file:" + buildDir + repoPath;
}
}

View File

@@ -61,9 +61,9 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
private ConfigurableEnvironment environment;
private String username;
private String username;
private String password;
private String password;
public JGitEnvironmentRepository(ConfigurableEnvironment environment) {
this.environment = environment;
@@ -151,7 +151,8 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
}
Assert.state(basedir.mkdirs(), "Could not create basedir: " + basedir);
if (uri.startsWith("file:")) {
FileSystemUtils.copyRecursively(new UrlResource(uri).getFile(), basedir);
FileSystemUtils.copyRecursively(new UrlResource(uri).getFile(),
basedir);
git = Git.open(basedir);
}
else {
@@ -203,7 +204,8 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
}
private void setCredentialsProvider(TransportCommand<?, ?> cmd) {
cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username,
password));
}
private void trackBranch(Git git, CheckoutCommand checkout, String label) {

View File

@@ -2,6 +2,9 @@ package org.springframework.cloud.config.server;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
@@ -15,13 +18,18 @@ import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConfigServerApplication.class)
@IntegrationTest({"server.port:0", "spring.config.name:configserver"})
@IntegrationTest({"server.port:0", "spring.config.name:configserver", "spring.cloud.config.server.uri:file:./target/test-classes/config-repo"})
@WebAppConfiguration
@ActiveProfiles("test")
public class ApplicationTests {
@Value("${local.server.port}")
private int port;
@BeforeClass
public static void init() throws IOException{
ConfigServerTestUtils.prepareLocalRepo();
}
@Test
public void contextLoads() {

View File

@@ -21,16 +21,11 @@ import static org.junit.Assert.assertTrue;
import java.io.File;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.cloud.config.Environment;
import com.jcraft.jsch.Session;
import org.springframework.core.env.StandardEnvironment;
/**
* @author Dave Syer
@@ -42,27 +37,12 @@ public class JGitEnvironmentRepositoryTests {
private JGitEnvironmentRepository repository = new JGitEnvironmentRepository(
environment);
private File basedir = new File("target/config-repo");
private File basedir = new File("target/config");
@Before
public void init() throws Exception {
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
@Override
protected void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
});
File dotGit = new File("target/test-classes/config-repo/.git");
File git = new File("target/test-classes/config-repo/git");
if (git.exists()) {
if (dotGit.exists()) {
FileUtils.delete(dotGit, FileUtils.RECURSIVE);
}
}
git.renameTo(dotGit);
repository
.setUri(environment
.resolvePlaceholders("file:./target/test-classes/config-repo"));
String uri = ConfigServerTestUtils.prepareLocalRepo();
repository.setUri(uri);
if (basedir.exists()) {
FileUtils.delete(basedir, FileUtils.RECURSIVE);
}