Skip to content

Commit 398467e

Browse files
authored
Create IntTests suite, they are comp tests which can be run within native image #480 (#537)
* upgrading rest assured due groovy error with nativeTest task Error encountered while parsing java.lang.invoke.MutableCallSite.setTarget(MutableCallSite.java:155) Parsing context: at java.lang.invoke.SwitchPoint.invalidateAll(SwitchPoint.java:225) at org.codehaus.groovy.vmplugin.v8.IndyInterface.invalidateSwitchPoints(IndyInterface.java:186) at org.codehaus.groovy.vmplugin.v8.IndyInterface$$Lambda/0x00000007c2399648.updateConstantMetaClass(Unknown Source) � * ignoring rest assured error and leaving it fail at tests if the problematic code will be used anyway * int test it's working * clean code * fixing bug * configuring native test at the same source set as test * enabling native image test * adjusting reflection generation * adjusts * test is working * adjusting conf path * print test logs to console * trying to get logs to check why test is failing * finding 'stream closed' cause * handling fatal errors * testing fatal error handling * delete hello world int test * re-enabling restassured * reverting restassured feature disabling * clean code * updating the docs about native image test * refactoring * refactoring * refactoring * refactoring * explaining about the kind of tests * [Gradle Release Plugin] - new version commit: '3.25.9-snapshot'. * fixing test * release notes * refactoring the docs * unnecessary path * clean code * make test repeatable * testing test class and refactoring * refactoring class * fixing test * caching * testing * creating flags * wasn't using the default config when file was empty * make test repeatable * hostnames list must be changable * refactoring * refactoring * troubleshooting * refactorings * testing * refactoring * testing * finally fixing bug which gets the wrong list * leading with methods * fixing field parsing * reverting * clean code
1 parent 360cf67 commit 398467e

File tree

12 files changed

+187
-75
lines changed

12 files changed

+187
-75
lines changed

RELEASE-NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.25.10
2+
* Create `IntTests` suite, they are comp tests which can be run within native image, see the docs #480
3+
* Upgrading necessary deps
4+
15
## 3.25.9
26
* Handling and logging fatal errors #480
37

build.gradle

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ sourceSets {
3535
compileClasspath += sourceSets.test.runtimeClasspath
3636
runtimeClasspath += sourceSets.test.runtimeClasspath
3737
}
38-
}
39-
4038

39+
}
4140

