Skip to content

Commit

Permalink
Merge pull request #4158 from Coduz/feat-improveJobEngineTest
Browse files Browse the repository at this point in the history
✅ [Job] Improved test execution times for Job / JobEngine tests
  • Loading branch information
Coduz authored Dec 17, 2024
2 parents 747af8a + 9561c0c commit b8a833b
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,25 @@
@ScenarioScoped
public class InitShiro {

private static final Logger logger = LoggerFactory.getLogger(InitShiro.class);
private static final Logger LOG = LoggerFactory.getLogger(InitShiro.class);

@Given("^Init Security Context$")
public void start() throws IOException {
logger.info("Init shiro security manager...");
LOG.info("Init shiro security manager...");
try {
SecurityManager securityManager = SecurityUtils.getSecurityManager();
logger.info("Found Shiro security manager {}", securityManager);
LOG.info("Found Shiro security manager {}", securityManager);
} catch (UnavailableSecurityManagerException e) {
logger.info("Init shiro security manager...");
LOG.info("Init shiro security manager...");
SecurityUtil.initSecurityManager();
}
logger.info("Init shiro security manager... DONE");
LOG.info("Init shiro security manager... DONE");
}

@Given("^Reset Security Context$")
public void stop() {
logger.info("Reset shiro security manager...");
LOG.info("Reset shiro security manager...");
SecurityUtils.setSecurityManager(null);
logger.info("Reset shiro security manager... DONE");
LOG.info("Reset shiro security manager... DONE");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*******************************************************************************/
package org.eclipse.kapua.qa.integration.steps;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.inject.Singleton;
Expand Down Expand Up @@ -49,14 +47,14 @@

import javax.inject.Inject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -76,7 +74,7 @@ public class DockerSteps {
private static final String API_IMAGE = "kapua-api";
private static final List<String> DEFAULT_DEPLOYMENT_CONTAINERS_NAME;
private static final List<String> DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME;
private static final int WAIT_COUNT = 120;//total wait time = 240 secs (120 * 2000ms)
private static final int WAIT_COUNT = 120; //total wait time = 240 secs (120 * 2000ms)
private static final long WAIT_STEP = 2000;
private static final long WAIT_FOR_DB = 10000;
private static final long WAIT_FOR_ES = 10000;
Expand Down Expand Up @@ -106,6 +104,7 @@ public class DockerSteps {
DEFAULT_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.AUTH_SERVICE_CONTAINER_NAME);
DEFAULT_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.MESSAGE_BROKER_CONTAINER_NAME);
DEFAULT_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.API_CONTAINER_NAME);

DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME = new ArrayList<>();
DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.JOB_ENGINE_CONTAINER_NAME);
DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.EVENTS_BROKER_CONTAINER_NAME);
Expand Down Expand Up @@ -235,6 +234,90 @@ public void startFullDockerEnvironment() throws Exception {
}
}

/**
* Starts Docker container requested.
*
* @param dockerContainers The Docker containers to start
* @throws Exception
* @since 2.1.0
*/
@Given("Start Docker environment with resources")
public void startDockerEnvironmentWithResources(List<String> dockerContainers) throws Exception {
cleanDockerEnvironmentInternal();

pullImage(ES_IMAGE);

createNetwork();

for (String dockerContainer : dockerContainers) {
switch (dockerContainer) {
case "db": {
startDBContainer(BasicSteps.DB_CONTAINER_NAME);
synchronized (this) {
this.wait(WAIT_FOR_DB);
}
} break;
case "es": {
startESContainer(BasicSteps.ES_CONTAINER_NAME);
synchronized (this) {
this.wait(WAIT_FOR_ES);
}
} break;
case "events-broker": {
startEventBrokerContainer(BasicSteps.EVENTS_BROKER_CONTAINER_NAME);
synchronized (this) {
this.wait(WAIT_FOR_EVENTS_BROKER);
}
} break;
case "job-engine": {
startJobEngineContainer(BasicSteps.JOB_ENGINE_CONTAINER_NAME);
synchronized (this) {
this.wait(WAIT_FOR_JOB_ENGINE);
}
} break;
case "message-broker": {
startMessageBrokerContainer(BasicSteps.MESSAGE_BROKER_CONTAINER_NAME);
synchronized (this) {
this.wait(WAIT_FOR_BROKER);
}
} break;
case "broker-auth-service": {
startAuthServiceContainer(BasicSteps.AUTH_SERVICE_CONTAINER_NAME);

long timeout = System.currentTimeMillis();
while (System.currentTimeMillis() - timeout < 30000) {
isServiceReady(AUTH_SERVICE_CHECK_WEB_APP);
Thread.sleep(500);
}
} break;
case "consumer-lifecycle": {
startLifecycleConsumerContainer(BasicSteps.LIFECYCLE_CONSUMER_CONTAINER_NAME);

long timeout = System.currentTimeMillis();
while (System.currentTimeMillis() - timeout < 30000) {
isServiceReady(LIFECYCLE_CHECK_WEB_APP);
Thread.sleep(500);
}
} break;
default:
throw new UnsupportedOperationException("Unknown container resource: " + dockerContainer);
}
}
}

private void cleanDockerEnvironmentInternal() throws DockerException, InterruptedException {
removeContainers(
Arrays.asList(
BasicSteps.JOB_ENGINE_CONTAINER_NAME,
BasicSteps.EVENTS_BROKER_CONTAINER_NAME,
BasicSteps.ES_CONTAINER_NAME,
BasicSteps.DB_CONTAINER_NAME
)
);

removeNetwork();
}

