spring-cloud-contract icon indicating copy to clipboard operation
spring-cloud-contract copied to clipboard

Cannot set fixed port in mvn stub runner (httpPort is ignored)

Open laffuste opened this issue 9 years ago • 3 comments

When starting spring-cloud-contract-maven-plugin in local and specifying a port, server listens a random port.

Steps to reproduce

Observed in my project and in spring-cloud-samples/spring-cloud-contract-samples/producer (branch 1.1.x).

mvn spring-cloud-contract:run -DhttpPort=11111

Or adding it in:

 <plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <version>${spring-cloud-contract.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <httpPort>11111</httpPort>               
                    <baseClassForTests>com.foo.bar</baseClassForTests>
                </configuration>
            </plugin>

And mvn spring-cloud-contract:run.

Expected Results

Server listening on 11111.

Observed Results

[INFO] Tomcat started on port(s): 11260 (http)

Notes
  1. Debug info: The parameter is picked up and set in StubRunnerOptions.stubsIdsToPortMapping as an entry of null:11111, but never gets resolved since it is attempted later on with a key matching the local stub config (instead of null).

  2. I start with mvn spring-cloud-contract:run instead of mvn spring-cloud-contract-verifier:run as stated in the usage page.

  3. Default port should be 8080, but this is not respected neither.

Link to maven plugin goal.

laffuste avatar Aug 22 '17 11:08 laffuste

From what I see for the httpPort

/**
	 * HTTP port for WireMock server
	 */
	@Parameter(property = "spring.cloud.contract.verifier.http.port", defaultValue = "8080")
	private int httpPort;

to work you MUST NOT pass the stubs via the spring.cloud.contract.verifier.stubs user property.:

@Parameter(property = "spring.cloud.contract.verifier.stubs")
	private String stubs;

This is the code that runs it.

if (isNullOrEmpty(this.stubs)) {
			StubRunnerOptions options = optionsBuilder
					.withPort(this.httpPort)
					.build();
			StubRunner stubRunner = this.localStubRunner.run(resolveStubsDirectory().getAbsolutePath(), options);
			batchStubRunner = new BatchStubRunner(Collections.singleton(stubRunner));
		} 

It will resolve the stubs from a folder and run them from there. If you do pass the stubs then the code looks like this:

StubRunnerOptions options = optionsBuilder
					.withStubs(this.stubs)
					.withMinMaxPort(this.minPort, this.maxPort)
					.build();
			batchStubRunner = this.remoteStubRunner.run(options, this.repoSession);

That means you need to pass the range of ports (min, max). The defaults look like this:

@Parameter(property = "spring.cloud.contract.verifier.http.minPort", defaultValue = "10000")
	private int minPort;

	@Parameter(property = "spring.cloud.contract.verifier.http.maxPort", defaultValue = "15000")
	private int maxPort;

Does it make sense? For sure I need to complete this task - https://github.com/spring-cloud/spring-cloud-contract/issues/340 so that there are no problems with understanding how things work :/

marcingrzejszczak avatar Aug 22 '17 11:08 marcingrzejszczak

The stubs param is not passed neither by command line arg nor pom param, and the code falls indeed inside the isNullOrEmpty(this.stubs) condition.

When this.localStubRunner.run is executed, the port will already be mapped to a null key in StubRunnerOptions options. Further down the stack an attempt to retrieve with a not null key on options will yield nothing (no port mapping) and then a random port will be assigned.

(I'm not with my machine at the moment, cannot check the exact lines)

laffuste avatar Aug 22 '17 13:08 laffuste

Ok I've analyzed the problem and there is one bug and one misuse ;) The bug I'll fix in a second and the misuse is that you should use spring.cloud.contract.verifier.http.port and then it works fine.

marcingrzejszczak avatar Aug 28 '17 12:08 marcingrzejszczak