Skip to content

Commit

Permalink
Always run known hosts tests locally
Browse files Browse the repository at this point in the history
No issues have been detected in local execution of the known hosts tests.
Don't reduce the detection opportunities in local development environments
or in environments that are not one of the ci.jenkins.io servers.
  • Loading branch information
MarkEWaite committed Dec 6, 2024
1 parent 58b1b7b commit b21b944
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

Expand All @@ -11,7 +12,6 @@
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;
import org.awaitility.Awaitility;
import org.junit.Assume;
import org.junit.Before;
Expand Down Expand Up @@ -43,8 +43,7 @@ public void assignVerifiers() throws IOException {

@Test
public void connectWhenHostKeyNotInKnownHostsFileForOtherHostNameThenShouldFail() throws Exception {
// Only test 10% of the time to reduce load on ssh providers
Assume.assumeTrue(ThreadLocalRandom.current().nextInt(10) == 0);
Assume.assumeTrue(runKnownHostsTests());
fakeKnownHosts = knownHostsTestUtil.createFakeKnownHosts("fake2.ssh", "known_hosts_fake2", FILE_CONTENT);
KnownHostsFileVerifier knownHostsFileVerifier = spy(new KnownHostsFileVerifier());
when(knownHostsFileVerifier.getKnownHostsFile()).thenReturn(fakeKnownHosts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ public static String nonGitHubHost() {
return nonGitHubHosts[ThreadLocalRandom.current().nextInt(nonGitHubHosts.length)];
}

private static final String JENKINS_URL =
System.getenv("JENKINS_URL") != null ? System.getenv("JENKINS_URL") : "http://localhost:8080/";

/* Return true if known hosts tests should be run in this context */
public static boolean runKnownHostsTests() {
if (!JENKINS_URL.contains("ci.jenkins.io")) {
/* Always run the tests if not on ci.jenkins.io */
return true;
}
// Only test 10% of the time on ci.jenkins.io to reduce load on ssh providers
return ThreadLocalRandom.current().nextInt(10) == 0;
}

/* Always return false, retained for test compatibility */
public static boolean isKubernetesCI() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;

import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
Expand All @@ -13,7 +14,6 @@
import java.nio.file.Path;
import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;
import org.awaitility.Awaitility;
import org.junit.Assume;
import org.junit.Before;
Expand Down Expand Up @@ -41,8 +41,7 @@ public void assignVerifier() { // For github.com

@Test
public void connectWhenHostKeyProvidedForOtherHostNameThenShouldFail() throws Exception {
// Only test 10% of the time to reduce load on ssh providers
Assume.assumeTrue(ThreadLocalRandom.current().nextInt(10) == 0);
Assume.assumeTrue(runKnownHostsTests());
HostKeyVerifierFactory verifier = new ManuallyProvidedKeyVerifier(hostKey);

KnownHostsTestUtil.connectToHost(
Expand Down

0 comments on commit b21b944

Please sign in to comment.