Skip to content

Commit d866dba

Browse files
authored
Adding a method to retrieve resultFileResources from gcloud easily (#64)
* Adding a method to retieve resultFileResources from gcloud easily * Updating to use gcsPath directly * adding null check if path doesn't exist * addressing feedback
1 parent ad31f58 commit d866dba

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/gcloud/GoogleCloudApi.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ internal interface GoogleCloudApi {
7878
* Walks all entries under the given [gcsPath].
7979
*/
8080
suspend fun walkEntries(gcsPath: GcsPath): Sequence<BlobVisitor>
81+
82+
/**
83+
* Gets the blobvisitor for the given [gcsPath] if the gcsPath represents a file.
84+
* Returns null if the gcsPath points to a folder
85+
*/
86+
suspend fun getBlob(gcsPath: GcsPath): BlobVisitor?
8187
}
8288

8389
/**
@@ -193,6 +199,19 @@ private class GoogleCloudApiImpl(
193199
}
194200
}
195201

202+
override suspend fun getBlob(
203+
gcsPath: GcsPath
204+
): BlobVisitor? {
205+
val blobId = gcsPath.blobId
206+
val blob = service.get(blobId)
207+
return if (blob != null) {
208+
BlobVisitorImpl(
209+
rootBlobId = blobId,
210+
blob = blob
211+
)
212+
} else null
213+
}
214+
196215
override suspend fun existingFilePath(relativePath: String): GcsPath? {
197216
val blobId = createBlobId(relativePath)
198217
val blob = service.get(blobId)

AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestRunnerService.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ interface TestRunnerService {
104104
testIdentifiers: List<TestIdentifier>
105105
): Map<TestIdentifier, List<TestCaseArtifact>>?
106106

107+
/**
108+
* Gets the resultFileResource represented by the [GcsPath]
109+
* Returns null if gcsPath points to a folder
110+
*/
111+
suspend fun getResultFileResource(
112+
gcsPath: GcsPath
113+
): ResultFileResource?
114+
107115
companion object {
108116
/**
109117
* Creates an implementation of [TestRunnerService].

AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImpl.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@ internal class TestRunnerServiceImpl internal constructor(
229229
visitor.fileName.startsWith(testName)
230230
}
231231
val testIdentifier = testNames[testName]
232-
println("filename = ${visitor.fileName}")
233-
println("testName = $testName")
234-
println("testIdentifier = $testIdentifier")
235232
if (testIdentifier != null) {
236233
testArtifactsBlobs.getOrPut(testIdentifier) {
237234
mutableListOf()
@@ -247,6 +244,16 @@ internal class TestRunnerServiceImpl internal constructor(
247244
return testArtifactsBlobs
248245
}
249246

247+
override suspend fun getResultFileResource(
248+
gcsPath: GcsPath
249+
): TestRunnerService.ResultFileResource? {
250+
return googleCloudApi.getBlob(gcsPath)?.let { blobVisitor ->
251+
ResultFileResourceImpl(
252+
blobVisitor
253+
)
254+
}
255+
}
256+
250257
suspend fun getTestMatrixResults(
251258
testMatrixId: String
252259
): List<TestRunnerService.TestRunResult>? {

AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/fake/FakeGoogleCloudApi.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ internal class FakeGoogleCloudApi(
6464
}
6565
}
6666

67+
override suspend fun getBlob(gcsPath: GcsPath): BlobVisitor? {
68+
return if (artifacts.containsKey(gcsPath)) {
69+
object : BlobVisitor {
70+
override val relativePath: String
71+
get() = ""
72+
override val gcsPath: GcsPath
73+
get() = gcsPath
74+
override fun obtainInputStream(): InputStream {
75+
return artifacts[gcsPath]?.inputStream() ?: InputStream.nullInputStream()
76+
}
77+
}
78+
} else null
79+
}
80+
6781
override suspend fun existingFilePath(relativePath: String): GcsPath? {
6882
val path = makeGcsPath(relativePath)
6983
return if (artifacts.containsKey(path)) {

AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImplTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dev.androidx.ci.testRunner
22

33
import com.google.common.truth.Truth.assertThat
44
import dev.androidx.ci.fake.FakeBackend
5+
import dev.androidx.ci.gcloud.GcsPath
56
import dev.androidx.ci.generated.ftl.ClientInfo
67
import dev.androidx.ci.generated.ftl.ClientInfoDetail
78
import dev.androidx.ci.generated.ftl.EnvironmentMatrix
@@ -101,6 +102,56 @@ class TestRunnerServiceImplTest {
101102
).isEqualTo(3)
102103
}
103104

105+
@Test
106+
fun getBlob() = runBlocking {
107+
// load some data into the backend
108+
val resultRelativePath = "my-test-matrix-results"
109+
val resultPath = "${fakeBackend.fakeGoogleCloudApi.rootGcsPath}/$resultRelativePath"
110+
fakeBackend.fakeGoogleCloudApi.upload(
111+
"$resultRelativePath/test1",
112+
"test1".toByteArray(Charsets.UTF_8)
113+
)
114+
115+
// verify file existing in backend is returned
116+
val test1 = fakeBackend.fakeGoogleCloudApi.getBlob(
117+
GcsPath("$resultPath/test1")
118+
)
119+
assertThat(test1).isNotNull()
120+
assertThat(test1?.gcsPath?.path).isEqualTo("$resultPath/test1")
121+
assertThat(
122+
subject.getResultFileResource(
123+
GcsPath("$resultPath/test1")
124+
)?.readFully()
125+
).isNotEmpty()
126+
127+
assertThat(
128+
subject.getResultFileResource(
129+
GcsPath("$resultPath/test1")
130+
)?.readFully()
131+
).isEqualTo("test1".toByteArray(Charsets.UTF_8))
132+
133+
// no blob should be returned for a folder
134+
val test2 = fakeBackend.fakeGoogleCloudApi.getBlob(
135+
GcsPath(resultPath)
136+
)
137+
assertThat(test2).isNull()
138+
assertThat(
139+
subject.getResultFileResource(
140+
GcsPath(resultPath)
141+
)?.readFully()
142+
).isNull()
143+
144+
// no blob should be returned for files that don't exist in backend
145+
val test3 = fakeBackend.fakeGoogleCloudApi.getBlob(
146+
GcsPath("$resultPath/test3")
147+
)
148+
assertThat(test3).isNull()
149+
assertThat(
150+
subject.getResultFileResource(
151+
GcsPath("$resultPath/test3")
152+
)?.readFully()
153+
).isNull()
154+
}
104155
@Test
105156
fun schedule() = runBlocking<Unit> {
106157
val apk1Bytes = byteArrayOf(1, 2, 3, 4, 5)

0 commit comments

Comments
 (0)