diff options
author | Morten Tokle <mortent@verizonmedia.com> | 2021-12-14 13:43:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-14 13:43:25 +0100 |
commit | 001ab777e557b4e08c95e25013a7894385db9e25 (patch) | |
tree | 9de8415f52c015b9388b22a6cabc0d44e33d3cd5 | |
parent | 98e1ca90c7342cc0cf112cf6cca7d88b8d74fec0 (diff) | |
parent | 7cc75f5c8fa9faf5c9f20ee1a7218c7085a61002 (diff) |
Merge pull request #20500 from vespa-engine/mortent/testfwk-feedclient
Provide feed client builder implementation for tests
11 files changed, 83 insertions, 6 deletions
diff --git a/cloud-tenant-base-dependencies-enforcer/pom.xml b/cloud-tenant-base-dependencies-enforcer/pom.xml index d2ebb7ba9eb..9250feca285 100644 --- a/cloud-tenant-base-dependencies-enforcer/pom.xml +++ b/cloud-tenant-base-dependencies-enforcer/pom.xml @@ -42,6 +42,7 @@ <hk2.version>2.5.0-b32</hk2.version> <hk2.osgi-resource-locator.version>1.0.1</hk2.osgi-resource-locator.version> + <httpclient5.version>5.1.2</httpclient5.version> <jackson2.version>2.12.1</jackson2.version> <jackson-databind.version>${jackson2.version}</jackson-databind.version> <javassist.version>3.20.0-GA</javassist.version> @@ -202,6 +203,8 @@ <include>com.yahoo.vespa:tenant-cd-api:*:jar:test</include> <include>com.yahoo.vespa:tenant-cd-commons:*:jar:test</include> <include>com.yahoo.vespa:vespa-athenz:*:jar:test</include> + <include>com.yahoo.vespa:vespa-feed-client:*:jar:test</include> + <include>com.yahoo.vespa:vespa-feed-client-api:*:jar:test</include> <include>com.yahoo.vespa:vespa_jersey2:*:pom:test</include> <include>com.yahoo.vespa:vespaclient-core:*:jar:test</include> <include>com.yahoo.vespa:vsm:*:jar:test</include> @@ -241,6 +244,9 @@ <include>org.antlr:antlr4-runtime:4.5:jar:test</include> <include>org.apache.commons:commons-exec:1.3:jar:test</include> <include>org.apache.commons:commons-math3:3.6.1:jar:test</include> + <include>org.apache.httpcomponents.client5:httpclient5:${httpclient5.version}:jar:test</include> + <include>org.apache.httpcomponents.core5:httpcore5:${httpclient5.version}:jar:test</include> + <include>org.apache.httpcomponents.core5:httpcore5-h2:${httpclient5.version}:jar:test</include> <include>org.apache.httpcomponents:httpclient:4.5.12:jar:test</include> <include>org.apache.httpcomponents:httpcore:4.4.13:jar:test</include> <include>org.apache.opennlp:opennlp-tools:1.8.4:jar:test</include> diff --git a/cloud-tenant-cd/src/main/java/ai/vespa/hosted/cd/cloud/impl/VespaTestRuntime.java b/cloud-tenant-cd/src/main/java/ai/vespa/hosted/cd/cloud/impl/VespaTestRuntime.java index db9072e6198..201ddcb3908 100644 --- a/cloud-tenant-cd/src/main/java/ai/vespa/hosted/cd/cloud/impl/VespaTestRuntime.java +++ b/cloud-tenant-cd/src/main/java/ai/vespa/hosted/cd/cloud/impl/VespaTestRuntime.java @@ -10,6 +10,7 @@ import ai.vespa.hosted.api.TestConfig; import ai.vespa.hosted.cd.Deployment; import ai.vespa.hosted.cd.TestRuntime; import ai.vespa.hosted.cd.commons.HttpDeployment; +import ai.vespa.hosted.cd.commons.FeedClientBuilder; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.Environment; import com.yahoo.config.provision.zone.ZoneId; @@ -39,7 +40,10 @@ public class VespaTestRuntime implements TestRuntime { } private VespaTestRuntime(TestConfig config) { this.config = config; - this.deploymentToTest = new HttpDeployment(config.deployments().get(config.zone()), new DefaultEndpointAuthenticator(config.system())); + DefaultEndpointAuthenticator authenticator = new DefaultEndpointAuthenticator(config.system()); + this.deploymentToTest = new HttpDeployment(config.deployments().get(config.zone()), authenticator); + FeedClientBuilder.setEndpointAuthenticator(authenticator); + System.setProperty(ai.vespa.feed.client.FeedClientBuilder.PREFERRED_IMPLEMENTATION_PROPERTY, FeedClientBuilder.class.getName()); } @Override diff --git a/hosted-tenant-base/pom.xml b/hosted-tenant-base/pom.xml index 0dc7aee7cd4..e7c364cb7de 100644 --- a/hosted-tenant-base/pom.xml +++ b/hosted-tenant-base/pom.xml @@ -200,6 +200,8 @@ org.junit.jupiter:junit-jupiter-api:provided, org.junit.jupiter:junit-jupiter-engine:test, org.junit.vintage:junit-vintage-engine:test, + com.yahoo.vespa:vespa-feed-client:runtime, <!-- prevent effective compile scope of vespa-feed-client in test bundle --> + com.yahoo.vespa:vespa-feed-client-api:provided, com.yahoo.vespa:tenant-cd-api:provided </testBundleScopeOverrides> </configuration> diff --git a/tenant-cd-api/pom.xml b/tenant-cd-api/pom.xml index 5ac52748152..60d862a8844 100644 --- a/tenant-cd-api/pom.xml +++ b/tenant-cd-api/pom.xml @@ -58,6 +58,12 @@ <version>${hosted-tenant-base-junit-version}</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>vespa-feed-client-api</artifactId> + <version>${project.version}</version> + <scope>compile</scope> + </dependency> </dependencies> <build> diff --git a/tenant-cd-api/src/main/java/ai/vespa/feed/client/package-info.java b/tenant-cd-api/src/main/java/ai/vespa/feed/client/package-info.java new file mode 100644 index 00000000000..3871dc1fa3d --- /dev/null +++ b/tenant-cd-api/src/main/java/ai/vespa/feed/client/package-info.java @@ -0,0 +1,5 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +package ai.vespa.feed.client; + +import com.yahoo.osgi.annotation.ExportPackage;
\ No newline at end of file diff --git a/tenant-cd-commons/pom.xml b/tenant-cd-commons/pom.xml index 1d76654fbf7..0ada43bca0b 100644 --- a/tenant-cd-commons/pom.xml +++ b/tenant-cd-commons/pom.xml @@ -42,6 +42,18 @@ <artifactId>config-provisioning</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>vespa-feed-client</artifactId> + <version>${project.version}</version> + <exclusions> + <exclusion> + <groupId>com.yahoo.vespa</groupId> + <artifactId>vespa-feed-client-api</artifactId> + </exclusion> + </exclusions> + <scope>compile</scope> + </dependency> </dependencies> <build> diff --git a/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/EndpointAuthenticator.java b/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/EndpointAuthenticator.java index 90775ab76af..5317cf01e21 100644 --- a/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/EndpointAuthenticator.java +++ b/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/EndpointAuthenticator.java @@ -4,6 +4,7 @@ package ai.vespa.hosted.cd.commons; import javax.net.ssl.SSLContext; import java.net.http.HttpRequest; import java.security.NoSuchAlgorithmException; +import java.util.Map; /** * Adds environment dependent authentication to HTTP request against Vespa deployments. @@ -20,8 +21,7 @@ public interface EndpointAuthenticator { default SSLContext sslContext() { try { return SSLContext.getDefault(); - } - catch (NoSuchAlgorithmException e) { + } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } @@ -31,4 +31,7 @@ public interface EndpointAuthenticator { return request; } + default Map<String, String> authorizationHeaders() { + return Map.of(); + } } diff --git a/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/FeedClientBuilder.java b/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/FeedClientBuilder.java new file mode 100644 index 00000000000..892c8b63645 --- /dev/null +++ b/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/FeedClientBuilder.java @@ -0,0 +1,25 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package ai.vespa.hosted.cd.commons; + +import ai.vespa.feed.client.impl.FeedClientBuilderImpl; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; + +/** + * @author mortent + */ +public class FeedClientBuilder extends FeedClientBuilderImpl { + + static AtomicReference<EndpointAuthenticator> endpointAuthenticator = new AtomicReference<>(); + + public static void setEndpointAuthenticator(EndpointAuthenticator authenticator) { + endpointAuthenticator.set(authenticator); + } + + public FeedClientBuilder() { + super.setSslContext(Objects.requireNonNull(endpointAuthenticator.get(), FeedClientBuilder.class.getName() + " is not initialized").sslContext()); + endpointAuthenticator.get().authorizationHeaders().forEach(super::addRequestHeader); + } +} diff --git a/tenant-cd-commons/src/main/resources/META-INF.services/ai.vespa.feed.client.FeedClientBuilder b/tenant-cd-commons/src/main/resources/META-INF.services/ai.vespa.feed.client.FeedClientBuilder new file mode 100644 index 00000000000..69073a3951c --- /dev/null +++ b/tenant-cd-commons/src/main/resources/META-INF.services/ai.vespa.feed.client.FeedClientBuilder @@ -0,0 +1,2 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +ai.vespa.hosted.cd.commons.FeedClientBuilder
\ No newline at end of file diff --git a/vespa-feed-client-api/abi-spec.json b/vespa-feed-client-api/abi-spec.json index cabe9afde20..a9047365a7a 100644 --- a/vespa-feed-client-api/abi-spec.json +++ b/vespa-feed-client-api/abi-spec.json @@ -139,7 +139,9 @@ "public abstract ai.vespa.feed.client.FeedClientBuilder setEndpointUris(java.util.List)", "public abstract ai.vespa.feed.client.FeedClient build()" ], - "fields": [] + "fields": [ + "public static final java.lang.String PREFERRED_IMPLEMENTATION_PROPERTY" + ] }, "ai.vespa.feed.client.FeedException": { "superClass": "java.lang.RuntimeException", diff --git a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java index daf3f62dac1..05bc608df27 100644 --- a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java +++ b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java @@ -9,6 +9,7 @@ import java.net.URI; import java.nio.file.Path; import java.security.PrivateKey; import java.security.cert.X509Certificate; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -24,17 +25,26 @@ import java.util.function.Supplier; */ public interface FeedClientBuilder { + String PREFERRED_IMPLEMENTATION_PROPERTY = "vespa.feed.client.builder.implementation"; + /** Creates a builder for a single container endpoint **/ static FeedClientBuilder create(URI endpoint) { return create(Collections.singletonList(endpoint)); } /** Creates a builder for multiple container endpoints **/ static FeedClientBuilder create(List<URI> endpoints) { + String defaultImplementation = "ai.vespa.feed.client.impl.FeedClientBuilderImpl"; + String preferredImplementation = System.getProperty(PREFERRED_IMPLEMENTATION_PROPERTY, defaultImplementation); Iterator<FeedClientBuilder> iterator = ServiceLoader.load(FeedClientBuilder.class).iterator(); if (iterator.hasNext()) { - return iterator.next().setEndpointUris(endpoints); + List<FeedClientBuilder> builders = new ArrayList<>(); + iterator.forEachRemaining(builders::add); + return builders.stream() + .filter(builder -> preferredImplementation.equals(builder.getClass().getName())) + .findFirst() + .orElse(builders.get(0)); } else { try { - Class<?> aClass = Class.forName("ai.vespa.feed.client.impl.FeedClientBuilderImpl"); + Class<?> aClass = Class.forName(preferredImplementation); for (Constructor<?> constructor : aClass.getConstructors()) { if (constructor.getParameterTypes().length==0) { return ((FeedClientBuilder)constructor.newInstance()).setEndpointUris(endpoints); |