Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<jenkins.baseline>2.504</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
<spotless.check.skip>false</spotless.check.skip>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -131,6 +132,12 @@
<artifactId>workflow-multibranch</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
package org.jenkinsci.plugins.github_branch_source;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;

import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.SingleRootFileSource;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.io.File;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

/** @author Liam Newman */
@WithJenkins
public abstract class AbstractGitHubWireMockTest {

// By default the wiremock tests will run without proxy
// The tests will use only the stubbed data and will fail if requests are made for missing data.
// You can use the proxy while writing and debugging tests.
private static final boolean useProxy =
private static final boolean USE_PROXY =
!System.getProperty("test.github.useProxy", "false").equals("false");

@ClassRule
public static JenkinsRule r = new JenkinsRule();
protected static JenkinsRule r;

public static WireMockRuleFactory factory = new WireMockRuleFactory();
private static final WireMockExtensionFactory FACTORY = new WireMockExtensionFactory();

@Rule
public WireMockRule githubRaw =
factory.getRule(WireMockConfiguration.options().dynamicPort().usingFilesUnderClasspath("raw"));
@RegisterExtension
protected final WireMockExtension githubRaw =
FACTORY.getExtension(WireMockConfiguration.options().dynamicPort().usingFilesUnderClasspath("raw"));

@Rule
public WireMockRule githubApi = factory.getRule(WireMockConfiguration.options()
@RegisterExtension
protected final WireMockExtension githubApi = FACTORY.getExtension(WireMockConfiguration.options()
.dynamicPort()
.usingFilesUnderClasspath("api")
.extensions(new ResponseTransformer() {
Expand All @@ -48,10 +46,11 @@ public Response transform(Request request, Response response, FileSource files,
.but()
.body(response.getBodyAsString()
.replace(
"https://api.github.com/", "http://localhost:" + githubApi.port() + "/")
"https://api.github.com/",
"http://localhost:" + githubApi.getPort() + "/")
.replace(
"https://raw.githubusercontent.com/",
"http://localhost:" + githubRaw.port() + "/"))
"http://localhost:" + githubRaw.getPort() + "/"))
.build();
}
return response;
Expand All @@ -63,11 +62,14 @@ public String getName() {
}
}));

