Skip to content

Commit

Permalink
feat: smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez committed Aug 23, 2024
1 parent 0b836a5 commit 602bac1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/ab4f3405-e013-4caf-89d7-1d820bcdd228.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "ab4f3405-e013-4caf-89d7-1d820bcdd228",
"type": "feature",
"description": "Add support for smoke tests"
}
1 change: 1 addition & 0 deletions codegen/aws-sdk-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
api(libs.smithy.aws.cloudformation.traits)
api(libs.smithy.protocol.test.traits)
implementation(libs.smithy.aws.endpoints)
implementation(libs.smithy.smoke.test.traits)

testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.jupiter.params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
*/
package aws.sdk.kotlin.codegen

import software.amazon.smithy.kotlin.codegen.KotlinSettings
import software.amazon.smithy.kotlin.codegen.core.*
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
import software.amazon.smithy.kotlin.codegen.model.hasTrait
import software.amazon.smithy.kotlin.codegen.rendering.GradleWriter
import software.amazon.smithy.kotlin.codegen.rendering.smoketests.smokeTestDenyList
import software.amazon.smithy.kotlin.codegen.utils.operations
import software.amazon.smithy.model.Model
import software.amazon.smithy.smoketests.traits.SmokeTestsTrait
import java.util.concurrent.atomic.AtomicBoolean

// TODO - would be nice to allow integrations to define custom settings in the plugin
// e.g. we could then more consistently apply this integration if we could define a property like: `build.isAwsSdk: true`
Expand All @@ -16,6 +23,14 @@ import software.amazon.smithy.kotlin.codegen.rendering.GradleWriter
*/
class GradleGenerator : KotlinIntegration {

private var hasSmokeTests = AtomicBoolean(false)

// Only used to access settings - will always be true
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean {
hasSmokeTests.set(model.operations(settings.service).any { it.hasTrait<SmokeTestsTrait>() })
return super.enabledForService(model, settings)
}

// Specify to run last, to ensure all other integrations have had a chance to register dependencies.
override val order: Byte
get() = 127
Expand Down Expand Up @@ -50,6 +65,10 @@ class GradleGenerator : KotlinIntegration {
writer
.write("")
.withBlock("kotlin {", "}") {
if (hasSmokeTests.get() && !smokeTestDenyList.contains(ctx.settings.sdkId)) {
generateSmokeTestJarTask(writer)
generateSmokeTestTask(writer)
}
withBlock("sourceSets {", "}") {
allDependencies
.sortedWith(compareBy({ it.config }, { it.artifact }))
Expand All @@ -69,4 +88,41 @@ class GradleGenerator : KotlinIntegration {
val contents = writer.toString()
delegator.fileManifest.writeFile("build.gradle.kts", contents)
}

private fun generateSmokeTestJarTask(writer: GradleWriter) {
val moneySign = "$"
writer.withBlock("jvm {", "}") {
withBlock("compilations {", "}") {
write("val main = getByName(\"main\")")
withBlock("tasks {", "}") {
withBlock("register<Jar>(\"smokeTestJar\") {", "}") {
write("description = \"Creates smoke tests jar\"")
write("group = \"application\"")
write("dependsOn(build)")
withBlock("manifest {", "}") {
write("attributes[\"Main-Class\"] = \"smoketests.SmokeTestsKt\"")
}
write("duplicatesStrategy = DuplicatesStrategy.EXCLUDE")
write("from(configurations.getByName(\"jvmRuntimeClasspath\").map { if (it.isDirectory) it else zipTree(it) }, main.output.classesDirs)")
write("archiveBaseName.set(\"$moneySign{project.name}-smoketests\")")
}
}
}
}
writer.emptyLine()
}

private fun generateSmokeTestTask(writer: GradleWriter) {
val moneySign = "$"
writer.withBlock("tasks.register<JavaExec>(\"smokeTest\") {", "}") {
write("description = \"Runs smoke tests jar\"")
write("group = \"verification\"")
write("dependsOn(tasks.getByName(\"smokeTestJar\"))")
emptyLine()
write("val sdkVersion: String by project")
write("val jarFile = file(\"build/libs/$moneySign{project.name}-smoketests-\$sdkVersion.jar\")")
write("classpath = files(jarFile)")
}
writer.emptyLine()
}
}
4 changes: 4 additions & 0 deletions codegen/sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ val stageSdks = tasks.register("stageSdks") {
from("$projectionOutputDir/src")
into("${it.destinationDir}/generated-src")
}
copy {
from("$projectionOutputDir/jvm-src")
into("${it.destinationDir}/generated-src-jvm")
}
copy {
from("$projectionOutputDir/build.gradle.kts")
into(it.destinationDir)
Expand Down
4 changes: 4 additions & 0 deletions services/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ subprojects {
kotlin.srcDir("generated-src/main/kotlin")
}

getByName("jvmMain") {
kotlin.srcDir("generated-src-jvm/main/java")
}

getByName("commonTest") {
kotlin.srcDir("generated-src/test")

Expand Down

0 comments on commit 602bac1

Please sign in to comment.