From 7eefea63a2ea8ccec47fbd9aa5c4f297ab415535 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Thu, 25 Jan 2024 13:27:41 +0100 Subject: [PATCH] Fix integration tests --- .../plugins/datadog/DatadogUtilities.java | 59 ++-- .../datadog/listeners/DatadogSCMListener.java | 18 +- .../listeners/DatadogStepListener.java | 13 +- .../plugins/datadog/model/BuildData.java | 36 --- .../plugins/datadog/util/git/GitUtils.java | 6 +- .../listeners/DatadogBuildListenerIT.java | 85 +++--- .../listeners/DatadogGraphListenerTest.java | 252 +++++++----------- .../listeners/DatadogTraceAbstractTest.java | 8 +- .../testPipelineSuccessLocalCheckout.txt | 2 +- .../testPipelinesOverrideGitCommit.txt | 2 +- 10 files changed, 216 insertions(+), 265 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java index cc22d68a..c6d79226 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java @@ -39,13 +39,12 @@ of this software and associated documentation files (the "Software"), to deal import java.io.InputStream; import java.io.InputStreamReader; import java.net.Inet4Address; -import java.net.MalformedURLException; -import java.net.URL; import java.net.UnknownHostException; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -98,6 +97,7 @@ public class DatadogUtilities { /** * @return - The descriptor for the Datadog plugin. In this case the global configuration. */ + @Nullable public static DatadogGlobalConfiguration getDatadogGlobalDescriptor() { try { return ExtensionList.lookupSingleton(DatadogGlobalConfiguration.class); @@ -586,6 +586,14 @@ public static String getHostname(EnvVars envVars) { return null; } + private static final Pattern VALID_HOSTNAME_RFC_1123_PATTERN = Pattern.compile("^(([a-zA-Z0-9]|" + + "[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*" + + "([A-Za-z0-9]|" + + "[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"); + + private static final Collection LOCAL_HOSTS = Arrays.asList("localhost", "localhost.localdomain", + "localhost6.localdomain6", "ip6-localhost"); + /** * Validator function to ensure that the hostname is valid. Also, fails on * empty String. @@ -598,20 +606,17 @@ public static Boolean isValidHostname(String hostname) { return false; } - String[] localHosts = {"localhost", "localhost.localdomain", - "localhost6.localdomain6", "ip6-localhost"}; - String VALID_HOSTNAME_RFC_1123_PATTERN = "^(([a-zA-Z0-9]|" - + "[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*" - + "([A-Za-z0-9]|" - + "[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"; - String host = hostname.toLowerCase(); - // Check if hostname is local - if (Arrays.asList(localHosts).contains(host)) { + if (LOCAL_HOSTS.contains(hostname.toLowerCase())) { logger.fine(String.format("Hostname: %s is local", hostname)); return false; } + if (isPrivateIPv4Address(hostname)) { + logger.fine(String.format("Hostname: %s is a private IPv4 address", hostname)); + return false; + } + // Ensure proper length if (hostname.length() > MAX_HOSTNAME_LEN) { logger.fine(String.format("Hostname: %s is too long (max length is %s characters)", @@ -619,14 +624,38 @@ public static Boolean isValidHostname(String hostname) { return false; } - // Check compliance with RFC 1123 - Pattern r = Pattern.compile(VALID_HOSTNAME_RFC_1123_PATTERN); - Matcher m = r.matcher(hostname); - // Final check: Hostname matches RFC1123? + Matcher m = VALID_HOSTNAME_RFC_1123_PATTERN.matcher(hostname); return m.find(); } + private static boolean isPrivateIPv4Address(String ipAddress) { + if (ipAddress == null || ipAddress.isEmpty()) { + return false; + } + + String[] parts = ipAddress.split("\\."); + if (parts.length != 4) { + return false; + } + + try { + int firstOctet = Integer.parseInt(parts[0]); + int secondOctet = Integer.parseInt(parts[1]); + + if (firstOctet == 10) { + return true; + } else if (firstOctet == 172 && (secondOctet >= 16 && secondOctet <= 31)) { + return true; + } else if (firstOctet == 192 && secondOctet == 168) { + return true; + } + return false; + } catch (NumberFormatException e) { + return false; + } + } + public static Map> getComputerTags(Computer computer) { Set labels = null; try { diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java index 40b1dee1..04d00ae9 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java @@ -41,6 +41,7 @@ of this software and associated documentation files (the "Software"), to deal import java.util.Map; import java.util.Set; import java.util.logging.Logger; +import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.datadog.jenkins.plugins.datadog.DatadogClient; import org.datadog.jenkins.plugins.datadog.DatadogEvent; @@ -89,13 +90,16 @@ public void onCheckout(Run build, SCM scm, FilePath workspace, TaskListene return; } + logger.fine("Start DatadogSCMListener#onCheckout"); + if (isGit(scm)) { EnvVars environment = build.getEnvironment(listener); GitClient gitClient = GitUtils.newGitClient(listener, environment, workspace); - if (gitClient != null) { - populateCommitInfo(build, gitClient); - populateRepositoryInfo(build, gitClient, environment); - } + populateCommitInfo(build, gitClient); + populateRepositoryInfo(build, gitClient, environment); + } else { + logger.fine("Will not populate git commit and repository info for non-git SCM: " + + (scm != null ? scm.getType() : null)); } DatadogJobProperty prop = DatadogUtilities.getDatadogJobProperties(build); @@ -103,8 +107,6 @@ public void onCheckout(Run build, SCM scm, FilePath workspace, TaskListene return; } - logger.fine("Start DatadogSCMListener#onCheckout"); - // Get Datadog Client Instance DatadogClient client = ClientFactory.getClient(); if (client == null) { @@ -146,7 +148,7 @@ private boolean isGit(SCM scm) { return scmType != null && scmType.toLowerCase().contains("git"); } - private void populateCommitInfo(final Run run, final GitClient gitClient) { + private void populateCommitInfo(final Run run, @Nullable final GitClient gitClient) { long start = System.currentTimeMillis(); try { GitCommitAction commitAction = run.getAction(GitCommitAction.class); @@ -200,7 +202,7 @@ private void populateCommitInfo(final Run run, final GitClient gitClient) } } - private void populateRepositoryInfo(final Run run, final GitClient gitClient, final EnvVars environment) { + private void populateRepositoryInfo(final Run run, @Nullable final GitClient gitClient, final EnvVars environment) { long start = System.currentTimeMillis(); try { GitRepositoryAction repoAction = run.getAction(GitRepositoryAction.class); diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogStepListener.java b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogStepListener.java index be217cc2..73fa89a2 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogStepListener.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogStepListener.java @@ -138,11 +138,11 @@ private static String getNodeHostname(final StepContext stepContext) { EnvVars envVars = stepContext.get(EnvVars.class); if (envVars != null) { String ddHostname = envVars.get(DatadogGlobalConfiguration.DD_CI_HOSTNAME); - if (ddHostname != null) { + if (DatadogUtilities.isValidHostname(ddHostname)) { return ddHostname; } String hostname = envVars.get("HOSTNAME"); - if (hostname != null) { + if (DatadogUtilities.isValidHostname(hostname)) { return hostname; } } @@ -153,7 +153,14 @@ private static String getNodeHostname(final StepContext stepContext) { try { Computer computer = stepContext.get(Computer.class); if(computer != null) { - return computer.getHostName(); + String computerHostName = computer.getHostName(); + if (DatadogUtilities.isValidHostname(computerHostName)) { + return computerHostName; + } + String computerNodeName = DatadogUtilities.getNodeName(computer); + if (DatadogUtilities.isMainNode(computerNodeName)) { + return DatadogUtilities.getHostname(null); + } } } catch (Exception e){ logger.fine("Unable to extract hostname from StepContext."); diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/model/BuildData.java b/src/main/java/org/datadog/jenkins/plugins/datadog/model/BuildData.java index be705e26..37e8b903 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/model/BuildData.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/model/BuildData.java @@ -641,10 +641,6 @@ public String getExecutorNumber(String value) { return defaultIfNull(executorNumber, value); } - public String getJavaHome(String value) { - return defaultIfNull(javaHome, value); - } - public String getWorkspace(String value) { return defaultIfNull(workspace, value); } @@ -661,66 +657,34 @@ public String getGitMessage(String value) { return defaultIfNull(gitMessage, value); } - public void setGitMessage(String gitMessage) { - this.gitMessage = gitMessage; - } - public String getGitAuthorName(final String value) { return defaultIfNull(gitAuthorName, value); } - public void setGitAuthorName(String gitAuthorName) { - this.gitAuthorName = gitAuthorName; - } - public String getGitAuthorEmail(final String value) { return defaultIfNull(gitAuthorEmail, value); } - public void setGitAuthorEmail(String gitAuthorEmail) { - this.gitAuthorEmail = gitAuthorEmail; - } - public String getGitCommitterName(final String value) { return defaultIfNull(gitCommitterName, value); } - public void setGitCommitterName(String gitCommitterName) { - this.gitCommitterName = gitCommitterName; - } - public String getGitCommitterEmail(final String value) { return defaultIfNull(gitCommitterEmail, value); } - public void setGitCommitterEmail(String gitCommitterEmail) { - this.gitCommitterEmail = gitCommitterEmail; - } - public String getGitAuthorDate(final String value) { return defaultIfNull(gitAuthorDate, value); } - public void setGitAuthorDate(String gitAuthorDate) { - this.gitAuthorDate = gitAuthorDate; - } - public String getGitCommitterDate(final String value) { return defaultIfNull(gitCommitterDate, value); } - public void setGitCommitterDate(String gitCommitterDate) { - this.gitCommitterDate = gitCommitterDate; - } - public String getGitDefaultBranch(String value) { return defaultIfNull(gitDefaultBranch, value); } - public void setGitDefaultBranch(String gitDefaultBranch) { - this.gitDefaultBranch = gitDefaultBranch; - } - public String getGitTag(String value) { return defaultIfNull(gitTag, value); } diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/GitUtils.java b/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/GitUtils.java index 7ce19c60..5a1c8cab 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/GitUtils.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/GitUtils.java @@ -19,6 +19,7 @@ import java.util.Map; import java.util.logging.Logger; import java.util.regex.Pattern; +import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.datadog.jenkins.plugins.datadog.audit.DatadogAudit; import org.datadog.jenkins.plugins.datadog.model.GitCommitAction; @@ -44,7 +45,7 @@ private GitUtils() { * @param gitClient the Git client used. * @return revCommit */ - public static RevCommit searchRevCommit(final GitClient gitClient, final String gitCommit) { + public static RevCommit searchRevCommit(@Nullable final GitClient gitClient, final String gitCommit) { try { if (gitClient == null) { LOGGER.fine("Unable to search RevCommit. GitClient is null"); @@ -64,7 +65,7 @@ public static RevCommit searchRevCommit(final GitClient gitClient, final String * @param gitClient The Git client to use to obtain the repository information * @return repositoryInfo */ - public static RepositoryInfo searchRepositoryInfo(final GitClient gitClient) { + public static RepositoryInfo searchRepositoryInfo(@Nullable final GitClient gitClient) { try { if (gitClient == null) { LOGGER.fine("Unable to search RevCommit. GitClient is null"); @@ -86,6 +87,7 @@ public static RepositoryInfo searchRepositoryInfo(final GitClient gitClient) { * @param workspace the workspace to use to build the Git client * @return gitClient */ + @Nullable public static GitClient newGitClient(final TaskListener listener, final EnvVars envVars, final FilePath workspace) { long start = System.currentTimeMillis(); try { diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerIT.java b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerIT.java index 33b476f3..d98433c0 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerIT.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerIT.java @@ -27,13 +27,11 @@ import hudson.model.Label; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitSCM; -import hudson.plugins.git.browser.GitRepositoryBrowser; import hudson.plugins.git.extensions.impl.LocalBranch; import hudson.slaves.DumbSlave; import hudson.slaves.EnvironmentVariablesNodeProperty; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.util.Collections; import java.util.List; import java.util.Map; @@ -48,10 +46,11 @@ import org.datadog.jenkins.plugins.datadog.model.BuildPipelineNode; import org.datadog.jenkins.plugins.datadog.traces.CITags; import org.datadog.jenkins.plugins.datadog.traces.message.TraceSpan; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.jvnet.hudson.test.ExtractResourceSCM; import org.jvnet.hudson.test.JenkinsRule; public class DatadogBuildListenerIT extends DatadogTraceAbstractTest { @@ -59,12 +58,31 @@ public class DatadogBuildListenerIT extends DatadogTraceAbstractTest { private static final String SAMPLE_SERVICE_NAME = "sampleServiceName"; @ClassRule - public static JenkinsRule jenkinsRule = new JenkinsRule(); + public static final JenkinsRule jenkinsRule = new JenkinsRule(); + + private static FilePath localGitRepoPath; + private DatadogClientStub clientStub; - static { + @BeforeClass + public static void setUp() { // to allow checkout from local git repositories - needed for some tests GitSCM.ALLOW_LOCAL_CHECKOUT = true; + + localGitRepoPath = jenkinsRule.jenkins.getRootPath().child("tmp").child("git-repo"); + try (InputStream gitZip = DatadogBuildListenerIT.class.getClassLoader().getResourceAsStream("org/datadog/jenkins/plugins/datadog/listeners/git/gitFolder.zip")) { + localGitRepoPath.deleteRecursive(); + localGitRepoPath.mkdirs(); + localGitRepoPath.unzipFrom(gitZip); + + } catch (Exception e) { + throw new RuntimeException("Could not create local git repo at " + localGitRepoPath.getRemote(), e); + } + } + + @AfterClass + public static void tearDown() throws IOException, InterruptedException { + localGitRepoPath.deleteRecursive(); } @Before @@ -122,14 +140,12 @@ public void testTraces() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL", "file:///tmp/git-repo/"); + env.put("GIT_URL", "file://" + localGitRepoPath.getRemote()); jenkins.getGlobalNodeProperties().add(prop); - createLocallyAvailableGitRepo(jenkins); - final FreeStyleProject project = jenkinsRule.createFreeStyleProject("buildIntegrationSuccess"); - GitSCM git = new GitSCM(GitSCM.createRepoList("file:///tmp/git-repo/", null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); + GitSCM git = new GitSCM(GitSCM.createRepoList("file://" + localGitRepoPath.getRemote(), null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); project.setScm(git); final FilePath ws = jenkins.getWorkspaceFor(project); @@ -144,7 +160,7 @@ public void testTraces() throws Exception { assertEquals(1, spans.size()); final TraceSpan buildSpan = spans.get(0); - assertGitVariablesOnSpan(buildSpan, "master"); + assertGitVariablesOnSpan(buildSpan, "master", localGitRepoPath.getRemote()); final Map meta = buildSpan.getMeta(); final Map metrics = buildSpan.getMetrics(); assertEquals(BuildPipelineNode.NodeType.PIPELINE.getBuildLevel(), meta.get(CITags._DD_CI_BUILD_LEVEL)); @@ -174,15 +190,6 @@ public void testTraces() throws Exception { assertCleanupActions(run); } - private void createLocallyAvailableGitRepo(Jenkins jenkins) throws IOException, InterruptedException { - try (InputStream gitZip = getClass().getClassLoader().getResourceAsStream("org/datadog/jenkins/plugins/datadog/listeners/git/gitFolder.zip")) { - FilePath gitRepoPath = jenkins.createPath("/tmp/git-repo"); - gitRepoPath.deleteRecursive(); - gitRepoPath.mkdirs(); - gitRepoPath.unzipFrom(gitZip); - } - } - @Test public void testGitDefaultBranch() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; @@ -190,16 +197,14 @@ public void testGitDefaultBranch() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL", "file:///tmp/git-repo/"); + env.put("GIT_URL", "file://" + localGitRepoPath.getRemote()); final String defaultBranch = "refs/heads/hardcoded-master"; env.put("DD_GIT_DEFAULT_BRANCH", defaultBranch); jenkins.getGlobalNodeProperties().add(prop); - createLocallyAvailableGitRepo(jenkins); - final FreeStyleProject project = jenkinsRule.createFreeStyleProject("buildIntegrationSuccessDefaultBranch"); - GitSCM git = new GitSCM(GitSCM.createRepoList("file:///tmp/git-repo/", null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); + GitSCM git = new GitSCM(GitSCM.createRepoList("file://" + localGitRepoPath.getRemote(), null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); project.setScm(git); project.scheduleBuild2(0).get(); @@ -209,7 +214,7 @@ public void testGitDefaultBranch() throws Exception { assertEquals(1, spans.size()); final TraceSpan buildSpan = spans.get(0); - assertGitVariablesOnSpan(buildSpan, "hardcoded-master"); + assertGitVariablesOnSpan(buildSpan, "hardcoded-master", localGitRepoPath.getRemote()); } @Test @@ -221,18 +226,16 @@ public void testUserSuppliedGitWithoutCommitInfo() throws Exception { env.put(GIT_BRANCH, "not-valid-branch"); env.put(GIT_COMMIT, "not-valid-commit"); - env.put(DD_GIT_REPOSITORY_URL, "file:///tmp/git-repo/"); + env.put(DD_GIT_REPOSITORY_URL, "file://" + localGitRepoPath.getRemote()); env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); final String defaultBranch = "refs/heads/hardcoded-master"; env.put(DD_GIT_DEFAULT_BRANCH, defaultBranch); jenkins.getGlobalNodeProperties().add(prop); - createLocallyAvailableGitRepo(jenkins); - final FreeStyleProject project = jenkinsRule.createFreeStyleProject("buildIntegrationSuccessUserSuppliedGitWithoutCommitInfo"); - GitSCM git = new GitSCM(GitSCM.createRepoList("file:///tmp/git-repo/", null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); + GitSCM git = new GitSCM(GitSCM.createRepoList("file://" + localGitRepoPath.getRemote(), null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); project.setScm(git); project.scheduleBuild2(0).get(); @@ -242,7 +245,7 @@ public void testUserSuppliedGitWithoutCommitInfo() throws Exception { assertEquals(1, spans.size()); final TraceSpan buildSpan = spans.get(0); - assertGitVariablesOnSpan(buildSpan, "hardcoded-master"); + assertGitVariablesOnSpan(buildSpan, "hardcoded-master", localGitRepoPath.getRemote()); } @Test @@ -253,7 +256,7 @@ public void testUserSuppliedGitWithCommitInfo() throws Exception { env.put(GIT_REPOSITORY_URL, "not-valid-repo"); env.put(GIT_BRANCH, "not-valid-branch"); env.put(GIT_COMMIT, "not-valid-commit"); - env.put(DD_GIT_REPOSITORY_URL, "file:///tmp/git-repo/"); + env.put(DD_GIT_REPOSITORY_URL, "file://" + localGitRepoPath.getRemote()); env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); env.put(DD_GIT_COMMIT_MESSAGE, "hardcoded-message"); @@ -267,11 +270,9 @@ public void testUserSuppliedGitWithCommitInfo() throws Exception { env.put(DD_GIT_DEFAULT_BRANCH, defaultBranch); jenkins.getGlobalNodeProperties().add(prop); - createLocallyAvailableGitRepo(jenkins); - final FreeStyleProject project = jenkinsRule.createFreeStyleProject("buildIntegrationSuccessUserSuppliedGitWithCommitInfo"); - GitSCM git = new GitSCM(GitSCM.createRepoList("file:///tmp/git-repo/", null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); + GitSCM git = new GitSCM(GitSCM.createRepoList("file://" + localGitRepoPath.getRemote(), null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); project.setScm(git); project.scheduleBuild2(0).get(); @@ -292,7 +293,7 @@ public void testUserSuppliedGitWithCommitInfo() throws Exception { assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get(CITags.GIT_COMMIT__SHA)); assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get(CITags.GIT_COMMIT_SHA)); assertEquals("master", meta.get(CITags.GIT_BRANCH)); - assertEquals("file:///tmp/git-repo/", meta.get(CITags.GIT_REPOSITORY_URL)); + assertEquals("file://" + localGitRepoPath.getRemote(), meta.get(CITags.GIT_REPOSITORY_URL)); assertEquals("hardcoded-master", meta.get(CITags.GIT_DEFAULT_BRANCH)); } @@ -306,7 +307,7 @@ public void testUserSuppliedGitWithCommitInfoWebhook() throws Exception { env.put(GIT_REPOSITORY_URL, "not-valid-repo"); env.put(GIT_BRANCH, "not-valid-branch"); env.put(GIT_COMMIT, "not-valid-commit"); - env.put(DD_GIT_REPOSITORY_URL, "file:///tmp/git-repo/"); + env.put(DD_GIT_REPOSITORY_URL, "file://" + localGitRepoPath.getRemote()); env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); env.put(DD_GIT_COMMIT_MESSAGE, "hardcoded-message"); @@ -320,11 +321,9 @@ public void testUserSuppliedGitWithCommitInfoWebhook() throws Exception { env.put(DD_GIT_DEFAULT_BRANCH, defaultBranch); jenkins.getGlobalNodeProperties().add(prop); - createLocallyAvailableGitRepo(jenkins); - final FreeStyleProject project = jenkinsRule.createFreeStyleProject("buildIntegrationSuccessUserSuppliedGitWithCommitInfoWebhook"); - GitSCM git = new GitSCM(GitSCM.createRepoList("file:///tmp/git-repo/", null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); + GitSCM git = new GitSCM(GitSCM.createRepoList("file://" + localGitRepoPath.getRemote(), null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); project.setScm(git); project.scheduleBuild2(0).get(); @@ -344,7 +343,7 @@ public void testUserSuppliedGitWithCommitInfoWebhook() throws Exception { assertEquals("hardcoded-committer-date", meta.getString("commit_time")); assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.getString("sha")); assertEquals("master", meta.getString("branch")); - assertEquals("file:///tmp/git-repo/", meta.getString("repository_url")); + assertEquals("file://" + localGitRepoPath.getRemote(), meta.getString("repository_url")); assertEquals("hardcoded-master", meta.getString("default_branch")); } @@ -411,14 +410,12 @@ public void testGitAlternativeRepoUrlWebhook() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL_1", "file:///tmp/git-repo/"); + env.put("GIT_URL_1", "file://" + localGitRepoPath.getRemote()); jenkins.getGlobalNodeProperties().add(prop); - createLocallyAvailableGitRepo(jenkins); - final FreeStyleProject project = jenkinsRule.createFreeStyleProject("buildIntegrationSuccessAltRepoUrlWebhook"); - GitSCM git = new GitSCM(GitSCM.createRepoList("file:///tmp/git-repo/", null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); + GitSCM git = new GitSCM(GitSCM.createRepoList("file://" + localGitRepoPath.getRemote(), null), Collections.singletonList(new BranchSpec("*/master")), null, null, Collections.singletonList(new LocalBranch("master"))); project.setScm(git); final FilePath ws = jenkins.getWorkspaceFor(project); @@ -432,7 +429,7 @@ public void testGitAlternativeRepoUrlWebhook() throws Exception { assertEquals(1, webhooks.size()); final JSONObject webhook = webhooks.get(0); - assertGitVariablesOnWebhook(webhook, "master"); + assertGitVariablesOnWebhook(webhook, "master", localGitRepoPath.getRemote()); } @Test diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListenerTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListenerTest.java index 268f9801..63a1bea6 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListenerTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListenerTest.java @@ -33,14 +33,19 @@ import hudson.triggers.SCMTrigger.SCMTriggerCause; import hudson.triggers.TimerTrigger.TimerTriggerCause; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import jenkins.model.Jenkins; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; @@ -62,7 +67,9 @@ import org.jenkinsci.plugins.workflow.graph.BlockStartNode; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; @@ -74,13 +81,34 @@ public class DatadogGraphListenerTest extends DatadogTraceAbstractTest { @ClassRule public static JenkinsRule jenkinsRule; + private static FilePath localGitRepoPath; + static { // to allow checkout from local git repositories - needed for some tests System.setProperty("hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT", "true"); + jenkinsRule = new JenkinsRule(); jenkinsRule.timeout = 600; // default value of 180 is too small for all the test cases in this class } + @BeforeClass + public static void setUp() { + localGitRepoPath = jenkinsRule.jenkins.getRootPath().child("tmp").child("git-repo"); + try (InputStream gitZip = DatadogBuildListenerIT.class.getClassLoader().getResourceAsStream("org/datadog/jenkins/plugins/datadog/listeners/git/gitFolder.zip")) { + localGitRepoPath.deleteRecursive(); + localGitRepoPath.mkdirs(); + localGitRepoPath.unzipFrom(gitZip); + + } catch (Exception e) { + throw new RuntimeException("Could not create local git repo at " + localGitRepoPath.getRemote(), e); + } + } + + @AfterClass + public static void tearDown() throws IOException, InterruptedException { + localGitRepoPath.deleteRecursive(); + } + private DatadogGraphListener listener; private DatadogClientStub clientStub; @@ -154,10 +182,7 @@ public void testIntegration() throws Exception { windowsEnvVars.put("HOSTNAME", windowsHostname); jenkinsRule.createOnlineSlave(new LabelAtom("windows"), windowsEnvVars); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineDefinition.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineDefinition.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); WorkflowRun run = job.scheduleBuild2(0).get(); BufferedReader br = new BufferedReader(run.getLogReader()); @@ -217,13 +242,10 @@ public void testIntegrationGitInfo() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL", "file:///tmp/git-repo/"); + env.put("GIT_URL", "file://" + localGitRepoPath.getRemote()); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationSingleCommit"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccessLocalCheckout.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccessLocalCheckout.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -239,19 +261,16 @@ public void testIntegrationGitInfo() throws Exception { final List spans = clientStub.getSpans(); assertEquals(3, spans.size()); final TraceSpan buildSpan = spans.get(0); - assertGitVariablesOnSpan(buildSpan, "master"); + assertGitVariablesOnSpan(buildSpan, "master", localGitRepoPath.getRemote()); } @Test public void testIntegrationNonCIVisibilityEnvVars() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; WorkflowJob job = jenkins.createProject(WorkflowJob.class, "testPipelineGitBranchEnv"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineGitBranchEnv.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineGitBranchEnv.txt"); - String[] expectedTags = new String[]{ + String[] expectedTags = new String[]{ "jenkins_url:" + DatadogUtilities.getJenkinsUrl(), "user_id:anonymous", "job:testPipelineGitBranchEnv", @@ -274,13 +293,10 @@ public void testIntegrationGitInfoWebhooks() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL", "file:///tmp/git-repo/"); + env.put("GIT_URL", "file://" + localGitRepoPath.getRemote()); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationSingleCommitWebhooks"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccessLocalCheckout.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccessLocalCheckout.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -295,7 +311,7 @@ public void testIntegrationGitInfoWebhooks() throws Exception { clientStub.waitForWebhooks(3); final List webhook = clientStub.getWebhooks(); assertEquals(3, webhook.size()); - assertGitVariablesOnWebhook(webhook.get(0), "master"); + assertGitVariablesOnWebhook(webhook.get(0), "master", localGitRepoPath.getRemote()); } @Test @@ -305,15 +321,12 @@ public void testIntegrationGitInfoDefaultBranchEnvVar() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL", "file:///tmp/git-repo/"); + env.put("GIT_URL", "file://" + localGitRepoPath.getRemote()); final String defaultBranch = "refs/heads/hardcoded-master"; env.put("DD_GIT_DEFAULT_BRANCH", defaultBranch); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationSingleCommitDefaultBranchEnvVar"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccessLocalCheckout.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccessLocalCheckout.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -329,7 +342,7 @@ public void testIntegrationGitInfoDefaultBranchEnvVar() throws Exception { final List spans = clientStub.getSpans(); assertEquals(3, spans.size()); final TraceSpan buildSpan = spans.get(0); - assertGitVariablesOnSpan(buildSpan, "hardcoded-master"); + assertGitVariablesOnSpan(buildSpan, "hardcoded-master", localGitRepoPath.getRemote()); } @Test @@ -339,13 +352,10 @@ public void testIntegrationGitInfoOverrideCommit() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL", "file:///tmp/git-repo/"); + env.put("GIT_URL", "file://" + localGitRepoPath.getRemote()); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationOverrideCommit"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelinesOverrideGitCommit.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelinesOverrideGitCommit.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -365,6 +375,27 @@ public void testIntegrationGitInfoOverrideCommit() throws Exception { } } + private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$([A-Z_]+)"); + + private String getPipelineDefinition(String file) throws IOException { + Map replacements = Collections.singletonMap("LOCAL_REPO_URL", "file://" + localGitRepoPath.getRemote()); + + String pipelineDefinition; + try (InputStream is = DatadogGraphListenerTest.class.getResourceAsStream(file)) { + StringBuffer pipelineBuilder = new StringBuffer(); + String pipelineTemplate = IOUtils.toString(is, StandardCharsets.UTF_8); + Matcher m = PLACEHOLDER_PATTERN.matcher(pipelineTemplate); + while (m.find()) { + String placeholder = m.group(1); + m.appendReplacement(pipelineBuilder, replacements.get(placeholder)); + } + m.appendTail(pipelineBuilder); + pipelineDefinition = pipelineBuilder.toString(); + } + + return pipelineDefinition; + } + @Test public void testIntegrationGitAlternativeRepoUrl() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; @@ -372,13 +403,10 @@ public void testIntegrationGitAlternativeRepoUrl() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL_1", "file:///tmp/git-repo/"); + env.put("GIT_URL_1", "file://" + localGitRepoPath.getRemote()); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationAltRepoUrl"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelinesOverrideGitCommit.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelinesOverrideGitCommit.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -394,7 +422,7 @@ public void testIntegrationGitAlternativeRepoUrl() throws Exception { final List spans = clientStub.getSpans(); assertEquals(5, spans.size()); for(TraceSpan span : spans) { - assertEquals("file:///tmp/git-repo/", span.getMeta().get(CITags.GIT_REPOSITORY_URL)); + assertEquals("file://" + localGitRepoPath.getRemote(), span.getMeta().get(CITags.GIT_REPOSITORY_URL)); } } @@ -407,13 +435,10 @@ public void testIntegrationGitAlternativeRepoUrlWebhooks() throws Exception { EnvVars env = prop.getEnvVars(); env.put("GIT_BRANCH", "master"); env.put("GIT_COMMIT", "401d997a6eede777602669ccaec059755c98161f"); - env.put("GIT_URL_1", "file:///tmp/git-repo/"); + env.put("GIT_URL_1", "file://" + localGitRepoPath.getRemote()); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationAltRepoUrlWebhooks"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelinesOverrideGitCommit.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelinesOverrideGitCommit.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -429,7 +454,7 @@ public void testIntegrationGitAlternativeRepoUrlWebhooks() throws Exception { final List webhooks = clientStub.getWebhooks(); assertEquals(5, webhooks.size()); for(JSONObject webhook : webhooks) { - assertEquals("file:///tmp/git-repo/", webhook.getJSONObject("git").get("repository_url")); + assertEquals("file://" + localGitRepoPath.getRemote(), webhook.getJSONObject("git").get("repository_url")); } } @@ -438,16 +463,13 @@ public void testUserSuppliedGitWithoutCommitInfo() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; final EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); EnvVars env = prop.getEnvVars(); - env.put(DD_GIT_REPOSITORY_URL, "file:///tmp/git-repo/"); + env.put(DD_GIT_REPOSITORY_URL, "file://" + localGitRepoPath.getRemote()); env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); env.put(DD_GIT_TAG, "0.1.0"); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationUserSuppliedGitWithoutCommitInfo"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccessLocalCheckout.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccessLocalCheckout.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -463,7 +485,7 @@ public void testUserSuppliedGitWithoutCommitInfo() throws Exception { final List spans = clientStub.getSpans(); assertEquals(3, spans.size()); final TraceSpan buildSpan = spans.get(0); - assertGitVariablesOnSpan(buildSpan, "master"); + assertGitVariablesOnSpan(buildSpan, "master", localGitRepoPath.getRemote()); final Map meta = buildSpan.getMeta(); assertEquals("0.1.0", meta.get(CITags.GIT_TAG)); } @@ -484,16 +506,13 @@ public void testUserSuppliedGitWithoutCommitInfoWebhooks() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; final EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); EnvVars env = prop.getEnvVars(); - env.put(DD_GIT_REPOSITORY_URL, "file:///tmp/git-repo/"); + env.put(DD_GIT_REPOSITORY_URL, "file://" + localGitRepoPath.getRemote()); env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); env.put(DD_GIT_TAG, "0.1.0"); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationUserSuppliedGitWithoutCommitInfoWebhooks"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccessLocalCheckout.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccessLocalCheckout.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -509,7 +528,7 @@ public void testUserSuppliedGitWithoutCommitInfoWebhooks() throws Exception { final List webhooks = clientStub.getWebhooks(); assertEquals(3, webhooks.size()); final JSONObject webhook = webhooks.get(0); - assertGitVariablesOnWebhook(webhook, "master"); + assertGitVariablesOnWebhook(webhook, "master", localGitRepoPath.getRemote()); assertEquals("0.1.0", webhook.getJSONObject("git").get("tag")); } @@ -518,7 +537,7 @@ public void testUserSuppliedGitWithCommitInfo() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; final EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); EnvVars env = prop.getEnvVars(); - env.put(DD_GIT_REPOSITORY_URL, "file:///tmp/git-repo/"); + env.put(DD_GIT_REPOSITORY_URL, "file://" + localGitRepoPath.getRemote()); env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); env.put(DD_GIT_COMMIT_MESSAGE, "hardcoded-message"); @@ -531,10 +550,7 @@ public void testUserSuppliedGitWithCommitInfo() throws Exception { final String defaultBranch = "refs/heads/hardcoded-master"; env.put(DD_GIT_DEFAULT_BRANCH, defaultBranch); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationUserSuppliedGitWithCommitInfo"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccessLocalCheckout.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccessLocalCheckout.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final FilePath ws = jenkins.getWorkspaceFor(job); @@ -561,7 +577,7 @@ public void testUserSuppliedGitWithCommitInfo() throws Exception { assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get(CITags.GIT_COMMIT__SHA)); assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get(CITags.GIT_COMMIT_SHA)); assertEquals("master", meta.get(CITags.GIT_BRANCH)); - assertEquals("file:///tmp/git-repo/", meta.get(CITags.GIT_REPOSITORY_URL)); + assertEquals("file://" + localGitRepoPath.getRemote(), meta.get(CITags.GIT_REPOSITORY_URL)); assertEquals("hardcoded-master", meta.get(CITags.GIT_DEFAULT_BRANCH)); } @@ -574,10 +590,7 @@ public void testRawRepositoryUrl() throws Exception { env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationRawRepositoryUrl"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); jenkins.getGlobalNodeProperties().add(prop); @@ -603,10 +616,7 @@ public void testFilterSensitiveInfoRepoUrl() throws Exception { env.put(DD_GIT_BRANCH, "master"); env.put(DD_GIT_COMMIT_SHA, "401d997a6eede777602669ccaec059755c98161f"); WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationFilterSensitiveInfoRepoUrl"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); jenkins.getGlobalNodeProperties().add(prop); @@ -626,10 +636,7 @@ public void testFilterSensitiveInfoRepoUrl() throws Exception { @Test public void testStageNamePropagation() throws Exception{ WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationStages"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineStages.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineStages.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); new Thread(() -> { try { @@ -664,10 +671,7 @@ public void testStageNamePropagationWebhook() throws Exception{ clientStub.configureForWebhooks(); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationStagesWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineStagesWebhook.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineStagesWebhook.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); new Thread(() -> { try { @@ -707,10 +711,7 @@ public void testStageNamePropagationWebhook() throws Exception{ @Test public void testIntegrationPipelineQueueTimeOnStages() throws Exception { WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationQueueTimeOnStages"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineQueueOnStages.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineQueueOnStages.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); // schedule build and wait for it to get queued new Thread(() -> { @@ -815,10 +816,7 @@ public void testIntegrationPipelineQueueTimeOnPipeline() throws Exception { env.put("NODE_NAME", "testPipeline"); jenkinsRule.jenkins.getGlobalNodeProperties().add(envProps); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationQueueTimeOnPipeline"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineQueueOnPipeline.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineQueueOnPipeline.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); // schedule build and wait for it to get queued @@ -862,10 +860,7 @@ public void testIntegrationPipelineQueueTimeOnPipeline() throws Exception { public void testIntegrationNoFailureTag() throws Exception { jenkinsRule.createOnlineSlave(); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationSuccess"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); final WorkflowRun run = job.scheduleBuild2(0).get(); String hostname = DatadogUtilities.getHostname(null); @@ -968,10 +963,7 @@ public void testIntegrationNoFailureTag() throws Exception { @Test public void testIntegrationPipelineSkippedLogic() throws Exception { WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-SkippedLogic"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSkippedLogic.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSkippedLogic.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -989,10 +981,7 @@ public void testIntegrationPipelineSkippedLogicWebhook() throws Exception { clientStub.configureForWebhooks(); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-SkippedLogicWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSkippedLogic.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSkippedLogic.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -1013,10 +1002,7 @@ public void testIntegrationTracesDisabled() throws Exception{ jenkinsRule.createOnlineSlave(); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationSuccess-notraces"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); String hostname = DatadogUtilities.getHostname(null); @@ -1076,10 +1062,7 @@ public void getTimeTest() { @Test public void testStagesNodeNames_complexPipelineStages01() throws Exception { WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "complexPipelineStages01"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineComplexStages01.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineComplexStages01.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); // schedule build and wait for it to get queued new Thread(() -> { @@ -1179,10 +1162,7 @@ public void testGlobalTagsPropagationsTraces() throws Exception { jenkinsRule.createOnlineSlave(new LabelAtom("testGlobalTags")); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-GlobalTagsPropagation_job"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineGlobalTags.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineGlobalTags.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -1215,10 +1195,7 @@ public void testGlobalTagsPropagationsTraces() throws Exception { @Test public void testErrorPropagationOnStages() throws Exception { WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-errorPropagationStages"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream(getFailingPipelineDefinitionName()), - "UTF-8" - ); + String definition = getPipelineDefinition(getFailingPipelineDefinitionName()); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -1245,10 +1222,7 @@ public void testErrorPropagationOnStagesWebhook() throws Exception { clientStub.configureForWebhooks(); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-errorPropagationStagesWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream(getFailingPipelineDefinitionName()), - "UTF-8" - ); + String definition = getPipelineDefinition(getFailingPipelineDefinitionName()); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -1281,10 +1255,7 @@ private boolean isRunningOnWindows() { @Test public void testUnstablePropagationOnStages() throws Exception { WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-unstablePropagationStages"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineUnstableOnStages.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineUnstableOnStages.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -1311,10 +1282,7 @@ public void testUnstablePropagationOnStagesWebhook() throws Exception { clientStub.configureForWebhooks(); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegration-unstablePropagationStagesWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineUnstableOnStages.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineUnstableOnStages.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); @@ -1342,10 +1310,7 @@ public void testCustomHostnameForWorkers() throws Exception { env.put("DD_CI_HOSTNAME", "testDDCiHostname"); jenkinsRule.jenkins.getGlobalNodeProperties().add(envProps); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationCustomHostname"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineOnWorkers.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineOnWorkers.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); // schedule build and wait for it to get queued @@ -1391,10 +1356,7 @@ public void testCustomHostnameForWorkersWebhook() throws Exception { env.put("DD_CI_HOSTNAME", "testDDCiHostname"); jenkinsRule.jenkins.getGlobalNodeProperties().add(envProps); WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineIntegrationCustomHostnameWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineOnWorkersWebhook.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineOnWorkersWebhook.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); // schedule build and wait for it to get queued @@ -1538,10 +1500,7 @@ private void assertNoneNameParallelStep(TraceSpan step, TraceSpan stage01, Trace public void testIsManualTrue() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationIsManualTrue"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); CauseAction causeAction = new CauseAction(new TimerTriggerCause(), new UserIdCause("johanna")); job.scheduleBuild2(0, causeAction).get(); @@ -1560,10 +1519,7 @@ public void testIsManualTrueWebhooks() throws Exception { clientStub.configureForWebhooks(); Jenkins jenkins = jenkinsRule.jenkins; WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationIsManualTrueWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); CauseAction causeAction = new CauseAction(new TimerTriggerCause(), new UserIdCause("johanna")); job.scheduleBuild2(0, causeAction).get(); @@ -1579,10 +1535,7 @@ public void testIsManualTrueWebhooks() throws Exception { public void testIsManualFalse() throws Exception { Jenkins jenkins = jenkinsRule.jenkins; WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationIsManualFalse"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); CauseAction causeAction = new CauseAction(new TimerTriggerCause(), new SCMTriggerCause("scm")); job.scheduleBuild2(0, causeAction).get(); @@ -1609,10 +1562,7 @@ public void testIsManualFalseWebhooks() throws Exception { clientStub.configureForWebhooks(); Jenkins jenkins = jenkinsRule.jenkins; WorkflowJob job = jenkins.createProject(WorkflowJob.class, "pipelineIntegrationIsManualFalseWebhook"); - String definition = IOUtils.toString( - this.getClass().getResourceAsStream("testPipelineSuccess.txt"), - "UTF-8" - ); + String definition = getPipelineDefinition("testPipelineSuccess.txt"); job.setDefinition(new CpsFlowDefinition(definition, true)); CauseAction causeAction = new CauseAction(new TimerTriggerCause(), new SCMTriggerCause("scm")); job.scheduleBuild2(0, causeAction).get(); @@ -1726,7 +1676,7 @@ public void testUnstablePropagationOnNestedStages() throws Exception { private void givenPipeline(String name, String definitionPath) throws Exception { WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, name); - String definition = IOUtils.toString(this.getClass().getResourceAsStream(definitionPath), "UTF-8"); + String definition = getPipelineDefinition(definitionPath); job.setDefinition(new CpsFlowDefinition(definition, true)); job.scheduleBuild2(0).get(); } diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogTraceAbstractTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogTraceAbstractTest.java index 3ea69acb..9f1c47cf 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogTraceAbstractTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogTraceAbstractTest.java @@ -18,7 +18,7 @@ public abstract class DatadogTraceAbstractTest { - protected void assertGitVariablesOnSpan(TraceSpan span, String defaultBranch) { + protected void assertGitVariablesOnSpan(TraceSpan span, String defaultBranch, String localGitRepoPath) { final Map meta = span.getMeta(); assertEquals("Initial commit\n", meta.get(CITags.GIT_COMMIT_MESSAGE)); assertEquals("John Doe", meta.get(CITags.GIT_COMMIT_AUTHOR_NAME)); @@ -30,11 +30,11 @@ protected void assertGitVariablesOnSpan(TraceSpan span, String defaultBranch) { assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get(CITags.GIT_COMMIT__SHA)); assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get(CITags.GIT_COMMIT_SHA)); assertEquals("master", meta.get(CITags.GIT_BRANCH)); - assertEquals("file:///tmp/git-repo/", meta.get(CITags.GIT_REPOSITORY_URL)); + assertEquals("file://" + localGitRepoPath, meta.get(CITags.GIT_REPOSITORY_URL)); assertEquals(defaultBranch, meta.get(CITags.GIT_DEFAULT_BRANCH)); } - protected void assertGitVariablesOnWebhook(JSONObject webhook, String defaultBranch) { + protected void assertGitVariablesOnWebhook(JSONObject webhook, String defaultBranch, String localGitRepoPath) { JSONObject meta = webhook.getJSONObject("git"); assertEquals("Initial commit\n", meta.get("message")); assertEquals("John Doe", meta.get("author_name")); @@ -45,7 +45,7 @@ protected void assertGitVariablesOnWebhook(JSONObject webhook, String defaultBra assertEquals("2020-10-08T07:49:32.000Z", meta.get("commit_time")); assertEquals("401d997a6eede777602669ccaec059755c98161f", meta.get("sha")); assertEquals("master", meta.get("branch")); - assertEquals("file:///tmp/git-repo/", meta.get("repository_url")); + assertEquals("file://" + localGitRepoPath, meta.get("repository_url")); assertEquals(defaultBranch, meta.get("default_branch")); } diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelineSuccessLocalCheckout.txt b/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelineSuccessLocalCheckout.txt index b5797b7c..3762efaf 100644 --- a/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelineSuccessLocalCheckout.txt +++ b/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelineSuccessLocalCheckout.txt @@ -5,7 +5,7 @@ pipeline { steps { script { git branch: 'master', - url: 'file:///tmp/git-repo/' + url: '$LOCAL_REPO_URL' } } } diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelinesOverrideGitCommit.txt b/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelinesOverrideGitCommit.txt index bbe7b5ac..8db6fa58 100644 --- a/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelinesOverrideGitCommit.txt +++ b/src/test/resources/org/datadog/jenkins/plugins/datadog/listeners/testPipelinesOverrideGitCommit.txt @@ -5,7 +5,7 @@ pipeline { steps { withEnv(["GIT_COMMIT=ccccbbbb"]) { git branch: 'master', - url: 'file:///tmp/git-repo/' + url: '$LOCAL_REPO_URL' } } }