Replace deployer with a simpler library

Instead of an app, it is now a library with some utilities
(principally ApplicationBootstrap) for launching a Spring Boot
application, extracting a function, and registering it in the
FunctionRegistry.
This commit is contained in:
Dave Syer
2018-04-25 12:44:16 +01:00
parent 59f94c1533
commit 7fa0ed7b6b
28 changed files with 1769 additions and 1240 deletions

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2012-2015 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.function.deployer;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* A test suite for probing weird ordering problems in the tests.
*
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ FunctionAppDeployerTests.class,
FunctionExtractingFunctionCatalogTests.class,
FunctionExtractingFunctionCatalogIntegrationTests.class })
@Ignore
public class AdhocTestSuite {
}

View File

@@ -1,129 +0,0 @@
/*
* Copyright 2012-2015 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.function.deployer;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.boot.loader.thin.ArchiveUtils;
import org.springframework.boot.loader.tools.LogbackInitializer;
import org.springframework.cloud.deployer.spi.app.DeploymentState;
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.thin.ThinJarAppDeployer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
* @author Mark Fisher
*/
@RunWith(Parameterized.class)
public class FunctionAppDeployerTests {
static {
LogbackInitializer.initialize();
}
private static ThinJarAppDeployer deployer = new ThinJarAppDeployer();
@BeforeClass
public static void skip() {
try {
ArchiveUtils.getArchiveRoot(ArchiveUtils
.getArchive("maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT"));
}
catch (Exception e) {
Assume.assumeNoException(
"Could not locate jar for tests. Please build spring-cloud-function locally first.",
e);
}
}
@Parameterized.Parameters
public static List<Object[]> data() {
// Repeat a couple of times to ensure it's consistent
return Arrays.asList(new Object[2][0]);
}
@Test
public void directory() throws Exception {
String first = deploy("file:../spring-cloud-function-samples/function-sample/target/classes", "",
"--spring.cloud.function.stream.supplier.enabled=false");
// Deployment is blocking so it either failed or succeeded.
assertThat(deployer.status(first).getState()).isEqualTo(DeploymentState.deployed);
deployer.undeploy(first);
}
@Test
public void web() throws Exception {
String first = deploy("maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT", "",
"--spring.cloud.function.stream.supplier.enabled=false");
// Deployment is blocking so it either failed or succeeded.
assertThat(deployer.status(first).getState()).isEqualTo(DeploymentState.deployed);
deployer.undeploy(first);
}
@Test
public void stream() throws Exception {
String first = deploy("maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT",
"spring.cloud.deployer.thin.profile=rabbit",
"--spring.cloud.function.stream.supplier.enabled=false", "--debug=true");
// Deployment is blocking so it either failed or succeeded.
assertThat(deployer.status(first).getState()).isEqualTo(DeploymentState.deployed);
deployer.undeploy(first);
}
private String deploy(String jarName, String properties, String... args)
throws Exception {
Resource resource = new FileSystemResource(
ArchiveUtils.getArchiveRoot(ArchiveUtils.getArchive(jarName)));
AppDefinition definition = new AppDefinition(resource.getFilename(),
Collections.emptyMap());
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource,
properties(properties), Arrays.asList(args));
String deployed = deployer.deploy(request);
return deployed;
}
private Map<String, String> properties(String properties) {
Map<String, String> map = new LinkedHashMap<>();
Properties props = StringUtils.splitArrayElementsIntoProperties(
StringUtils.commaDelimitedListToStringArray(properties), "=");
if (props != null) {
for (Object name : props.keySet()) {
String key = (String) name;
map.put(key, props.getProperty(key));
}
}
return map;
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright 2017 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.function.deployer;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FunctionConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(properties = {
"function.location=file:target/it/support/target/function-sample-1.0.0.M1.jar", })
public abstract class FunctionConfigurationTests {
@Autowired
protected MessageCollector messageCollector;
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=com.example.functions.Emitter" })
public static class SourceTests extends FunctionConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("one"));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = {
"function.bean=com.example.functions.Emitter,com.example.functions.LengthCounter" })
public static class CompositeTests extends FunctionConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is(3));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = {
"function.bean=com.example.functions.LengthCounter" })
public static class ProcessorTests extends FunctionConfigurationTests {
@Autowired
private Processor processor;
@Test
public void test() throws Exception {
processor.input().send(MessageBuilder.withPayload("hello").build());
Message<?> received = messageCollector.forChannel(processor.output()).poll(1,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("hello".length()));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = {
"function.bean=com.example.functions.DoubleLogger" })
public static class SinkTests extends FunctionConfigurationTests {
@Autowired
private Sink sink;
@Test
public void test() throws Exception {
// Can't assert side effects.
sink.input().send(MessageBuilder.withPayload(5).build());
}
}
}

View File

