Skip to content

Commit 263d4a8

Browse files
authored
Merge pull request amihaiemil#140 from llorllale/127
(amihaiemil#127) Image.run()
2 parents ba7aedb + 0753842 commit 263d4a8

File tree

6 files changed

+134
-25
lines changed

6 files changed

+134
-25
lines changed

src/main/java/com/amihaiemil/docker/Image.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,13 @@ public interface Image extends JsonObject {
7676
void tag(
7777
String repo, String name
7878
) throws IOException, UnexpectedResponseException;
79+
80+
/**
81+
* Run this image.
82+
* @return The container for the running image.
83+
* @throws IOException If something goes wrong.
84+
* @throws UnexpectedResponseException If the status response is not
85+
* the expected one.
86+
*/
87+
Container run() throws IOException, UnexpectedResponseException;
7988
}

src/main/java/com/amihaiemil/docker/RtDocker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public final Containers containers() {
7878
@Override
7979
public final Images images() {
8080
return new RtImages(
81-
this.client, URI.create(this.baseUri.toString() + "/images")
81+
this.client, URI.create(this.baseUri.toString() + "/images"), this
8282
);
8383
}
8484

src/main/java/com/amihaiemil/docker/RtImage.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,27 @@ final class RtImage extends JsonResource implements Image {
5151
*/
5252
private final URI baseUri;
5353

54+
/**
55+
* Docker API.
56+
*/
57+
private final Docker docker;
58+
5459
/**
5560
* Ctor.
5661
* @param rep JsonObject representation of this Image.
5762
* @param client The http client.
5863
* @param uri The URI for this image.
64+
* @param dkr The docker entry point.
65+
* @checkstyle ParameterNumber (5 lines)
5966
*/
60-
RtImage(final JsonObject rep, final HttpClient client, final URI uri) {
67+
RtImage(
68+
final JsonObject rep, final HttpClient client,
69+
final URI uri, final Docker dkr
70+
) {
6171
super(rep);
6272
this.client = client;
6373
this.baseUri = uri;
74+
this.docker = dkr;
6475
}
6576

6677
@Override
@@ -77,7 +88,8 @@ public Iterable<Image> history() {
7788
json -> new RtImage(
7889
json,
7990
this.client,
80-
this.baseUri
91+
this.baseUri,
92+
this.docker
8193
)
8294
);
8395
}
@@ -115,4 +127,15 @@ public void tag(
115127
tag.releaseConnection();
116128
}
117129
}
130+
131+
@Override
132+
public Container run() throws IOException, UnexpectedResponseException {
133+
final Container container = this.docker.containers().create(
134+
this.baseUri.getPath().substring(
135+
this.baseUri.getPath().lastIndexOf('/') + 1
136+
)
137+
);
138+
container.start();
139+
return container;
140+
}
118141
}

src/main/java/com/amihaiemil/docker/RtImages.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,21 @@ final class RtImages implements Images {
5353
*/
5454
private final URI baseUri;
5555

56+
/**
57+
* Docker API.
58+
*/
59+
private final Docker docker;
60+
5661
/**
5762
* Ctor.
5863
* @param client The http client.
5964
* @param uri The URI for this Images API.
65+
* @param dkr The docker entry point.
6066
*/
61-
RtImages(final HttpClient client, final URI uri) {
67+
RtImages(final HttpClient client, final URI uri, final Docker dkr) {
6268
this.client = client;
6369
this.baseUri = uri;
70+
this.docker = dkr;
6471
}
6572

6673
@Override
@@ -83,7 +90,8 @@ public Image pull(
8390
this.client,
8491
URI.create(
8592
this.baseUri.toString() + "/" + name
86-
)
93+
),
94+
this.docker
8795
);
8896
} finally {
8997
create.releaseConnection();
@@ -125,7 +133,8 @@ public Iterator<Image> iterator() {
125133
this.client,
126134
URI.create(
127135
this.baseUri.toString() + "/" + json.getString("Id")
128-
)
136+
),
137+
this.docker
129138
)
130139
);
131140
}

src/test/java/com/amihaiemil/docker/RtImageTestCase.java

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
import com.amihaiemil.docker.mock.Condition;
3030
import com.amihaiemil.docker.mock.Response;
3131
import java.net.URI;
32+
import java.util.concurrent.atomic.AtomicBoolean;
3233
import javax.json.Json;
3334
import javax.json.JsonObject;
3435
import org.apache.http.HttpStatus;
36+
import org.apache.http.client.HttpClient;
3537
import org.hamcrest.MatcherAssert;
3638
import org.hamcrest.Matchers;
3739
import org.junit.Test;
40+
import org.mockito.Mockito;
3841

