Skip to content

Commit

Permalink
feat: integrate new enterprise start mecanism depending on deployment…
Browse files Browse the repository at this point in the history
…, closes HYB-568

Modifications:

Drop unexisting methods
Integrate new enterprise start depending on deployment
  • Loading branch information
tpetillot committed Apr 4, 2024
1 parent c5f0f2a commit 1b8ea53
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 231 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<maven-plugin-annotations.version>3.6.2</maven-plugin-annotations.version>
<header.basedir>${project.basedir}</header.basedir>
<junit.version>5.10.2</junit.version>
<gatling-enterprise-plugin-commons.version>1.9.0-M13</gatling-enterprise-plugin-commons.version>
<gatling-enterprise-plugin-commons.version>1.9.0-M14</gatling-enterprise-plugin-commons.version>

<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<maven-plugin-plugin.version>3.11.0</maven-plugin-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.gatling.plugin.*;
import io.gatling.plugin.client.EnterpriseClient;
import io.gatling.plugin.client.http.HttpEnterpriseClient;
import io.gatling.plugin.client.HttpEnterpriseClient;
import io.gatling.plugin.exceptions.UnsupportedClientException;
import io.gatling.plugin.io.JavaPluginScanner;
import io.gatling.plugin.io.PluginIO;
Expand Down
34 changes: 1 addition & 33 deletions src/main/java/io/gatling/mojo/CommonLogMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,14 @@
*/
package io.gatling.mojo;

import io.gatling.plugin.model.Simulation;
import java.net.URL;

