diff --git a/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/DdFileUploadTask.kt b/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/DdFileUploadTask.kt index 6cb8cefd..39d6e60a 100644 --- a/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/DdFileUploadTask.kt +++ b/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/DdFileUploadTask.kt @@ -41,7 +41,11 @@ abstract class DdFileUploadTask @Inject constructor( var apiKey: String = "" private val disableGzipOption: Provider = - providerFactory.gradleProperty(DdFileUploadTask.DISABLE_GZIP_GRADLE_PROPERTY) + providerFactory.gradleProperty(DISABLE_GZIP_GRADLE_PROPERTY) + + // needed for functional tests, because we don't have real API key + private val emulateNetworkCall: Provider = + providerFactory.gradleProperty(EMULATE_UPLOAD_NETWORK_CALL) /** * Source of the API key set: environment, gradle property, etc. @@ -170,7 +174,8 @@ abstract class DdFileUploadTask @Inject constructor( buildId = buildId.get() ), repositories.firstOrNull(), - !disableGzipOption.isPresent + !disableGzipOption.isPresent, + emulateNetworkCall.isPresent ) } catch (e: Exception) { caughtErrors.add(e) @@ -298,7 +303,7 @@ abstract class DdFileUploadTask @Inject constructor( } val jsonObject = JSONObject() - jsonObject.put("version", RESPOSITORY_FILE_VERSION) + jsonObject.put("version", REPOSITORY_FILE_VERSION) jsonObject.put("data", data) repositoryFile.parentFile.mkdirs() @@ -306,7 +311,7 @@ abstract class DdFileUploadTask @Inject constructor( } internal companion object { - private const val RESPOSITORY_FILE_VERSION = 1 + private const val REPOSITORY_FILE_VERSION = 1 private const val INDENT = 4 private const val DATADOG_CI_API_KEY_PROPERTY = "apiKey" @@ -316,6 +321,7 @@ abstract class DdFileUploadTask @Inject constructor( internal val LOGGER = Logging.getLogger("DdFileUploadTask") const val DISABLE_GZIP_GRADLE_PROPERTY = "dd-disable-gzip" + const val EMULATE_UPLOAD_NETWORK_CALL = "dd-emulate-upload-call" const val API_KEY_MISSING_ERROR = "Make sure you define an API KEY to upload your mapping files to Datadog. " + "Create a DD_API_KEY or DATADOG_API_KEY environment variable, gradle" + diff --git a/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploader.kt b/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploader.kt index 8f722eeb..8ff59e74 100644 --- a/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploader.kt +++ b/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploader.kt @@ -13,6 +13,7 @@ import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.OkHttpClient +import okhttp3.Protocol import okhttp3.Request import okhttp3.RequestBody import okhttp3.RequestBody.Companion.asRequestBody @@ -52,7 +53,8 @@ internal class OkHttpUploader : Uploader { apiKey: String, identifier: DdAppIdentifier, repositoryInfo: RepositoryInfo?, - useGzip: Boolean + useGzip: Boolean, + emulateNetworkCall: Boolean ) { LOGGER.info("Uploading file ${fileInfo.fileName} with tags $identifier (site=${site.domain}):") if (fileInfo.extraAttributes.isNotEmpty()) { @@ -81,7 +83,16 @@ internal class OkHttpUploader : Uploader { val call = client.newCall(request) val response = try { - call.execute() + if (emulateNetworkCall) { + Response.Builder() + .request(request) + .protocol(Protocol.HTTP_2) + .message("fake-response") + .code(HttpURLConnection.HTTP_ACCEPTED) + .build() + } else { + call.execute() + } } catch (e: Throwable) { LOGGER.error("Error uploading the mapping file for $identifier", e) null diff --git a/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/Uploader.kt b/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/Uploader.kt index 0e601c05..c9dc9473 100644 --- a/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/Uploader.kt +++ b/dd-sdk-android-gradle-plugin/src/main/kotlin/com/datadog/gradle/plugin/internal/Uploader.kt @@ -56,6 +56,7 @@ internal interface Uploader { apiKey: String, identifier: DdAppIdentifier, repositoryInfo: RepositoryInfo?, - useGzip: Boolean + useGzip: Boolean = true, + emulateNetworkCall: Boolean = false ) } diff --git a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdAndroidGradlePluginFunctionalTest.kt b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdAndroidGradlePluginFunctionalTest.kt index e7383f83..160a5a7f 100644 --- a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdAndroidGradlePluginFunctionalTest.kt +++ b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdAndroidGradlePluginFunctionalTest.kt @@ -602,10 +602,11 @@ internal class DdAndroidGradlePluginFunctionalTest { taskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then assertThat(result).containsInOutput("Creating request with GZIP encoding.") @@ -663,10 +664,11 @@ internal class DdAndroidGradlePluginFunctionalTest { "--info", "--stacktrace", "-PDD_API_KEY=fakekey", - "-Pdd-disable-gzip" + "-Pdd-disable-gzip", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then assertThat(result).containsInOutput("Creating request without GZIP encoding.") @@ -727,8 +729,8 @@ internal class DdAndroidGradlePluginFunctionalTest { gradleRunner { withArguments("--info", ":samples:app:assembleRelease") } .build() - val result = gradleRunner { withArguments(taskName, "--info") } - .buildAndFail() + val result = gradleRunner { withArguments(taskName, "--info", "-Pdd-emulate-upload-call") } + .build() // Then val buildIdInOriginFile = testProjectDir.findBuildIdInOriginFile(variant) @@ -783,10 +785,11 @@ internal class DdAndroidGradlePluginFunctionalTest { taskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then val buildIdInOriginFile = testProjectDir.findBuildIdInOriginFile(variant) @@ -838,10 +841,11 @@ internal class DdAndroidGradlePluginFunctionalTest { taskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then val buildIdInOriginFile = testProjectDir.findBuildIdInOriginFile(variant) @@ -902,10 +906,11 @@ internal class DdAndroidGradlePluginFunctionalTest { taskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then assertThat(result).containsInOutput( @@ -985,10 +990,11 @@ internal class DdAndroidGradlePluginFunctionalTest { taskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then val buildIdInOriginFile = testProjectDir.findBuildIdInOriginFile(variant) @@ -1033,10 +1039,11 @@ internal class DdAndroidGradlePluginFunctionalTest { taskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then val buildIdInOriginFile = testProjectDir.findBuildIdInOriginFile(variant) @@ -1086,20 +1093,22 @@ internal class DdAndroidGradlePluginFunctionalTest { ndkSymbolUploadTaskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() val mappingResult = gradleRunner { withArguments( mappingUploadTaskName, "--info", "--stacktrace", - "-PDD_API_KEY=fakekey" + "-PDD_API_KEY=fakekey", + "-Pdd-emulate-upload-call" ) } - .buildAndFail() + .build() // Then val buildIdInOriginFile = testProjectDir.findBuildIdInOriginFile(variant) diff --git a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdMappingFileUploadTaskTest.kt b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdMappingFileUploadTaskTest.kt index e99ba6ff..8681dd18 100644 --- a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdMappingFileUploadTaskTest.kt +++ b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdMappingFileUploadTaskTest.kt @@ -170,7 +170,8 @@ internal class DdMappingFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) assertThat(fakeRepositoryFile.readText()) .isEqualTo( @@ -219,7 +220,8 @@ internal class DdMappingFileUploadTaskTest { ) ), eq(fakeRepoInfo), - useGzip = eq(true) + useGzip = eq(true), + emulateNetworkCall = eq(false) ) assertThat(lastValue.file).hasSameTextualContentAs( fileFromResourcesPath("mapping-with-aliases.txt") @@ -270,7 +272,8 @@ internal class DdMappingFileUploadTaskTest { ) ), eq(fakeRepoInfo), - useGzip = eq(true) + useGzip = eq(true), + emulateNetworkCall = eq(false) ) assertThat(lastValue.file.readLines()).isEqualTo(expectedLines) } @@ -325,7 +328,8 @@ internal class DdMappingFileUploadTaskTest { ) ), eq(fakeRepoInfo), - useGzip = eq(true) + useGzip = eq(true), + emulateNetworkCall = eq(false) ) assertThat(lastValue.file.readLines()).isEqualTo(expectedLines) } @@ -366,7 +370,8 @@ internal class DdMappingFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) assertThat(fakeRepositoryFile.readText()) .isEqualTo( @@ -408,7 +413,8 @@ internal class DdMappingFileUploadTaskTest { buildId = fakeBuildId ), null, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -551,7 +557,8 @@ internal class DdMappingFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) assertThat(fakeRepositoryFile.readText()) .isEqualTo( diff --git a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdNdkSymbolFileUploadTaskTest.kt b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdNdkSymbolFileUploadTaskTest.kt index 95d7b4fc..fe5abbc2 100644 --- a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdNdkSymbolFileUploadTaskTest.kt +++ b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/DdNdkSymbolFileUploadTaskTest.kt @@ -166,7 +166,8 @@ internal class DdNdkSymbolFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -209,7 +210,8 @@ internal class DdNdkSymbolFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } } @@ -253,7 +255,8 @@ internal class DdNdkSymbolFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) Assertions.assertThat(fakeRepositoryFile.readText()) .isEqualTo( @@ -296,7 +299,8 @@ internal class DdNdkSymbolFileUploadTaskTest { buildId = fakeBuildId ), null, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -382,7 +386,6 @@ internal class DdNdkSymbolFileUploadTaskTest { // Given testedTask.site = siteName - val fakeSoFile = writeFakeSoFile("arm64-v8a") // When assertThrows { @@ -429,7 +432,8 @@ internal class DdNdkSymbolFileUploadTaskTest { buildId = fakeBuildId ), fakeRepoInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } diff --git a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploaderTest.kt b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploaderTest.kt index c6a84952..aac7b2e8 100644 --- a/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploaderTest.kt +++ b/dd-sdk-android-gradle-plugin/src/test/kotlin/com/datadog/gradle/plugin/internal/OkHttpUploaderTest.kt @@ -164,7 +164,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) // Then @@ -209,7 +210,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = false + useGzip = false, + emulateNetworkCall = false ) // Then @@ -254,7 +256,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, null, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) // Then @@ -295,7 +298,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -348,7 +352,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -398,7 +403,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -464,7 +470,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -518,7 +525,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -571,7 +579,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } @@ -621,7 +630,8 @@ internal class OkHttpUploaderTest { fakeApiKey, fakeIdentifier, fakeRepositoryInfo, - useGzip = true + useGzip = true, + emulateNetworkCall = false ) } assertThat(exception.message).isEqualTo(