Skip to content

Commit 17caf34

Browse files
committed
Always run known hosts tests locally
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.
1 parent 58b1b7b commit 17caf34

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/test/java/org/jenkinsci/plugins/gitclient/verifier/KnownHostsFileVerifierTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
6+
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;
67
import static org.mockito.Mockito.spy;
78
import static org.mockito.Mockito.when;
89

@@ -11,7 +12,6 @@
1112
import java.io.File;
1213
import java.io.IOException;
1314
import java.time.Duration;
14-
import java.util.concurrent.ThreadLocalRandom;
1515
import org.awaitility.Awaitility;
1616
import org.junit.Assume;
1717
import org.junit.Before;
@@ -43,8 +43,7 @@ public void assignVerifiers() throws IOException {
4343

4444
@Test
4545
public void connectWhenHostKeyNotInKnownHostsFileForOtherHostNameThenShouldFail() throws Exception {
46-
// Only test 10% of the time to reduce load on ssh providers
47-
Assume.assumeTrue(ThreadLocalRandom.current().nextInt(10) == 0);
46+
Assume.assumeTrue(runKnownHostsTests());
4847
fakeKnownHosts = knownHostsTestUtil.createFakeKnownHosts("fake2.ssh", "known_hosts_fake2", FILE_CONTENT);
4948
KnownHostsFileVerifier knownHostsFileVerifier = spy(new KnownHostsFileVerifier());
5049
when(knownHostsFileVerifier.getKnownHostsFile()).thenReturn(fakeKnownHosts);

src/test/java/org/jenkinsci/plugins/gitclient/verifier/KnownHostsTestUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ public static String nonGitHubHost() {
148148
return nonGitHubHosts[ThreadLocalRandom.current().nextInt(nonGitHubHosts.length)];
149149
}
150150

151+
private static final String JENKINS_URL =
152+
System.getenv("JENKINS_URL") != null ? System.getenv("JENKINS_URL") : "http://localhost:8080/";
153+
154+
/* Return true if known hosts tests should be run in this context */
155+
public static boolean runKnownHostsTests() {
156+
if (!JENKINS_URL.contains("ci.jenkins.io")) {
157+
/* Always run the tests if not on ci.jenkins.io */
158+
return true;
159+
}
160+
// Only test 10% of the time on ci.jenkins.io to reduce load on ssh providers
161+
return ThreadLocalRandom.current().nextInt(10) == 0;
162+
}
163+
151164
/* Always return false, retained for test compatibility */
152165
public static boolean isKubernetesCI() {
153166
return false;

src/test/java/org/jenkinsci/plugins/gitclient/verifier/ManuallyProvidedKeyVerifierTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
6+
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;
67

78
import hudson.model.StreamBuildListener;
89
import hudson.model.TaskListener;
@@ -13,7 +14,6 @@
1314
import java.nio.file.Path;
1415
import java.time.Duration;
1516
import java.util.Collections;
16-
import java.util.concurrent.ThreadLocalRandom;
1717
import org.awaitility.Awaitility;
1818
import org.junit.Assume;
1919
import org.junit.Before;
@@ -41,8 +41,7 @@ public void assignVerifier() { // For github.com
4141

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

4847
KnownHostsTestUtil.connectToHost(

0 commit comments

Comments
 (0)