Skip to content

Commit

Permalink
Fix check local port available on start remote-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
juazugas committed Jan 19, 2023
1 parent 174d189 commit 043def3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage:
./scripts/extract-changelog-for-version.sh 1.3.37 5
```
### 1.11-SNAPSHOT
* Fix #2003: check local port available on start remote-dev
* Fix #1439: Add hint to use jkube.domain if createExternalUrls is used without domain
* Fix #1459: Route Generation should support `8443` as default web port
* Fix #1546: Migrate to JUnit5 testing framework
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public void stop() {

private void checkEnvironment() {
for (RemoteService remoteService : context.getRemoteDevelopmentConfig().getRemoteServices()) {
try (ServerSocket ignore = new ServerSocket(remoteService.getPort())) {
try (ServerSocket ignore = new ServerSocket(remoteService.getLocalPort())) {
logger.debug("Local port '%s' for remote service '%s:%s' is available",
remoteService.getLocalPort(), remoteService.getHostname(), remoteService.getPort());
} catch (Exception e) {
throw new IllegalStateException(
"Local port '" + remoteService.getPort() + "' is already in use (" + remoteService.getHostname() + ")");
"Local port '" + remoteService.getLocalPort() + "' is already in use (" + remoteService.getHostname() + ")");
}
if (kubernetesClient.services().withName(remoteService.getHostname()).get() == null) {
logger.warn("Service '%s' does not exist in the cluster, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.IoUtil;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -42,6 +46,11 @@ void setUp() {
logger = spy(new KitLogger.StdoutLogger());
}

@AfterEach
void tearDown() {
reset(logger);
}

@Test
@DisplayName("service can be stopped before it is started")
void canBeStoppedBeforeStart() {
Expand All @@ -67,18 +76,44 @@ void canBeStoppedMultipleTimesBeforeStart() {
@Test
@DisplayName("start initiates PortForwarder and KubernetesSshServiceForwarder")
void startInitiatesChildProcesses() {
// When
startDevelopmentServiceWithConfig(RemoteDevelopmentConfig.builder().build());

// Then
verify(logger, times(1))
.debug("Creating or replacing Kubernetes services for exposed ports from local environment");
verify(logger, times(1))
.debug("Starting Kubernetes SSH service forwarder...");
verify(logger, times(1))
.debug("Starting port forwarder...");
}

@Test
@DisplayName("start initiates if LocalPort for remote service are available")
void startInitsIfLocalPortAvailable() {
RemoteService remoteService = RemoteService.builder()
.hostname("remote-host").localPort(IoUtil.getFreeRandomPort()).port(1234).build();
RemoteDevelopmentConfig config = RemoteDevelopmentConfig.builder().remoteService(remoteService).build();

// When
startDevelopmentServiceWithConfig(config);

// Then
verify(logger, times(1))
.debug("Local port '%s' for remote service '%s:%s' is available",
remoteService.getLocalPort(),remoteService.getHostname(),remoteService.getPort());
verify(logger, times(1))
.debug("Creating or replacing Kubernetes services for exposed ports from local environment");
}

private void startDevelopmentServiceWithConfig(RemoteDevelopmentConfig remoteDevelopmentConfig) {
CompletableFuture<Void> future = null;
try {
// When
future = new RemoteDevelopmentService(logger, kubernetesClient, RemoteDevelopmentConfig.builder().build())
future = new RemoteDevelopmentService(logger, kubernetesClient, remoteDevelopmentConfig)
.start();
// Then
verify(logger, times(1))
.debug("Starting Kubernetes SSH service forwarder...");
verify(logger, times(1))
.debug("Starting port forwarder...");
} finally {
Optional.ofNullable(future).ifPresent(f -> f.cancel(true));
}
}

}

0 comments on commit 043def3

Please sign in to comment.