Connection Refused in Jenkins pipeline only

288 views
Skip to first unread message

Paul Brown

unread,
May 18, 2022, 7:05:04 AM5/18/22
to wiremock-user

I have wiremock working locally on port 8080, or any other port for that matter. But when it builds in a jenkins pipeline I get a connection refused error message. I've tried various ports which all work locally, but nothing works in the jenkins pipeline.

The error is thrown in jenkins when the stubFor method is invoked


private void configureStubs() {
int mockPort = MockServiceContainer.mockUrl().getPort();
configureFor(MockServiceContainer.HOST, mockPort);

// get current members stub
stubFor(get(urlEqualTo("/v2/d73db7ba-5329-464d-988e-6a170c6476bd/members/current"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBody(GET_CURRENT_MEMBERS_MOCK_RESPONSE)));
}

public class MockServiceContainer {
private static final Logger LOG = LoggerFactory.getLogger(MockServiceContainer.class);

private static GenericContainer<?> container;

public static final int PORT = 8080;
public static final String HOST = "localhost";
public static final String HOST_IP = "127.0.0.1";
public static final String PROTOCOL = "http://";

public static GenericContainer<?> start() {
if (container == null) {
container = new
GenericContainer(DockerImageName.parse("wiremock/wiremock:2.32.0-alpine")) {
@Override
public GenericContainer withExposedPorts(Integer... ports) {
super.addFixedExposedPort(8888, PORT);
return super.withExposedPorts(ports);
}
}
.withExposedPorts(PORT)
.withExtraHost(HOST, HOST_IP);

container.start();
}
return container;
}

public static HttpClient createMockHttpClient() {
return HttpClient.newHttpClient();
}

public static URL mockUrl() { return resolveUrl(container.getHost(), container.getMappedPort(PORT)); }

private static URL resolveUrl(String address, int port) {
try {
return new URL(PROTOCOL + address + ":" + port);
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}
}
}

Tom Akehurst

unread,
May 18, 2022, 3:56:19 PM5/18/22
to wiremock-user
I would suggest getting it to log the result of the container.getMappedPort(PORT)) call, as if this isn't returning the correct value, then the connection will fail.
Reply all
Reply to author
Forward
0 new messages