Browse Source

Add framework for parser integration tests.

Using docker we can bring up and tear down IRCds pretty trivially.
This allows us to test the parser in real-world situations.
pull/126/head
Chris Smith 7 years ago
parent
commit
4fa1e43f8f

+ 18
- 0
irc/build.gradle View File

@@ -1,3 +1,21 @@
1
+sourceSets {
2
+    integTest {
3
+        java.srcDirs = ['integ-test']
4
+    }
5
+}
6
+
7
+task integTest(type: Test) {
8
+    testClassesDir = sourceSets.integTest.output.classesDir
9
+    classpath = sourceSets.integTest.runtimeClasspath
10
+}
11
+
12
+check.dependsOn integTest
13
+
1 14
 dependencies {
2 15
     compile find("common")
16
+    integTestCompile sourceSets.main.output
17
+    integTestCompile configurations.testCompile
18
+    integTestCompile sourceSets.test.output
19
+    integTestCompile group: 'com.github.docker-java', name: 'docker-java', version: '3.0.0'
20
+    integTestRuntime configurations.testRuntime
3 21
 }

+ 70
- 0
irc/integ-test/com/dmdirc/parser/irc/integration/ExampleTest.java View File

@@ -0,0 +1,70 @@
1
+/*
2
+ * Copyright (c) 2006-2016 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.parser.irc.integration;
24
+
25
+import com.dmdirc.parser.events.ServerReadyEvent;
26
+import com.dmdirc.parser.interfaces.Parser;
27
+import com.dmdirc.parser.irc.IRCParser;
28
+import com.dmdirc.parser.irc.integration.util.DockerContainerRule;
29
+
30
+import java.net.URI;
31
+import java.util.concurrent.Semaphore;
32
+import java.util.concurrent.TimeUnit;
33
+
34
+import org.junit.Rule;
35
+import org.junit.Test;
36
+
37
+import net.engio.mbassy.listener.Handler;
38
+import net.engio.mbassy.listener.Listener;
39
+import net.engio.mbassy.listener.References;
40
+
41
+public class ExampleTest {
42
+
43
+    @Rule
44
+    public final DockerContainerRule containerRule =
45
+            new DockerContainerRule("cloudposse/unrealircd");
46
+
47
+    @Test
48
+    public void testBasicConnect() throws InterruptedException {
49
+        final Parser ircParser = new IRCParser(URI.create(
50
+                "irc://" + containerRule.getContainerIpAddress()));
51
+        final ConnectListener listener = new ConnectListener();
52
+        ircParser.getCallbackManager().subscribe(listener);
53
+        ircParser.connect();
54
+        listener.connected.tryAcquire(2500, TimeUnit.MILLISECONDS);
55
+    }
56
+
57
+    @Listener(references = References.Strong)
58
+    private static final class ConnectListener {
59
+
60
+        final Semaphore connected = new Semaphore(0);
61
+
62
+        @Handler
63
+        public void onConnect(final ServerReadyEvent event) {
64
+            connected.release();
65
+        }
66
+
67
+    }
68
+
69
+
70
+} 

+ 82
- 0
irc/integ-test/com/dmdirc/parser/irc/integration/util/DockerContainerRule.java View File

@@ -0,0 +1,82 @@
1
+/*
2
+ * Copyright (c) 2006-2016 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.parser.irc.integration.util;
24
+
25
+import com.google.common.collect.Iterables;
26
+
27
+import org.junit.Assume;
28
+import org.junit.rules.ExternalResource;
29
+
30
+import com.github.dockerjava.api.DockerClient;
31
+import com.github.dockerjava.api.model.ContainerNetwork;
32
+import com.github.dockerjava.core.DockerClientBuilder;
33
+import com.github.dockerjava.core.command.PullImageResultCallback;
34
+
35
+/**
36
+ * Rule that launchers a container with a given image, and tears it down on completion.
37
+ */
38
+public class DockerContainerRule extends ExternalResource {
39
+
40
+    private final String image;
41
+    private DockerClient client;
42
+    private String container;
43
+    private String ip;
44
+
45
+    public DockerContainerRule(final String image) {
46
+        this.image = image;
47
+    }
48
+
49
+    @Override
50
+    @SuppressWarnings("resource")
51
+    protected void before() throws Throwable {
52
+        super.before();
53
+
54
+        client = DockerClientBuilder.getInstance().build();
55
+        client.pullImageCmd(image).exec(new PullImageResultCallback()).awaitSuccess();
56
+
57
+        container = client.createContainerCmd(image).exec().getId();
58
+        client.startContainerCmd(container).exec();
59
+
60
+        final ContainerNetwork network = Iterables.getFirst(
61
+                client.inspectContainerCmd(container)
62
+                        .exec()
63
+                        .getNetworkSettings()
64
+                        .getNetworks().values(), null);
65
+        Assume.assumeNotNull(network);
66
+
67
+        ip = network.getIpAddress();
68
+    }
69
+
70
+    @Override
71
+    protected void after() {
72
+        super.after();
73
+
74
+        client.stopContainerCmd(container).exec();
75
+        client.removeContainerCmd(container).withForce(true).exec();
76
+    }
77
+
78
+    public String getContainerIpAddress() {
79
+        return ip;
80
+    }
81
+
82
+}

Loading…
Cancel
Save