3942
/**
4043
* Unit tests for RtImage.
@@ -44,6 +47,10 @@
4447
* @checkstyle MethodName (500 lines)
4548
*/
4649
public final class RtImageTestCase {
50+
/**
51+
* Mock docker.
52+
*/
53+
private static final Docker DOCKER = Mockito.mock(Docker.class);
4754

4855
/**
4956
* RtImage can return info about itself.
@@ -72,7 +79,8 @@ public void inspectsItself() throws Exception {
7279
req -> req.getRequestLine().getUri().endsWith("/456/json")
7380
)
7481
),
75-
URI.create("http://localhost:80/1.30/images/456")
82+
URI.create("http://localhost:80/1.30/images/456"),
83+
DOCKER
7684
);
7785
final JsonObject info = image.inspect();
7886
MatcherAssert.assertThat(info.keySet(), Matchers.hasSize(4));
@@ -105,7 +113,8 @@ public void returnsHistory() {
105113
Json.createArrayBuilder().build().toString()
106114
)
107115
),
108-
URI.create("http://localhost:80/1.30/images/456")
116+
URI.create("http://localhost:80/1.30/images/456"),
117+
DOCKER
109118
).history(),
110119
Matchers.allOf(
111120
Matchers.notNullValue(),
@@ -135,7 +144,8 @@ public void deleteSendsCorrectRequest() throws Exception {
135144
)
136145
)
137146
),
138-
URI.create("http://localhost/images/test")
147+
URI.create("http://localhost/images/test"),
148+
DOCKER
139149
).delete();
140150
}
141151

@@ -151,7 +161,8 @@ public void deleteErrorOn404() throws Exception {
151161
new AssertRequest(
152162
new Response(HttpStatus.SC_NOT_FOUND)
153163
),
154-
URI.create("http://localhost/images/test")
164+
URI.create("http://localhost/images/test"),
165+
DOCKER
155166
).delete();
156167
}
157168

@@ -167,7 +178,8 @@ public void deleteErrorOn409() throws Exception {
167178
new AssertRequest(
168179
new Response(HttpStatus.SC_CONFLICT)
169180
),
170-
URI.create("http://localhost/images/test")
181+
URI.create("http://localhost/images/test"),
182+
DOCKER
171183
).delete();
172184
}
173185

@@ -183,7 +195,8 @@ public void deleteErrorOn500() throws Exception {
183195
new AssertRequest(
184196
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
185197
),
186-
URI.create("http://localhost/images/test")
198+
URI.create("http://localhost/images/test"),
199+
DOCKER
187200
).delete();
188201
}
189202

@@ -210,7 +223,8 @@ public void tagsOk() throws Exception {
210223
)
211224
)
212225
),
213-
URI.create("http://localhost/images/123")
226+
URI.create("http://localhost/images/123"),
227+
DOCKER
214228
).tag("myrepo/myimage", "mytag");
215229
}
216230

@@ -226,7 +240,8 @@ public void tagErrorIfResponseIs400() throws Exception {
226240
new AssertRequest(
227241
new Response(HttpStatus.SC_BAD_REQUEST)
228242
),
229-
URI.create("https://localhost")
243+
URI.create("https://localhost"),
244+
DOCKER
230245
).tag("myrepo/myimage", "mytag");
231246
}
232247

@@ -242,7 +257,8 @@ public void tagErrorIfResponseIs404() throws Exception {
242257
new AssertRequest(
243258
new Response(HttpStatus.SC_NOT_FOUND)
244259
),
245-
URI.create("https://localhost")
260+
URI.create("https://localhost"),
261+
DOCKER
246262
).tag("myrepo/myimage", "mytag");
247263
}
248264

@@ -258,7 +274,8 @@ public void tagErrorIfResponseIs409() throws Exception {
258274
new AssertRequest(
259275
new Response(HttpStatus.SC_CONFLICT)
260276
),
261-
URI.create("https://localhost")
277+
URI.create("https://localhost"),
278+
DOCKER
262279
).tag("myrepo/myimage", "mytag");
263280
}
264281

@@ -274,7 +291,42 @@ public void tagErrorIfResponseIs500() throws Exception {
274291
new AssertRequest(
275292
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
276293
),
277-
URI.create("https://localhost")
294+
URI.create("https://localhost"),
295+
DOCKER
278296
).tag("myrepo/myimage", "mytag");
279297
}
298+
299+
/**
300+
* RtImage can run itself.
301+
* @throws Exception If something goes wrong.
302+
*/
303+
@Test
304+
public void runsItselfOk() throws Exception {
305+
final AtomicBoolean started = new AtomicBoolean(false);
306+
final Container container = Mockito.mock(Container.class);
307+
Mockito.doAnswer(
308+
invocation -> {
309+
started.set(true);
310+
return null;
311+
}
312+
).when(container).start();
313+
final Containers containers = Mockito.mock(Containers.class);
314+
Mockito.doReturn(container)
315+
.when(containers)
316+
.create(Mockito.eq("image123"));
317+
Mockito.doReturn(containers).when(DOCKER).containers();
318+
MatcherAssert.assertThat(
319+
new RtImage(
320+
Mockito.mock(JsonObject.class),
321+
Mockito.mock(HttpClient.class),
322+
URI.create("http://localhost/images/image123"),
323+
DOCKER
324+
).run(),
325+
Matchers.is(container)
326+
);
327+
MatcherAssert.assertThat(
328+
started.get(),
329+
Matchers.is(true)
330+
);
331+
}
280332
}

