Skip to content

Commit f69bdde

Browse files
committed
Simplify tests
1 parent b9e4cde commit f69bdde

5 files changed

Lines changed: 52 additions & 88 deletions

File tree

‎buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleDependencyAge.kt‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ internal class MuzzleDependencyAge(
2020
private val cutoff = buildStartedAt.minus(minimumAgeHours.toLong(), HOURS)
2121
private val timestamps = mutableMapOf<String, Timestamp>()
2222
private val unavailableRepositories = mutableMapOf<String, Timestamp>()
23-
var timestampLookupCount: Int = 0
24-
private set
2523

2624
init {
2725
require(minimumAgeHours >= 0) { "Muzzle minimum dependency age must be non-negative" }
@@ -48,7 +46,6 @@ internal class MuzzleDependencyAge(
4846
val url = "$repositoryUrl/$pomPath"
4947
val timestamp = timestamps[url] ?: unavailableRepositories[repositoryUrl] ?: timestamps.getOrPut(url) {
5048
try {
51-
timestampLookupCount++
5249
lookup(url)
5350
} catch (e: IOException) {
5451
if (repositoryUrl.startsWith("https://") || repositoryUrl.startsWith("http://")) {

‎buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/planner/MuzzlePlansValueSource.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ internal abstract class MuzzlePlansValueSource :
4242
}
4343
}
4444
Logging.getLogger(MuzzlePlansValueSource::class.java).info(
45-
"Muzzle planned ${plans.size} checks for ${requests.size} modules with " +
46-
"${ages.values.sumOf { it.timestampLookupCount }} timestamp lookups in " +
45+
"Muzzle planned ${plans.size} checks for ${requests.size} modules in " +
4746
"${(System.nanoTime() - startNanos) / 1_000_000}ms"
4847
)
4948
return plans

‎buildSrc/src/test/kotlin/datadog/gradle/plugin/muzzle/MuzzleDependencyAgeFunctionalTest.kt‎

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,28 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
8282
assertThat(eligible.task("${assertionPrefix}1.0.0")?.outcome).isEqualTo(SUCCESS)
8383
}
8484

85-
@Test
86-
fun `reconsiders publication timestamps when configuration caching is requested`() {
85+
@ParameterizedTest
86+
@ValueSource(booleans = [false, true])
87+
fun `cached graphs revalidate newly eligible releases`(incompatible: Boolean) {
8788
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
8889
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.1.0"), publishedAt = Instant.now())
8990
writeProject(libraryProject(fixture))
91+
if (incompatible) {
92+
writeScanPlugin(
93+
"""
94+
try (java.io.InputStream input = testApplicationClassLoader.getResourceAsStream(
95+
"META-INF/maven/com.example.test/demo-lib/pom.properties")) {
96+
java.util.Properties properties = new java.util.Properties();
97+
properties.load(input);
98+
if ("1.1.0".equals(properties.getProperty("version"))) {
99+
throw new IllegalStateException("Newly eligible version is incompatible");
100+
}
101+
} catch (java.io.IOException e) {
102+
throw new AssertionError(e);
103+
}
104+
"""
105+
)
106+
}
90107

91108
val first = runMuzzle("--configuration-cache")
92109
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
@@ -100,43 +117,14 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
100117
val pom = fixture.repoDir.resolve("com/example/test/demo-lib/1.1.0/demo-lib-1.1.0.pom")
101118
check(pom.setLastModified(Instant.now().minusSeconds(72 * 3600L).toEpochMilli()))
102119

103-
val second = runMuzzle("--configuration-cache")
104-
assertThat(second.output).contains("BUILD SUCCESSFUL").doesNotContain("Reusing configuration cache")
105-
assertThat(second.task("${assertionPrefix}1.1.0")?.outcome).isEqualTo(SUCCESS)
106-
}
107-
108-
@Test
109-
fun `a deferred incompatible release fails validation after becoming eligible with configuration caching`() {
110-
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
111-
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.1.0"), publishedAt = Instant.now())
112-
writeProject(libraryProject(fixture))
113-
writeScanPlugin(
114-
"""
115-
try (java.io.InputStream input = testApplicationClassLoader.getResourceAsStream(
116-
"META-INF/maven/com.example.test/demo-lib/pom.properties")) {
117-
java.util.Properties properties = new java.util.Properties();
118-
properties.load(input);
119-
if ("1.1.0".equals(properties.getProperty("version"))) {
120-
throw new IllegalStateException("Newly eligible version is incompatible");
121-
}
122-
} catch (java.io.IOException e) {
123-
throw new AssertionError(e);
124-
}
125-
"""
126-
)
127-
128-
val first = runMuzzle("--configuration-cache")
129-
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
130-
assertThat(first.task("${assertionPrefix}1.1.0")).isNull()
131-
132-
val pom = fixture.repoDir.resolve("com/example/test/demo-lib/1.1.0/demo-lib-1.1.0.pom")
133-
check(pom.setLastModified(Instant.now().minusSeconds(72 * 3600L).toEpochMilli()))
134-
135-
val second = runMuzzle("--configuration-cache", expectFailure = true)
136-
assertThat(second.output).contains("Muzzle validation failed").doesNotContain("Reusing configuration cache")
137-
assertThat(second.task("${assertionPrefix}1.1.0")?.outcome).isEqualTo(FAILED)
138-
assertThat(resultFile("muzzle-AssertPass-com.example.test-demo-lib-1.1.0").toFile().readText())
139-
.contains("Newly eligible version is incompatible")
120+
val second = runMuzzle("--configuration-cache", expectFailure = incompatible)
121+
assertThat(second.output).doesNotContain("Reusing configuration cache")
122+
.contains(if (incompatible) "Muzzle validation failed" else "BUILD SUCCESSFUL")
123+
assertThat(second.task("${assertionPrefix}1.1.0")?.outcome).isEqualTo(if (incompatible) FAILED else SUCCESS)
124+
if (incompatible) {
125+
assertThat(resultFile("muzzle-AssertPass-com.example.test-demo-lib-1.1.0").toFile().readText())
126+
.contains("Newly eligible version is incompatible")
127+
}
140128
}
141129

142130
@Test
@@ -190,24 +178,6 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
190178
.containsExactlyElementsOf(first.tasks.filter { it.path.startsWith(assertionPrefix) }.map { it.path })
191179
}
192180

193-
@Test
194-
fun `core JDK checks reuse configuration cache without publication lookups`() {
195-
writeProject(
196-
"""
197-
plugins { id("java"); id("dd-trace-java.muzzle") }
198-
muzzle { pass { coreJdk() } }
199-
"""
200-
)
201-
202-
val first = runMuzzle("--configuration-cache")
203-
val second = runMuzzle("--configuration-cache")
204-
205-
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
206-
assertThat(second.output).contains("BUILD SUCCESSFUL", "Reusing configuration cache")
207-
assertThat(first.task("$task-AssertPass-core-jdk")?.outcome).isEqualTo(SUCCESS)
208-
assertThat(second.output).doesNotContain("Muzzle deferring", "Muzzle retaining")
209-
}
210-
211181
@Test
212182
fun `invalid cooldown fails before dependency resolution`() {
213183
writeProject(libraryProject(fixture))

‎buildSrc/src/test/kotlin/datadog/gradle/plugin/muzzle/MuzzleDependencyAgeTest.kt‎

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,26 @@ class MuzzleDependencyAgeTest {
4848
.contains("com.example:lib:1.2", "eligible at 2026-09-30T12:00:01Z", "48h cooldown")
4949
}
5050

51-
@Test
52-
fun `falls back to another repository when age is unavailable`() {
51+
@ParameterizedTest
52+
@CsvSource(value = ["central,true", "central-proxy,false"])
53+
fun `uses configured extra repositories before any Central fallback`(primaryId: String, oldEnough: Boolean) {
54+
val primary = if (primaryId == "central-proxy") proxy else repositories.single()
55+
val extra = repository("extra", "https://extra.example/maven2/")
5356
val policy = policy { url ->
54-
if (url.startsWith("https://repo.example/")) {
57+
if (url.startsWith(primary.url)) {
5558
Timestamp(null, "HTTP 404")
5659
} else {
57-
Timestamp(now.minusSeconds(72 * 3600L))
60+
Timestamp(if (oldEnough) now.minusSeconds(72 * 3600L) else now)
5861
}
5962
}
60-
val repos = repositories + repository("extra", "https://extra.example/maven2/")
6163

62-
assertThat(policy.isEligible("com.example", "lib", "1.0", repos)).isTrue()
64+
assertThat(policy.isEligible("com.example", "lib", "1.0", listOf(primary, extra))).isEqualTo(oldEnough)
6365
assertThat(requested).containsExactly(
64-
"https://repo.example/maven2/com/example/lib/1.0/lib-1.0.pom",
65-
"https://extra.example/maven2/com/example/lib/1.0/lib-1.0.pom"
66+
"${primary.url.trimEnd('/')}/com/example/lib/1.0/lib-1.0.pom",
67+
"${extra.url}com/example/lib/1.0/lib-1.0.pom"
6668
)
67-
assertThat(warnings).isEmpty()
69+
if (oldEnough) assertThat(warnings).isEmpty()
70+
else assertThat(warnings).singleElement().asString().contains("Muzzle deferring com.example:lib:1.0")
6871
}
6972

7073
@ParameterizedTest
@@ -102,20 +105,6 @@ class MuzzleDependencyAgeTest {
102105
assertThat(policy.isEligible("com.example", "lib", "1.0", listOf(proxy))).isEqualTo(oldEnough)
103106
}
104107

105-
@Test
106-
fun `tries configured extra repositories before the Central timestamp fallback`() {
107-
val policy = policy { url ->
108-
if (url.startsWith(proxy.url)) Timestamp(null, "HTTP 404") else Timestamp(now)
109-
}
110-
val extra = repository("extra", "https://extra.example/maven2/")
111-
112-
assertThat(policy.isEligible("com.example", "lib", "1.0", listOf(proxy, extra))).isFalse()
113-
assertThat(requested).containsExactly(
114-
"https://proxy.example/maven2/com/example/lib/1.0/lib-1.0.pom",
115-
"https://extra.example/maven2/com/example/lib/1.0/lib-1.0.pom"
116-
)
117-
}
118-
119108
@ParameterizedTest
120109
@ValueSource(booleans = [true, false])
121110
fun `caches proxy and Central timestamps including unavailable timestamps`(available: Boolean) {
@@ -128,7 +117,6 @@ class MuzzleDependencyAgeTest {
128117
assertThat(requested).containsExactly(
129118
"https://proxy.example/maven2/com/example/lib/1.0/lib-1.0.pom", centralPom
130119
)
131-
assertThat(policy.timestampLookupCount).isEqualTo(2)
132120
if (available) assertThat(warnings).isEmpty()
133121
else assertThat(warnings).hasSize(2).allSatisfy {
134122
assertThat(it).contains("Muzzle retaining com.example:lib:1.0", "cannot verify publication age", "central-timestamp: HTTP 429")
@@ -339,7 +327,6 @@ class MuzzleDependencyAgeTest {
339327

340328
assertThat(policy.isEligible("com.example", "lib", "1.0", emptyList())).isTrue()
341329
assertThat(policy.isEligible("com.example", "lib", "1.0", listOf(proxy))).isTrue()
342-
assertThat(policy.timestampLookupCount).isZero()
343330
assertThat(warnings).isEmpty()
344331
}
345332

‎buildSrc/src/test/kotlin/datadog/gradle/plugin/muzzle/MuzzlePluginFunctionalTest.kt‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ class MuzzlePluginFunctionalTest : MuzzlePluginTestFixture() {
302302
"""
303303
)
304304

305-
val result = run(":dd-java-agent:instrumentation:demo:muzzle", "--stacktrace")
305+
val result = run(
306+
":dd-java-agent:instrumentation:demo:muzzle", "--stacktrace", "--configuration-cache",
307+
env = mapOf("MIN_DEPENDENCY_AGE_HOURS" to "48")
308+
)
306309
val muzzleTaskPath = ":dd-java-agent:instrumentation:demo:muzzle"
307310
val passDirectiveTaskPath = ":dd-java-agent:instrumentation:demo:muzzle-AssertPass-core-jdk"
308311
val failDirectiveTaskPath = ":dd-java-agent:instrumentation:demo:muzzle-AssertFail-core-jdk"
@@ -330,6 +333,14 @@ class MuzzlePluginFunctionalTest : MuzzlePluginTestFixture() {
330333
assertThat(failDirectiveResult).isRegularFile()
331334
assertThat(passDirectiveResult.readText()).isEqualTo("PASSING")
332335
assertThat(failDirectiveResult.readText()).isEqualTo("PASSING")
336+
assertThat(result.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
337+
338+
val reused = run(
339+
muzzleTaskPath, "--stacktrace", "--configuration-cache",
340+
env = mapOf("MIN_DEPENDENCY_AGE_HOURS" to "48")
341+
)
342+
assertThat(reused.output).contains("BUILD SUCCESSFUL", "Reusing configuration cache")
343+
.doesNotContain("Muzzle deferring", "Muzzle retaining")
333344
}
334345

335346
@Test

0 commit comments

Comments
 (0)