@@ -1,130 +0,0 @@
/*
* Copyright 2016-2017 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.function.deployer;
import java.net.URI;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.SocketUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class FunctionExtractingFunctionCatalogIntegrationTests {
private static ConfigurableApplicationContext context;
private static int port;
@BeforeClass
public static void open() throws Exception {
port = SocketUtils.findAvailableTcpPort();
// System.setProperty("debug", "true");
context = new ApplicationRunner().start("--server.port=" + port, "--debug",
"--logging.level.org.springframework.cloud.function=DEBUG");
deploy("sample", "maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT");
}
private static void deploy(String name, String path) throws Exception {
ResponseEntity<String> result = new TestRestTemplate().postForEntity(
"http://localhost:" + port + "/admin/" + name + "?path=" + path, "",
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private static String undeploy(String name) throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.delete(new URI("http://localhost:" + port + "/admin/" + name)).build(),
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
return result.getBody();
}
@AfterClass
public static void close() {
if (context != null) {
context.close();
}
}
@Test
public void listing() {
assertThat(new TestRestTemplate()
.getForObject("http://localhost:" + port + "/admin", String.class))
.startsWith("{").contains("sample");
}
@Test
public void words() {
assertThat(new TestRestTemplate().getForObject(
"http://localhost:" + port + "/stream/sample/words", String.class))
.isEqualTo("[\"foo\",\"bar\"]");
}
@Test
public void missing() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.get(new URI("http://localhost:" + port + "/stream/missing/words"))
.build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
public void uppercase() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.post(new URI("http://localhost:" + port + "/stream/sample/uppercase"))
.contentType(MediaType.TEXT_PLAIN)
.body("foo"), String.class);
assertThat(result.getBody()).isEqualTo("FOO");
}
@Test
public void another() throws Exception {
deploy("pof",
"maven://io.spring.sample:function-sample-pof:jar:exec:1.0.0.BUILD-SNAPSHOT");
assertThat(new TestRestTemplate().postForObject(
"http://localhost:" + port + "/stream/pof/greeter", "Foo",
String.class)).isEqualTo("Hello Foo");
}
@Test
public void cycle() throws Exception {
String undeploy = undeploy("sample");
assertThat(undeploy.contains("\"name\":\"sample\""));
assertThat(undeploy.contains(
"\"path\":\"maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT\""));
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.get(new URI("http://localhost:" + port + "/stream/sample/words"))
.build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
deploy("sample", "maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT");
assertThat(new TestRestTemplate().postForObject(
"http://localhost:" + port + "/stream/sample/uppercase", "foo",
String.class)).isEqualTo("FOO");
}
}

View File

@@ -1,129 +0,0 @@
/*
* Copyright 2016-2017 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.function.deployer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.loader.tools.LogbackInitializer;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
*/
public class FunctionExtractingFunctionCatalogTests {
private static String id;
static {
LogbackInitializer.initialize();
}
private static FunctionExtractingFunctionCatalog deployer = new FunctionExtractingFunctionCatalog();
@Rule
public ExpectedException expected = ExpectedException.none();
@Before
public void init() throws Exception {
if (id == null) {
deploy("sample",
"maven://io.spring.sample:function-sample:1.0.0.BUILD-SNAPSHOT");
// "--debug");
id = deploy("pojos",
"maven://io.spring.sample:function-sample-pojo:1.0.0.BUILD-SNAPSHOT");
}
}
@AfterClass
public static void close() {
if (id != null) {
deployer.undeploy("sample");
deployer.undeploy("pojos");
}
}
@Test
public void listFunctions() throws Exception {
assertThat(deployer.getNames(Function.class)).contains("sample/uppercase",
"pojos/uppercase");
}
@Test
public void nameFunction() throws Exception {
assertThat(deployer.getName(deployer.lookup(Function.class, "sample/uppercase")))
.isEqualTo("sample/uppercase");
}
@Test
public void deployAndExtractFunctions() throws Exception {
// This one can only work if you change the boot classpath to contain reactor-core
// and reactive-streams
expected.expect(ClassCastException.class);
Function<Flux<String>, Flux<String>> function = deployer.lookup(Function.class,
"pojos/uppercase");
Flux<String> result = function.apply(Flux.just("foo"));
assertThat(result.blockFirst()).isEqualTo("FOO");
}
@Test
public void listConsumers() throws Exception {
assertThat(deployer.getNames(Consumer.class)).isEmpty();
}
@Test
public void deployAndExtractConsumers() throws Exception {
assertThat(deployer.<Consumer<?>>lookup(Consumer.class, "pojos/sink")).isNull();
}
@Test
public void listSuppliers() throws Exception {
assertThat(deployer.getNames(Supplier.class)).contains("sample/words",
"pojos/words");
}
@Test
public void nameSupplier() throws Exception {
assertThat(deployer.getName(deployer.lookup(Supplier.class, "sample/words")))
.isEqualTo("sample/words");
}
@Test
public void deployAndExtractSuppliers() throws Exception {
assertThat(deployer.<Supplier<?>>lookup(Supplier.class, "sample/words"))
.isNotNull();
assertThat(deployer.<Supplier<?>>lookup(Supplier.class, "pojos/words"))
.isNotNull();
}
private static String deploy(String name, String path, String... args)
throws Exception {
String deployed = deployer.deploy(name, path, args);
return deployed;
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright 2017 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.function.deployer;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FunctionConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(properties = {
"function.location=file:target/it/support/target/function-sample-1.0.0.M1-exec.jar", })
public abstract class SpringFunctionAppConfigurationTests {
@Autowired
protected MessageCollector messageCollector;
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=myEmitter",
"function.main=com.example.functions.FunctionApp" })
public static class SourceTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("one"));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=myEmitter,myCounter",
"function.main=com.example.functions.FunctionApp" })
public static class CompositeTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is(3));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=myCounter",
"function.main=com.example.functions.FunctionApp" })
public static class ProcessorTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Processor processor;
@Test
public void test() throws Exception {
processor.input().send(MessageBuilder.withPayload("hello").build());
Message<?> received = messageCollector.forChannel(processor.output()).poll(1,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("hello".length()));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=myDoubler",
"function.main=com.example.functions.FunctionApp" })
public static class SinkTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Sink sink;
@Test
public void test() throws Exception {
// Can't assert side effects.
sink.input().send(MessageBuilder.withPayload(5).build());
}
}
}