Skip to content

Commit

Permalink
Fix gcs apk name if any duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-goral committed Jun 17, 2020
1 parent 35972df commit 7fc9832
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
36 changes: 27 additions & 9 deletions test_runner/src/main/kotlin/ftl/gc/GcStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.cloud.storage.BlobInfo
import com.google.cloud.storage.Storage
import com.google.cloud.storage.StorageOptions
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper
import com.google.common.annotations.VisibleForTesting
import ftl.args.IArgs
import ftl.args.IosArgs
import ftl.config.FtlConstants
Expand Down Expand Up @@ -51,7 +52,7 @@ object GcStorage {
if (file.startsWith(GCS_PREFIX)) return file

return upload(
file = file,
filePath = file,
fileBytes = Files.readAllBytes(Paths.get(file)),
rootGcsBucket = rootGcsBucket,
runGcsPath = runGcsPath
Expand Down Expand Up @@ -82,7 +83,7 @@ object GcStorage {
fun uploadCiJUnitXml(testResult: JUnitTestResult, args: IArgs, fileName: String) {
if (args.resultsBucket.isBlank() || args.resultsDir.isBlank()) return
upload(
file = fileName,
filePath = fileName,
fileBytes = testResult.xmlToString().toByteArray(),
rootGcsBucket = args.resultsBucket,
runGcsPath = args.resultsDir
Expand All @@ -94,7 +95,7 @@ object GcStorage {

fun uploadXCTestFile(fileName: String, gcsBucket: String, runGcsPath: String, fileBytes: ByteArray): String =
upload(
file = fileName,
filePath = fileName,
fileBytes = fileBytes,
rootGcsBucket = gcsBucket,
runGcsPath = runGcsPath
Expand All @@ -110,13 +111,30 @@ object GcStorage {
return null
}

private fun upload(file: String, fileBytes: ByteArray, rootGcsBucket: String, runGcsPath: String): String {
val fileName = Paths.get(file).fileName.toString()
return uploadCache[fileName] ?: uploadCache.computeIfAbsent(fileName) {
val gcsFilePath = GCS_PREFIX + join(rootGcsBucket, runGcsPath, fileName)
private val duplicatedGcsPathCounter = ConcurrentHashMap<String, Int>()

@VisibleForTesting
internal fun upload(
filePath: String,
fileBytes: ByteArray,
rootGcsBucket: String,
runGcsPath: String,
storage: Storage = GcStorage.storage
): String {
val file = File(filePath)
val absolutePath = file.absolutePath
val fileName = file.name
return uploadCache[absolutePath] ?: uploadCache.computeIfAbsent(absolutePath) {
val gcsPath = join(runGcsPath, fileName)
val index = duplicatedGcsPathCounter.merge(gcsPath, 0) { old, _ -> old + 1 }
val validGcsPath = when {
index == 0 -> gcsPath
file.extension.isBlank() -> "${gcsPath}_$index"
else -> gcsPath.replace(".${file.extension}", "_$index.${file.extension}")
}

// 404 Not Found error when rootGcsBucket does not exist
val fileBlob = BlobInfo.newBuilder(rootGcsBucket, join(runGcsPath, fileName)).build()
val fileBlob = BlobInfo.newBuilder(rootGcsBucket, validGcsPath).build()

val progress = ProgressBar()
try {
Expand All @@ -127,7 +145,7 @@ object GcStorage {
} finally {
progress.stop()
}
gcsFilePath
GCS_PREFIX + join(rootGcsBucket, validGcsPath)
}
}

Expand Down
33 changes: 33 additions & 0 deletions test_runner/src/test/kotlin/ftl/gc/GcStorageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

Expand All @@ -22,4 +23,36 @@ class GcStorageTest {

GcStorage.upload(androidArgs.appApk!!, "does-not-exist", "nope")
}

@Test
fun `fix duplicated file names`() {
Assert.assertEquals(
listOf(
"gs://bucket/gcsPath/foo",
"gs://bucket/gcsPath/bar",
"gs://bucket/gcsPath/bar_1",
"gs://bucket/gcsPath/bar_2",
"gs://bucket/gcsPath/baz.foo",
"gs://bucket/gcsPath/baz_1.foo",
"gs://bucket/gcsPath/baz_2.foo"
),
listOf(
"path/foo",
"path/bar",
"path1/bar",
"path2/bar",
"path/baz.foo",
"path1/baz.foo",
"path2/baz.foo"
).map { filePath ->
GcStorage.upload(
filePath = filePath,
fileBytes = ByteArray(0),
rootGcsBucket = "bucket",
runGcsPath = "gcsPath",
storage = mockk(relaxed = true)
)
}
)
}
}

0 comments on commit 7fc9832

Please sign in to comment.