Skip to content

Commit

Permalink
Add a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed May 14, 2024
1 parent 494e3a2 commit 48c36eb
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import hudson.EnvVars;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
import hudson.model.Computer;
import hudson.model.FreeStyleBuild;
import hudson.model.Hudson;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.IOException;
Expand All @@ -22,7 +25,6 @@
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class BuildDataTest {
Expand Down Expand Up @@ -100,7 +102,28 @@ public void testBuildTagFallsBackToAlternativeEnvVars() throws Exception {
assertEquals("jenkins-jobName-buildNumber", buildData.getBuildTag(""));
}

@Test
public void testNodeHostnameIsUsedForFreestyleBuilds() throws Exception {
String computerHostname = "computer-hostname";

Computer computer = mock(Computer.class);
when(computer.getHostName()).thenReturn(computerHostname);

Node node = mock(Node.class);
when(node.toComputer()).thenReturn(computer);

FreeStyleBuild build = givenJobRun(FreeStyleBuild.class, "jobName", "jobParentName", mock(Hudson.class), Collections.emptyMap());
when(build.getBuiltOn()).thenReturn(node);

BuildData buildData = whenCreatingBuildData(build);
assertEquals(computerHostname, buildData.getHostname(""));
}

private Run<?, ?> givenJobRun(String jobName, String jobParentName, ItemGroup<?> jobParent, Map<String, String> environment) throws Exception {
return givenJobRun(Run.class, jobName, jobParentName, jobParent, environment);
}

private <T extends Run> T givenJobRun(Class<T> runClass, String jobName, String jobParentName, ItemGroup<?> jobParent, Map<String, String> environment) throws Exception {
when(jobParent.getFullName()).thenReturn(jobParentName);

Job job = mock(Job.class);
Expand All @@ -110,7 +133,7 @@ public void testBuildTagFallsBackToAlternativeEnvVars() throws Exception {
EnvVars envVars = new EnvVars();
envVars.putAll(environment);

Run run = mock(Run.class);
T run = mock(runClass);
when(run.getEnvironment(any())).thenReturn(envVars);
when(run.getParent()).thenReturn(job);
when(run.getCharset()).thenReturn(StandardCharsets.UTF_8);
Expand Down

0 comments on commit 48c36eb

Please sign in to comment.