Skip to content

Commit 4a34171

Browse files
authored
test(server): add tests for metadata routes (#1942)
Part of #1938. Adding tests, especially the one capturing the current buggy behavior.
1 parent bce8663 commit 4a34171

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,18 @@ fun main() {
5353
appModule(
5454
buildVersionArtifacts = ::buildVersionArtifacts,
5555
buildPackageArtifacts = ::buildPackageArtifacts,
56+
getGithubAuthToken = ::getGithubAuthToken,
5657
)
5758
}.start(wait = true)
5859
}
5960

6061
fun Application.appModule(
6162
buildVersionArtifacts: (ActionCoords) -> Map<String, Artifact>?,
6263
buildPackageArtifacts: suspend (ActionCoords, String, (Collection<ActionCoords>) -> Unit) -> Map<String, String>,
64+
getGithubAuthToken: () -> String,
6365
) {
6466
val bindingsCache = buildBindingsCache(buildVersionArtifacts)
65-
val metadataCache = buildMetadataCache(bindingsCache, buildPackageArtifacts)
67+
val metadataCache = buildMetadataCache(bindingsCache, buildPackageArtifacts, getGithubAuthToken)
6668
installPlugins(prometheusRegistry)
6769

6870
routing {
@@ -86,6 +88,7 @@ private fun buildBindingsCache(
8688
private fun buildMetadataCache(
8789
bindingsCache: LoadingCache<ActionCoords, ArtifactResult>,
8890
buildPackageArtifacts: suspend (ActionCoords, String, (Collection<ActionCoords>) -> Unit) -> Map<String, String>,
91+
getGithubAuthToken: () -> String,
8992
): LoadingCache<ActionCoords, MetadataResult> =
9093
Caffeine
9194
.newBuilder()

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ArtifactRoutesTest :
2525
},
2626
// Irrelevant for these tests.
2727
buildPackageArtifacts = { _, _, _ -> emptyMap() },
28+
getGithubAuthToken = { "" },
2829
)
2930
}
3031

@@ -45,6 +46,7 @@ class ArtifactRoutesTest :
4546
buildVersionArtifacts = { null },
4647
// Irrelevant for these tests.
4748
buildPackageArtifacts = { _, _, _ -> emptyMap() },
49+
getGithubAuthToken = { "" },
4850
)
4951
}
5052

@@ -64,6 +66,7 @@ class ArtifactRoutesTest :
6466
buildVersionArtifacts = { error("An internal error occurred!") },
6567
// Irrelevant for these tests.
6668
buildPackageArtifacts = { _, _, _ -> emptyMap() },
69+
getGithubAuthToken = { "" },
6770
)
6871
}
6972

@@ -87,6 +90,7 @@ class ArtifactRoutesTest :
8790
buildVersionArtifacts = mockBuildVersionArtifacts,
8891
// Irrelevant for these tests.
8992
buildPackageArtifacts = { _, _, _ -> emptyMap() },
93+
getGithubAuthToken = { "" },
9094
)
9195
}
9296

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package io.github.typesafegithub.workflows.jitbindingserver
2+
3+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.matchers.shouldBe
6+
import io.ktor.client.request.get
7+
import io.ktor.client.statement.bodyAsText
8+
import io.ktor.http.HttpStatusCode
9+
import io.ktor.server.testing.testApplication
10+
import io.mockk.every
11+
import io.mockk.mockk
12+
import io.mockk.verify
13+
14+
class MetadataRoutesTest :
15+
FunSpec({
16+
context("artifacts for a given package") {
17+
test("when some artifacts were generated") {
18+
testApplication {
19+
// Given
20+
application {
21+
appModule(
22+
buildPackageArtifacts = { _, _, _ ->
23+
mapOf("maven-metadata.xml" to "Some XML contents")
24+
},
25+
getGithubAuthToken = { "some-token" },
26+
// Irrelevant for these tests.
27+
buildVersionArtifacts = { emptyMap() },
28+
)
29+
}
30+
31+
// When
32+
val response = client.get("some-owner/some-action/maven-metadata.xml")
33+
34+
// Then
35+
response.status shouldBe HttpStatusCode.OK
36+
response.bodyAsText() shouldBe "Some XML contents"
37+
}
38+
}
39+
40+
test("when no artifacts could be generated") {
41+
testApplication {
42+
// Given
43+
application {
44+
appModule(
45+
buildPackageArtifacts = { _, _, _ ->
46+
emptyMap()
47+
},
48+
getGithubAuthToken = { "some-token" },
49+
// Irrelevant for these tests.
50+
buildVersionArtifacts = { emptyMap() },
51+
)
52+
}
53+
54+
// When
55+
val response = client.get("some-owner/some-action/maven-metadata.xml")
56+
57+
// Then
58+
response.status shouldBe HttpStatusCode.NotFound
59+
}
60+
}
61+
62+
test("when artifact generation fails") {
63+
testApplication {
64+
// Given
65+
application {
66+
appModule(
67+
buildPackageArtifacts = { _, _, _ ->
68+
error("An internal error occurred!")
69+
},
70+
getGithubAuthToken = { "some-token" },
71+
// Irrelevant for these tests.
72+
buildVersionArtifacts = { emptyMap() },
73+
)
74+
}
75+
76+
// When
77+
val response = client.get("some-owner/some-action/maven-metadata.xml")
78+
79+
// Then
80+
response.status shouldBe HttpStatusCode.InternalServerError
81+
}
82+
}
83+
84+
test("when binding generation fails and then succeeds, and two requests are made") {
85+
testApplication {
86+
// Given
87+
val mockBuildPackageArtifacts =
88+
mockk<
89+
(
90+
ActionCoords,
91+
String,
92+
(Collection<ActionCoords>) -> Unit,
93+
) -> Map<String, String>,
94+
>()
95+
every { mockBuildPackageArtifacts(any(), any(), any()) } throws
96+
Exception("An internal error occurred!") andThen
97+
mapOf("maven-metadata.xml" to "Some XML contents")
98+
application {
99+
appModule(
100+
buildPackageArtifacts = mockBuildPackageArtifacts,
101+
getGithubAuthToken = { "some-token" },
102+
// Irrelevant for these tests.
103+
buildVersionArtifacts = { emptyMap() },
104+
)
105+
}
106+
107+
// When
108+
val response = client.get("some-owner/some-action/maven-metadata.xml")
109+
// Then
110+
response.status shouldBe HttpStatusCode.InternalServerError
111+
112+
// When
113+
val response2 = client.get("some-owner/some-action/maven-metadata.xml")
114+
// Then
115+
// This assertion represents an undesired behavior - it should retry generating the artifact
116+
// and return the actual POM.
117+
// TODO fix in https://github.com/typesafegithub/github-workflows-kt/issues/1938
118+
response2.status shouldBe HttpStatusCode.InternalServerError
119+
120+
// This assertion represents an undesired behavior - it should call the artifact generation twice,
121+
// first with exception, and then when it's successful.
122+
// TODO fix in https://github.com/typesafegithub/github-workflows-kt/issues/1938
123+
verify(exactly = 1) { mockBuildPackageArtifacts(any(), any(), any()) }
124+
}
125+
}
126+
}
127+
})

0 commit comments

Comments
 (0)