Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Coulon2016-08-25 15:18:19 +0000
committerJeff Johnston2016-08-26 16:57:06 +0000
commite271693a9737a77d8dcc0463dcd5497e3adc9749 (patch)
tree8a3978c254d9675259bb0eeef1bcc3b3b5189b83
parentc98537761a49266ac998b46049fdac7a1e5b7b14 (diff)
downloadorg.eclipse.linuxtools-e271693a9737a77d8dcc0463dcd5497e3adc9749.tar.gz
org.eclipse.linuxtools-e271693a9737a77d8dcc0463dcd5497e3adc9749.tar.xz
org.eclipse.linuxtools-e271693a9737a77d8dcc0463dcd5497e3adc9749.zip
Bug 499854 - Connection is not added(same parameters, different name)
Refactored the validators in the NewDockerConnectionPage by adding a search for connection with the same Unix/TCP settings when the name is changed. The page cannot complete if the user gives a new name for a connection with known Unix socket path or TCP host. Added SWTBot tests. Change-Id: I99a4987c8c2c4c80aeffc58085ac1e7c94df50c9 Signed-off-by: Xavier Coulon <xcoulon@redhat.com> Reviewed-on: https://git.eclipse.org/r/79747 Tested-by: Hudson CI Reviewed-by: Jeff Johnston <jjohnstn@redhat.com> (cherry picked from commit f107e191a5cbe83dccc25172d74676b325bfbcc7) Reviewed-on: https://git.eclipse.org/r/79872
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/MockDockerConnectionSettingsFinder.java10
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionSWTBotTest.java96
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java87
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties2
4 files changed, 170 insertions, 25 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/MockDockerConnectionSettingsFinder.java b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/MockDockerConnectionSettingsFinder.java
index 43d3843c86..7b09a7cacb 100644
--- a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/MockDockerConnectionSettingsFinder.java
+++ b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/MockDockerConnectionSettingsFinder.java
@@ -68,15 +68,21 @@ public class MockDockerConnectionSettingsFinder {
* able to detect a <strong>valid TCP Connection</strong> to a Docker daemon.
*/
public static void validTCPConnectionAvailable() {
+ validTCPConnectionAvailable("mock", "tcp://1.2.3.4:1234", "/path/to/certs");
+ }
+
+ public static void validTCPConnectionAvailable(final String connectionName, final String host,
+ final String pathToCerts) {
+
final IDockerConnectionSettingsFinder validTCPSocketConnectionAvailable = Mockito
.mock(IDockerConnectionSettingsFinder.class);
- final TCPConnectionSettings tcpConnectionSettings = new TCPConnectionSettings("tcp://1.2.3.4:1234", "/path/to/certs");
+ final TCPConnectionSettings tcpConnectionSettings = new TCPConnectionSettings(host, pathToCerts);
//tcpConnectionSettings.setName("mock");
tcpConnectionSettings.setSettingsResolved(true);
Mockito.when(validTCPSocketConnectionAvailable.findDefaultConnectionSettings()).thenReturn(tcpConnectionSettings);
Mockito.when(
validTCPSocketConnectionAvailable.resolveConnectionName(Matchers.any(IDockerConnectionSettings.class)))
- .thenReturn("mock");
+ .thenReturn(connectionName);
DockerConnectionManager.getInstance().setConnectionSettingsFinder(validTCPSocketConnectionAvailable);
}
diff --git a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionSWTBotTest.java b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionSWTBotTest.java
index fd25a5ceea..e55e736f20 100644
--- a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionSWTBotTest.java
+++ b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionSWTBotTest.java
@@ -16,11 +16,17 @@ import java.io.IOException;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.linuxtools.docker.core.IDockerConnection;
+import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
import org.eclipse.linuxtools.internal.docker.core.SystemUtils;
+import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerClientFactory;
+import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerConnectionFactory;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerConnectionSettingsFinder;
+import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.ButtonAssertion;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.CheckBoxAssertion;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.CloseShellRule;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.CloseWelcomePageRule;
+import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.DockerConnectionManagerUtils;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.RadioAssertion;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.SWTBotTreeItemAssertions;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.SWTUtils;
@@ -47,6 +53,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
+import com.spotify.docker.client.DockerClient;
+
/**
* Testing the {@link NewDockerConnection} {@link Wizard}
*/
@@ -83,6 +91,22 @@ public class NewDockerConnectionSWTBotTest {
this.addConnectionButton = dockerExplorerViewBot.toolbarButton("&Add Connection");
}
+ private IDockerConnection configureUnixSocketConnection(final String connectionName, final String pathToSocket) {
+ final DockerClient client = MockDockerClientFactory.build();
+ final DockerConnection dockerConnection = MockDockerConnectionFactory.from(connectionName, client)
+ .withUnixSocketConnectionSettings(pathToSocket);
+ DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
+ return dockerConnection;
+ }
+
+ private IDockerConnection configureTCPConnection(final String connectionName, final String host) {
+ final DockerClient client = MockDockerClientFactory.build();
+ final DockerConnection dockerConnection = MockDockerConnectionFactory.from(connectionName, client)
+ .withTCPConnectionSettings(host, null);
+ DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
+ return dockerConnection;
+ }
+
@Test
public void shouldShowCustomUnixSocketSettingsWhenNoConnectionAvailable() {
// given
@@ -176,6 +200,78 @@ public class NewDockerConnectionSWTBotTest {
}
@Test
+ public void shouldNotAllowNewConnectionWithDifferentNameAndSameUnixSocketSettings() throws IOException {
+ // given
+ final String dockerSocketTmpPath = File.createTempFile("docker", ".sock").getAbsolutePath();
+ MockDockerConnectionSettingsFinder.validUnixSocketConnectionAvailable("Mock",
+ "unix://" + dockerSocketTmpPath);
+ // add an existing connection based on the settings above
+ configureUnixSocketConnection("Mock", dockerSocketTmpPath);
+ // when open wizard
+ addConnectionButton.click();
+ bot.waitUntil(Conditions.shellIsActive(WizardMessages.getString("NewDockerConnection.title"))); //$NON-NLS-1$
+ // when changing connection name
+ bot.text(0).setText("foo");
+ // then the wizard should not allow for completion because a connection
+ // with the connection settings already exists.
+ ButtonAssertion.assertThat(bot.button("Finish")).isNotEnabled();
+ }
+
+ @Test
+ public void shouldNotAllowNewConnectionWithDifferentNameAndSameTCPSettings() throws IOException {
+ // given
+ MockDockerConnectionSettingsFinder.validTCPConnectionAvailable("Mock", "https://foo:1234", null);
+ // add an existing connection based on the settings above
+ configureTCPConnection("Mock", "https://foo:1234");
+ // when open wizard
+ addConnectionButton.click();
+ bot.waitUntil(Conditions.shellIsActive(WizardMessages.getString("NewDockerConnection.title"))); //$NON-NLS-1$
+ // when changing connection name
+ bot.text(0).setText("foo");
+ // then the wizard should not allow for completion because a connection
+ // with the connection settings already exists.
+ ButtonAssertion.assertThat(bot.button("Finish")).isNotEnabled();
+ }
+
+ @Test
+ public void shouldAllowNewConnectionWithDifferentNameAndUnixSettings() throws IOException {
+ // given
+ final String dockerSocketTmpPath = File.createTempFile("docker", ".sock").getAbsolutePath();
+ configureUnixSocketConnection("Bar", dockerSocketTmpPath);
+ MockDockerConnectionSettingsFinder.validUnixSocketConnectionAvailable("Mock",
+ "unix://" + dockerSocketTmpPath);
+ final String otherDockerSocketTmpPath = File.createTempFile("docker", ".sock").getAbsolutePath();
+ // when open wizard
+ addConnectionButton.click();
+ bot.waitUntil(Conditions.shellIsActive(WizardMessages.getString("NewDockerConnection.title"))); //$NON-NLS-1$
+ // when changing connection name
+ bot.text(0).setText("foo");
+ bot.checkBox(0).select();
+ bot.text(1).setText(otherDockerSocketTmpPath);
+ // then the wizard should not allow for completion because a connection
+ // with the connection settings already exists.
+ ButtonAssertion.assertThat(bot.button("Finish")).isEnabled();
+ }
+
+ @Test
+ public void shouldAllowNewConnectionWithDifferentNameAndTCPSettings() throws IOException {
+ // given
+ MockDockerConnectionSettingsFinder.validTCPConnectionAvailable("Mock", "https://foo:1234", null);
+ // add an existing connection based on the settings above
+ configureTCPConnection("Mock", "https://foo");
+ // when open wizard
+ addConnectionButton.click();
+ bot.waitUntil(Conditions.shellIsActive(WizardMessages.getString("NewDockerConnection.title"))); //$NON-NLS-1$
+ // when changing connection name
+ bot.text(0).setText("foo");
+ bot.checkBox(0).select();
+ bot.text(2).setText("https://bar:1234");
+ // then the wizard should not allow for completion because a connection
+ // with the connection settings already exists.
+ ButtonAssertion.assertThat(bot.button("Finish")).isEnabled();
+ }
+
+ @Test
public void shouldPopulateConnectionWithClipboard() {
verifyPopulateConnectionWithClipboard(DND.CLIPBOARD);
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
index 28d2c8d353..ee8b19e565 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
@@ -48,6 +48,7 @@ import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.EnumDockerConnectionSettings;
import org.eclipse.linuxtools.docker.core.IDockerConnectionSettings;
+import org.eclipse.linuxtools.docker.core.IDockerConnectionSettings.BindingType;
import org.eclipse.linuxtools.docker.ui.Activator;
import org.eclipse.linuxtools.internal.docker.core.DefaultDockerConnectionSettingsFinder;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
@@ -370,12 +371,12 @@ public class NewDockerConnectionPage extends WizardPage {
// validations will be performed when the user changes the value
// only, not at the dialog opening
dbc.addValidationStatusProvider(
- new ConnectionNameValidator(connectionNameModelObservable));
- dbc.addValidationStatusProvider(
- new UnixSocketValidator(unixSocketBindingModeModelObservable,
+ new UnixSocketValidator(connectionNameModelObservable,
+ unixSocketBindingModeModelObservable,
unixSocketPathModelObservable));
dbc.addValidationStatusProvider(
- new TcpHostValidator(tcpConnectionBindingModeModelObservable,
+ new TcpHostValidator(connectionNameModelObservable,
+ tcpConnectionBindingModeModelObservable,
tcpHostModelObservable));
dbc.addValidationStatusProvider(new TcpCertificatesValidator(
tcpConnectionBindingModeModelObservable,
@@ -845,15 +846,20 @@ public class NewDockerConnectionPage extends WizardPage {
}
}
- private static class ConnectionNameValidator extends MultiValidator {
+ private static abstract class ConnectionNameValidator
+ extends MultiValidator {
private final IObservableValue<String> connectionNameModelObservable;
- public ConnectionNameValidator(
+ ConnectionNameValidator(
final IObservableValue<String> connectionNameModelObservable) {
this.connectionNameModelObservable = connectionNameModelObservable;
}
+ public IObservableValue<String> getConnectionNameModelObservable() {
+ return this.connectionNameModelObservable;
+ }
+
@Override
public IObservableList<IObservableValue<String>> getTargets() {
WritableList<IObservableValue<String>> targets = new WritableList<>();
@@ -877,14 +883,16 @@ public class NewDockerConnectionPage extends WizardPage {
}
}
- private static class UnixSocketValidator extends MultiValidator {
+ private static class UnixSocketValidator extends ConnectionNameValidator {
private final IObservableValue<Boolean> unixSocketBindingModeModelObservable;
private final IObservableValue<String> unixSocketPathModelObservable;
public UnixSocketValidator(
+ final IObservableValue<String> connectionNameModelObservable,
final IObservableValue<Boolean> unixSocketBindingModeModelObservable,
final IObservableValue<String> unixSocketPathModelObservable) {
+ super(connectionNameModelObservable);
this.unixSocketBindingModeModelObservable = unixSocketBindingModeModelObservable;
this.unixSocketPathModelObservable = unixSocketPathModelObservable;
}
@@ -892,45 +900,59 @@ public class NewDockerConnectionPage extends WizardPage {
@Override
public IObservableList<IObservableValue<String>> getTargets() {
WritableList<IObservableValue<String>> targets = new WritableList<>();
+ targets.add(getConnectionNameModelObservable());
targets.add(unixSocketPathModelObservable);
return targets;
}
@Override
protected IStatus validate() {
+ final IStatus connectionNameStatus = super.validate();
final Boolean unixSocketBindingMode = this.unixSocketBindingModeModelObservable
.getValue();
final String unixSocketPath = this.unixSocketPathModelObservable
.getValue();
+ if (!connectionNameStatus.isOK()) {
+ return connectionNameStatus;
+ }
if (unixSocketBindingMode) {
if (unixSocketPath == null || unixSocketPath.isEmpty()) {
return ValidationStatus.error(WizardMessages.getString(
"DockerConnectionPage.validation.missingUnixSocket.msg")); //$NON-NLS-1$
}
try {
+ // validate the Unix socket format
final URI unixSocketURI = new URI(unixSocketPath);
if (unixSocketURI.getScheme() != null
&& !unixSocketURI.getScheme().equals("unix")) { //$NON-NLS-1$
return ValidationStatus.error(WizardMessages.getString(
"DockerConnectionPage.validation.invalidUnixSocketScheme.msg")); //$NON-NLS-1$
}
- if (unixSocketURI.getPath() != null) {
- final File unixSocket = new File(
- unixSocketURI.getPath());
- if (!unixSocket.exists()) {
- return ValidationStatus
- .error(WizardMessages.getString(
- "DockerConnectionPage.validation.invalidUnixSocketPath.msg")); //$NON-NLS-1$
- } else if (!unixSocket.canRead()
- || !unixSocket.canWrite()) {
- return ValidationStatus
- .error(WizardMessages.getString(
- "DockerConnectionPage.validation.unreadableUnixSocket.msg")); //$NON-NLS-1$
- }
- } else {
+ if (unixSocketURI.getPath() == null) {
return ValidationStatus.error(WizardMessages.getString(
"DockerConnectionPage.validation.invalidUnixSocketPath.msg")); //$NON-NLS-1$
-
+ }
+ final File unixSocket = new File(unixSocketURI.getPath());
+ if (!unixSocket.exists()) {
+ return ValidationStatus.error(WizardMessages.getString(
+ "DockerConnectionPage.validation.invalidUnixSocketPath.msg")); //$NON-NLS-1$
+ } else if (!unixSocket.canRead()
+ || !unixSocket.canWrite()) {
+ return ValidationStatus.error(WizardMessages.getString(
+ "DockerConnectionPage.validation.unreadableUnixSocket.msg")); //$NON-NLS-1$
+ }
+ // check for other, existing connections with the same
+ // settings
+ final boolean duplicateConnection = DockerConnectionManager
+ .getInstance().getAllConnections().stream()
+ .filter(c -> c.getSettings()
+ .getType() == BindingType.UNIX_SOCKET_CONNECTION)
+ .map(c -> (UnixSocketConnectionSettings) c
+ .getSettings())
+ .anyMatch(s -> s.getPath().equals(unixSocketPath));
+ if (duplicateConnection) {
+ return ValidationStatus.error(WizardMessages.getString(
+ "DockerConnectionPage.validation.duplicateUnixSocketPath.msg")); //$NON-NLS-1$
}
} catch (URISyntaxException e) {
return ValidationStatus.error(WizardMessages.getString(
@@ -943,14 +965,16 @@ public class NewDockerConnectionPage extends WizardPage {
}
- private static class TcpHostValidator extends MultiValidator {
+ private static class TcpHostValidator extends ConnectionNameValidator {
private final IObservableValue<Boolean> tcpConnectionBindingModeModelObservable;
private final IObservableValue<String> tcpHostModelObservable;
public TcpHostValidator(
+ final IObservableValue<String> connectionNameModelObservable,
final IObservableValue<Boolean> tcpConnectionBindingModeModelObservable,
final IObservableValue<String> tcpHostModelObservable) {
+ super(connectionNameModelObservable);
this.tcpConnectionBindingModeModelObservable = tcpConnectionBindingModeModelObservable;
this.tcpHostModelObservable = tcpHostModelObservable;
}
@@ -958,6 +982,7 @@ public class NewDockerConnectionPage extends WizardPage {
@Override
public IObservableList<IObservableValue<String>> getTargets() {
WritableList<IObservableValue<String>> targets = new WritableList<>();
+ targets.add(getConnectionNameModelObservable());
targets.add(tcpHostModelObservable);
return targets;
}
@@ -967,6 +992,10 @@ public class NewDockerConnectionPage extends WizardPage {
final Boolean tcpConnectionBindingMode = this.tcpConnectionBindingModeModelObservable
.getValue();
final String tcpHost = this.tcpHostModelObservable.getValue();
+ final IStatus connectionNameStatus = super.validate();
+ if (!connectionNameStatus.isOK()) {
+ return connectionNameStatus;
+ }
if (tcpConnectionBindingMode) {
if (tcpHost == null || tcpHost.isEmpty()) {
return ValidationStatus.error(WizardMessages.getString(
@@ -992,6 +1021,18 @@ public class NewDockerConnectionPage extends WizardPage {
"DockerConnectionPage.validation.invalidTcpConnectionPort.msg")); //$NON-NLS-1$
}
+ // check for other, existing connections with the same
+ // settings
+ final boolean duplicateConnection = DockerConnectionManager
+ .getInstance().getAllConnections().stream()
+ .filter(c -> c.getSettings()
+ .getType() == BindingType.TCP_CONNECTION)
+ .map(c -> (TCPConnectionSettings) c.getSettings())
+ .anyMatch(s -> s.getHost().equals(tcpHost));
+ if (duplicateConnection) {
+ return ValidationStatus.error(WizardMessages.getString(
+ "DockerConnectionPage.validation.duplicateUnixSocketPath.msg")); //$NON-NLS-1$
+ }
} catch (URISyntaxException e) {
// URI is not valid
return ValidationStatus.error(WizardMessages.getString(
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties
index 8bd63eafae..e2d85e74bf 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties
@@ -199,12 +199,14 @@ DockerConnectionPage.validation.duplicateConnectionName.msg=A connection with th
DockerConnectionPage.validation.missingUnixSocket.msg=Unix socket path is missing
DockerConnectionPage.validation.invalidUnixSocketScheme.msg=Unix socket scheme is invalid: only "unix" is allowed
DockerConnectionPage.validation.invalidUnixSocketPath.msg=Unix socket path is invalid
+DockerConnectionPage.validation.duplicateUnixSocketPath.msg=A connection with the same Unix socket path already exists
DockerConnectionPage.validation.unreadableUnixSocket.msg=Unix socket path is not readable or writable
DockerConnectionPage.validation.missingTcpConnectionURI.msg=TCP connection URI is missing
DockerConnectionPage.validation.invalidTcpConnectionURI.msg=TCP connection URI is invalid
DockerConnectionPage.validation.invalidTcpConnectionScheme.msg=TCP connection URI scheme is invalid. Only "tcp", "http" and "https" are allowed.
DockerConnectionPage.validation.invalidTcpConnectionHost.msg=TCP connection URI host is missing or invalid
DockerConnectionPage.validation.invalidTcpConnectionPort.msg=TCP connection URI port is missing or invalid
+DockerConnectionPage.validation.duplicateTcpConnectionHost.msg=A connection with the same TCP host path already exists
DockerConnectionPage.validation.missingTcpCertPath.msg=Path to TCP connection certificates is missing
DockerConnectionPage.validation.invalidTcpCertPath.msg=Path to TCP connection certificates is invalid
DockerConnectionPage.validation.unreadableTcpCertPath.msg=Path to TCP connection certificates is not readable

Back to the top