Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Jan 25, 2024
1 parent 36d59f5 commit 7eefea6
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> 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.
Expand All @@ -598,35 +606,56 @@ 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)",
hostname, MAX_HOSTNAME_LEN));
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<String, Set<String>> getComputerTags(Computer computer) {
Set<LabelAtom> labels = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,22 +90,23 @@ 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);
if (prop == null || !prop.isEmitSCMEvents()) {
return;
}

logger.fine("Start DatadogSCMListener#onCheckout");

// Get Datadog Client Instance
DatadogClient client = ClientFactory.getClient();
if (client == null) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 7eefea6

Please sign in to comment.