Skip to content

Commit 03ed09e

Browse files
committed
Simplify
1 parent 4853c1c commit 03ed09e

2 files changed

Lines changed: 88 additions & 196 deletions

File tree

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

Lines changed: 38 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@ import org.assertj.core.api.Assertions.assertThat
55
import org.gradle.testkit.runner.TaskOutcome.FAILED
66
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
77
import org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE
8+
import org.junit.jupiter.api.BeforeEach
89
import org.junit.jupiter.api.Test
910
import org.junit.jupiter.params.ParameterizedTest
1011
import org.junit.jupiter.params.provider.ValueSource
1112
import java.time.Instant
1213

1314
class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
15+
private lateinit var fixture: MavenRepoFixture
1416
private val task = ":dd-java-agent:instrumentation:demo:muzzle"
1517
private val assertionPrefix = "$task-AssertPass-com.example.test-demo-lib-"
1618

19+
@BeforeEach
20+
fun setup() {
21+
fixture = createMavenRepoFixture()
22+
writeNoopScanPlugin()
23+
}
24+
1725
@Test
1826
fun `default cooldown selects eligible versions across modules`() {
19-
val fixture = createMavenRepoFixture()
2027
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
2128
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.1.0"), publishedAt = Instant.now())
2229
val script = libraryProject(fixture)
2330
writeProject(script)
2431
addSubproject("dd-java-agent:instrumentation:other", script)
25-
writeNoopScanPlugin()
2632

27-
val result = run(
28-
"muzzle",
29-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl),
30-
unsetEnv = setOf("MIN_DEPENDENCY_AGE_HOURS")
31-
)
33+
val result = runMuzzle(requestedTask = "muzzle", hours = null)
3234

3335
assertThat(result.output).contains("BUILD SUCCESSFUL", "48h cooldown", "com.example.test:demo-lib:1.1.0")
3436
for (project in listOf("demo", "other")) {
@@ -40,95 +42,71 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
4042

4143
@Test
4244
fun `checks the dependency when its publication age cannot be verified`() {
43-
val fixture = createMavenRepoFixture()
4445
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
4546
// File URLs omit Last-Modified when the modification time is zero.
4647
val pom = fixture.repoDir.resolve("com/example/test/demo-lib/1.0.0/demo-lib-1.0.0.pom")
4748
check(pom.setLastModified(0L))
4849
writeProject(libraryProject(fixture))
49-
writeNoopScanPlugin()
5050

51-
val result = run(
52-
task,
53-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "48")
54-
)
51+
val result = runMuzzle()
5552

5653
assertThat(result.output).contains("BUILD SUCCESSFUL", "Muzzle retaining com.example.test:demo-lib:1.0.0", "cannot verify publication age")
5754
assertThat(result.task("${assertionPrefix}1.0.0")?.outcome).isEqualTo(SUCCESS)
5855
}
5956

6057
@Test
6158
fun `zero property overrides the environment and checks fresh releases`() {
62-
val fixture = createMavenRepoFixture()
6359
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"), publishedAt = Instant.now())
6460
writeProject(libraryProject(fixture))
65-
writeNoopScanPlugin()
6661

67-
val result = run(
68-
task,
69-
"-PmuzzleMinDependencyAgeHours=0",
70-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "96")
71-
)
62+
val result = runMuzzle("-PmuzzleMinDependencyAgeHours=0", hours = "96")
7263

7364
assertThat(result.task("${assertionPrefix}1.0.0")?.outcome).isEqualTo(SUCCESS)
7465
assertThat(result.output).doesNotContain("Muzzle deferring")
7566
}
7667

7768
@Test
7869
fun `reused daemon reads cooldown configuration from each build`() {
79-
val fixture = createMavenRepoFixture()
8070
fixture.publishVersions(
8171
"com.example.test",
8272
"demo-lib",
8373
listOf("1.0.0"),
8474
publishedAt = Instant.now().minusSeconds(72 * 3600L)
8575
)
8676
writeProject(libraryProject(fixture))
87-
writeNoopScanPlugin()
8877

89-
val deferred = run(
90-
task,
91-
expectFailure = true,
92-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "96")
93-
)
78+
val deferred = runMuzzle(expectFailure = true, hours = "96")
9479
assertThat(deferred.output).contains("No eligible muzzle artifacts", "96h publication cooldown")
9580

96-
val eligible = run(
97-
task,
98-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "48")
99-
)
81+
val eligible = runMuzzle()
10082
assertThat(eligible.task("${assertionPrefix}1.0.0")?.outcome).isEqualTo(SUCCESS)
10183
}
10284

