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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/*******************************************************************************
* 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.views;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.linuxtools.docker.core.IDockerContainer;
import org.eclipse.linuxtools.docker.core.IDockerImage;
import org.eclipse.linuxtools.docker.core.IDockerImageHierarchyNode;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockContainerFactory;
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.swt.ClearConnectionManagerRule;
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.DockerImageHierarchyViewAssertions;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.MenuAssertion;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.SWTUtils;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.TabDescriptorAssertions;
import org.eclipse.linuxtools.internal.docker.ui.testutils.swt.TestLoggerRule;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.PropertySheet;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.Image;
/**
* Testing the {@link DockerImageHierarchyView} call from the
* {@link DockerExplorerView}.
*/
public class DockerImageHierarchyViewSWTBotTest {
private SWTWorkbenchBot bot = new SWTWorkbenchBot();
private SWTBotView dockerExplorerViewBot;
private DockerExplorerView dockerExplorerView;
@ClassRule
public static CloseWelcomePageRule closeWelcomePage = new CloseWelcomePageRule(
CloseWelcomePageRule.DOCKER_PERSPECTIVE_ID);
@Rule
public TestLoggerRule watcher = new TestLoggerRule();
@Rule
public ClearConnectionManagerRule clearConnectionManager = new ClearConnectionManagerRule();
private DockerConnection connection;
@Before
public void setupViews() {
this.bot = new SWTWorkbenchBot();
SWTUtils.asyncExec(() -> {
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.showView(DockerExplorerView.VIEW_ID);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Failed to open Docker Explorer view: " + e.getMessage());
}
});
this.dockerExplorerViewBot = bot.viewById(DockerExplorerView.VIEW_ID);
this.dockerExplorerView = (DockerExplorerView) (dockerExplorerViewBot.getViewReference().getView(true));
// make sure that the Docker Image Hierarchy view is closed
this.bot.views().stream().filter(v -> v.getReference().getId().equals(DockerImageHierarchyView.VIEW_ID))
.forEach(v -> v.close());
}
@Before
public void setupData() {
// data is built as follows:
// root_image
// |- foo_image1
// _|- foo_container1 (Up)
// _|- foo_image2
// __|- foo_container21 (Exited)
// __|- foo_container22 (Paused)
// |- bar_image11
// _|- bar_container1
final Image rootImage = MockImageFactory.id("sha256:root_image").name("root_image").build();
final Image fooImage1 = MockImageFactory.id("sha256:foo_image1").name("foo_image1")
.parentId("sha256:root_image").build();
final Image fooImage2 = MockImageFactory.id("sha256:foo_image2").name("foo_image2", "foo_image2_alias")
.parentId("sha256:foo_image1").build();
final Container fooContainer1 = MockContainerFactory.id("sha256:foo_container1").name("foo_container1")
.imageName("foo_image1").status("Up").build();
final Container fooContainer21 = MockContainerFactory.id("sha256:foo_container21").name("foo_container21")
.imageName("foo_image2").status("Exited").build();
final Container fooContainer22 = MockContainerFactory.id("sha256:foo_container22").name("foo_container22")
.imageName("foo_image2_alias").status("Up (Paused)").build();
final Image barImage1 = MockImageFactory.id("sha256:bar_image1").name("bar_image1")
.parentId("sha256:root_image").build();
final Container barContainer1 = MockContainerFactory.id("sha256:bar_container1").name("bar_container1")
.imageName("bar_image1").build();
final DockerClient client = MockDockerClientFactory.image(rootImage).image(fooImage1).container(fooContainer1)
.image(fooImage2).container(fooContainer21).container(fooContainer22).image(barImage1)
.container(barContainer1).build();
this.connection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
this.connection.getImages(true);
this.connection.getContainers(true);
DockerConnectionManagerUtils.configureConnectionManager(connection);
}
@After
public void hideMenu() {
try {
SWTUtils.hideMenu(dockerExplorerViewBot.bot().tree());
} catch (WidgetNotFoundException e) {
// ignore if widget is not found, that's probably because there's no
// tree in the
// Docker Explorer view for the test that just ran.
}
}
private List<String> getChildrenElementIds(final IDockerImageHierarchyNode fooImageHierarchy) {
return fooImageHierarchy.getChildren().stream().map(e -> {
if (e.getElement() instanceof IDockerImage) {
return ((IDockerImage) e.getElement()).id();
}
return ((IDockerContainer) e.getElement()).id();
}).collect(Collectors.toList());
}
private DockerImageHierarchyView getDockerImageHierarchyView() {
final SWTBotView hierarchyViewBot = bot.viewById(DockerImageHierarchyView.VIEW_ID);
return (DockerImageHierarchyView) hierarchyViewBot.getViewReference().getView(true);
}
private static SWTBotTreeItem selectImageInTreeView(final SWTWorkbenchBot bot, final String... path) {
final SWTBotView dockerImageHierarchyViewBot = bot.viewById(DockerImageHierarchyView.VIEW_ID);
final DockerImageHierarchyView dockerImageHierarchyView = (DockerImageHierarchyView) (dockerImageHierarchyViewBot
.getViewReference().getView(true));
SWTUtils.asyncExec(() -> dockerImageHierarchyView.getCommonViewer().expandAll());
// when a second call to expand the container is done (because the first
// expandAll stopped with a "Loading..." job that retrieved the
// containers)
return SWTUtils.getTreeItem(dockerImageHierarchyViewBot, path).select();
}
@Test
public void shouldDisplayImageHierarchyViewWhenSelectingImage() {
// given
SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Images", "foo_image1").select();
// when
dockerExplorerViewBot.bot().tree().contextMenu("Open Image Hierarchy").click(); //$NON-NLS-1$
// then the view should be visible and selection should be on
// foo_image1
DockerImageHierarchyViewAssertions.assertThat(getDockerImageHierarchyView())
.hasSelectedElement(this.connection.getImage("sha256:foo_image1"));
}
@Test
public void shouldDisplayImageHierarchyViewWhenSelectingImageAlias() {
// given
SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Images", "foo_image2_alias").select();
// when
dockerExplorerViewBot.bot().tree().contextMenu("Open Image Hierarchy").click(); //$NON-NLS-1$
// then the view should be visible and selection should be on
// foo_image2
DockerImageHierarchyViewAssertions.assertThat(getDockerImageHierarchyView())
.hasSelectedElement(this.connection.getImage("sha256:foo_image2"));
}
@Test
public void shouldDisplayImageHierarchyViewWhenSelectingContainer() {
// given
SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Containers", "foo_container1").select();
// when
dockerExplorerViewBot.bot().tree().contextMenu("Open Image Hierarchy").click(); //$NON-NLS-1$
// then the view should be visible and selection should be on
// foo_container1
DockerImageHierarchyViewAssertions.assertThat(SWTUtils.getView(this.bot, DockerImageHierarchyView.VIEW_ID))
.hasSelectedElement(this.connection.getContainer("sha256:foo_container1"));
}
@Test
public void shouldRetrieveImageHierarchyFromRootImage() {
// given
final IDockerImage rootImage = this.connection.getImage("sha256:root_image");
// when
final IDockerImageHierarchyNode rootImageHierarchy = this.connection.resolveImageHierarchy(rootImage);
// then
assertThat(rootImageHierarchy).isNotNull();
assertThat(rootImageHierarchy.getElement()).isEqualTo(rootImage);
// 2 direct children: foo_image1 and bar_image1
assertThat(rootImageHierarchy.getChildren()).hasSize(2);
assertThat(rootImageHierarchy.getParent()).isNull();
final IDockerImageHierarchyNode fooImage1Hierarchy = rootImageHierarchy.getChild("sha256:foo_image1");
assertThat(fooImage1Hierarchy.getParent()).isEqualTo(rootImageHierarchy);
// 2 direct children: foo_image2/foo_image2_alias and foo_container1
assertThat(fooImage1Hierarchy.getChildren()).hasSize(2);
}
@Test
public void shouldRetrieveImageHierarchyFromIntermediateImage() {
// given
final IDockerImage fooImage1 = this.connection.getImage("sha256:foo_image1");
// when
final IDockerImageHierarchyNode fooImage1Hierarchy = this.connection.resolveImageHierarchy(fooImage1);
// then
assertThat(fooImage1Hierarchy).isNotNull();
assertThat(fooImage1Hierarchy.getElement()).isEqualTo(fooImage1);
assertThat(getChildrenElementIds(fooImage1Hierarchy)).contains("sha256:foo_container1", "sha256:foo_image2");
final IDockerImage rootElement = (IDockerImage) fooImage1Hierarchy.getParent().getElement();
assertThat(rootElement.id()).isEqualTo("sha256:root_image");
// the parent only shows this child element, not its whole descendants
assertThat(fooImage1Hierarchy.getParent().getChildren()).containsExactly(fooImage1Hierarchy);
final IDockerImageHierarchyNode fooImage2HierarchyNode = fooImage1Hierarchy.getChild("sha256:foo_image2");
assertThat(fooImage2HierarchyNode).isNotNull();
// 2 child containers: foo_container21 and foo_container22
assertThat(fooImage2HierarchyNode.getChildren()).hasSize(2);
final IDockerImageHierarchyNode fooContainer1HierarchyNode = fooImage1Hierarchy
.getChild("sha256:foo_container1");
assertThat(fooContainer1HierarchyNode).isNotNull();
assertThat(fooContainer1HierarchyNode.getChildren()).isEmpty();
}
@Test
public void shouldRetrieveImageHierarchyFromLeafImage() {
// given
final IDockerImage fooImage2 = this.connection.getImage("sha256:foo_image2");
// when
final IDockerImageHierarchyNode fooImage2Hierarchy = this.connection.resolveImageHierarchy(fooImage2);
// then
assertThat(fooImage2Hierarchy).isNotNull();
assertThat(fooImage2Hierarchy.getElement()).isEqualTo(fooImage2);
// 2 containers: foo_container21 and foo_container22
assertThat(fooImage2Hierarchy.getChildren()).hasSize(2);
assertThat((IDockerContainer) fooImage2Hierarchy.getChild("sha256:foo_container21").getElement()).isNotNull();
final IDockerImageHierarchyNode fooImage1Hierarchy = fooImage2Hierarchy.getParent();
final IDockerImage fooImage = (IDockerImage) fooImage1Hierarchy.getElement();
assertThat(fooImage.id()).isEqualTo("sha256:foo_image1");
// in this case, intermediate images shows a single child
assertThat(fooImage1Hierarchy.getChildren()).hasSize(1);
assertThat(fooImage1Hierarchy.getChildren()).containsExactly(fooImage2Hierarchy);
}
@Test
public void shouldRetrieveImageHierarchyFromContainerBasedOnIntermediateImage() {
// given
final IDockerContainer fooContainer1 = this.connection.getContainer("sha256:foo_container1");
// when
final IDockerImageHierarchyNode fooContainer1Hierarchy = this.connection.resolveImageHierarchy(fooContainer1);
// then
assertThat(fooContainer1Hierarchy).isNotNull();
assertThat(fooContainer1Hierarchy.getElement()).isEqualTo(fooContainer1);
assertThat(fooContainer1Hierarchy.getChildren()).isEmpty();
final IDockerImage fooImage1 = (IDockerImage) fooContainer1Hierarchy.getParent().getElement();
assertThat(fooImage1.id()).isEqualTo("sha256:foo_image1");
// parent images hierarchy only shows the selected container as its
// child.
assertThat(fooContainer1Hierarchy.getParent().getChildren()).containsExactly(fooContainer1Hierarchy);
}
@Test
public void shouldRetrieveImageHierarchyFromContainerBasedOnLeafImage() {
// given
// when
final IDockerContainer barContainer1 = this.connection.getContainer("sha256:bar_container1");
final IDockerImageHierarchyNode barContainer1Hierarchy = this.connection.resolveImageHierarchy(barContainer1);
// then
assertThat(barContainer1Hierarchy).isNotNull();
assertThat(barContainer1Hierarchy.getElement()).isEqualTo(barContainer1);
assertThat(barContainer1Hierarchy.getChildren()).isEmpty();
final IDockerImage barImageElement = (IDockerImage) barContainer1Hierarchy.getParent().getElement();
assertThat(barImageElement.id()).isEqualTo("sha256:bar_image1");
}
@Test
public void shouldShowSelectedImageInPropertiesView() {
// given
final PropertySheet propertySheet = SWTUtils
.syncExec(() -> SWTUtils.getView(bot, "org.eclipse.ui.views.PropertySheet", true));
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root");
// show container info in Properties view
SWTUtils.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Show In", "Properties")
.click();
// the properties view should be visible and filled with images details
assertThat(propertySheet.getCurrentPage()).isInstanceOf(TabbedPropertySheetPage.class);
final TabbedPropertySheetPage currentPage = (TabbedPropertySheetPage) propertySheet.getCurrentPage();
TabDescriptorAssertions.assertThat(currentPage.getSelectedTab()).isNotNull()
.hasId("org.eclipse.linuxtools.docker.ui.properties.image.info");
}
@Test
public void shouldShowSelectedContainerInPropertiesView() {
// given
final PropertySheet propertySheet = SWTUtils
.syncExec(() -> SWTUtils.getView(bot, "org.eclipse.ui.views.PropertySheet", true));
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_container1");
// show container info in Properties view
SWTUtils.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Show In", "Properties")
.click();
assertThat(propertySheet.getCurrentPage()).isInstanceOf(TabbedPropertySheetPage.class);
final TabbedPropertySheetPage currentPage = (TabbedPropertySheetPage) propertySheet.getCurrentPage();
TabDescriptorAssertions.assertThat(currentPage.getSelectedTab()).isNotNull()
.hasId("org.eclipse.linuxtools.docker.ui.properties.container.info");
}
@Test
public void shouldProvideEnabledRemoveCommandOnSelectedImage() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Remove");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledAddTagCommandOnSelectedImage() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Add Tag");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledPushCommandOnSelectedImage() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Push...");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledStartCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_image2", "foo_container21");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Start");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledPauseCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_container1");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Pause");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledUnpauseCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_image2", "foo_container22");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Unpause");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledKillCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_container1");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Kill");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledRemoveCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_image2", "foo_container21");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Remove");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledCommitCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_image2", "foo_container21");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Commit");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledDisplayLogCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_image2", "foo_container21");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Display Log");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
@Test
public void shouldProvideEnabledRemoveLogCommandOnSelectedContainer() {
// given
shouldDisplayImageHierarchyViewWhenSelectingImage();
// when
selectImageInTreeView(bot, "root", "foo_image1", "foo_image2", "foo_container21");
final SWTBotMenu menu = SWTUtils
.getContextMenu(bot.viewById(DockerImageHierarchyView.VIEW_ID).bot().tree(), "Remove Log");
// then
MenuAssertion.assertThat(menu).isVisible().isEnabled();
}
}
|