1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*******************************************************************************
* Copyright (c) 2015 Red Hat.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.linuxtools.internal.docker.ui.wizards;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
import org.eclipse.linuxtools.internal.docker.ui.launch.IRunDockerImageLaunchConfigurationConstants;
import org.eclipse.linuxtools.internal.docker.ui.launch.LaunchConfigurationUtils;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockContainerFactory;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockContainerInfoFactory;
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.MockImageFactory;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockImageInfoFactory;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.ButtonAssertion;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.ClearConnectionManagerRule;
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.SWTUtils;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerView;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.ContainerInfo;
import com.spotify.docker.client.messages.Image;
import com.spotify.docker.client.messages.ImageInfo;
/**
* Testing the {@link ImageRun} wizard
*/
public class ImageRunSWTBotTest {
private SWTWorkbenchBot bot = new SWTWorkbenchBot();
private DockerExplorerView dockerExplorerView;
private SWTBotView dockerExplorerViewBot;
@ClassRule
public static CloseWelcomePageRule closeWelcomePage = new CloseWelcomePageRule(
CloseWelcomePageRule.DOCKER_PERSPECTIVE_ID);
@Rule
public ClearConnectionManagerRule clearConnectionManager = new ClearConnectionManagerRule();
@Rule
public CloseShellRule closeShell = new CloseShellRule(IDialogConstants.CANCEL_LABEL);
@Before
public void lookupDockerExplorerView() {
dockerExplorerViewBot = bot.viewById("org.eclipse.linuxtools.docker.ui.dockerExplorerView");
dockerExplorerView = (DockerExplorerView) (dockerExplorerViewBot.getViewReference().getView(true));
dockerExplorerViewBot.show();
dockerExplorerViewBot.setFocus();
}
@Test
public void shouldReportErrorIfContainerWithSameNameExists() {
// given
final DockerClient client = MockDockerClientFactory.image(MockImageFactory.name("foo:latest").build())
.container(MockContainerFactory.name("foo_bar").build()).build();
final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
// expand the 'Images' node and select the 'foo' image
SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Images", "foo").select();
// when opening the "Run Image..." wizard
final SWTBotTree dockerExplorerViewTreeBot = dockerExplorerViewBot.bot().tree();
dockerExplorerViewTreeBot.contextMenu("Run...").click();
// when use an existing container name
bot.textWithLabel(WizardMessages.getString("ImageRunSelectionPage.containerName")).setText("foo_bar");
// then
// wait for https://bugs.eclipse.org/bugs/show_bug.cgi?id=482889 to be
// able to check the wizard validation message
//assertThat(bot.text(WizardMessages.getString("ImageRunSelectionPage.containerWithSameName"))).isNotNull();
ButtonAssertion.assertThat(bot.button("Finish")).isNotEnabled();
}
@Test
public void shouldNotReportErrorIfNoContainerWithSameNameExists() {
// given
final DockerClient client = MockDockerClientFactory.image(MockImageFactory.name("foo:latest").build())
.container(MockContainerFactory.name("foo_bar").build()).build();
final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
// expand the 'Images' node and select the 'foo' image
SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Images", "foo").select();
// when opening the "Run Image..." wizard
final SWTBotTree dockerExplorerViewTreeBot = dockerExplorerViewBot.bot().tree();
dockerExplorerViewTreeBot.contextMenu("Run...").click();
// when use an existing container name
bot.textWithLabel(WizardMessages.getString("ImageRunSelectionPage.containerName")).setText("foo_bar_baz");
// then
assertThat(bot.button("Finish").isEnabled()).isEqualTo(true);
}
@Test
public void shouldCreateLaunchConfigurationWhenRunningNamedContainer()
throws InterruptedException, DockerException, CoreException {
// image to use
final String imageName = "foo/bar:latest";
final Image image = MockImageFactory.id("1a2b3c4d5e6f7g").name(imageName).build();
final ImageInfo imageInfo = MockImageInfoFactory.volume("/foo/bar")
.command(Arrays.asList("the", "command")).entrypoint(Arrays.asList("the", "entrypoint")).build();
// container to be created
final String containerName = "foo_bar";
final Container createdContainer = MockContainerFactory.id("MockContainer").name(containerName)
.imageName("1a2b3c4d5e6f7g").status("Started 1 second ago").build();
final ContainerInfo containerInfo = MockContainerInfoFactory.build();
final DockerClient client = MockDockerClientFactory.image(image, imageInfo)
//.container(createdContainer, containerInfo)
.build();
// expected response when creating the container
final ContainerCreation containerCreation = Mockito.mock(ContainerCreation.class);
Mockito.when(containerCreation.id()).thenReturn("MockContainer");
Mockito.when(client.createContainer(Matchers.any(), Matchers.any())).thenReturn(containerCreation);
final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
// configure the Connection Manager
DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
// when select image and click on run to open the wizard
SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Images", "foo/bar").select();
dockerExplorerViewBot.bot().tree().contextMenu("Run...").click();
bot.waitUntil(Conditions.shellIsActive("Run a Docker Image"), TimeUnit.SECONDS.toMillis(1)); //$NON-NLS-1$
// configure container
bot.text(0).setText(containerName);
// bot.button("Next >").click();
// update the client to make sure the container exists once the call to "Finish" is done
MockDockerClientFactory.addContainer(client, createdContainer, containerInfo);
bot.button("Finish").click();
// wait for background job to complete
SWTUtils.waitForJobsToComplete();
// then
// check that the client was called
Mockito.verify(client).createContainer(Matchers.any(), Matchers.eq(containerName));
// check that a launch configuration was created
final ILaunchConfiguration launchConfiguration = LaunchConfigurationUtils.getLaunchConfigurationByName(
IRunDockerImageLaunchConfigurationConstants.CONFIG_TYPE_ID, "foo_bar_latest");
assertThat(launchConfiguration).isNotNull();
}
}
|