From 043def3024ad96810e86de52b9320b80fb9d8a40 Mon Sep 17 00:00:00 2001 From: juazugas Date: Sat, 14 Jan 2023 23:39:23 +0100 Subject: [PATCH] Fix check local port available on start remote-dev --- CHANGELOG.md | 1 + .../remotedev/RemoteDevelopmentService.java | 4 +- .../RemoteDevelopmentServiceTest.java | 49 ++++++++++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f3a45f862..b050007efc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/jkube-kit/remote-dev/src/main/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentService.java b/jkube-kit/remote-dev/src/main/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentService.java index 805690529a..636a626773 100644 --- a/jkube-kit/remote-dev/src/main/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentService.java +++ b/jkube-kit/remote-dev/src/main/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentService.java @@ -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, " + diff --git a/jkube-kit/remote-dev/src/test/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentServiceTest.java b/jkube-kit/remote-dev/src/test/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentServiceTest.java index 13ac70412d..36330fe65c 100644 --- a/jkube-kit/remote-dev/src/test/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentServiceTest.java +++ b/jkube-kit/remote-dev/src/test/java/org/eclipse/jkube/kit/remotedev/RemoteDevelopmentServiceTest.java @@ -17,6 +17,9 @@ 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; @@ -24,6 +27,7 @@ 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; @@ -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() { @@ -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 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)); } } + }