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

Move tests from core #81

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
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
108 changes: 108 additions & 0 deletions src/test/java/hudson/tasks/EnvVarsInConfigTasksTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package hudson.tasks;

import static hudson.tasks._ant.Messages.Ant_ExecutableNotFound;
import static org.junit.Assert.assertFalse;

import hudson.EnvVars;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.JDK;
import hudson.model.Result;
import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import hudson.tasks.Ant.AntInstallation;
import org.junit.Assume;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.ToolInstallations;

public class EnvVarsInConfigTasksTest {
public static final String DUMMY_LOCATION_VARNAME = "TOOLS_DUMMY_LOCATION";

private DumbSlave agentEnv = null;
private DumbSlave agentRegular = null;

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();

@Rule public JenkinsRule j = new JenkinsRule();

@Rule public TemporaryFolder tmp = new TemporaryFolder();

@Before
public void setUp() throws Exception {
JDK defaultJDK = j.jenkins.getJDK(null);
JDK varJDK = new JDK("varJDK", withVariable(defaultJDK.getHome()));
j.jenkins.getJDKs().add(varJDK);

// Ant with a variable in its path
AntInstallation ant = ToolInstallations.configureDefaultAnt(tmp);
AntInstallation antInstallation =
new AntInstallation(
"varAnt", withVariable(ant.getHome()), JenkinsRule.NO_PROPERTIES);
j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).setInstallations(antInstallation);

// create agents
EnvVars additionalEnv = new EnvVars(DUMMY_LOCATION_VARNAME, "");
agentEnv = j.createSlave(new LabelAtom("agentEnv"), additionalEnv);
agentRegular = j.createSlave(new LabelAtom("agentRegular"));
}

private String withVariable(String s) {
return s + "${" + DUMMY_LOCATION_VARNAME + "}";
}

@Test
public void testFreeStyleAntOnAgent() throws Exception {
Assume.assumeFalse(
"Cannot do testFreeStyleAntOnAgent without ANT_HOME",
j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations().length
== 0);

FreeStyleProject project = j.createFreeStyleProject();
project.setJDK(j.jenkins.getJDK("varJDK"));
project.setScm(new ExtractResourceSCM(getClass().getResource("/simple-projects.zip")));
String buildFile = "build.xml${" + DUMMY_LOCATION_VARNAME + "}";
// we need additional escapes because bash itself expanding
project.getBuildersList()
.add(
new Ant(
"-Dtest.property=cor${" + DUMMY_LOCATION_VARNAME + "}rect",
"varAnt",
"",
buildFile,
""));

// test the regular agent - variable not expanded
project.setAssignedLabel(agentRegular.getSelfLabel());
FreeStyleBuild build = project.scheduleBuild2(0).get();

j.assertBuildStatus(Result.FAILURE, build);

j.assertLogContains(Ant_ExecutableNotFound("varAnt"), build);

// test the agent with prepared environment
project.setAssignedLabel(agentEnv.getSelfLabel());
build = project.scheduleBuild2(0).get();

j.assertBuildStatusSuccess(build);

// Check variable was expanded
j.assertLogContains("Ant home: ", build);
j.assertLogContains("Test property: correct", build);
assertFalse(
JenkinsRule.getLog(build)
.matches("(?s)^.*Ant home: [^\\n\\r]*" + DUMMY_LOCATION_VARNAME + ".*$"));
assertFalse(
JenkinsRule.getLog(build)
.matches(
"(?s)^.*Test property: [^\\n\\r]*"
+ DUMMY_LOCATION_VARNAME
+ ".*$"));
}
}
103 changes: 103 additions & 0 deletions src/test/java/hudson/tools/ToolLocationNodePropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Tom Huybrechts
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.tools;

import hudson.EnvVars;
import hudson.Functions;
import hudson.model.Build;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import hudson.tasks.Ant;
import hudson.tasks.Ant.AntInstallation;
import hudson.tasks.BatchFile;
import hudson.tasks.Shell;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SingleFileSCM;
import org.jvnet.hudson.test.ToolInstallations;

/**
* This class tests that environment variables from node properties are applied, and that the
* priority is maintained: parameters > slave node properties > master node properties
*/
public class ToolLocationNodePropertyTest {

@Rule public JenkinsRule j = new JenkinsRule();
@Rule public TemporaryFolder tmp = new TemporaryFolder();

private DumbSlave slave;
private FreeStyleProject project;

private void configureDumpEnvBuilder() {
if (Functions.isWindows()) project.getBuildersList().add(new BatchFile("set"));
else project.getBuildersList().add(new Shell("export"));
}

@Test
public void ant() throws Exception {
Ant.AntInstallation ant = ToolInstallations.configureDefaultAnt(tmp);
String antPath = ant.getHome();
Jenkins.get()
.getDescriptorByType(Ant.DescriptorImpl.class)
.setInstallations(new AntInstallation("ant", "THIS IS WRONG"));

project.setScm(new SingleFileSCM("build.xml", "<project name='foo'/>"));
project.getBuildersList().add(new Ant("-version", "ant", null, null, null));
configureDumpEnvBuilder();

Build build = project.scheduleBuild2(0).get();
j.assertBuildStatus(Result.FAILURE, build);

ToolLocationNodeProperty property =
new ToolLocationNodeProperty(
new ToolLocationNodeProperty.ToolLocation(
j.jenkins.getDescriptorByType(AntInstallation.DescriptorImpl.class),
"ant",
antPath));
slave.getNodeProperties().add(property);

build = project.scheduleBuild2(0).get();
System.out.println(build.getLog());
j.assertBuildStatus(Result.SUCCESS, build);
}

@Before
public void setUp() throws Exception {
EnvVars env = new EnvVars();
// we don't want Maven, Ant, etc. to be discovered in the path for this test to work,
// but on Unix these tools rely on other basic Unix tools (like env) for its operation,
// so empty path breaks the test.
env.put("PATH", "/bin:/usr/bin");
env.put("M2_HOME", "empty");
slave = j.createSlave(new LabelAtom("slave"), env);
project = j.createFreeStyleProject();
project.setAssignedLabel(slave.getSelfLabel());
}
}
Binary file added src/test/resources/simple-projects.zip
Binary file not shown.