forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushImageCmdIT.java
More file actions
129 lines (102 loc) · 4.65 KB
/
Copy pathPushImageCmdIT.java
File metadata and controls
129 lines (102 loc) · 4.65 KB
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
package com.github.dockerjava.cmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.core.RemoteApiVersion;
import com.github.dockerjava.junit.PrivateRegistryRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static com.github.dockerjava.utils.TestUtils.getVersion;
import static com.github.dockerjava.utils.TestUtils.isNotSwarm;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;
public class PushImageCmdIT extends CmdIT {
public static final Logger LOG = LoggerFactory.getLogger(PushImageCmdIT.class);
@ClassRule
public static PrivateRegistryRule REGISTRY = new PrivateRegistryRule();
@Rule
public ExpectedException exception = ExpectedException.none();
private AuthConfig authConfig;
@Before
public void beforeTest() throws Exception {
authConfig = REGISTRY.getAuthConfig();
}
@Test
public void pushLatest() throws Exception {
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true").exec();
LOG.info("Created container {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
LOG.info("Committing container: {}", container.toString());
String imgName = authConfig.getRegistryAddress() + "/push-latest";
String imageId = dockerRule.getClient().commitCmd(container.getId())
.withRepository(imgName)
.exec();
// we have to block until image is pushed
dockerRule.getClient().pushImageCmd(imgName)
.withAuthConfig(authConfig)
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
LOG.info("Removing image: {}", imageId);
dockerRule.getClient().removeImageCmd(imageId).exec();
dockerRule.getClient().pullImageCmd(imgName)
.withTag("latest")
.withAuthConfig(authConfig)
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
}
@Test
public void pushNonExistentImage() throws Exception {
if (isNotSwarm(dockerRule.getClient()) && getVersion(dockerRule.getClient())
.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
exception.expect(DockerClientException.class);
} else {
exception.expect(NotFoundException.class);
}
dockerRule.getClient().pushImageCmd(UUID.randomUUID().toString().replace("-", ""))
.start()
.awaitCompletion(30, TimeUnit.SECONDS); // exclude infinite await sleep
}
@Test
public void testPushImageWithValidAuth() throws Exception {
String imgName = REGISTRY.createTestImage("push-image-with-valid-auth");
// stream needs to be fully read in order to close the underlying connection
dockerRule.getClient().pushImageCmd(imgName)
.withAuthConfig(authConfig)
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
}
@Test
public void testPushImageWithNoAuth() throws Exception {
String imgName = REGISTRY.createTestImage("push-image-with-no-auth");
exception.expect(DockerClientException.class);
// stream needs to be fully read in order to close the underlying connection
dockerRule.getClient().pushImageCmd(imgName)
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
}
@Test
public void testPushImageWithInvalidAuth() throws Exception {
AuthConfig invalidAuthConfig = new AuthConfig()
.withUsername("testuser")
.withPassword("testwrongpassword")
.withEmail("foo@bar.de")
.withRegistryAddress(authConfig.getRegistryAddress());
String imgName = REGISTRY.createTestImage("push-image-with-invalid-auth");
exception.expect(DockerClientException.class);
// stream needs to be fully read in order to close the underlying connection
dockerRule.getClient().pushImageCmd(imgName)
.withAuthConfig(invalidAuthConfig)
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
}
}