Skip to content

Commit

Permalink
Forked environment pass thru (#73)
Browse files Browse the repository at this point in the history
* chaining the environment variable through to forked processes for #70
  • Loading branch information
iantmoore committed Nov 10, 2017
1 parent 309dd56 commit 124c202
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Requirements
-----
* Changed the root report page to be index.html. Added a redirect page in for the old.
* Glossary changes - got rid of the noise when extracting substeps tag info. Enabled the migration to new qualified custom glossary tags 'org.substeps.step.example' and 'org.substeps.step.section'
* some sonar suggested fixes
* If a -Denvironment= variable is set, pass through to the forked VM process. Can be overriden from the parent process using the vmArgs parameter in config if required.

1.1.2
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ object SubstepsConfigLoader {
resolveConfig(masterCfg, mavenConfigSettings, envConfig)
}

def getEnvironmentName() : String = {

Option(System.getProperty("ENVIRONMENT")) match {
case None => System.getProperty("environment")
case Some(env) => env
}
}

def environmentConfigFile() = {

val envConfigFile =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,27 @@
import com.technophobia.substeps.stepimplementations.MockStepImplementations;
import com.technophobia.substeps.steps.TestStepImplementations;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.substeps.config.SubstepsConfigLoader;
import org.substeps.report.IExecutionResultsCollector;
import org.substeps.runner.NewSubstepsExecutionConfig;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import static org.hamcrest.CoreMatchers.is;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;

import static org.mockito.Mockito.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.*;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.substeps.config.SubstepsConfigLoader;
import org.substeps.execution.ExecutionNodeResultNotificationHandler;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -157,7 +158,7 @@ private InputStreamConsumer startMBeanJVM() throws MojoExecutionException {

InputStreamConsumer localConsumer = null;

final List<String> command = buildSubstepsRunnerCommand();
final List<String> command = buildSubstepsRunnerCommand(this.log, createClasspathString(), this.jmxPort, this.vmArgs);

final ProcessBuilder processBuilder = new ProcessBuilder(command);

Expand Down Expand Up @@ -186,14 +187,8 @@ private InputStreamConsumer startMBeanJVM() throws MojoExecutionException {
return localConsumer;
}

/**
* @return
* @throws MojoExecutionException
* @throws DependencyResolutionRequiredException
*/
private List<String> buildSubstepsRunnerCommand() throws MojoExecutionException {
private static List<String> buildSubstepsRunnerCommand(Log log, final String classpath, int jmxPort, String vmArgs) throws MojoExecutionException {

final String classpath = createClasspathString();

final List<String> command = Lists.newArrayList();

Expand All @@ -206,29 +201,42 @@ private List<String> buildSubstepsRunnerCommand() throws MojoExecutionException
if (javaHome == null) {
// not sure how we'd get here - maven running without JAVA_HOME
// set..??
this.log.warn("unable to resolve JAVA_HOME variable, assuming java is on the path...");
log.warn("unable to resolve JAVA_HOME variable, assuming java is on the path...");
command.add("java");
} else {
command.add(javaHome + File.separator + "bin" + File.separator + "java");
}

command.add("-Dfile.encoding=UTF-8");
command.add("-Dcom.sun.management.jmxremote.port=" + this.jmxPort);
command.add("-Dcom.sun.management.jmxremote.port=" + jmxPort);
command.add("-Dcom.sun.management.jmxremote.authenticate=false");
command.add("-Dcom.sun.management.jmxremote.ssl=false");
command.add("-Djava.rmi.server.hostname=localhost");

if (this.vmArgs != null && !this.vmArgs.isEmpty()) {
final String[] args = this.vmArgs.split(" ");
String currentSpecifiedEnvironment = SubstepsConfigLoader.getEnvironmentName();

// if vmArgs doesn't specify and we've got an env var set, use it
if ( currentSpecifiedEnvironment != null && !currentSpecifiedEnvironment.isEmpty()){

if (vmArgs == null || (vmArgs != null && !vmArgs.toLowerCase().contains("-denvironment="))){
command.add("-Denvironment=" + currentSpecifiedEnvironment);
}
}

if (vmArgs != null && !vmArgs.isEmpty()) {
final String[] args = vmArgs.split(" ");
for (final String arg : args) {
command.add(arg);
this.log.info("Adding jvm arg: " + arg);
log.info("Adding jvm arg: " + arg);
}
}

command.add("-classpath");
command.add(classpath);
command.add("com.technophobia.substeps.jmx.SubstepsJMXServer");

// chain through any environment var that's set

return command;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.technophobia.substeps.mojo.runner;

import com.technophobia.substeps.runner.ForkedRunner;
import org.junit.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.mockito.Mockito;
import org.apache.maven.plugin.logging.Log;
import org.substeps.config.SubstepsConfigLoader;
import scala.Int;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.StringEndsWith.endsWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;


//import static org.mockito.Mockito.*;
import static org.hamcrest.Matchers.empty;
//import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.*;
import static org.mockito.Mockito.mock;


public class ForkedRunnerTest {

private Method commandBuilderMethod;
private static String previousEnvironment = null;

@BeforeClass
public static void preClassSetup(){

previousEnvironment = SubstepsConfigLoader.getEnvironmentName();
}

@AfterClass
public static void postClassTearDown(){
System.clearProperty("environment");
if (previousEnvironment != null) {
System.setProperty("environment", previousEnvironment);
}
}


@Before
public void setup() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{
commandBuilderMethod = ForkedRunner.class.getDeclaredMethod("buildSubstepsRunnerCommand", Log.class, String.class, Integer.TYPE, String.class);
commandBuilderMethod.setAccessible(true);

System.clearProperty("environment");
System.clearProperty("ENVIRONMENT");

}

private List<String> invoke(String cp, int port, String vmArgs) throws InvocationTargetException, IllegalAccessException{
Log log = mock(Log.class);
List<String> results =
(List<String>) commandBuilderMethod.invoke(ForkedRunner.class, log, cp, port, vmArgs);

for (String r : results){
System.out.println(r);
}

return results;
}

@Test
public void testCommandConstructionWithSingleVMArgNoEnv () throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

int port = 9292;
List<String> results = invoke("myclasspath", port, "-XWhatever");

Assert.assertThat(results, is(not(empty())));

Assert.assertThat(results.get(0), endsWith("/java"));

Assert.assertThat(results.get(2), is("-Dcom.sun.management.jmxremote.port=" + port));

Assert.assertThat(results.get(6), is("-XWhatever"));

Assert.assertThat(results.get(8), is("myclasspath"));

Assert.assertThat(results.get(9), is("com.technophobia.substeps.jmx.SubstepsJMXServer"));
}

@Test
public void testChainingOfEnvironmentParamWhenNoConflictingVmArg() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

int port = 9292;

System.setProperty("environment", "travis");

List<String> results = invoke("myclasspath", port, null);

Assert.assertThat(results.get(6), is("-Denvironment=travis"));

}

@Test
public void testChainingOfEnvironmentParamWhenConflictingVmArg() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

int port = 9292;

System.setProperty("ENVIRONMENT", "travis");

List<String> results = invoke("myclasspath", port, "-DENVIRONMENT=jenkins");

Assert.assertThat(results.get(6), is("-DENVIRONMENT=jenkins"));

}
}

0 comments on commit 124c202

Please sign in to comment.