10385
@Test
10486
fun `reconsiders publication timestamps when configuration caching is requested`() {
105-
val fixture = createMavenRepoFixture()
10687
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
10788
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.1.0"), publishedAt = Instant.now())
10889
writeProject(libraryProject(fixture))
109-
writeNoopScanPlugin()
110-
val environment = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "48")
11190

112-
val first = run(task, "--configuration-cache", env = environment)
91+
val first = runMuzzle("--configuration-cache")
11392
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
11493
assertThat(first.task("${assertionPrefix}1.1.0")).isNull()
11594

116-
val unchanged = run(task, "--configuration-cache", env = environment)
95+
val unchanged = runMuzzle("--configuration-cache")
11796
assertThat(unchanged.output).contains("BUILD SUCCESSFUL", "Reusing configuration cache")
11897
assertThat(unchanged.task("${assertionPrefix}1.0.0")?.outcome).isEqualTo(UP_TO_DATE)
11998
assertThat(unchanged.task("${assertionPrefix}1.1.0")).isNull()
12099

121100
val pom = fixture.repoDir.resolve("com/example/test/demo-lib/1.1.0/demo-lib-1.1.0.pom")
122101
check(pom.setLastModified(Instant.now().minusSeconds(72 * 3600L).toEpochMilli()))
123102

124-
val second = run(task, "--configuration-cache", env = environment)
103+
val second = runMuzzle("--configuration-cache")
125104
assertThat(second.output).contains("BUILD SUCCESSFUL").doesNotContain("Reusing configuration cache")
126105
assertThat(second.task("${assertionPrefix}1.1.0")?.outcome).isEqualTo(SUCCESS)
127106
}
128107

129108
@Test
130109
fun `a deferred incompatible release fails validation after becoming eligible with configuration caching`() {
131-
val fixture = createMavenRepoFixture()
132110
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
133111
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.1.0"), publishedAt = Instant.now())
134112
writeProject(libraryProject(fixture))
@@ -146,16 +124,15 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
146124
}
147125
"""
148126
)
149-
val environment = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "48")
150127

151-
val first = run(task, "--configuration-cache", env = environment)
128+
val first = runMuzzle("--configuration-cache")
152129
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
153130
assertThat(first.task("${assertionPrefix}1.1.0")).isNull()
154131

155132
val pom = fixture.repoDir.resolve("com/example/test/demo-lib/1.1.0/demo-lib-1.1.0.pom")
156133
check(pom.setLastModified(Instant.now().minusSeconds(72 * 3600L).toEpochMilli()))
157134

158-
val second = run(task, "--configuration-cache", expectFailure = true, env = environment)
135+
val second = runMuzzle("--configuration-cache", expectFailure = true)
159136
assertThat(second.output).contains("Muzzle validation failed").doesNotContain("Reusing configuration cache")
160137
assertThat(second.task("${assertionPrefix}1.1.0")?.outcome).isEqualTo(FAILED)
161138
assertThat(resultFile("muzzle-AssertPass-com.example.test-demo-lib-1.1.0").toFile().readText())
@@ -164,34 +141,23 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
164141

165142
@Test
166143
fun `DSL cooldown overrides lazily configured property and environment defaults`() {
167-
val fixture = createMavenRepoFixture()
168144
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"), publishedAt = Instant.now())
169145
writeProject(libraryProject(fixture) + "\nmuzzle { minimumDependencyAgeHours.set(0) }")
170-
writeNoopScanPlugin()
171146

172-
val result = run(
173-
task,
174-
"-PmuzzleMinDependencyAgeHours=invalid",
175-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "invalid")
176-
)
147+
val result = runMuzzle("-PmuzzleMinDependencyAgeHours=invalid", hours = "invalid")
177148

178149
assertThat(result.output).contains("BUILD SUCCESSFUL").doesNotContain("Muzzle deferring")
179150
assertThat(result.task("${assertionPrefix}1.0.0")?.outcome).isEqualTo(SUCCESS)
180151
}
181152

182153
@Test
183154
fun `each module may override its cooldown without changing other modules`() {
184-
val fixture = createMavenRepoFixture()
185155
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.0.0"))
186156
fixture.publishVersions("com.example.test", "demo-lib", listOf("1.1.0"), publishedAt = Instant.now())
187157
writeProject(libraryProject(fixture) + "\nmuzzle { minimumDependencyAgeHours.set(0) }")
188158
addSubproject("dd-java-agent:instrumentation:other", libraryProject(fixture))
189-
writeNoopScanPlugin()
190159

191-
val result = run(
192-
"muzzle",
193-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "48")
194-
)
160+
val result = runMuzzle(requestedTask = "muzzle")
195161

196162
assertThat(result.output).contains("BUILD SUCCESSFUL")
197163
assertThat(result.task("${assertionPrefix}1.1.0")?.outcome).isEqualTo(SUCCESS)
@@ -201,30 +167,21 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
201167
@ParameterizedTest
202168
@ValueSource(strings = ["compileMuzzle", "compileJava"])
203169
fun `compilation does not resolve versions or cooldown configuration`(compilationTask: String) {
204-
val fixture = createMavenRepoFixture()
205170
writeProject(libraryProject(fixture).replace("demo-lib", "absent-lib"))
206-
writeNoopScanPlugin()
207171

208-
val result = run(
209-
":dd-java-agent:instrumentation:demo:$compilationTask",
210-
"--configuration-cache",
211-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl, "MIN_DEPENDENCY_AGE_HOURS" to "invalid")
212-
)
172+
val result = runMuzzle("--configuration-cache", requestedTask = ":dd-java-agent:instrumentation:demo:$compilationTask", hours = "invalid")
213173

214174
assertThat(result.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
215175
.doesNotContain("Muzzle retaining", "Muzzle deferring", "Muzzle version range resolution failed")
216176
}
217177

218178
@Test
219179
fun `zero cooldown and large sampled ranges reuse an unchanged configuration cache`() {
220-
val fixture = createMavenRepoFixture()
221180
fixture.publishVersions("com.example.test", "demo-lib", (0..49).map { "1.$it.0" }, publishedAt = Instant.now())
222181
writeProject(libraryProject(fixture) + "\nmuzzle { minimumDependencyAgeHours.set(0) }")
223-
writeNoopScanPlugin()
224-
val environment = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl)
225182

226-
val first = run(task, "--configuration-cache", env = environment)
227-
val second = run(task, "--configuration-cache", env = environment)
183+
val first = runMuzzle("--configuration-cache")
184+
val second = runMuzzle("--configuration-cache")
228185

229186
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
230187
assertThat(second.output).contains("BUILD SUCCESSFUL", "Reusing configuration cache")
@@ -241,10 +198,9 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
241198
muzzle { pass { coreJdk() } }
242199
"""
243200
)
244-
writeNoopScanPlugin()
245201