src/test/java/com/amihaiemil/docker/RtImagesTestCase.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.hamcrest.MatcherAssert;
3636
import org.hamcrest.Matchers;
3737
import org.junit.Test;
38+
import org.mockito.Mockito;
3839

3940
/**
4041
* Unit tests for {@link RtImages}.
@@ -44,6 +45,11 @@
4445
* @checkstyle MethodName (500 lines)
4546
*/
4647
public final class RtImagesTestCase {
48+
/**
49+
* Mock docker.
50+
*/
51+
private static final Docker DOCKER = Mockito.mock(Docker.class);
52+
4753
/**
4854
* Must return the same number of images as there are elements in the
4955
* json array returned by the service.
@@ -64,7 +70,9 @@ public void iteratesImages() {
6470
.add("Id", "sha256:3e314f95dcace0f5e")
6571
).build().toString()
6672
)
67-
), URI.create("http://localhost")
73+
),
74+
URI.create("http://localhost"),
75+
DOCKER
6876
).forEach(image -> count.incrementAndGet());
6977
MatcherAssert.assertThat(
7078
count.get(),
@@ -85,7 +93,9 @@ public void iteratesZeroImages() throws Exception {
8593
HttpStatus.SC_OK,
8694
Json.createArrayBuilder().build().toString()
8795
)
88-
), URI.create("http://localhost")
96+
),
97+
URI.create("http://localhost"),
98+
DOCKER
8999
).forEach(image -> count.incrementAndGet());
90100
MatcherAssert.assertThat(
91101
count.get(),
@@ -103,7 +113,8 @@ public void iterateFailsIfResponseIs500() throws Exception {
103113
new AssertRequest(
104114
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
105115
),
106-
URI.create("http://localhost")
116+
URI.create("http://localhost"),
117+
DOCKER
107118
).iterator();
108119
}
109120

@@ -131,7 +142,8 @@ public void createSetsGivenParameters() throws Exception {
131142
}
132143
)
133144
),
134-
URI.create("http://localhost")
145+
URI.create("http://localhost"),
146+
DOCKER
135147
).pull("testImage", "1.23");
136148
}
137149

@@ -146,7 +158,8 @@ public void createErrorOnStatus404() throws Exception {
146158
new AssertRequest(
147159
new Response(HttpStatus.SC_NOT_FOUND)
148160
),
149-
URI.create("http://localhost")
161+
URI.create("http://localhost"),
162+
DOCKER
150163
).pull("", "");
151164
}
152165

@@ -161,7 +174,8 @@ public void createErrorOnStatus500() throws Exception {
161174
new AssertRequest(
162175
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
163176
),
164-
URI.create("http://localhost")
177+
URI.create("http://localhost"),
178+
DOCKER
165179
).pull("", "");
166180
}
167181

@@ -185,7 +199,8 @@ public void prunesOk() throws Exception {
185199
.getUri().endsWith("/images/prune")
186200
)
187201
),
188-
URI.create("http://localhost/images")
202+
URI.create("http://localhost/images"),
203+
DOCKER
189204
).prune();
190205
}
191206

@@ -200,7 +215,8 @@ public void pruneThrowsErrorOnResponse500() throws Exception {
200215
new AssertRequest(
201216
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
202217
),
203-
URI.create("http://localhost/images")
218+
URI.create("http://localhost/images"),
219+
DOCKER
204220
).prune();
205221
}
206222
}

0 commit comments

Comments
 (0)