Skip to content

Commit

Permalink
Test Lambda and remote container packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Karm committed Jun 14, 2024
1 parent 5b8248f commit aa7e3ac
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 0 deletions.
125 changes: 125 additions & 0 deletions integration-tests/awt-packaging/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-integration-test-awt-packaging</artifactId>
<name>Quarkus - Integration Tests - AWT Packaging</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jaxb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-awt</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-amazon-lambda</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jaxb-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-awt-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.it.jaxb;

import java.awt.Image;

import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book {

private String title;
private Image cover;

public Book() {
}

public Book(String title, Image cover) {
this.title = title;
this.cover = cover;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Image getCover() {
return cover;
}

public void setCover(Image cover) {
this.cover = cover;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.it.jaxb;

import java.io.StringReader;

import jakarta.inject.Named;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;

import org.jboss.logging.Logger;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

@Named("test")
public class Lambda implements RequestHandler<String, String> {

private static final Logger LOGGER = Logger.getLogger(Lambda.class);

@Override
public String handleRequest(String input, Context context) {
try {
final JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
final Book book = (Book) jaxbContext.createUnmarshaller().unmarshal(new StringReader(input));
return String.valueOf(book.getCover().getHeight(null));
} catch (JAXBException e) {
context.getLogger().log("Error: " + e.getMessage());
LOGGER.error(e);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.it.jaxb;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.jboss.logging.Logger;

@Path("/jaxb")
@ApplicationScoped
public class Resource {

private static final Logger LOGGER = Logger.getLogger(Resource.class);

@Path("/book")
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_PLAIN)
public Response postBook(Book book) {
LOGGER.info("Received book: " + book);
try {
return Response.accepted().entity(book.getCover().getHeight(null)).build();
} catch (Exception e) {
LOGGER.error(e);
return Response.serverError().entity(e.getMessage()).build();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.lambda.handler=true
quarkus.native.remote-container-build=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.it.jaxb;

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.MediaType.APPLICATION_XML;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;

import io.quarkus.amazon.lambda.test.LambdaClient;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class AwtJaxbTest {

public static final String BOOK_WITH_IMAGE = "<book>" +
"<cover>iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAIElEQVR4XmNgGCngPxSgi6MAZAU4FeOUQAdEKwQBdKsBOgof4SXid6kAAAAASUVORK5CYII=</cover>"
+
"<title>Foundation</title>" +
"</book>";

/**
* Smoke tests that we have .so files
* copied over from the remote build container.
*/
@Test
public void book() {
given()
.when()
.header("Content-Type", APPLICATION_XML)
.body(BOOK_WITH_IMAGE)
.when()
.post("/jaxb/book")
.then()
.statusCode(HttpStatus.SC_ACCEPTED)
// The height in pixels of the book's cover image.
.body(is("10"));
}

/**
* Smoke tests that our Lambda function makes at
* least some sense, but it doesn't talk to real AWS API.
*/
@Test
public void testLambdaStream() {
assertEquals("10", LambdaClient.invoke(String.class, BOOK_WITH_IMAGE));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.it.jaxb;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class AwtJaxbTestIT extends AwtJaxbTest {

/**
* Test is native image only, we need all artifacts to be packaged
* already, e.g. function.zip
* </br>
* Tests that the same set of .so files that was copied over
* from the remote build container is also packaged into the
* zip file that will be deployed to AWS Lambda.
*
* @throws java.io.IOException
*/
@Test
public void testPackaging() throws IOException {
final Path targetPath = Paths.get(".", "target").toAbsolutePath();
final Set<String> localLibs = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(targetPath, "*.so")) {
for (Path entry : stream) {
localLibs.add(entry.getFileName().toString());
}
}

final Path zipPath = targetPath.resolve("function.zip").toAbsolutePath();
assertTrue(Files.exists(zipPath), "Expected " + zipPath + " to exist");
final Set<String> awsLambdaLibs = new HashSet<>();
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipPath))) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (entry.getName().endsWith(".so")) {
awsLambdaLibs.add(entry.getName());
}
zipInputStream.closeEntry();
}
}
assertEquals(localLibs, awsLambdaLibs,
"The sets of .so libs produced by the build and the set in .zip file MUST be the same. It was: "
+ localLibs + " vs. " + awsLambdaLibs);
}
}
1 change: 1 addition & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<modules>
<module>avro-reload</module>
<module>awt</module>
<module>awt-packaging</module>
<module>no-awt</module>
<module>bouncycastle</module>
<module>bouncycastle-fips</module>
Expand Down

0 comments on commit aa7e3ac

Please sign in to comment.