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

@@ -1,8 +1,9 @@
package sample;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -12,6 +13,7 @@ import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.cloud.config.server.ConfigServerTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@@ -28,22 +30,25 @@ public class ApplicationTests {
private int port;
@BeforeClass
public static void startConfigServer() {
public static void startConfigServer() throws IOException {
String repo = ConfigServerTestUtils.prepareLocalRepo();
ConfigurableApplicationContext context = SpringApplication.run(
org.springframework.cloud.config.server.ConfigServerApplication.class,
"--server.port=" + configPort, "--spring.config.name=server");
configPort = ((EmbeddedWebApplicationContext) context).getEmbeddedServletContainer().getPort();
"--server.port=" + configPort, "--spring.config.name=server",
"--spring.cloud.config.server.uri=" + repo);
configPort = ((EmbeddedWebApplicationContext) context)
.getEmbeddedServletContainer().getPort();
System.setProperty("config.port", "" + configPort);
}
@Test
public void contextLoads() {
String foo = new TestRestTemplate().getForObject("http://localhost:" + port
+ "/env/foo", String.class);
+ "/env/info.foo", String.class);
assertEquals("bar", foo);
}
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
configPort = 8888;
startConfigServer();
SpringApplication.run(Application.class, args);

View File

@@ -0,0 +1,3 @@
info:
foo: bar
raw: true

View File

@@ -0,0 +1 @@
foo: bar

View File

@@ -0,0 +1,2 @@
7df4a26d5437d9d4090cd5809967f870444cde8f not-for-merge branch 'raw' of dsyer@localhost:/home/dsyer/dev/platform/config/spring-platform-config-server/target/test-classes/config-repo
7df4a26d5437d9d4090cd5809967f870444cde8f not-for-merge tag 'foo' of dsyer@localhost:/home/dsyer/dev/platform/config/spring-platform-config-server/target/test-classes/config-repo

View File

@@ -0,0 +1 @@
ref: refs/heads/raw

View File

@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
logallrefupdates = true
[branch "raw"]

View File

@@ -0,0 +1,3 @@
1 1
7df4a26d5437d9d4090cd5809967f870444cde8f 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0
1

View File

@@ -0,0 +1 @@
0000000000000000000000000000000000000000 7df4a26d5437d9d4090cd5809967f870444cde8f Dave Syer <dsyer@gopivotal.com> 1406860717 -0700

View File

@@ -0,0 +1 @@
0000000000000000000000000000000000000000 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 Dave Syer <dsyer@gopivotal.com> 1406860776 -0700 branch: Created from 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0

View File

@@ -0,0 +1 @@
0000000000000000000000000000000000000000 7df4a26d5437d9d4090cd5809967f870444cde8f Dave Syer <dsyer@gopivotal.com> 1406860717 -0700

View File

@@ -0,0 +1 @@
9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0

View File

@@ -0,0 +1 @@
7df4a26d5437d9d4090cd5809967f870444cde8f

View File

@@ -0,0 +1 @@
7df4a26d5437d9d4090cd5809967f870444cde8f

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);
}