246-
val first = run(task, "--configuration-cache")
247-
val second = run(task, "--configuration-cache")
202+
val first = runMuzzle("--configuration-cache")
203+
val second = runMuzzle("--configuration-cache")
248204

249205
assertThat(first.output).contains("BUILD SUCCESSFUL", "Configuration cache entry stored")
250206
assertThat(second.output).contains("BUILD SUCCESSFUL", "Reusing configuration cache")
@@ -254,20 +210,26 @@ class MuzzleDependencyAgeFunctionalTest : MuzzlePluginTestFixture() {
254210

255211
@Test
256212
fun `invalid cooldown fails before dependency resolution`() {
257-
val fixture = createMavenRepoFixture()
258213
writeProject(libraryProject(fixture))
259214

260-
val result = run(
261-
task,
262-
"-PmuzzleMinDependencyAgeHours=-1",
263-
expectFailure = true,
264-
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl)
265-
)
215+
val result = runMuzzle("-PmuzzleMinDependencyAgeHours=-1", expectFailure = true)
266216

267217
assertThat(result.output).contains("must be a non-negative integer")
268218
.doesNotContain("Muzzle version range resolution failed")
269219
}
270220

221+
private fun runMuzzle(
222+
vararg arguments: String,
223+
requestedTask: String = task,
224+
hours: String? = "48",
225+
expectFailure: Boolean = false
226+
) = run(
227+
requestedTask, *arguments, expectFailure = expectFailure,
228+
env = mapOf("MAVEN_REPOSITORY_PROXY" to fixture.repoUrl) +
229+
if (hours == null) emptyMap() else mapOf("MIN_DEPENDENCY_AGE_HOURS" to hours),
230+
unsetEnv = if (hours == null) setOf("MIN_DEPENDENCY_AGE_HOURS") else emptySet()
231+
)
232+
271233
private fun libraryProject(fixture: MavenRepoFixture) = """
272234
plugins {
273235
id("java")

0 commit comments

Comments
 (0)