Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] Added and reviewed tests from @kazesberger #63

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.Result;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.tasks.Shell;
import hudson.util.DescribableList;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

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

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
Expand All @@ -33,7 +37,8 @@ public void testGlobalPropertiesWithWORKSPACE() throws Exception {
final String workspaceName = "WORKSPACE";

//A global node property TEST_WORKSPACE
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = Hudson.getInstance().getGlobalNodeProperties();
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = jenkins.getInstance()
.getGlobalNodeProperties();
globalNodeProperties.add(new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry(testWorkspaceVariableName, testWorkspaceVariableValue)));

EnvInjectJobProperty jobProperty = new EnvInjectJobProperty();
Expand Down Expand Up @@ -68,8 +73,10 @@ public void testGlobalPropertiesSetWORKSPACE() throws Exception {
final String testJobVariableName = "TEST_JOB_WORKSPACE";
final String testJobVariableExprValue = "${WORKSPACE}";

DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = Hudson.getInstance().getGlobalNodeProperties();
globalNodeProperties.add(new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry(testGlobalVariableName, testGlobalVariableValue)));
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = jenkins.getInstance()
.getGlobalNodeProperties();
globalNodeProperties.add(new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry
(testGlobalVariableName, testGlobalVariableValue)));

StringBuffer propertiesContent = new StringBuffer();
propertiesContent.append(testJobVariableName).append("=").append(testJobVariableExprValue);
Expand All @@ -91,4 +98,62 @@ public void testGlobalPropertiesSetWORKSPACE() throws Exception {

assertEquals(result_testGlobalVariableName, result_testJobVariableName);
}

@Test
public void testChangeOfGlobalPropertyGetsRecognizedWhenWithoutJobPropertyAndRunOnSlaves() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

wow. It should be splitted to small test methods. Think now it checks EVERITHING.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lanwen Indeed. The PR is [WiP]. I thought to review this kind of things. Anyway, thanks so much.

FreeStyleProject project = jenkins.createFreeStyleProject();

final String testVariableName = "TESTVAR";
final String testVariableValue = "value1";
final String testVariableValueAfterChange = "value2";

//A global node property TESTVAR
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = jenkins.getInstance()
.getGlobalNodeProperties();
EnvironmentVariablesNodeProperty.Entry testVarEntry = new EnvironmentVariablesNodeProperty.Entry(testVariableName, testVariableValue);
EnvironmentVariablesNodeProperty testVarNodePropertyItem = new EnvironmentVariablesNodeProperty(testVarEntry);
globalNodeProperties.add(testVarNodePropertyItem);

Node slaveNode = jenkins.createOnlineSlave();
project.setAssignedNode(slaveNode);
project.getBuildersList().add(new Shell("echo \"TESTVAR=$TESTVAR\""));

// we do NOT add a jobProperty - we want to test "default" behaviour of jenkins with installed envinject plugin
// so we run the build right away
FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());
assertEquals(slaveNode.getNodeName(), build.getBuiltOn().getNodeName());

// assert correct injection of testVariable #1
org.jenkinsci.lib.envinject.EnvInjectAction action = build.getAction(org.jenkinsci.lib.envinject.EnvInjectAction.class);
Map<String, String> envVars = action.getEnvMap();
String actualTestVariableValueInBuild = envVars.get(testVariableName);
assertNotNull("actual testVariableValue is null", actualTestVariableValueInBuild);
assertEquals(testVariableValue, actualTestVariableValueInBuild);

Set<Entry<String, String>> beforeChange = jenkins.getInstance().getComputer(slaveNode.getNodeName())
.getEnvironment()
.entrySet();

// now we change the global property variable value...
testVarEntry = new EnvironmentVariablesNodeProperty.Entry(testVariableName, testVariableValueAfterChange);
jenkins.getInstance().getGlobalNodeProperties().add(new EnvironmentVariablesNodeProperty(testVarEntry));

Set<Entry<String, String>> afterChange = jenkins.getInstance().getComputer(slaveNode.getNodeName()).getEnvironment()
.entrySet();
// environment of the slave does not change without restarting it. assert it to make test fail if there will be
// some kind of auto-restart to reload config.
assertEquals(beforeChange, afterChange);
assertEquals(beforeChange.toString(), afterChange.toString());

//...run the job again...
FreeStyleBuild secondBuild = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, secondBuild.getResult());
assertEquals(slaveNode.getNodeName(), secondBuild.getBuiltOn().getNodeName());

//...and expect the testvariable to have the changed value
org.jenkinsci.lib.envinject.EnvInjectAction action2 = secondBuild.getAction(org.jenkinsci.lib.envinject.EnvInjectAction.class);
assertNotNull("actual testVariableValue is null", action2.getEnvMap().get(testVariableName));
assertEquals(testVariableValueAfterChange, action2.getEnvMap().get(testVariableName));
}
}