public final class CommonLogMessage {

private CommonLogMessage() {}

public static String simulationCreated(Simulation simulation) {
return "Successfully created simulation " + simulation.name + " with ID " + simulation.id;
}

public static String simulationChosen(Simulation simulation) {
return "Chose to start simulation " + simulation.name + " with ID " + simulation.id;
}

public static String simulationConfiguration(
Simulation simulation, String simulationIdSetting, boolean waitForRunEnd) {
final StringBuilder builder = new StringBuilder();
if (simulationIdSetting == null) {
builder
.append("To start the same simulation again, specify -Dgatling.enterprise.simulationId=")
.append(simulation.id)
.append(", or add the configuration to your pom.xml, e.g.:\n")
.append(pluginConfiguration("simulationId", simulation.id.toString()))
.append("\n");
}
if (!waitForRunEnd) {
builder
.append(
"To wait for the end of the run when starting a simulation on Gatling Enterprise, specify -Dgatling.enterprise.waitForRunEnd=true, or add the configuration to your pom.xml, e.g.:\n")
.append(pluginConfiguration("waitForRunEnd", "true"))
.append("\n");
}
return builder.toString();
}

public static String simulationStartSuccess(URL enterpriseUrl, String reportsPath) {
return "Simulation successfully started; the report will be available at "
+ enterpriseUrl
+ reportsPath;
return "Simulation successfully started; reports are available at " + enterpriseUrl + reportsPath;
}

/**
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/io/gatling/mojo/EnterpriseDeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.gatling.plugin.deployment.DeploymentConfiguration;
import io.gatling.plugin.exceptions.EnterprisePluginException;
import io.gatling.plugin.model.BuildTool;
import io.gatling.plugin.model.DeploymentInfo;
import java.io.File;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
Expand All @@ -29,6 +30,7 @@
@Execute(goal = "enterprisePackage")
@Mojo(name = "enterpriseDeploy", requiresDependencyResolution = ResolutionScope.TEST)
public final class EnterpriseDeployMojo extends AbstractEnterprisePluginMojo {
public static final String CONTEXT_ENTERPRISE_DEPLOY_INFO = "enterprise_deploy_info";

@Override
public void execute() throws MojoFailureException {
Expand All @@ -38,13 +40,16 @@ public void execute() throws MojoFailureException {
final Boolean isPrivateRepositoryEnabled = controlPlaneUrl != null;
final BatchEnterprisePlugin plugin = initBatchEnterprisePlugin();
try {
plugin.deployFromDescriptor(
deploymentFile,
packageFile,
mavenProject.getArtifactId(),
isPrivateRepositoryEnabled,
BuildTool.MAVEN,
getClass().getPackage().getImplementationVersion());
DeploymentInfo deploymentInfo =
plugin.deployFromDescriptor(
deploymentFile,
packageFile,
mavenProject.getArtifactId(),
isPrivateRepositoryEnabled,
BuildTool.MAVEN,
getClass().getPackage().getImplementationVersion());

getPluginContext().put(CONTEXT_ENTERPRISE_DEPLOY_INFO, deploymentInfo);
} catch (EnterprisePluginException e) {
throw new MojoFailureException(e.getMessage(), e);
}
Expand Down
160 changes: 18 additions & 142 deletions src/main/java/io/gatling/mojo/EnterpriseStartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,96 +18,34 @@

import io.gatling.plugin.EnterprisePlugin;
import io.gatling.plugin.exceptions.EnterprisePluginException;
import io.gatling.plugin.model.DeploymentInfo;
import io.gatling.plugin.model.RunSummary;
import io.gatling.plugin.model.SimulationEndResult;
import io.gatling.plugin.model.SimulationStartResult;
import io.gatling.plugin.util.PropertiesParserUtil;
import java.io.File;
import java.util.Map;
import java.util.UUID;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

/**
* Mojo to package, upload and start a simulation on Gatling Enterprise Cloud.
* Mojo start a deployed simulation on Gatling Enterprise Cloud.
*
* <ul>
* <li>By default, this goal will prompt you to choose to run a simulation already configured on
* Gatling Enterprise or configure a new one, and provide all required details.
* <li>If a simulationId is set, this goal will automatically choose to start that simulation.
* <li>By default, this goal will prompt you to choose to run a deployed simulation
* <li>If a simulation name is passed as property, this goal will automatically start that
* simulation.
* <li>If Maven is run in batch mode, any interactive prompts will be disabled and the goal will
* fail if user input is required (see Maven's batch mode here:
* https://maven.apache.org/ref/3-LATEST/maven-embedder/cli.html#batch-mode).
* </ul>
*/
@Execute(goal = "enterprisePackage")
@Execute(goal = "enterpriseDeploy")
@Mojo(name = "enterpriseStart", requiresDependencyResolution = ResolutionScope.TEST)
public final class EnterpriseStartMojo extends AbstractEnterprisePluginMojo {

/**
* List of exclude patterns to use when scanning for simulation classes. Excludes none by default.
*/
@Parameter(property = "gatling.excludes")
private String[] excludes;

/** The fully qualified name of the Simulation class to run. */
@Parameter(property = "gatling.simulationClass")
private String simulationClass;

/** The ID of the team used when configuring a new package or simulation on Gatling Enterprise. */
@Parameter(property = "gatling.enterprise.teamId")
private String teamId;

/**
* The ID of a simulation already configured on Gatling Enterprise. If 'simulationId' is
* configured, gatling:enterpriseStart will upload your updated code to the package configured for
* that simulation, and start the simulation.
*/
@Parameter(property = "gatling.enterprise.simulationId")
private String simulationId;

/**
* The ID of a package already configured on Gatling Enterprise. When configuring a new simulation
* on Gatling Enterprise, this will force the use of an existing package for that simulation.
*/
@Parameter(property = "gatling.enterprise.packageId")
private String packageId;

/**
* Provides system properties when starting a simulation, in addition to the ones which may
* already be defined for that simulation (see
* https://gatling.io/docs/enterprise/cloud/reference/user/simulations/#step-3-injector-parameters).
* To provide system properties on the command line, use the format
* -Dgatling.enterprise.simulationSystemProperties=key1=value1,key2=value2
*/
@Parameter private Map<String, String> simulationSystemProperties;

/**
* Alternative to simulationSystemProperties. Use the following format: key1=value1,key2=value2
* This is meant to be used on the command line, rather than in the pom.xml.
*/
@Parameter(property = "gatling.enterprise.simulationSystemProperties")
private String simulationSystemPropertiesString;

/**
* Provides additional environment variables when starting a simulation, in addition to the ones
* which may already be defined for that simulation (see
* https://gatling.io/docs/enterprise/cloud/reference/user/simulations/#step-3-injector-parameters).
* To provide environment variables on the command line, use the format use
* -Dgatling.enterprise.simulationEnvironmentVariables=key1=value1,key2=value2
*/
@Parameter private Map<String, String> simulationEnvironmentVariables;

/**
* Alternative to simulationEnvironmentVariables. Use the following format:
* key1=value1,key2=value2 This is meant to be used on the command line, rather than in the
* pom.xml.
*/
@Parameter(property = "gatling.enterprise.simulationEnvironmentVariables")
private String simulationEnvironmentVariablesString;
@Parameter(property = "gatling.enterprise.simulationName")
private String simulationName;

/**
* Wait for the result after starting the simulation on Gatling Enterprise, and complete with an
Expand All @@ -118,87 +56,25 @@ public final class EnterpriseStartMojo extends AbstractEnterprisePluginMojo {

@Override
public void execute() throws MojoFailureException {
final UUID teamIdUuid = teamId != null ? UUID.fromString(teamId) : null;
final UUID packageIdUuid = packageId != null ? UUID.fromString(packageId) : null;
if (simulationSystemProperties == null) {
// @Parameter(defaultValue = ...) only works for properties with a single value
simulationSystemProperties = Map.of();
}
final File file = enterprisePackage();

final Map context = getPluginContext();
final DeploymentInfo deploymentInfo =
(DeploymentInfo) context.get(EnterpriseDeployMojo.CONTEXT_ENTERPRISE_DEPLOY_INFO);
final EnterprisePlugin plugin = initEnterprisePlugin(interactive());

final SimulationStartResult startResult =
RecoverEnterprisePluginException.handle(
() ->
simulationId == null
? createAndStartSimulation(plugin, file, teamIdUuid, packageIdUuid)
: startExistingSimulation(plugin, file),
getLog());

getLog()
.info(
CommonLogMessage.simulationStartSuccess(
enterpriseUrl, startResult.runSummary.reportsPath));

if (simulationId == null || !waitForRunEnd) {
getLog()
.info(
CommonLogMessage.simulationConfiguration(
startResult.simulation, simulationId, waitForRunEnd));
try {
RunSummary runSummary = plugin.startSimulation(simulationName, deploymentInfo);
getLog().info(CommonLogMessage.simulationStartSuccess(enterpriseUrl, runSummary.reportsPath));
waitForRunEnd(plugin, runSummary);
} catch (EnterprisePluginException e) {
throw new MojoFailureException(
"Unhandled Gatling Enterprise plugin exception: " + e.getMessage(), e);
}

waitForRunEnd(plugin, startResult.runSummary);
}

private EnterprisePlugin initEnterprisePlugin(boolean isInteractive) throws MojoFailureException {
return isInteractive ? initInteractiveEnterprisePlugin() : initBatchEnterprisePlugin();
}

private SimulationStartResult startExistingSimulation(
EnterprisePlugin enterprisePlugin, File file) throws EnterprisePluginException {
getLog().info("Uploading and starting simulation...");
return enterprisePlugin.uploadPackageAndStartSimulation(
UUID.fromString(simulationId),
selectProperties(simulationSystemProperties, simulationSystemPropertiesString),
selectProperties(simulationEnvironmentVariables, simulationEnvironmentVariablesString),
simulationClass,
file);
}

private SimulationStartResult createAndStartSimulation(
EnterprisePlugin enterprisePlugin, File file, UUID teamIdUuid, UUID packageIdUuid)
throws EnterprisePluginException {
final SimulationStartResult result =
enterprisePlugin.createAndStartSimulation(
teamIdUuid,
mavenProject.getGroupId(),
mavenProject.getArtifactId(),
simulationClass,
packageIdUuid,
selectProperties(simulationSystemProperties, simulationSystemPropertiesString),
selectProperties(simulationEnvironmentVariables, simulationEnvironmentVariablesString),
file);

logSimulationCreatedOrChosen(result);
return result;
}

private void logSimulationCreatedOrChosen(SimulationStartResult result) {
if (result.createdSimulation) {
getLog().info(CommonLogMessage.simulationCreated(result.simulation));
} else {
getLog().info(CommonLogMessage.simulationChosen(result.simulation));
}
}

private Map<String, String> selectProperties(
Map<String, String> propertiesMap, String propertiesString) {
return (propertiesMap == null || propertiesMap.isEmpty())
? PropertiesParserUtil.parseProperties(propertiesString)
: propertiesMap;
}

private void waitForRunEnd(EnterprisePlugin plugin, RunSummary startedRun)
throws MojoFailureException {
if (waitForRunEnd) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/gatling/mojo/MojoConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package io.gatling.mojo;

import java.util.*;

public final class MojoConstants {

private MojoConstants() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
package io.gatling.mojo;

import io.gatling.plugin.EmptyChoicesException;
import io.gatling.plugin.exceptions.*;
import io.gatling.plugin.model.Simulation;
import java.util.stream.Collectors;
import io.gatling.plugin.exceptions.EnterprisePluginException;
import io.gatling.plugin.exceptions.UnsupportedJavaVersionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;

public final class RecoverEnterprisePluginException {

@FunctionalInterface
public static interface EnterprisePluginExceptionFunction<R> {
public interface EnterprisePluginExceptionFunction<R> {
R apply() throws EnterprisePluginException;
}

Expand All @@ -47,47 +46,6 @@ static <R> R handle(EnterprisePluginExceptionFunction<R> f, Log log) throws Mojo
+ e.supportedVersion
+ " or lower.";
throw new MojoFailureException(msg);
} catch (SeveralTeamsFoundException e) {
final String availableTeams =
e.getAvailableTeams().stream()
.map(t -> String.format("- %s (%s)\n", t.id, t.name))
.collect(Collectors.joining());
final String teamExample = e.getAvailableTeams().get(0).id.toString();
final String msg =
"Several teams were found to create a simulation.\n"
+ "Available teams:\n"
+ availableTeams
+ CommonLogMessage.missingConfiguration(
"team", "teamId", "gatling.enterprise.teamId", null, teamExample);
throw new MojoFailureException(msg);
} catch (SeveralSimulationClassNamesFoundException e) {
final String availableClasses =
e.getAvailableSimulationClassNames().stream()
.map(s -> String.format("- %s\n", s))
.collect(Collectors.joining());
final String classExample = e.getAvailableSimulationClassNames().stream().findFirst().get();
final String msg =
"Several simulation classes were found.\n"
+ "Available classes:\n"
+ availableClasses
+ "\n"
+ CommonLogMessage.missingConfiguration(
"class", "simulationClass", "gatling.simulationClass", null, classExample);
throw new MojoFailureException(msg);
} catch (SimulationStartException e) {
final Simulation simulation = e.getSimulation();
if (e.isCreated()) {
log.info(CommonLogMessage.simulationCreated(simulation));
}
final String msg =
"Failed to start simulation.\n"
+ String.format(
"Simulation %s with ID %s exists but could not be started: ",
simulation.name, simulation.id)
+ e.getCause().getMessage()
+ "\n"
+ CommonLogMessage.simulationConfiguration(simulation, null, false);
throw new MojoFailureException(msg, e);
} catch (EmptyChoicesException e) {
throw new MojoFailureException(e.getMessage(), e);
} catch (EnterprisePluginException e) {
Expand Down

0 comments on commit 1b8ea53

Please sign in to comment.