Skip to content

Commit

Permalink
Workaround to fix OpenShiftClient.execOnPod issue
Browse files Browse the repository at this point in the history
Issue on fabric8 side, related to quarkus-qe/quarkus-test-framework#270 and reported in fabric8io/kubernetes-client#3473
  • Loading branch information
Sgitario committed Sep 16, 2021
1 parent 9dd2011 commit 952194f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.ws.rs.client.ClientBuilder;
Expand All @@ -23,6 +27,7 @@
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.bootstrap.Service;
import io.quarkus.test.bootstrap.inject.OpenShiftClient;
import io.quarkus.test.utils.Command;

public abstract class BaseOpenShiftAlertEventsIT {

Expand Down Expand Up @@ -85,7 +90,7 @@ private void thenKafkaConsumerMetricsAreFound() throws Exception {

private void thenMetricIsExposedInPrometheus(String name, Predicate<String> valueMatcher) throws Exception {
await().ignoreExceptions().atMost(ASSERT_PROMETHEUS_TIMEOUT_MINUTES, TimeUnit.MINUTES).untilAsserted(() -> {
String output = client.execOnPod(PROMETHEUS_NAMESPACE, PROMETHEUS_POD, PROMETHEUS_CONTAINER, "curl",
String output = execOnPod(PROMETHEUS_NAMESPACE, PROMETHEUS_POD, PROMETHEUS_CONTAINER, "curl",
"http://localhost:9090/api/v1/query?query=" + name);

assertTrue(output.contains("\"status\":\"success\""), "Verify the status was ok");
Expand Down Expand Up @@ -126,6 +131,19 @@ private Double extractValueFromMetric(String line) {
return Double.parseDouble(line.substring(line.lastIndexOf(" ")));
}

// TODO: Will be fixed once either https://github.com/fabric8io/kubernetes-client/issues/3473 is fixed
// or when releasing test framework to 0.0.12.
private String execOnPod(String prometheusNamespace, String prometheusPod, String prometheusContainer, String... input)
throws IOException, InterruptedException {
List<String> output = new ArrayList<>();
List<String> args = new ArrayList<>();
args.addAll(Arrays.asList("oc", "exec", prometheusPod, "-c", prometheusContainer, "-n", prometheusNamespace));
args.addAll(Arrays.asList(input));
new Command(args).outputToLines(output).runAndWait();

return output.stream().collect(Collectors.joining(System.lineSeparator()));
}

protected static void loadServiceMonitor(Service app) {
client.apply(Paths.get("target/test-classes/service-monitor.yaml"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javax.inject.Inject;

Expand All @@ -19,6 +24,7 @@
import io.quarkus.test.scenarios.OpenShiftDeploymentStrategy;
import io.quarkus.test.scenarios.OpenShiftScenario;
import io.quarkus.test.services.QuarkusApplication;
import io.quarkus.test.utils.Command;

/**
* The application contains a `PrimeNumberResource` resource that generates a few metrics:
Expand Down Expand Up @@ -79,7 +85,7 @@ private void whenCheckPrimeNumber(int number) {

private void thenMetricIsExposedInPrometheus(String name, Integer expected) throws Exception {
await().ignoreExceptions().atMost(ASSERT_PROMETHEUS_TIMEOUT_MINUTES, TimeUnit.MINUTES).untilAsserted(() -> {
String output = client.execOnPod(PROMETHEUS_NAMESPACE, PROMETHEUS_POD, PROMETHEUS_CONTAINER, "curl",
String output = execOnPod(PROMETHEUS_NAMESPACE, PROMETHEUS_POD, PROMETHEUS_CONTAINER, "curl",
"http://localhost:9090/api/v1/query?query=" + primeNumberCustomMetricName(name));

assertTrue(output.contains("\"status\":\"success\""), "Verify the status was ok");
Expand Down Expand Up @@ -109,6 +115,19 @@ private String primeNumberCustomMetricName(String metricName) {
return String.format(metricName, uniqueId);
}

// TODO: Will be fixed once either https://github.com/fabric8io/kubernetes-client/issues/3473 is fixed
// or when releasing test framework to 0.0.12.
private String execOnPod(String prometheusNamespace, String prometheusPod, String prometheusContainer, String... input)
throws IOException, InterruptedException {
List<String> output = new ArrayList<>();
List<String> args = new ArrayList<>();
args.addAll(Arrays.asList("oc", "exec", prometheusPod, "-c", prometheusContainer, "-n", prometheusNamespace));
args.addAll(Arrays.asList(input));
new Command(args).outputToLines(output).runAndWait();

return output.stream().collect(Collectors.joining(System.lineSeparator()));
}

private static void loadServiceMonitor(Service app) {
client.apply(Paths.get("target/test-classes/service-monitor.yaml"));
}
Expand Down

0 comments on commit 952194f

Please sign in to comment.