From c8546053c5380b4ef5c1ae96717460e1d3931153 Mon Sep 17 00:00:00 2001 From: Julien Durillon Date: Tue, 21 Sep 2021 11:43:41 +0200 Subject: [PATCH] Release 1.0.4. Changelog: - Allow to override standalone warp10 configuration in the container. --- pom.xml | 2 +- .../warp10/Warp10Container.java | 54 +++++++++++++++---- .../warp10/Warp10ContainerTest.java | 17 ++++++ src/test/resources/macros/me/testlimit.mc2 | 52 ++++++++++++++++++ 4 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/macros/me/testlimit.mc2 diff --git a/pom.xml b/pom.xml index 9272f39..326056c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ https://github.com/CleverCloud/testcontainers-warp10 com.clever-cloud testcontainers-warp10 - 1.0.3 + 1.0.4 11 diff --git a/src/main/java/com/clevercloud/testcontainers/warp10/Warp10Container.java b/src/main/java/com/clevercloud/testcontainers/warp10/Warp10Container.java index 421bebc..aa65b8e 100644 --- a/src/main/java/com/clevercloud/testcontainers/warp10/Warp10Container.java +++ b/src/main/java/com/clevercloud/testcontainers/warp10/Warp10Container.java @@ -30,23 +30,32 @@ public Warp10Container(final String tag) { this(DEFAULT_IMAGE_NAME.withTag(tag)); } + /** * Instantiate warp10 container with server-side macros. * - * @param tag version tag for the docker image. + * @param tag version tag for the docker image. * @param macrosFolder File pointing at the macros folder you want to install. * This should be a full "macros folder" as defined in warp10's doc: * the macros **must** be placed into subfolders. */ public Warp10Container(final String tag, final File macrosFolder) { - super(new ImageFromDockerfile() - .withFileFromFile(macrosFolder.getPath(), macrosFolder) - .withDockerfileFromBuilder(builder -> builder - .from(DEFAULT_IMAGE_NAME.withTag(tag).asCanonicalNameString()) - .add(macrosFolder.getPath(), "/opt/warp10/macros/") - .build() - ) - ); + this(tag, macrosFolder, null); + } + + /** + * Instantiate warp10 container with server-side macros and config override. + * + * @param tag version tag for the docker image. + * @param macrosFolder File pointing at the macros folder you want to install. + * This should be a full "macros folder" as defined in warp10's doc: + * the macros **must** be placed into subfolders. + * @param configFolder File pointing at the configuration files you want to install. + * The files in this folder should look like XX-name.conf.template (e.g. 20-warpscript.conf.template). + * They will override the template configuration files in /opt/warp10/conf.templates/standalone + */ + public Warp10Container(final String tag, final File macrosFolder, final File configFolder) { + super(setupImage(tag, macrosFolder, configFolder)); this.init(DEFAULT_IMAGE_NAME.withTag(tag)); } @@ -56,6 +65,33 @@ public Warp10Container(final DockerImageName dockerImageName) { this.init(dockerImageName); } + private static ImageFromDockerfile setupImage(final String tag, final File macrosFolder, final File configFolder) { + ImageFromDockerfile image = new ImageFromDockerfile(); + if (macrosFolder != null) { + if (macrosFolder.exists()) { + image.withFileFromFile(macrosFolder.getPath(), macrosFolder); + } else { + throw new RuntimeException(String.format("Macro folder %s does not exist", macrosFolder.getPath())); + } + } + if (configFolder != null) { + if (configFolder.exists()) { + image.withFileFromFile(configFolder.getPath(), configFolder); + } else { + throw new RuntimeException(String.format("Config folder %s does not exist", configFolder.getPath())); + } + } + return image.withDockerfileFromBuilder(builder -> { + builder.from(DEFAULT_IMAGE_NAME.withTag(tag).asCanonicalNameString()); + if (macrosFolder != null && macrosFolder.exists()) + builder.add(macrosFolder.getPath(), "/opt/warp10/macros/"); + if (configFolder != null && configFolder.exists()) + builder.add(configFolder.getPath(), "/opt/warp10/conf.templates/standalone/"); + + builder.build(); + }); + } + private void init(final DockerImageName dockerImageName) { logger().info("Starting a Warp10 container using [{}]", dockerImageName); addExposedPort(WARP10_DEFAULT_PORT); diff --git a/src/test/java/com/clevercloud/testcontainers/warp10/Warp10ContainerTest.java b/src/test/java/com/clevercloud/testcontainers/warp10/Warp10ContainerTest.java index 142f435..76e3dae 100644 --- a/src/test/java/com/clevercloud/testcontainers/warp10/Warp10ContainerTest.java +++ b/src/test/java/com/clevercloud/testcontainers/warp10/Warp10ContainerTest.java @@ -18,6 +18,7 @@ public class Warp10ContainerTest { private static final String Warp10GTS = "1// test{} 42"; private static final String Warp10MacroGTS = "1// test{} 42\n50// test{} 1337"; private static final String Warp10FetchMacroGTS = "[ '%s' 'test' {} 40 NOW 10 ] @me/test"; + private static final String Warp10FetchMacroAndConfigGTS = "[ '%s' 'test' {} 40 NOW 10 ] @me/testlimit"; private static final String Warp10UpdateAPI = "/api/v0/update"; private static final String Warp10Version = "2.7.5"; @@ -63,6 +64,22 @@ public void warp10WithMacros() throws IOException { } } + @Test + public void warp10WithMacrosAndConfig() throws IOException { + try (Warp10Container container = new Warp10Container(Warp10Version, new File("src/test/resources/macros"), new File("src/test/resources/conf.d"))) { + container.start(); + + Response putGTS = warp10Request(container, Warp10UpdateAPI, Warp10MacroGTS, container.getWriteToken()); + assertEquals(200, putGTS.code()); + + Response getGTS = warp10Request(container, Warp10FetchAPI, String.format(Warp10FetchMacroAndConfigGTS, container.getReadToken()), null); + System.out.println(getGTS.body().string()); + assertEquals(200, getGTS.code()); + assertNotNull(getGTS.header(Warp10FetchedHeader)); + assertEquals(1, Integer.parseInt(getGTS.header(Warp10FetchedHeader))); + } + } + private Response warp10Request(Warp10Container container, String path, String body, String auth) throws IOException { URL postGTS = new URL("http", container.getHTTPHost(), container.getHTTPPort(), path); diff --git a/src/test/resources/macros/me/testlimit.mc2 b/src/test/resources/macros/me/testlimit.mc2 new file mode 100644 index 0000000..c122896 --- /dev/null +++ b/src/test/resources/macros/me/testlimit.mc2 @@ -0,0 +1,52 @@ +<% + SAVE 'context' STORE + + 'list' STORE + NOW 'NOW' STORE + + $list SIZE 6 == + 'Fetch_after can only have 6 parameters : read_token, class, labels, start, end, active_after' + ASSERTMSG + + $list 0 GET 'rtoken' STORE + $list 1 GET 'class' STORE + $list 2 GET 'labels' STORE + $list 3 GET 'start_ts' STORE + $list 4 GET 'end_ts' STORE + $list 5 GET 'active_after_ts' STORE + + + <% + ISAUTHENTICATED NOT + %> + <% + $rtoken AUTHENTICATE + %> + IFT + + 1000000000 LIMIT + + // Activity is taken account after at least 2 h + <% $active_after_ts $NOW 2 h - > %> + <% + { + 'token' $rtoken + 'labels' $labels + 'class' $class + 'start' $start_ts + 'end' $end_ts + 'active.after' $NOW 2 h - + } FETCH + %> + <% + { + 'token' $rtoken + 'active.after' $active_after_ts + 'labels' $labels + 'class' $class + 'start' $start_ts + 'end' $end_ts + } FETCH + %> IFTE + $context RESTORE +%> \ No newline at end of file