Skip to content

Commit

Permalink
chore: Improve stability of integration tests
Browse files Browse the repository at this point in the history
- Avoid to use the very same Http server instance for multiple tests
- Avoid racing conditions where Http requests are sent by the Camel JBang process after the test has finished
- Introduce individual Http server instances for the tests (jiraServer, slackServer, petstoreServer, httpServer)
- Better separation of tests due to individual server ports
  • Loading branch information
christophd committed Jan 28, 2025
1 parent 873cdc2 commit 1f0a4a8
Show file tree
Hide file tree
Showing 26 changed files with 171 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,52 @@

import org.citrusframework.annotations.CitrusConfiguration;
import org.citrusframework.container.SequenceAfterTest;
import org.citrusframework.container.SequenceBeforeTest;
import org.citrusframework.http.server.HttpServer;
import org.citrusframework.spi.BindToRegistry;
import org.citrusframework.util.SocketUtils;
import org.springframework.http.HttpStatus;

import static org.citrusframework.actions.CreateVariablesAction.Builder.createVariables;
import static org.citrusframework.actions.PurgeEndpointAction.Builder.purgeEndpoints;
import static org.citrusframework.http.endpoint.builder.HttpEndpoints.http;

@CitrusConfiguration
public class EndpointAutoConfiguration {

private static final long SERVER_TIMEOUT = 60000L;

private static final int HTTP_SERVER_PORT = SocketUtils.findAvailableTcpPort(8801);
private static final int SLACK_SERVER_PORT = SocketUtils.findAvailableTcpPort(8802);
private static final int PETSTORE_SERVER_PORT = SocketUtils.findAvailableTcpPort(8803);
private static final int JIRA_SERVER_PORT = SocketUtils.findAvailableTcpPort(8804);

private final HttpServer httpServer = http()
.server()
.port(8081)
.port(HTTP_SERVER_PORT)
.timeout(SERVER_TIMEOUT)
.autoStart(true)
.build();

private final HttpServer slackServer = http()
.server()
.port(SLACK_SERVER_PORT)
.timeout(SERVER_TIMEOUT)
.autoStart(true)
.build();

private final HttpServer petstoreServer = http()
.server()
.port(PETSTORE_SERVER_PORT)
.timeout(SERVER_TIMEOUT)
.defaultStatus(HttpStatus.CREATED)
.timeout(60000L)
.autoStart(true)
.build();

private final HttpServer jiraServer = http()
.server()
.port(JIRA_SERVER_PORT)
.timeout(SERVER_TIMEOUT)
.autoStart(true)
.build();

Expand All @@ -40,12 +71,45 @@ public HttpServer httpServer() {
return httpServer;
}

@BindToRegistry
public HttpServer slackServer() {
return slackServer;
}

@BindToRegistry
public HttpServer petstoreServer() {
return petstoreServer;
}

@BindToRegistry
public HttpServer jiraServer() {
return jiraServer;
}

@BindToRegistry
public SequenceBeforeTest beforeTest() {
return SequenceBeforeTest.Builder.beforeTest()
.actions(
// Set server ports as test variables
createVariables()
.variable("http.server.port", String.valueOf(HTTP_SERVER_PORT))
.variable("slack.server.port", String.valueOf(SLACK_SERVER_PORT))
.variable("petstore.server.port", String.valueOf(PETSTORE_SERVER_PORT))
.variable("jira.server.port", String.valueOf(JIRA_SERVER_PORT))
)
.build();
}

@BindToRegistry
public SequenceAfterTest afterTest() {
return SequenceAfterTest.Builder.afterTest()
.actions(
// Auto purge Http server endpoint
purgeEndpoints().endpoint(httpServer)
purgeEndpoints()
.endpoint(httpServer)
.endpoint(slackServer)
.endpoint(petstoreServer)
.endpoint(jiraServer)
)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ actions:
file: "aws/s3/aws-s3-to-http.yaml"
systemProperties:
file: "aws/s3/application.properties"
properties:
- name: http.sink.url
value: "http://localhost:${http.server.port}/incoming"

# Publish event
- send:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ spec:
apiVersion: camel.apache.org/v1
name: http-sink
properties:
url: http://localhost:8081/incoming
url: "{{http.sink.url}}"
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ actions:
run:
integration:
file: "earthquake/earthquake-to-http.yaml"
systemProperties:
properties:
- name: http.sink.url
value: "http://localhost:${http.server.port}/test"

# Verify Http request
- http:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ spec:
apiVersion: camel.apache.org/v1
name: earthquake-source
sink:
uri: http://localhost:8081/test
uri: "{{http.sink.url}}"
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ name: jira-add-comment-sink-pipe-test
variables:
- name: "timer.source.period"
value: "10000"
- name: "jira.url"
value: "http://localhost:8081"
- name: "jira.project.key"
value: "CAMEL"
- name: "jira.issue.assignee"
Expand All @@ -46,6 +44,10 @@ variables:
- name: "jira.password"
value: "secr3t"
actions:
- createVariables:
variables:
- name: "jira.url"
value: "http://localhost:${jira.server.port}"
# Create Camel JBang integration
- camel:
jbang:
Expand All @@ -70,7 +72,7 @@ actions:

# Verify get issue request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
GET:
path: "/rest/api/latest/issue/${jira.issue.key}"
Expand All @@ -79,7 +81,7 @@ actions:
value: "Basic citrus:encodeBase64(${jira.username}:${jira.password})"

- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand Down Expand Up @@ -126,7 +128,7 @@ actions:
# Verify server info request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
GET:
path: "/rest/api/latest/serverInfo"
Expand All @@ -135,7 +137,7 @@ actions:
value: "Basic citrus:encodeBase64(${jira.username}:${jira.password})"

- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand All @@ -162,7 +164,7 @@ actions:
# Verify add comment request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
POST:
path: "/rest/api/latest/issue/${jira.issue.key}/comment"
Expand All @@ -176,7 +178,7 @@ actions:
}
- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ name: jira-add-issue-sink-pipe-test
variables:
- name: "timer.source.period"
value: "10000"
- name: "jira.url"
value: "http://localhost:8081"
- name: "jira.project.key"
value: "CAMEL"
- name: "jira.issue.assignee"
Expand All @@ -42,6 +40,10 @@ variables:
- name: "jira.password"
value: "secr3t"
actions:
- createVariables:
variables:
- name: "jira.url"
value: "http://localhost:${jira.server.port}"
# Create Camel JBang integration
- camel:
jbang:
Expand Down Expand Up @@ -74,7 +76,7 @@ actions:

# Verify issue type request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
GET:
path: "/rest/api/latest/issuetype"
Expand All @@ -83,7 +85,7 @@ actions:
value: "Basic citrus:encodeBase64(${jira.username}:${jira.password})"

- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand Down Expand Up @@ -115,7 +117,7 @@ actions:
# Verify add issue request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
POST:
path: "/rest/api/latest/issue"
Expand All @@ -142,7 +144,7 @@ actions:
}
- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