4241
dependencies {
4342

@@ -47,6 +46,9 @@ dependencies {
4746
compileOnly('com.mageddo.nativeimage:reflection-config-generator:2.4.4')
4847
annotationProcessor('com.mageddo.nativeimage:reflection-config-generator:2.4.4')
4948

49+
testCompileOnly('com.mageddo.nativeimage:reflection-config-generator:2.4.4')
50+
testAnnotationProcessor('com.mageddo.nativeimage:reflection-config-generator:2.4.4')
51+
5052
annotationProcessor("com.google.dagger:dagger-compiler:2.45")
5153
implementation("com.google.dagger:dagger:2.45")
5254

@@ -67,7 +69,7 @@ dependencies {
6769
implementation('com.github.docker-java:docker-java-core:3.3.4')
6870
implementation('com.github.docker-java:docker-java-transport-httpclient5:3.3.4')
6971

70-
implementation('info.picocli:picocli:4.7.1')
72+
implementation('info.picocli:picocli:4.7.6')
7173
implementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1')
7274

7375
implementation('com.github.ben-manes.caffeine:caffeine:3.1.8')
@@ -82,8 +84,8 @@ dependencies {
8284

8385
testImplementation("org.junit.jupiter:junit-jupiter:5.10.+")
8486
testImplementation('org.mockito:mockito-junit-jupiter:5.12.+')
85-
testImplementation('io.rest-assured:rest-assured:5.3.0')
86-
87+
testImplementation ('org.hamcrest:hamcrest:3.0')
88+
testImplementation('io.rest-assured:rest-assured:5.5.0')
8789

8890
}
8991

@@ -93,15 +95,33 @@ test {
9395
testLogging {
9496
events "passed", "skipped", "failed"
9597
}
98+
testLogging {
99+
showStandardStreams = true
100+
}
101+
}
102+
103+
def compTest = tasks.register("compTest", Test) {
104+
useJUnitPlatform()
105+
include "**/*CompTest.class", "**/*IntTest.class"
106+
failFast = true
107+
testLogging {
108+
events "passed", "skipped", "failed"
109+
}
110+
testLogging {
111+
showStandardStreams = true
112+
}
96113
}
97114

98-
task compTest(type: Test) {
115+
def intTest = tasks.register("intTest", Test) {
99116
useJUnitPlatform()
100-
include "**/*CompTest.class"
117+
include "**/*IntTest.class"
101118
failFast = true
102119
testLogging {
103120
events "passed", "skipped", "failed"
104121
}
122+
testLogging {
123+
showStandardStreams = true
124+
}
105125
}
106126

107127
task stressTest(type: Test) {
@@ -115,6 +135,9 @@ task stressTest(type: Test) {
115135
testLogging {
116136
events "passed", "skipped", "failed"
117137
}
138+
testLogging {
139+
showStandardStreams = true
140+
}
118141
}
119142

120143
compileJava {
@@ -162,7 +185,6 @@ tasks.register('nativeImageJar', ShadowJar) {
162185
exclude("/META-INF/*.SF")
163186
exclude("/META-INF/*.DSA")
164187
exclude("/META-INF/*.RSA")
165-
166188
}
167189

168190
shadowJar {
@@ -184,6 +206,11 @@ graalvmNative {
184206
buildArgs.add('-J-Xmx5G')
185207

186208
}
209+
210+
}
211+
registerTestBinary("intTest") {
212+
usingSourceSet(sourceSets.test)
213+
forTestTask(intTest)
187214
}
188215
}
189216

@@ -200,15 +227,11 @@ release {
200227

201228
}
202229

203-
// don't create tags as github-cli will do that
204-
preTagCommit.enabled = false
205-
createReleaseTag.enabled = false
206-
207-
confirmReleaseVersion {
208-
doLast {
209-
// codigo com a versao atual...
210-
}
230+
def dontCreateTagsAsTheCdWill(){
231+
preTagCommit.enabled = false
232+
createReleaseTag.enabled = false
211233
}
234+
dontCreateTagsAsTheCdWill()
212235

213236
tasks.register("updateNewVersion") {
214237
doLast {

docs/content/4-developing/_index.en.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,37 @@ $ docker-compose -f docker-compose-dev.yml run --rm -it backend bash
4242
$ java -jar dns-proxy-server-*-all.jar
4343
```
4444

45+
## Automated Tests
46+
47+
### Unit Tests
48+
49+
Verify the functionality of individual components or units of code, typically functions or methods,
50+
to ensure they work as expected.
51+
52+
### Comp Tests
53+
54+
Verify the functionality of entire flows but can mock some parts, example is an endpoint
55+
which changes some setting at config file, etc.
56+
57+
### Int Tests
58+
59+
Are like Comp Test but used to test native-image compiling, for that reason they
60+
can't mock using frameworks like Mockito, this kind of test need to evict mocks and when do,
61+
use flags instead of stubs/mocks.
62+
63+
Tests ending with `IntTest.java` can be run within the native image binary to check if
64+
the native-image compilation produces a working binary version.
65+
66+
Run Int Tests within the Native Image
67+
68+
```bash
69+
$ ./gradlew clean nativeIntTest
70+
```
71+
72+
Run Int Tests within the jar version
73+
74+
```bash
75+
$ ./gradlew clean intTest
76+
```
77+
4578
[1]: {{%relref "1-getting-started/requirements/_index.en.md" %}}

src/main/java/com/mageddo/dnsproxyserver/config/application/Configs.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ public class Configs {
1111
private static final Context context = Context.create();
1212

1313
public static Config getInstance() {
14-
final Config v = Singletons.get(Config.class);
15-
if (v != null) {
16-
return v;
17-
} else {
18-
return Singletons.createOrGet(Config.class, () -> {
19-
return context.configService().findCurrentConfig();
20-
});
21-
}
14+
return Singletons.createOrGet(Config.class, () -> {
15+
log.trace("status=cacheHotLoading");
16+
return context.configService().findCurrentConfig();
17+
});
2218
}
2319

2420
public static void clear() {
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
package com.mageddo.dnsproxyserver.di;
22

3-
import java.util.Collection;
4-
import java.util.Objects;
5-
63
public interface StartupEvent {
7-
8-
static boolean exists(Collection<StartupEvent> events, Class<?> clazz) {
9-
return lookup(events, clazz) != null;
10-
}
11-
12-
static <T> T lookup(Collection<StartupEvent> events, Class<T> clazz) {
13-
return (T) events
14-
.stream()
15-
.filter(it -> Objects.equals(clazz, it.getClass()))
16-
.findFirst()
17-
.orElse(null)
18-
;
19-
}
20-
214
void onStart();
225
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mageddo.dnsproxyserver.di;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.util.Collection;
6+
import java.util.Objects;
7+
8+
@Slf4j
9+
public class StartupEvents {
10+
public static boolean exists(Collection<StartupEvent> events, Class<?> classToFind) {
11+
return lookup(events, classToFind) != null;
12+
}
13+
14+
public static <T> T lookup(Collection<StartupEvent> events, Class<T> classToFind) {
15+
final var found = events
16+
.stream()
17+
.filter(it -> Objects.equals(classToFind, it.getClass()))
18+
.findFirst()
19+
.orElse(null);
20+
log.trace("found={}, classToFind={}", found, classToFind);
21+
return (T) found;
22+
}
23+
}

src/native-test/java/com/mageddo/dnsproxyserver/server/dns/solver/SolverSystemIT.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.mageddo.dnsproxyserver;
2+
3+
import com.mageddo.commons.concurrent.Threads;
4+
import com.mageddo.dns.utils.Messages;
5+
import com.mageddo.dnsproxyserver.config.application.Configs;
6+
import com.mageddo.dnsproxyserver.server.Starter;
7+
import com.mageddo.dnsproxyserver.solver.SimpleResolver;
8+
import com.mageddo.dnsproxyserver.utils.Ips;
9+
import com.mageddo.utils.Executors;
10+
import lombok.SneakyThrows;
11+
import lombok.extern.slf4j.Slf4j;
12+
import org.junit.jupiter.api.AfterAll;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import org.xbill.DNS.Message;
16+
import testing.templates.ConfigFlagArgsTemplates;
17+
18+
import java.time.Duration;
19+
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
@Slf4j
23+
public class AppIntTest {
24+
25+
@BeforeEach
26+
void beforeEach() {
27+
Starter.setMustStartFlagActive(true);
28+
Configs.clear();
29+
}
30+
31+
@AfterAll
32+
static void afterAll(){
33+
Starter.setMustStartFlagActive(false);
34+
}
35+
36+
@Test
37+
void appMustStartAndQuerySampleWithSuccess() {
38+
39+
final var hostToQuery = "dps-sample.dev";
40+
final var args = ConfigFlagArgsTemplates.withRandomPortsAndNotAsDefaultDns();
41+
final var app = new App(args);
42+
43+
try (final var executor = Executors.newThreadExecutor()) {
44+
45+
executor.submit(app::start);
46+
47+
Threads.sleep(Duration.ofSeconds(2));
48+
49+
final var port = app.getDnsServerPort();
50+
final var res = queryStartedServer(port, hostToQuery);
51+
assertTrue(Messages.isSuccess(res));
52+
53+
}
54+
55+
}
56+
57+
@SneakyThrows
58+
static Message queryStartedServer(Integer port, String host) {
59+
final var dnsServerAddress = Ips.getAnyLocalAddress(port);
60+
final var dnsClient = new SimpleResolver(dnsServerAddress);
61+
return dnsClient.send(Messages.aQuestion(host));
62+
}
63+
}

src/test/java/com/mageddo/dnsproxyserver/solver/docker/entrypoint/EventListenerCompTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mageddo.dnsproxyserver.di.Context;
44
import com.mageddo.dnsproxyserver.di.StartupEvent;
5+
import com.mageddo.dnsproxyserver.di.StartupEvents;
56
import dagger.sheath.junit.DaggerTest;
67
import org.junit.jupiter.api.Test;
78

@@ -22,7 +23,7 @@ void mustConfigureNetworkEventListener(){
2223
// arrange
2324

2425
// act
25-
final var found = StartupEvent.exists(this.events, EventListener.class);
26+
final var found = StartupEvents.exists(this.events, EventListener.class);
2627

2728
// assert
2829
assertTrue(found);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package testing.templates;
2+
3+
import com.mageddo.net.SocketUtils;
4+
5+
public class ConfigFlagArgsTemplates {
6+
public static String[] withRandomPortsAndNotAsDefaultDns() {
7+
final var webServerPort = SocketUtils.findRandomFreePort();
8+
final var dnsServerPort = SocketUtils.findRandomFreePort();
9+
10+
return new String[]{
11+
"--default-dns=false",
12+
"--web-server-port=" + webServerPort,
13+
"--server-port=" + dnsServerPort,
14+
"--log-level=TRACE",
15+
};
16+
}
17+
}

0 commit comments

Comments
 (0)