Skip to content

Commit

Permalink
Fix macro installation.
Browse files Browse the repository at this point in the history
  • Loading branch information
judu committed Sep 7, 2021
1 parent d996b5a commit 18f8174
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.utility.DockerImageName;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.Duration;

Expand All @@ -31,13 +30,33 @@ public Warp10Container(final String tag) {
this(DEFAULT_IMAGE_NAME.withTag(tag));
}

public Warp10Container(final String tag, final File macrosFolder) throws FileNotFoundException {
this(DEFAULT_IMAGE_NAME.withTag(tag), macrosFolder);
/**
* Instantiate warp10 container with server-side macros.
*
* @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.init(DEFAULT_IMAGE_NAME.withTag(tag));
}

public Warp10Container(final DockerImageName dockerImageName) {
super(dockerImageName);

this.init(dockerImageName);
}

private void init(final DockerImageName dockerImageName) {
logger().info("Starting a Warp10 container using [{}]", dockerImageName);
addExposedPort(WARP10_DEFAULT_PORT);
setWaitStrategy(new HttpWaitStrategy()
Expand All @@ -47,16 +66,6 @@ public Warp10Container(final DockerImageName dockerImageName) {
);
}

public Warp10Container(final DockerImageName dockerImageName, final File macrosFolder) throws FileNotFoundException {
this(dockerImageName);

if (!macrosFolder.exists()) {
throw new FileNotFoundException("File " + macrosFolder.getAbsolutePath() + " does not exist");
} else {
this.withFileSystemBind(macrosFolder.getAbsolutePath(), "/data/warp10/macros", BindMode.READ_ONLY);
}
}

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,83 @@
import okhttp3.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class Warp10ContainerTest {
private static final String Warp10AuthHeader = "X-Warp10-Token";
private static final String Warp10FetchAPI = "/api/v0/exec";
private static final String Warp10FetchGTS = "[ '%s' 'test' {} NOW -1 ] FETCH";
private static final String Warp10FetchedHeader = "X-Warp10-Fetched";
private static final String Warp10GTS = "1// test{} 42";
private static final String Warp10UpdateAPI = "/api/v0/update";
private static final String Warp10Version = "2.7.5";

@Test
public void warp10DefaultTest() {
try (Warp10Container container = new Warp10Container(Warp10Version)) {
container.start();
assertNotNull(container.getReadToken());
assertNotNull(container.getWriteToken());
}
}

@Test
public void warp10ReadWrite() throws IOException {
try (Warp10Container container = new Warp10Container(Warp10Version)) {
container.start();

Response putGTS = warp10Request(container, Warp10UpdateAPI, Warp10GTS, container.getWriteToken());
assertEquals(200, putGTS.code());

Response getGTS = warp10Request(container, Warp10FetchAPI, String.format(Warp10FetchGTS, container.getReadToken()), null);
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);

MediaType mediaType = MediaType.get("text/plain");
OkHttpClient client = new OkHttpClient();

RequestBody requestBody = RequestBody.create(body, mediaType);
Request.Builder requestBuilder = new Request.Builder()
.url(postGTS)
.post(requestBody);

if (auth != null) {
requestBuilder = requestBuilder.header(Warp10AuthHeader, auth);
}

Request request = requestBuilder.build();

return client.newCall(request).execute();
}
private static final String Warp10AuthHeader = "X-Warp10-Token";
private static final String Warp10FetchAPI = "/api/v0/exec";
private static final String Warp10FetchGTS = "[ '%s' 'test' {} NOW -1 ] FETCH";
private static final String Warp10FetchedHeader = "X-Warp10-Fetched";
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 Warp10UpdateAPI = "/api/v0/update";
private static final String Warp10Version = "2.7.5";

;

@Test
public void warp10DefaultTest() {
try (Warp10Container container = new Warp10Container(Warp10Version)) {
container.start();
assertNotNull(container.getReadToken());
assertNotNull(container.getWriteToken());
}
}

@Test
public void warp10ReadWrite() throws IOException {
try (Warp10Container container = new Warp10Container(Warp10Version)) {
container.start();

Response putGTS = warp10Request(container, Warp10UpdateAPI, Warp10GTS, container.getWriteToken());
assertEquals(200, putGTS.code());

Response getGTS = warp10Request(container, Warp10FetchAPI, String.format(Warp10FetchGTS, container.getReadToken()), null);
assertEquals(200, getGTS.code());
assertNotNull(getGTS.header(Warp10FetchedHeader));
assertEquals(1, Integer.parseInt(getGTS.header(Warp10FetchedHeader)));
}
}

@Test
public void warp10WithMacros() throws IOException {
try (Warp10Container container = new Warp10Container(Warp10Version, new File("src/test/resources/macros"))) {
container.start();

Response putGTS = warp10Request(container, Warp10UpdateAPI, Warp10MacroGTS, container.getWriteToken());
assertEquals(200, putGTS.code());

Response getGTS = warp10Request(container, Warp10FetchAPI, String.format(Warp10FetchMacroGTS, 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);

MediaType mediaType = MediaType.get("text/plain");
OkHttpClient client = new OkHttpClient();

RequestBody requestBody = RequestBody.create(body, mediaType);
Request.Builder requestBuilder = new Request.Builder()
.url(postGTS)
.post(requestBody);

if (auth != null) {
requestBuilder = requestBuilder.header(Warp10AuthHeader, auth);
}

Request request = requestBuilder.build();

return client.newCall(request).execute();
}
}
41 changes: 41 additions & 0 deletions src/test/resources/macros/me/test.mc2
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<%
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

// 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
%>

0 comments on commit 18f8174

Please sign in to comment.