name: jira-source-pipe-test
variables:
- name: "jira.url"
value: "http://localhost:8081"
- name: "jira.project.id"
value: "10001"
- name: "jira.issue.id"
Expand All @@ -34,6 +32,10 @@ variables:
- name: "jira.jql"
value: "assignee=citrus"
actions:
- createVariables:
variables:
- name: "jira.url"
value: "http://localhost:${jira.server.port}"
# Create Camel JBang integration
- camel:
jbang:
Expand All @@ -54,7 +56,7 @@ actions:

# Verify latest issue request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
GET:
path: "/rest/api/latest/search"
Expand All @@ -68,7 +70,7 @@ actions:
value: "Basic citrus:encodeBase64(${jira.username}:${jira.password})"

- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand All @@ -95,7 +97,7 @@ actions:
# Verify search request
- http:
server: "httpServer"
server: "jiraServer"
receiveRequest:
GET:
path: "/rest/api/latest/search"
Expand All @@ -111,7 +113,7 @@ actions:
value: "Basic citrus:encodeBase64(${jira.username}:${jira.password})"

- http:
server: "httpServer"
server: "jiraServer"
sendResponse:
response:
status: 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ actions:
file: "kafka/kafka-source-pipe.yaml"
systemProperties:
file: "kafka/application.properties"
properties:
- name: http.sink.url
value: "http://localhost:${http.server.port}/result"

# Verify topic subscription
- camel:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ spec:
securityProtocol: '{{kafka.securityProtocol}}'
deserializeHeaders: '{{kafka.deserializeHeaders}}'
sink:
uri: http://localhost:8081/result
uri: "{{http.sink.url}}"

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ actions:
file: "openapi/rest-openapi-sink-pipe.yaml"
systemProperties:
properties:
- name: "petstore.server.url"
value: "http://localhost:${petstore.server.port}"
- name: "petId"
value: "${petId}"
- name: "pet"
Expand All @@ -45,13 +47,13 @@ actions:

# Verify OpenAPI spec request
- http:
server: "httpServer"
server: "petstoreServer"
receiveRequest:
GET:
path: "/petstore/openapi.json"

- http:
server: "httpServer"
server: "petstoreServer"
sendResponse:
response:
status: 200
Expand All @@ -63,15 +65,15 @@ actions:

# Verify add pet request
- http:
server: "httpServer"
server: "petstoreServer"
receiveRequest:
POST:
path: "/petstore/pet"
body:
data: "${pet}"

- http:
server: "httpServer"
server: "petstoreServer"
sendResponse:
response:
status: 201
Expand Down
Loading

0 comments on commit 1f0a4a8

Please sign in to comment.