@Given("Start base docker environment")
public void startBaseDockerEnvironment() throws Exception {
stopBaseDockerEnvironment();
Expand Down Expand Up @@ -320,14 +403,14 @@ public void startApiDockerEnvironment(String tokenTTL, String refreshTokenTTL, i
}
}

private boolean areServicesReady() throws JsonParseException, JsonMappingException, IOException {
private boolean areServicesReady() throws IOException {
if (isServiceReady(LIFECYCLE_CHECK_WEB_APP) && isServiceReady(TELEMETRY_CHECK_WEB_APP) && isServiceReady(AUTH_SERVICE_CHECK_WEB_APP)) {
return true;
}
return false;
}

private boolean isServiceReady(String type) throws JsonParseException, JsonMappingException, IOException {
private boolean isServiceReady(String type) throws IOException {
URL serviceUrl = new URL(LIFECYCLE_HEALTH_URL);//lifecycle endpoint
if (TELEMETRY_CHECK_WEB_APP.equals(type)) {
serviceUrl = new URL(TELEMETRY_HEALTH_URL);//telemetry endpoint
Expand All @@ -336,50 +419,29 @@ private boolean isServiceReady(String type) throws JsonParseException, JsonMappi
}
logger.debug("Querying {} consumer status for url: {}", type, serviceUrl);
HttpURLConnection conn = null;
DataOutputStream out = null;
BufferedReader in = null;
InputStreamReader isr = null;

try {
conn = (HttpURLConnection) serviceUrl.openConnection();
conn.setConnectTimeout(HTTP_COMMUNICATION_TIMEOUT);
conn.setReadTimeout(HTTP_COMMUNICATION_TIMEOUT);
//works with spring boot actuator servlet mappings
// Works with spring boot actuator servlet mappings
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");

int status = conn.getResponseCode();
if (status == 200) {
isr = new InputStreamReader(conn.getInputStream());
in = new BufferedReader(isr);
return isRunning(MAPPER.readValue(in, Map.class));
try (InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader in = new BufferedReader(isr)) {
return isRunning(MAPPER.readValue(in, Map.class));
}
} else {
logger.info("Querying {} consumer status for url: {} - ERROR", type, serviceUrl);
return false;
}
} catch (IOException e) {
//nothing to do
} finally {
if (isr != null) {
try {
isr.close();
} catch (Exception e) {
logger.warn("Cannot close InputStreamReader", e.getMessage(), e);
}
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
logger.warn("Cannot close BufferedReader", e.getMessage(), e);
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
logger.warn("Cannot close DataOutputStream", e.getMessage(), e);
}
}
if (conn != null) {
try {
conn.disconnect();
Expand Down Expand Up @@ -636,7 +698,7 @@ public void stopContainer(List<String> names) throws DockerException, Interrupte
}

@Then("Remove container with name {string}")
public void removeContainers(List<String> names) throws DockerException, InterruptedException {
public void removeContainers(List<String> names) {
for (String name : names) {
removeContainer(name);
//search for images with / at the beginning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ Feature: Job Execution service CRUD tests
The Job service is responsible for maintaining the status of the target step executions.

@setup
Scenario: Init Security Context for all scenarios
Scenario: Setup test resources
Given Init Security Context
And Start base docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |

Scenario: Regular job execution creation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ Feature: Job service CRUD tests
The Job service is responsible for executing scheduled actions on various targets.

@setup
Scenario: Init Security Context for all scenarios
Scenario: Setup test resources
Given Init Security Context
And Start base docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |

Scenario: Regular job creation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ Feature: Job Target service CRUD tests
The Job service is responsible for maintaining a list of job targets.

@setup
Scenario: Init Security Context for all scenarios
Scenario: Setup test resources
Given Init Security Context
And Start base docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |

Scenario: Regular target creation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
Feature: Job Engine Service - Keystore Step Definitions

@setup
Scenario: Start full docker environment
Scenario: Setup test resources
Given Init Security Context
And Start full docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |
| message-broker |
| broker-auth-service |
| consumer-lifecycle |

#
# Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
Feature: JobEngineService tests for restarting job with offline device

@setup
Scenario: Start full docker environment
Scenario: Setup test resources
Given Init Security Context
And Start full docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |
| message-broker |
| broker-auth-service |
| consumer-lifecycle |

# *************************************************
# * Restarting a job with one Target and one Step *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
Feature: JobEngineService restart job tests with online device

@setup
Scenario: Start full docker environment
Scenario: Setup test resources
Given Init Security Context
And Start full docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |
| message-broker |
| broker-auth-service |
| consumer-lifecycle |

# *************************************************
# * Restarting a job with one Target and one Step *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
Feature: JobEngineService restart job tests with online device - second part

@setup
Scenario: Start full docker environment
Scenario: Setup test resources
Given Init Security Context
And Start full docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |
| message-broker |
| broker-auth-service |
| consumer-lifecycle |

# *************************************************
# * Restarting a job with one Target and one Step *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
Feature: JobEngineService tests for starting job with offline device

@setup
Scenario: Start full docker environment
Scenario: Setup test resources
Given Init Security Context
And Start full docker environment
And Start Docker environment with resources
| db |
| events-broker |
| job-engine |
| message-broker |
| broker-auth-service |
| consumer-lifecycle |

# ***********************************************
# * Starting a job with one Target and one Step *
Expand Down
Loading

0 comments on commit b8a833b

Please sign in to comment.