@Before
public void prepareMockGitHub() {
prepareMockGitHubFileMappings();
@BeforeAll
static void beforeAll(JenkinsRule rule) {
r = rule;
}

if (useProxy) {
@BeforeEach
void beforeEach() throws Exception {
if (USE_PROXY) {
githubApi.stubFor(get(urlMatching(".*"))
.atPriority(10)
.willReturn(aResponse().proxiedFrom("https://api.github.com/")));
Expand All @@ -76,17 +78,4 @@ public void prepareMockGitHub() {
.willReturn(aResponse().proxiedFrom("https://raw.githubusercontent.com/")));
}
}

void prepareMockGitHubFileMappings() {
new File("src/test/resources/api/mappings").mkdirs();
new File("src/test/resources/api/__files").mkdirs();
new File("src/test/resources/raw/mappings").mkdirs();
new File("src/test/resources/raw/__files").mkdirs();
githubApi.enableRecordMappings(
new SingleRootFileSource("src/test/resources/api/mappings"),
new SingleRootFileSource("src/test/resources/api/__files"));
githubRaw.enableRecordMappings(
new SingleRootFileSource("src/test/resources/raw/mappings"),
new SingleRootFileSource("src/test/resources/raw/__files"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.resetAllScenarios;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.ScenarioMappingBuilder;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import com.github.tomakehurst.wiremock.stubbing.Scenario;
import hudson.util.LogTaskListener;
Expand All @@ -25,14 +21,14 @@
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHRateLimit;
import org.kohsuke.github.GitHub;
import org.mockito.Mockito;

public class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest {
class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest {

private RingBufferLogHandler handler;
private LogTaskListener listener;
Expand All @@ -57,26 +53,21 @@ private long countOfOutputLinesContaining(String substring) {
return countOfOutputLines(m -> m.contains(substring));
}

public static int getRequestCount(WireMockServer server) {
return server.countRequestsMatching(RequestPatternBuilder.allRequests().build())
private static int getRequestCount(WireMockExtension extension) {
return extension
.countRequestsMatching(RequestPatternBuilder.allRequests().build())
.getCount();
}

private class RateLimit {
final int remaining;
final int limit;
final Date reset;
private record RateLimit(int limit, int remaining, Date reset) {}

RateLimit(int limit, int remaining, Date reset) {
this.limit = limit;
this.remaining = remaining;
this.reset = reset;
}
}
@Override
@BeforeEach
void beforeEach() throws Exception {
super.beforeEach();

@Before
public void setUp() throws Exception {
resetAllScenarios();
githubApi.resetScenarios();
githubRaw.resetScenarios();

handler = new RingBufferLogHandler(1000);
final Logger logger = Logger.getLogger(getClass().getName());
Expand All @@ -98,13 +89,12 @@ public void setUp() throws Exception {
ApiRateLimitChecker.resetLocalChecker();
}

@After
public void tearDown() throws Exception {
@AfterEach
void afterEach() {
GitHubConfiguration.get().setEndpoints(new ArrayList<>());
}

private void setupStubs(List<RateLimit> scenarios) throws Exception {

githubApi.stubFor(get(urlEqualTo("/meta"))
.willReturn(aResponse().withStatus(200).withBody("{\"verifiable_password_authentication\": false}")));

Expand Down Expand Up @@ -159,13 +149,13 @@ private void setupStubs(List<RateLimit> scenarios) throws Exception {
githubApi.stubFor(scenario);
}

github = Connector.connect("http://localhost:" + githubApi.port(), null);
github = Connector.connect("http://localhost:" + githubApi.getPort(), null);
initialRequestCount = getRequestCount(githubApi);
assertEquals(2, initialRequestCount);
}

@Test
public void NoCheckerConfigured() throws Exception {
void NoCheckerConfigured() throws Exception {
// set up scenarios
List<RateLimit> scenarios = new ArrayList<>();
long now = System.currentTimeMillis();
Expand Down Expand Up @@ -204,7 +194,7 @@ public void NoCheckerConfigured() throws Exception {
}

@Test
public void NoCheckerConfiguredWithEndpoint() throws Exception {
void NoCheckerConfiguredWithEndpoint() throws Exception {
// set up scenarios
List<RateLimit> scenarios = new ArrayList<>();
long now = System.currentTimeMillis();
Expand Down Expand Up @@ -240,7 +230,7 @@ public void NoCheckerConfiguredWithEndpoint() throws Exception {
* @author Julian V. Modesto
*/
@Test
public void ThrottleOnOverTestWithQuota() throws Exception {
void ThrottleOnOverTestWithQuota() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);

// set up scenarios
Expand Down Expand Up @@ -269,7 +259,7 @@ public void ThrottleOnOverTestWithQuota() throws Exception {
* @author Julian V. Modesto
*/
@Test
public void ThrottleOnNormalizeTestWithQuota() throws Exception {
void ThrottleOnNormalizeTestWithQuota() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);

// set up scenarios
Expand All @@ -296,7 +286,7 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception {
* @author Marc Salles Navarro
*/
@Test
public void NoThrottleTestShouldNotThrottle() throws Exception {
void NoThrottleTestShouldNotThrottle() throws Exception {
// set up scenarios
List<RateLimit> scenarios = new ArrayList<>();
int limit = 5000;
Expand Down Expand Up @@ -324,8 +314,7 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
* @author Marc Salles Navarro
*/
@Test
public void NoThrottleTestShouldNotThrottle404() throws Exception {

void NoThrottleTestShouldNotThrottle404() throws Exception {
setupStubs(new ArrayList<>());
GHRateLimit.Record initial = github.lastRateLimit().getCore();
assertEquals(2, getRequestCount(githubApi));
Expand All @@ -341,7 +330,8 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
github.getMeta();

// The core should be unknown, but different from initial
assertTrue(github.rateLimit().getCore() instanceof GHRateLimit.UnknownLimitRecord);
assertInstanceOf(
GHRateLimit.UnknownLimitRecord.class, github.rateLimit().getCore());
assertNotEquals(initial, github.rateLimit().getCore());

// there should be no output
Expand All @@ -357,7 +347,7 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
* @author Marc Salles Navarro
*/
@Test
public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);

// set up scenarios
Expand Down Expand Up @@ -391,7 +381,7 @@ public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws
* @author Julian V. Modesto
*/
@Test
public void ThrottleOnOverTest() throws Exception {
void ThrottleOnOverTest() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);

// set up scenarios
Expand Down Expand Up @@ -462,7 +452,7 @@ public void ThrottleOnOverTest() throws Exception {
* @author Julian V. Modesto
*/
@Test
public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
void ThrottleForNormalizeTestWithinIdeal() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);

List<RateLimit> scenarios = new ArrayList<>();
Expand Down Expand Up @@ -552,7 +542,7 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
* @author Julian V. Modesto
*/
@Test
public void NormalizeThrottleWithBurnedBuffer() throws Exception {
void NormalizeThrottleWithBurnedBuffer() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);

long now = System.currentTimeMillis();
Expand Down Expand Up @@ -614,7 +604,7 @@ public void NormalizeThrottleWithBurnedBuffer() throws Exception {
* @author Alex Taylor
*/
@Test
public void OnOverThrottleTimingRateLimitCheck() throws Exception {
void OnOverThrottleTimingRateLimitCheck() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);

// Longer timings that test defaults for more consistent measurements.
Expand Down Expand Up @@ -695,7 +685,7 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
* @author Alex Taylor
*/
@Test
public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
void NormalizeThrottleTimingRateLimitCheck() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);

ApiRateLimitChecker.setExpirationWaitMillis(60);
Expand Down Expand Up @@ -763,7 +753,7 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
* @author Alex Taylor
*/
@Test
public void NormalizeExpectedIdealOverTime() throws Exception {
void NormalizeExpectedIdealOverTime() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);

// Set up scenarios
Expand Down Expand Up @@ -840,7 +830,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
* @author Alex Taylor
*/
@Test
public void OnOverExpectedIdealOverTime() throws Exception {
void OnOverExpectedIdealOverTime() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);

long start = System.currentTimeMillis();
Expand Down Expand Up @@ -894,7 +884,7 @@ public void OnOverExpectedIdealOverTime() throws Exception {
* @author Alex Taylor
*/
@Test
public void ExpectedResetTimingNormalize() throws Exception {
void ExpectedResetTimingNormalize() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);

// Use a longer notification interval to make the test produce stable output
Expand Down Expand Up @@ -940,7 +930,7 @@ public void ExpectedResetTimingNormalize() throws Exception {
* @author Alex Taylor
*/
@Test
public void ExpectedResetTimingOnOver() throws Exception {
void ExpectedResetTimingOnOver() throws Exception {
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);

// Use a longer notification interval to make the test produce stable output
Expand Down
Loading