Skip to content

Commit

Permalink
[JENKINS-28764] Write tests and fix possible bug before shipping code
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jun 28, 2015
1 parent d57ac66 commit 2ee4d34
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/biouno/unochoice/model/ScriptlerScript.java
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;

import org.biouno.unochoice.util.Utils;
import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -87,7 +88,7 @@ public Object eval() {
* @see org.biouno.unochoice.model.Script#eval(java.util.Map)
*/
public Object eval(Map<String, String> parameters) {
final Map<String, String> envVars = System.getenv();
final Map<String, String> envVars = Utils.getSystemEnv();
Map<String, String> evaledParameters = new HashMap<String, String>(envVars);
// if we have any parameter that came from UI, let's eval and use them
if (parameters != null && parameters.size() > 0) {
Expand All @@ -100,7 +101,7 @@ public Object eval(Map<String, String> parameters) {
evaledParameters.put(key, value);
}
} else {
evaledParameters = this.getParameters();
evaledParameters.putAll(this.getParameters());
}
return this.toGroovyScript().eval(evaledParameters);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/biouno/unochoice/util/Utils.java
Expand Up @@ -24,6 +24,7 @@

package org.biouno.unochoice.util;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -98,4 +99,14 @@ public static String createRandomParameterName(String prefix, String suffix) {
return paramName;
}

/**
* Helped method to return the system environment variables. The main advantage
* over calling the System.getenv method directly, is that we can mock this call
* (System is final).
*
* @return System environment variables as map
*/
public static Map<String, String> getSystemEnv() {
return System.getenv();
}
}
68 changes: 68 additions & 0 deletions src/test/java/org/biouno/unochoice/issue28764/TestIssue28764.java
@@ -0,0 +1,68 @@
package org.biouno.unochoice.issue28764;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.biouno.unochoice.model.ScriptlerScript;
import org.biouno.unochoice.model.ScriptlerScriptParameter;
import org.biouno.unochoice.util.Utils;
import org.jenkinsci.plugins.scriptler.config.Parameter;
import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ScriptHelper.class, Utils.class })
public class TestIssue28764 {

private List<ScriptlerScriptParameter> params = null;

private Script script = null;

private ScriptlerScript scriptlerScript = null;

private Map<String, String> myEnv = null;

@Before
public void setUp() {
params = new ArrayList<ScriptlerScriptParameter>();

script = new Script("id", "name", "comment", true, "originCatalog",
"originScript", "originDate", true,
(Parameter[]) params.toArray(new Parameter[0]), false);
script.setScript("if(binding.variables.get('flag') == null) {return [1, 2, 3]} else {return [1000, 100, 1]}");

myEnv = new HashMap<String, String>();
myEnv.put("flag", "myflag");

scriptlerScript = new ScriptlerScript("id", params);

// Mock
PowerMockito.mockStatic(Utils.class);
PowerMockito.when(Utils.getSystemEnv()).thenReturn(this.myEnv);

PowerMockito.mockStatic(ScriptHelper.class);
PowerMockito.when(
ScriptHelper.getScript(Mockito.anyString(),
Mockito.anyBoolean())).thenReturn(this.script);
}

@SuppressWarnings("unchecked")
@Test
public void testEnvVarsExpanding() {
List<Integer> returnValue = (List<Integer>) scriptlerScript.eval();
assertEquals(Arrays.asList(new Integer[] { 1000, 100, 1 }), returnValue);
}

}
@@ -0,0 +1,5 @@
/**
* Tests for JENKINS-28764: Environment variables are not expanded and thus cannot be used as parameter values to
* Scriptler scripts
*/
package org.biouno.unochoice.issue28764;

8 comments on commit 2ee4d34

@wykapedia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows for the usage of environment variables in the Scriptler script, but it does not fix the expansion of environment variables which are placed in a Scriptler script parameter. Can this be fixed?

@kinow
Copy link
Member Author

@kinow kinow commented on 2ee4d34 Jan 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @wykapedia

Sure, can you file an issue in issues.jenkins-ci.org, with component=active-choices-plugin, please?

@wykapedia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried creating an account but it says I am a spambot. Can you create the issue or instruct me on how to get an account?

@kinow
Copy link
Member Author

@kinow kinow commented on 2ee4d34 Jan 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't followed exactly what's going on in the jenkins mailing list, but I saw heaps of messages about account creation. Looks like the infra guys are having some problems there.

https://issues.jenkins-ci.org/browse/JENKINS-32680

Created JENKINS-32680. I organise my work when I start a development cycle using JIRA for Jenkins plug-ins. But you can comment here if you have problems logging in to Jenkins JIRA. The only problem is that I don't get e-mails from GitHub when there are changes here (though normally I check my GitHub notifications via github.com)

@wykapedia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for creating the issue. Specifically, if I try to use a global environment variable or global password in a Scriptler parameter, it does not get expanded.

@kak-bo-che
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have had the same issue creating an account in the jenkins jira. For what its worth this is the work around I've been using in my own groovy scripts:

          def envVars = null

          def instance = Jenkins.getInstance()
          if (instance){
            def globalNodeProperties = instance.getGlobalNodeProperties()
            def envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
            envVars = envVarsNodePropertyList.get(0).getEnvVars()
            my_env_var = envVars['my_env_var']
          } else {
            my_env_var =  build.getEnvironment(listener).get('my_env_var')
          }

@kinow
Copy link
Member Author

@kinow kinow commented on 2ee4d34 Feb 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for showing us your work around @kak-bo-che

We fixed an issue with the jenkinsProject global property, and it was fixed based on the work around of another user in Groovy. So it may definitely help when we try to tackle this problem in the next release.

Stay tuned, but feel free to bump the thread here, on Jenkins JIRA, or in our mailing list if we take too long to make progress.

All the best,
Bruno

@dainiax
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kak-bo-che Your work around was the only one, which worked for me to get environment variables.
Still less hacky approach would be nice, but none of any examples worked.

Please sign in to comment.