Skip to content

Commit

Permalink
Merge pull request #255 from DataDog/nogorodnikov/rum-4374/emulate-up…
Browse files Browse the repository at this point in the history
…load-call-for-functional-tests

RUM-4374: Emulate upload network call for functional tests
  • Loading branch information
0xnm authored May 6, 2024
2 parents b1ee483 + c3626c4 commit 46ee416
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ abstract class DdFileUploadTask @Inject constructor(
var apiKey: String = ""

private val disableGzipOption: Provider<String> =
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<String> =
providerFactory.gradleProperty(EMULATE_UPLOAD_NETWORK_CALL)

/**
* Source of the API key set: environment, gradle property, etc.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -298,15 +303,15 @@ 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()
repositoryFile.writeText(jsonObject.toString(0))
}

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"
Expand All @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ internal interface Uploader {
apiKey: String,
identifier: DdAppIdentifier,
repositoryInfo: RepositoryInfo?,
useGzip: Boolean
useGzip: Boolean = true,
emulateNetworkCall: Boolean = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ internal class DdMappingFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
assertThat(fakeRepositoryFile.readText())
.isEqualTo(
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -270,7 +272,8 @@ internal class DdMappingFileUploadTaskTest {
)
),
eq(fakeRepoInfo),
useGzip = eq(true)
useGzip = eq(true),
emulateNetworkCall = eq(false)
)
assertThat(lastValue.file.readLines()).isEqualTo(expectedLines)
}
Expand Down Expand Up @@ -325,7 +328,8 @@ internal class DdMappingFileUploadTaskTest {
)
),
eq(fakeRepoInfo),
useGzip = eq(true)
useGzip = eq(true),
emulateNetworkCall = eq(false)
)
assertThat(lastValue.file.readLines()).isEqualTo(expectedLines)
}
Expand Down Expand Up @@ -366,7 +370,8 @@ internal class DdMappingFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
assertThat(fakeRepositoryFile.readText())
.isEqualTo(
Expand Down Expand Up @@ -408,7 +413,8 @@ internal class DdMappingFileUploadTaskTest {
buildId = fakeBuildId
),
null,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
}

Expand Down Expand Up @@ -551,7 +557,8 @@ internal class DdMappingFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
assertThat(fakeRepositoryFile.readText())
.isEqualTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ internal class DdNdkSymbolFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
}

Expand Down Expand Up @@ -209,7 +210,8 @@ internal class DdNdkSymbolFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
}
}
Expand Down Expand Up @@ -253,7 +255,8 @@ internal class DdNdkSymbolFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
Assertions.assertThat(fakeRepositoryFile.readText())
.isEqualTo(
Expand Down Expand Up @@ -296,7 +299,8 @@ internal class DdNdkSymbolFileUploadTaskTest {
buildId = fakeBuildId
),
null,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
}

Expand Down Expand Up @@ -382,7 +386,6 @@ internal class DdNdkSymbolFileUploadTaskTest {

// Given
testedTask.site = siteName
val fakeSoFile = writeFakeSoFile("arm64-v8a")

// When
assertThrows<IllegalStateException> {
Expand Down Expand Up @@ -429,7 +432,8 @@ internal class DdNdkSymbolFileUploadTaskTest {
buildId = fakeBuildId
),
fakeRepoInfo,
useGzip = true
useGzip = true,
emulateNetworkCall = false
)
}

Expand Down
Loading

0 comments on commit 46ee416

Please sign in to comment.