Skip to content

Commit 58b1b7b

Browse files
committed
Test host verifiers with a randomly selected ssh provider
The bitbucket.org ssh provider seems to be blocking access from the ci.jenkins.io agents. That causes tests to fail. Use other ssh providers. Reduce the load on ssh providers by only testing one in ten times. Spread the load by using a randomly selected ssh provider instead of always using the same ssh provider.
1 parent b3542c9 commit 58b1b7b

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
5-
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.isKubernetesCI;
5+
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
66
import static org.mockito.Mockito.spy;
77
import static org.mockito.Mockito.when;
88

@@ -11,7 +11,9 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313
import java.time.Duration;
14+
import java.util.concurrent.ThreadLocalRandom;
1415
import org.awaitility.Awaitility;
16+
import org.junit.Assume;
1517
import org.junit.Before;
1618
import org.junit.Rule;
1719
import org.junit.Test;
@@ -41,12 +43,14 @@ public void assignVerifiers() throws IOException {
4143

4244
@Test
4345
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);
4448
fakeKnownHosts = knownHostsTestUtil.createFakeKnownHosts("fake2.ssh", "known_hosts_fake2", FILE_CONTENT);
4549
KnownHostsFileVerifier knownHostsFileVerifier = spy(new KnownHostsFileVerifier());
4650
when(knownHostsFileVerifier.getKnownHostsFile()).thenReturn(fakeKnownHosts);
4751

4852
KnownHostsTestUtil.connectToHost(
49-
"bitbucket.org",
53+
nonGitHubHost(),
5054
22,
5155
fakeKnownHosts,
5256
knownHostsFileVerifier.forJGit(StreamBuildListener.fromStdout()),

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919
import java.util.Objects;
20+
import java.util.concurrent.ThreadLocalRandom;
2021
import java.util.function.Predicate;
2122
import org.apache.sshd.client.ClientBuilder;
2223
import org.apache.sshd.client.SshClient;
@@ -135,14 +136,20 @@ protected static boolean checkKeys(JGitClientSession s) {
135136
return verified;
136137
}
137138

138-
/* Return true if running on a Kubernetes pod on ci.jenkins.io */
139+
// Several different git providers with ssh access, use one randomly
140+
private static String[] nonGitHubHosts = {
141+
// bitbucket.org blocks requests from ci.jenkins.io agents
142+
// "bitbucket.org",
143+
"git.assembla.com", "gitea.com", "gitlab.com", "vs-ssh.visualstudio.com",
144+
};
145+
146+
/* Return hostname of a non-GitHub ssh provider */
147+
public static String nonGitHubHost() {
148+
return nonGitHubHosts[ThreadLocalRandom.current().nextInt(nonGitHubHosts.length)];
149+
}
150+
151+
/* Always return false, retained for test compatibility */
139152
public static boolean isKubernetesCI() {
140153
return false;
141-
// String kubernetesPort = System.getenv("KUBERNETES_PORT");
142-
// String buildURL = System.getenv("BUILD_URL");
143-
// return kubernetesPort != null
144-
// && !kubernetesPort.isEmpty()
145-
// && buildURL != null
146-
// && buildURL.startsWith("https://ci.jenkins.io/");
147154
}
148155
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
5-
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.isKubernetesCI;
5+
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
66

77
import hudson.model.StreamBuildListener;
88
import hudson.model.TaskListener;
@@ -13,7 +13,9 @@
1313
import java.nio.file.Path;
1414
import java.time.Duration;
1515
import java.util.Collections;
16+
import java.util.concurrent.ThreadLocalRandom;
1617
import org.awaitility.Awaitility;
18+
import org.junit.Assume;
1719
import org.junit.Before;
1820
import org.junit.Rule;
1921
import org.junit.Test;
@@ -32,17 +34,19 @@ public class ManuallyProvidedKeyVerifierTest {
3234
private String hostKey;
3335

3436
@Before
35-
public void assignVerifier() {
37+
public void assignVerifier() { // For github.com
3638
hostKey =
3739
"|1|7qEjynZk0IodegnbgoPEhWtdgA8=|bGs7a1ktbGWwPuZqqTbAazUAULM= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=";
3840
}
3941

4042
@Test
4143
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);
4246
HostKeyVerifierFactory verifier = new ManuallyProvidedKeyVerifier(hostKey);
4347

4448
KnownHostsTestUtil.connectToHost(
45-
"bitbucket.org",
49+
nonGitHubHost(),
4650
22,
4751
new File(testFolder.getRoot() + "/path/to/file/random"),
4852
verifier.forJGit(StreamBuildListener.fromStdout()),

0 commit comments

Comments
 (0)