Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3f8c0303bee0b85f146059cf9e0e6a23888bfc72 (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
/*******************************************************************************
 * Copyright (c) 2017 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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
 *
 * Contributor:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

package org.eclipse.linuxtools.docker.integration.tests.container;

import static org.junit.Assert.assertTrue;

import org.eclipse.linuxtools.docker.integration.tests.image.AbstractImageBotTest;
import org.eclipse.linuxtools.docker.integration.tests.mock.MockDockerConnectionManager;
import org.eclipse.linuxtools.docker.reddeer.condition.ContainerIsDeployedCondition;
import org.eclipse.linuxtools.docker.reddeer.core.ui.wizards.ImageRunNetworkPage;
import org.eclipse.linuxtools.docker.reddeer.core.ui.wizards.ImageRunResourceVolumesVariablesPage;
import org.eclipse.linuxtools.docker.reddeer.core.ui.wizards.ImageRunSelectionPage;
import org.eclipse.linuxtools.docker.reddeer.ui.DockerExplorerView;
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.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.eclipse.ui.views.properties.PropertySheet;
import org.eclipse.reddeer.workbench.core.condition.JobIsRunning;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerException;

/**
 *
 * @author jkopriva@redhat.com
 *
 */
public class NetworkModeTest extends AbstractImageBotTest {

	private static final String IMAGE_NAME = "docker.io/" + IMAGE_BUSYBOX;
	private static final String IMAGE_TAG = IMAGE_TAG_LATEST;
	private static final String CONTAINER_NAME = "test_run_busybox";
	private static final String NETWORK_MODE_DEFAULT = "default";
	private static final String NETWORK_MODE_BRIDGE = "bridge";
	private static final String NETWORK_MODE_HOST = "host";
	private static final String NETWORK_MODE_NONE = "none";

	ImageRunSelectionPage firstPage;

	@Before
	public void before() throws DockerException, InterruptedException {
		deleteAllConnections();
		getConnection();
		pullImage(IMAGE_NAME, IMAGE_TAG);
		new WaitWhile(new JobIsRunning());
		DockerExplorerView explorer = new DockerExplorerView();
		getConnection().getImage(IMAGE_NAME).run();
		firstPage = new ImageRunSelectionPage(explorer);
		firstPage.setContainerName(CONTAINER_NAME);
		firstPage.setAllocatePseudoTTY();
		firstPage.setKeepSTDINOpen();
		firstPage.next();
		ImageRunResourceVolumesVariablesPage variablesPage = new ImageRunResourceVolumesVariablesPage(firstPage);
		variablesPage.next();
	}

	@Test
	public void testDefaultMode() {
		ImageRunNetworkPage networkPage = new ImageRunNetworkPage(firstPage);
		networkPage.setDefaultNetworkMode();
		networkPage.finish();
		checkNetworkMode(NETWORK_MODE_DEFAULT);
	}

	@Test
	public void testBridgeMode() {
		ImageRunNetworkPage networkPage = new ImageRunNetworkPage(firstPage);
		networkPage.setBridgeNetworkMode();
		networkPage.finish();
		checkNetworkMode(NETWORK_MODE_BRIDGE);
	}

	@Test
	public void testHostMode() {
		ImageRunNetworkPage networkPage = new ImageRunNetworkPage(firstPage);
		networkPage.setHostNetworkMode();
		networkPage.finish();
		checkNetworkMode(NETWORK_MODE_HOST);
	}

	@Test
	public void testNoneMode() {
		ImageRunNetworkPage networkPage = new ImageRunNetworkPage(firstPage);
		networkPage.setNoneNetworkMode();
		networkPage.finish();
		checkNetworkMode(NETWORK_MODE_NONE);
	}

	@Override
	@After
	public void after() {
		deleteContainerIfExists(CONTAINER_NAME);
	}

	private void runContainer(String networkMode) {
		final DockerClient client = MockDockerClientFactory
				.container(MockContainerFactory.name(CONTAINER_NAME).status("Stopped").build(),
						MockContainerInfoFactory.link(IMAGE_NAME + ":" + IMAGE_TAG_LATEST).networkMode(networkMode)
								.id("TestTestTestTestTest").ipAddress("127.0.0.1").build())
				.image(MockImageFactory.id("987654321abcde").name(IMAGE_UHTTPD + ":" + IMAGE_TAG_LATEST).build())
				.build();
		final org.eclipse.linuxtools.internal.docker.core.DockerConnection dockerConnection = MockDockerConnectionFactory
				.from(DEFAULT_CONNECTION_NAME, client).withDefaultTCPConnectionSettings();
		MockDockerConnectionManager.configureConnectionManager(dockerConnection);
	}

	private void checkNetworkMode(String networkMode) {
		if (mockitoIsUsed()) {
			runContainer(networkMode);
			getConnection().refresh();
			new WaitUntil(new ContainerIsDeployedCondition(CONTAINER_NAME, getConnection()));
		}
		new WaitWhile(new JobIsRunning());
		PropertySheet propertiesView = openPropertiesTabForContainer("Inspect", CONTAINER_NAME);
		String networkProp = propertiesView.getProperty("HostConfig", "NetworkMode").getPropertyValue();
		assertTrue("Container is not running in " + networkMode + " network mode!", networkProp.equals(networkMode));
	}
}

Back to the top