Skip to content

Commit

Permalink
Merge pull request #4270 from vespa-engine/hmusum/move-test-2
Browse files Browse the repository at this point in the history
Move test from internal repo
  • Loading branch information
aressem authored Nov 14, 2024
2 parents bff0e69 + 0d37fe5 commit 5278b4e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/config/urldownload/app/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<services>

<container version="1.0" id="container">
<handler id="com.yahoo.test.TestHandler" bundle="app">
<binding>http://*/test/*</binding>
<config name="com.yahoo.test.test">
<myurl>https://data.vespa-cloud.com/onnx_models/bert-base-uncased-vocab.txt</myurl>
<arrayOfUrls>
<item>https://data.vespa-cloud.com/onnx_models/bert-base-uncased-vocab.txt</item>
</arrayOfUrls>
<mapOfUrls>
<item key="constant_tensor_1">https://data.vespa-cloud.com/onnx_models/bert-base-uncased-vocab.txt</item>
</mapOfUrls>
</config>
</handler>
</container>

</services>
42 changes: 42 additions & 0 deletions tests/config/urldownload/bundle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"?>
<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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yahoo.test</groupId>
<artifactId>app</artifactId>
<version>1.0.0</version>
<packaging>container-plugin</packaging> <!-- Use Vespa packaging -->

<parent>
<groupId>com.yahoo.vespa.systemtest</groupId>
<artifactId>test-apps-parent</artifactId>
<version>8-SNAPSHOT</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>com.yahoo.vespa</groupId>
<artifactId>container</artifactId>
<version>${vespa.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.yahoo.vespa</groupId>
<artifactId>bundle-plugin</artifactId>
<version>${vespa.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.yahoo.test;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class TestHandler extends ThreadedHttpRequestHandler {

private final File contents;
private final List<File> arrayOfUrls;
private final Map<String, File> mapOfUrls;

public TestHandler(TestConfig testConfig, Context context) {
super(context);
contents = testConfig.myurl();
arrayOfUrls = testConfig.arrayOfUrls();
mapOfUrls = testConfig.mapOfUrls();
}

@Override
public HttpResponse handle(HttpRequest httpRequest) {
try {
if (httpRequest.getUri().getPath().endsWith("numfiles")) {
return handleNumFiles();
}
return handleFileContents();
} catch (Exception e) {
return new HttpResponse(500) {
@Override
public void render(OutputStream outputStream) throws IOException {
outputStream.write(("Internal server error: " + e.getMessage()).getBytes());
}
};
}
}

private HttpResponse handleFileContents() {
final String fileContents = readContents();
return new HttpResponse(200) {
@Override
public void render(OutputStream outputStream) throws IOException {
outputStream.write(fileContents.getBytes());
}
};
}

private HttpResponse handleNumFiles() {
return new HttpResponse(200) {
@Override
public void render(OutputStream outputStream) throws IOException {
int numFiles = 0;
if (contents.exists()) numFiles += 1;
for (File f : arrayOfUrls) {
if (f.exists())
numFiles += 1;
}
for (File f : mapOfUrls.values()) {
if (f.exists())
numFiles += 1;
}
outputStream.write(String.format("%d", numFiles).getBytes());
}
};
}

private String readContents() {
if (contents.exists() && contents.length() > 0) {
try (BufferedReader br = new BufferedReader(new FileReader(contents))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line); sb.append("\n");
line = br.readLine();
}
return sb.toString();
} catch (IOException e) {
throw new RuntimeException("Exception reading file", e);
}
}
throw new RuntimeException("Downloaded URL could not be found.");
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package=com.yahoo.test

myurl url
arrayOfUrls[] url
mapOfUrls{} url

37 changes: 37 additions & 0 deletions tests/config/urldownload/urldownload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Private reason: Depends on artifactory

require 'config_test'
require 'app_generator/search_app'

class UrlDownloadTest < ConfigTest

def setup
set_owner("lesters")
set_description("Tests downloading files from urls")
@valgrind = false
end

def teardown
stop
end

def test_url_downloading
add_bundle_dir(selfdir + "bundle", "app")
deploy(selfdir + "/app")
start
container = vespa.container.values.first

result = container.http_get2("/test/numfiles")
puts "Number of downloaded files: " + result.body

assert(result.code == "200")
assert(result.body.to_i == 3)

result = container.http_get2("/test/")

assert(result.code == "200")
assert(result.body.length > 0)
assert(result.body.include? "cells")
end

end

0 comments on commit 5278b4e

Please sign in to comment.