Skip to content

Commit

Permalink
Address code review comments: add ZipkinElasticsearchStorageTest
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed May 16, 2024
1 parent cb0954f commit 9960350
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 12 deletions.
7 changes: 7 additions & 0 deletions docker/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ $ STORAGE_TYPE=elasticsearch
$ docker-compose -f docker-compose.yml -f docker-compose-${STORAGE_TYPE}.yml up
```

The `elasticsearch` storage type is also compatible with OpenSearch,
you can start the example setup like that:

```
$ docker-compose -f docker-compose.yml -f docker-compose-opensearch.yml up
```

This starts zipkin, the corresponding storage and makes an example request.
After that, it runs the dependencies job on-demand.

Expand Down
7 changes: 7 additions & 0 deletions main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
<artifactId>zipkin-dependencies-opensearch</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.linecorp.armeria</groupId>
<artifactId>armeria-junit5</artifactId>
<version>${armeria.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void main(String[] args) throws UnsupportedEncodingException {
+ "Options are: cassandra3, mysql, elasticsearch");
}
}

static String[] pathToUberJar() throws UnsupportedEncodingException {
URL jarFile = ZipkinDependenciesJob.class.getProtectionDomain().getCodeSource().getLocation();
return new File(jarFile.getPath()).isDirectory() ? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.Socket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -16,6 +17,7 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -24,8 +26,9 @@
import java.util.regex.Pattern;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,32 +42,52 @@ final class ZipkinElasticsearchStorage {
static final String PASSWORD = getEnv("ES_PASSWORD", null);

static TrustManager[] TRUST_ALL = new TrustManager [] {
new X509TrustManager() {
new X509ExtendedTrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}

@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
}
}
};

static String flavor() {
return flavor(HOSTS, USERNAME, PASSWORD);
}

static String flavor(String hosts, String username, String password) {
final HttpClient.Builder builder = HttpClient
.newBuilder()
.connectTimeout(Duration.ofSeconds(5));

if (USERNAME != null && PASSWORD != null) {
if (username != null && password != null) {
builder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray());
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
Expand All @@ -75,7 +98,7 @@ protected PasswordAuthentication getPasswordAuthentication() {

final HttpClient client = builder.sslContext(sslContext).build();
try {
for (String host: parseHosts(HOSTS)) {
for (String host: parseHosts(hosts)) {
final HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(host)).build();
try {
final HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
Expand Down Expand Up @@ -137,8 +160,4 @@ static String[] parseHosts(String hosts) {

return list.toArray(new String[0]);
}

public static void main(String[] s) {
System.out.println(flavor());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package zipkin2.dependencies;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.MediaType;
import com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension;

class ZipkinElasticsearchStorageTest {
static final AggregatedHttpResponse ELASTICSEARCH_RESPONSE = AggregatedHttpResponse.of(
HttpStatus.OK, MediaType.JSON_UTF_8, """
{
"name" : "zipkin-elasticsearch",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "wByRPgSgTryYl0TZXW4MsA",
"version" : {
"number" : "7.0.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "e4efcb5",
"build_date" : "2019-04-29T12:56:03.145736Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.7.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
""");
static final AggregatedHttpResponse OPENSEARCH_RESPONSE = AggregatedHttpResponse.of(
HttpStatus.OK, MediaType.JSON_UTF_8, """
{
"name" : "PV-NhJd",
"cluster_name" : "CollectorDBCluster",
"cluster_uuid" : "UjZaM0fQRC6tkHINCg9y8w",
"version" : {
"distribution" : "opensearch",
"number" : "2.11.1",
"build_type" : "tar",
"build_hash" : "6b1986e964d440be9137eba1413015c31c5a7752",
"build_date" : "2023-11-29T21:43:10.135035992Z",
"build_snapshot" : false,
"lucene_version" : "9.7.0",
"minimum_wire_compatibility_version" : "7.10.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "The OpenSearch Project: https://opensearch.org/"
}
""");

@RegisterExtension static MockWebServerExtension server = new MockWebServerExtension();

@Test void opensearch_http() throws Exception {
server.enqueue(OPENSEARCH_RESPONSE);

assertThat(ZipkinElasticsearchStorage.flavor(server.httpUri().toString(), null, null))
.isEqualTo("opensearch");
}

@Test void opensearch_https() throws Exception {
server.enqueue(OPENSEARCH_RESPONSE);

assertThat(ZipkinElasticsearchStorage.flavor(server.httpsUri().toString(), null, null))
.isEqualTo("opensearch");
}

@Test void elasticsearch_http() throws Exception {
server.enqueue(ELASTICSEARCH_RESPONSE);

assertThat(ZipkinElasticsearchStorage.flavor(server.httpUri().toString(), null, null))
.isEqualTo("elasticsearch");
}

@Test void elasticsearch_https() throws Exception {
server.enqueue(ELASTICSEARCH_RESPONSE);

assertThat(ZipkinElasticsearchStorage.flavor(server.httpsUri().toString(), null, null))
.isEqualTo("elasticsearch");
}
}

0 comments on commit 9960350

Please sign in to comment.