Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 01c0feef729b457e46cef71001c8a3f6cd7ecd65 (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*******************************************************************************
 * Copyright (c) 2016 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.launch;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
import org.eclipse.linuxtools.internal.docker.ui.jobs.JobMessages;
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.ProjectInitializationRule;
import org.eclipse.linuxtools.internal.docker.ui.testutils.RunWithProject;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.ClearConnectionManagerRule;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.ClearLaunchConfigurationsRule;
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.ProjectExplorerViewRule;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.SWTUtils;
import org.eclipse.linuxtools.internal.docker.ui.wizards.WizardMessages;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
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.ProgressHandler;

/**
 * Testing the {@link BuildDockerImageShortcut}
 */
public class BuildDockerImageShortcutSWTBotTest {

	@ClassRule
	public static CloseWelcomePageRule closeWelcomePage = new CloseWelcomePageRule(
			"org.eclipse.linuxtools.docker.ui.perspective");

	@Rule
	public ClearConnectionManagerRule clearConnectionManager = new ClearConnectionManagerRule();

	@Rule
	public ProjectInitializationRule projectInit = new ProjectInitializationRule();

	@Rule
	public ClearLaunchConfigurationsRule clearLaunchConfig = new ClearLaunchConfigurationsRule(
			IBuildDockerImageLaunchConfigurationConstants.CONFIG_TYPE_ID);

	@Rule
	public ProjectExplorerViewRule projectExplorerViewRule = new ProjectExplorerViewRule();

	private SWTWorkbenchBot bot = new SWTWorkbenchBot();

	/**
	 * @return the {@link SWTBotMenu} for the "Run as > Docker Image Build"
	 *         shortcut
	 */
	private SWTBotMenu getRunAsdockerImageBuildContextMenu(final String projectName, final String dockerFileName) {
		final SWTBotTreeItem fooProjectTreeItem = SWTUtils
				.getTreeItem(this.projectExplorerViewRule.getProjectExplorerBotView(), projectName);
		assertThat(fooProjectTreeItem).isNotNull();
		SWTUtils.syncExec(() -> fooProjectTreeItem.expand());
		final SWTBotTreeItem dockerfileTreeItem = SWTUtils.getTreeItem(fooProjectTreeItem, dockerFileName);
		assertThat(dockerfileTreeItem).isNotNull();
		SWTUtils.select(dockerfileTreeItem);
		final SWTBotMenu runAsDockerImageBuildMenu = SWTUtils.getContextMenu(
				this.projectExplorerViewRule.getProjectExplorerBotView().bot().tree(),
				"Run As", "1 Docker Image Build");
		return runAsDockerImageBuildMenu;
	}

	@Test
	@RunWithProject("foo")
	public void shouldDisableCommandOnFirstCallWhenMissingConnection() {
		// given no connection
		ClearConnectionManagerRule.removeAllConnections(DockerConnectionManager.getInstance());
		// when
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect an error dialog because no Docker connection exists
		assertThat(bot.shell(LaunchMessages.getString("BuildDockerImageShortcut.no.connections.msg")))
				.isNotNull();
		// closing the wizard
		SWTUtils.syncExec(() -> {
			bot.button("No").click();
		});
	}

	@Test
	@RunWithProject("foo")
	public void shouldPromptDialogThenBuildDockerImageOnFirstCall()
			throws InterruptedException, com.spotify.docker.client.exceptions.DockerException, IOException {
		// given
		final DockerClient client = MockDockerClientFactory.build();
		final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
		DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
		// when
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect a dialog, fill the "repository" text field and click "Ok"
		assertThat(bot.shell(WizardMessages.getString("ImageBuildDialog.title"))).isNotNull();
		bot.textWithLabel(WizardMessages.getString("ImageBuildDialog.repoNameLabel")).setText("foo/bar:latest");
		// when launching the build
		SWTUtils.syncExec(() -> {
			bot.button("OK").click();
		});
		// then the 'DockerConnection#buildImage(...) method should have been
		// called within the specified timeout
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(3)).times(1)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
	}

	@Test
	@RunWithProject("foo")
	public void shouldBuildDockerImageImmediatelyOnSecondCall()
			throws InterruptedException, com.spotify.docker.client.exceptions.DockerException, IOException {
		// given
		final DockerClient client = MockDockerClientFactory.build();
		final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
		DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
		// when
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect a dialog, fill the "repository" text field and click "Ok"
		assertThat(bot.shell(WizardMessages.getString("ImageBuildDialog.title"))).isNotNull();
		bot.textWithLabel(WizardMessages.getString("ImageBuildDialog.repoNameLabel")).setText("foo/bar:latest");
		// when launching the build
		SWTUtils.syncExec(() -> {
			bot.button("OK").click();
		});
		// then the 'DockerConnection#buildImage(...) method should have been
		// called within the specified timeout
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(3)).times(1)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
		// when trying to call again, there should be no dialog
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then a second call should have been done
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(3)).times(2)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
	}

	@Test
	@RunWithProject("foo")
	public void shouldNotBuildDockerImageOnSecondCallWhenAllConnectionWereRemoved()
			throws InterruptedException, com.spotify.docker.client.exceptions.DockerException, IOException {
		// given
		final DockerClient client = MockDockerClientFactory.build();
		final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
		DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
		// when
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect a dialog, fill the "repository" text field and click "Ok"
		assertThat(bot.shell(WizardMessages.getString("ImageBuildDialog.title"))).isNotNull();
		bot.textWithLabel(WizardMessages.getString("ImageBuildDialog.repoNameLabel")).setText("foo/bar:latest");
		// when launching the build
		SWTUtils.syncExec(() -> {
			bot.button("OK").click();
		});
		// then the 'DockerConnection#buildImage(...) method should have been
		// called within the specified timeout
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(30)).times(1)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
		// when trying to call again after connection was removed, there should
		// be an error dialog
		DockerConnectionManager.getInstance().removeConnection(dockerConnection);
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click(), false);
		final SWTBotShell shell = bot.shell("Edit Configuration");
		assertThat(shell).isNotNull();
		assertThat(shell.bot().button("Run").isEnabled()).isFalse();
		// closing the wizard
		SWTUtils.asyncExec(() -> {
			shell.bot().button(IDialogConstants.CLOSE_LABEL).click();
		}, false);
		// do not save the config while closing
		SWTUtils.syncExec(() -> {
			bot.button(IDialogConstants.NO_LABEL).click();
		});
	}

	@RunWithProject("foo")
	public void shouldPromptForAnotherConnectionWhenBuildingDockerImageOnSecondCallAfterConnectionWasReplaced()
			throws InterruptedException, com.spotify.docker.client.exceptions.DockerException, IOException {
		// given
		final DockerClient client = MockDockerClientFactory.build();
		final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
		DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
		// when
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect a dialog, fill the "repository" text field and click "Ok"
		assertThat(bot.shell(WizardMessages.getString("ImageBuildDialog.title"))).isNotNull();
		bot.textWithLabel(WizardMessages.getString("ImageBuildDialog.repoNameLabel")).setText("foo/bar:latest");
		// when launching the build
		SWTUtils.syncExec(() -> {
			bot.button("OK").click();
		});
		// then the 'DockerConnection#buildImage(...) method should have been
		// called within the specified timeout
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(3)).times(1)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
		// when trying to call again after connection was replaced, there should
		// be an error dialog
		final DockerConnection dockerConnection2 = MockDockerConnectionFactory.from("Test 2", client).withDefaultTCPConnectionSettings();
		DockerConnectionManagerUtils.configureConnectionManager(dockerConnection2);
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect a dialog, fill the "repository" text field and click "Ok"
		assertThat(bot.shell(WizardMessages.getString("ImageBuildDialog.title"))).isNotNull();
		bot.textWithLabel(WizardMessages.getString("ImageBuildDialog.repoNameLabel")).setText("foo/bar:latest");
		// when launching the build
		SWTUtils.syncExec(() -> {
			bot.button("OK").click();
		});
		// then the 'DockerConnection#buildImage(...) method should have been
		// called within the specified timeout
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(3)).times(2)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
	}

	@Test
	@RunWithProject("foo")
	public void shouldNotBuildDockerImageOnSecondCallWhenDockerfileWasRemoved()
			throws InterruptedException, com.spotify.docker.client.exceptions.DockerException, IOException, CoreException {
		// given
		final DockerClient client = MockDockerClientFactory.build();
		final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client)
				.withDefaultTCPConnectionSettings();
		DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
		// when
		SWTUtils.asyncExec(() -> getRunAsdockerImageBuildContextMenu("foo", "Dockerfile").click());
		// then expect a dialog, fill the "repository" text field and click "Ok"
		assertThat(bot.shell(WizardMessages.getString("ImageBuildDialog.title"))).isNotNull();
		bot.textWithLabel(WizardMessages.getString("ImageBuildDialog.repoNameLabel")).setText("foo/bar:latest");
		// when launching the build
		SWTUtils.syncExec(() -> {
			bot.button("OK").click();
		});
		// then the 'DockerConnection#buildImage(...) method should have been
		// called within the specified timeout
		Mockito.verify(client, Mockito.timeout((int) TimeUnit.SECONDS.toMillis(30)).times(1)).build(
				Matchers.any(Path.class), Matchers.any(String.class), Matchers.any(ProgressHandler.class),
				Matchers.anyVararg());
		// when trying to call again after file was removed, there should
		// be an error dialog
		projectInit.getProject().findMember("Dockerfile").delete(true, new NullProgressMonitor());
		bot.toolbarDropDownButtonWithTooltip("Run").menuItem("1 foo_bar [latest]").click();
		final SWTBotShell shell = bot.shell(JobMessages.getString("BuildImageJob.title")); //$NON-NLS-1$
		assertThat(shell).isNotNull();
		// closing the dialog
		SWTUtils.syncExec(() -> {
			shell.bot().button(IDialogConstants.OK_LABEL).click();
		});
	}

}

Back to the top