Skip to content

Commit

Permalink
chore: Fix CI jobs
Browse files Browse the repository at this point in the history
Make sure to load JBang  and initialize Camel CLI before running commands
  • Loading branch information
christophd committed Jan 12, 2023
1 parent 9eef8ad commit 5523311
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ public class VerifyKameletBindingAction extends AbstractKameletAction {

private final String bindingName;

private final int maxAttempts;
private final long delayBetweenAttempts;

/**
* Constructor using given builder.
* @param builder
*/
public VerifyKameletBindingAction(Builder builder) {
super("verify-kamelet-binding", builder);
this.bindingName = builder.bindingName;
this.maxAttempts = builder.maxAttempts;
this.delayBetweenAttempts = builder.delayBetweenAttempts;
}

@Override
Expand All @@ -66,20 +71,46 @@ public void doExecute(TestContext context) {

private void verifyLocalKameletBinding(String name, TestContext context) {
Long pid = context.getVariable(name + ":pid", Long.class);
Map<String, String> properties = camel().get(pid);
if ((!properties.isEmpty() && properties.get("STATUS").equals("Running"))) {
LOG.info(String.format("Verified Kamelet binding '%s' state 'Running' - All values OK!", name));
} else {
throw new ValidationException(String.format("Failed to retrieve Kamelet binding '%s' in state 'Running'", name));

for (int i = 0; i < maxAttempts; i++) {
Map<String, String> properties = camel().get(pid);
if ((!properties.isEmpty() && properties.get("STATUS").equals("Running"))) {
LOG.info(String.format("Verified Kamelet binding '%s' state 'Running' - All values OK!", name));
return;
}

LOG.info(String.format("Waiting for Kamelet binding '%s' to be in state 'Running'- retry in %s ms", name, delayBetweenAttempts));
try {
Thread.sleep(delayBetweenAttempts);
} catch (InterruptedException e) {
LOG.warn("Interrupted while waiting for Kamelet binding", e);
}
}

throw new ValidationException(String.format("Failed to retrieve Kamelet binding '%s' in state 'Running'", name));
}

private void verifyKameletBinding(String namespace, String name) {
CustomResourceDefinitionContext ctx = CamelKSupport.kameletBindingCRDContext(CamelKSettings.getKameletApiVersion());
KameletBinding binding = getKubernetesClient().customResources(ctx, KameletBinding.class, KameletBindingList.class)
.inNamespace(namespace)
.withName(name)
.get();

KameletBinding binding = null;
for (int i = 0; i < maxAttempts; i++) {
binding = getKubernetesClient().customResources(ctx, KameletBinding.class, KameletBindingList.class)
.inNamespace(namespace)
.withName(name)
.get();

if (binding == null) {
LOG.info(String.format("Waiting for Kamelet binding '%s' - retry in %s ms", name, delayBetweenAttempts));
try {
Thread.sleep(delayBetweenAttempts);
} catch (InterruptedException e) {
LOG.warn("Interrupted while waiting for Kamelet binding", e);
}
} else {
break;
}
}

if (binding == null) {
throw new ValidationException(String.format("Failed to retrieve Kamelet binding '%s' in namespace '%s'", name, namespace));
Expand All @@ -97,6 +128,10 @@ public static final class Builder extends AbstractKameletAction.Builder<VerifyKa

private String bindingName;

private int maxAttempts = CamelKSettings.getMaxAttempts();
private long delayBetweenAttempts = CamelKSettings.getDelayBetweenAttempts();


public Builder isAvailable() {
return this;
}
Expand All @@ -106,6 +141,16 @@ public Builder isAvailable(String name) {
return this;
}

public Builder maxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
return this;
}

public Builder delayBetweenAttempts(long delayBetweenAttempts) {
this.delayBetweenAttempts = delayBetweenAttempts;
return this;
}

@Override
public VerifyKameletBindingAction build() {
return new VerifyKameletBindingAction(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ public String ps() {
return p.getOutput();
}

/**
* Get Camel JBang version.
*/
public String version() {
ProcessAndOutput p = execute(camel("--version"));
return p.getOutput();
}

/**
* Get details for integration previously run via JBang Camel app. Integration is identified by its process id.
* @param pid
Expand Down Expand Up @@ -210,13 +218,13 @@ public List<Map<String, String>> getAll() {
}

private static void detectJBang() {
ProcessAndOutput result = version();
ProcessAndOutput result = getVersion();
if (result.getProcess().exitValue() == OK_EXIT_CODE) {
LOG.info("Found JBang v" + result.getOutput());
} else {
LOG.warn("JBang not found. Downloading ...");
download();
result = version();
result = getVersion();
if (result.getProcess().exitValue() == OK_EXIT_CODE) {
LOG.info("Using JBang v" + result.getOutput());
}
Expand Down Expand Up @@ -264,7 +272,7 @@ private static void download() {
installDir = installPath.resolve(homePath);
}

private static ProcessAndOutput version() {
private static ProcessAndOutput getVersion() {
return execute(jBang("version"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Long getCamelProcessId() {
try {
if (isUnix()) {
// wait for descendant process to be available
await().atMost(5000L, TimeUnit.MILLISECONDS)
await().atMost(15000L, TimeUnit.MILLISECONDS)
.until(() -> process.descendants().findAny().isPresent());
return process.descendants()
.filter(p -> p.info().commandLine().orElse("").contains(CamelJBangSettings.getCamelApp()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public void shouldCreateIntegrationWithConfigModeline() {

@Test
public void shouldCreateLocalJBangIntegration() {
camel().version();
CreateIntegrationAction action = new CreateIntegrationAction.Builder()
.client(kubernetesClient)
.integration("foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class DeleteIntegrationActionTest {
@BeforeClass
public static void setup() throws IOException {
sampleIntegration = new ClassPathResource("simple.groovy").getFile().toPath();
camel().version();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class VerifyIntegrationActionTest {
@BeforeClass
public static void setup() throws IOException {
sampleIntegration = new ClassPathResource("simple.groovy").getFile().toPath();
camel().version();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.citrusframework.yaks.YaksClusterType;
import org.citrusframework.yaks.camelk.actions.integration.CreateIntegrationActionTest;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -52,9 +53,13 @@ public class CreateKameletBindingActionTest {

private final TestContext context = TestContextFactory.newInstance().getObject();

@BeforeClass
public static void setup() throws IOException {
camel().version();
}

@Test
public void shouldCreateLocalJBangKameletBinding() throws IOException {
System.setProperty("yaks.jbang.camel.dump.integration.output", "true");
CreateKameletBindingAction action = new CreateKameletBindingAction.Builder()
.client(kubernetesClient)
.binding("timer-to-log-binding")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class DeleteKameletBindingActionTest {
@BeforeClass
public static void setup() throws IOException {
sampleBinding = new ClassPathResource("timer-to-log-binding.yaml").getFile().toPath();
camel().version();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.context.TestContextFactory;
Expand All @@ -33,18 +32,12 @@
import org.citrusframework.yaks.camelk.jbang.ProcessAndOutput;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import static org.awaitility.Awaitility.await;
import static org.citrusframework.yaks.camelk.jbang.CamelJBang.camel;

public class VerifyKameletBindingActionTest {

/** Logger */
private static final Logger LOG = LoggerFactory.getLogger(VerifyKameletBindingActionTest.class);

private final KubernetesMockServer k8sServer = new KubernetesMockServer(new Context(), new MockWebServer(),
new HashMap<>(), new KubernetesCrudDispatcher(), false);

Expand All @@ -57,24 +50,24 @@ public class VerifyKameletBindingActionTest {
@BeforeClass
public static void setup() throws IOException {
sampleBinding = new ClassPathResource("timer-to-log-binding.yaml").getFile().toPath();
camel().version();
}

@Test
public void shouldVerifyLocalJBangIntegration() {
ProcessAndOutput pao = camel().run("timer-to-log-binding.yaml", sampleBinding);
ProcessAndOutput pao = camel().run("timer-to-log-binding", sampleBinding);
Long pid = pao.getCamelProcessId();

try {
VerifyKameletBindingAction action = new VerifyKameletBindingAction.Builder()
.client(kubernetesClient)
.isAvailable("timer-to-log-binding.yaml")
.isAvailable("timer-to-log-binding")
.clusterType(YaksClusterType.LOCAL)
.maxAttempts(10)
.build();

context.setVariable("timer-to-log-binding.yaml:pid", pid);
context.setVariable("timer-to-log-binding.yaml:process:" + pid, pao);

await().atMost(30000L, TimeUnit.MILLISECONDS).until(() -> !camel().get(pid).isEmpty());
context.setVariable("timer-to-log-binding:pid", pid);
context.setVariable("timer-to-log-binding:process:" + pid, pao);

action.execute(context);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,4 @@ spec:
properties:
message: "Hello world!"
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: log-sink
properties:
showHeaders: true
uri: log:info

0 comments on commit 5523311

Please sign in to comment.