Skip to content

Commit

Permalink
Use gradle connector instead of process builder
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez committed Sep 20, 2024
1 parent ca30211 commit 08356db
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ class GradleGenerator : KotlinIntegration {
private fun generateSmokeTestTask(writer: GradleWriter, ctx: CodegenContext) {
val hasSuccessResponseTrait = ctx.model.expectShape<ServiceShape>(ctx.settings.service).hasTrait(SuccessResponseTrait.ID)
val hasFailedResponseTrait = ctx.model.expectShape<ServiceShape>(ctx.settings.service).hasTrait(FailedResponseTrait.ID)

// E2E tests don't have sdkVersion in jar names
val inTestingEnvironment = hasFailedResponseTrait || hasSuccessResponseTrait

/**
* E2E tests don't have sdkVersion in jar names. They're added later for publishing.
* @see SmokeTestE2ETest
*/
val jarName = if (inTestingEnvironment) "\${project.name}-smoketests.jar" else "\${project.name}-smoketests-\$sdkVersion.jar"

writer.withBlock("tasks.register<JavaExec>(#S) {", "}", "smokeTest") {
Expand Down
10 changes: 9 additions & 1 deletion tests/codegen/smoke-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ fun configureProject() {

dependencies {
codegen(project(":codegen:aws-sdk-codegen"))
implementation(libs.smithy.kotlin.codegen)
codegen(libs.smithy.cli)
codegen(libs.smithy.model)

implementation(libs.smithy.kotlin.codegen)
implementation("org.gradle:gradle-tooling-api:+")

testImplementation(libs.kotlin.test)
}
}
Expand Down Expand Up @@ -93,6 +95,12 @@ fun configureTasks() {
tasks.build {
dependsOn(tasks.getByName("stageServices"))
}

tasks.clean {
this@Build_gradle.projections.forEach { projection ->
delete("services/${projection.name}")
}
}
}

/**
Expand Down
46 changes: 21 additions & 25 deletions tests/codegen/smoke-tests/src/test/kotlin/SmokeTestE2ETest.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import org.gradle.tooling.GradleConnector
import software.amazon.smithy.kotlin.codegen.rendering.smoketests.SERVICE_FILTER
import software.amazon.smithy.kotlin.codegen.rendering.smoketests.SKIP_TAGS
import java.io.ByteArrayOutputStream
import java.io.File
import kotlin.test.*

class SmokeTestE2ETest {
@Test
fun successService() {
val smokeTestRunnerOutput = runSmokeTests("successService")
val smokeTestRunnerOutput = runSmokeTests()

assertContains(smokeTestRunnerOutput, "ok SuccessService SuccessTest - no error expected from service")
assertContains(smokeTestRunnerOutput, "ok SuccessService SuccessTestWithTags - no error expected from service")
}

@Test
fun failureService() {
val smokeTestRunnerOutput = runSmokeTests("failureService")
val smokeTestRunnerOutput = runSmokeTests()

assertContains(smokeTestRunnerOutput, "ok FailureService FailuresTest - error expected from service")
}

@Test
fun successServiceSkipTags() {
val envVars = mapOf(SKIP_TAGS to "success")
val smokeTestRunnerOutput = runSmokeTests("successService", envVars)
val smokeTestRunnerOutput = runSmokeTests(envVars)

assertContains(smokeTestRunnerOutput, "ok SuccessService SuccessTest - no error expected from service")
assertContains(smokeTestRunnerOutput, "ok SuccessService SuccessTestWithTags - no error expected from service # skip")
Expand All @@ -31,34 +33,28 @@ class SmokeTestE2ETest {
@Test
fun successServiceServiceFilter() {
val envVars = mapOf(SERVICE_FILTER to "Failure") // Only run tests for services with this SDK ID
val smokeTestRunnerOutput = runSmokeTests("successService", envVars)
val smokeTestRunnerOutput = runSmokeTests(envVars)

assertContains(smokeTestRunnerOutput, "ok SuccessService SuccessTest - no error expected from service # skip")
assertContains(smokeTestRunnerOutput, "ok SuccessService SuccessTestWithTags - no error expected from service # skip")
}
}

private fun runSmokeTests(service: String, envVars: Map<String, String> = emptyMap()): String {
val sdkRootDir = System.getProperty("user.dir") + "/../../../"

val output = StringBuilder()
val smokeTests = ProcessBuilder()
.command("ls")
.directory(File(sdkRootDir))
.redirectErrorStream(true)

envVars.forEach { (key, value) ->
smokeTests
.environment()
.put(key, value)
private fun runSmokeTests(envVars: Map<String, String> = emptyMap()): String {
val currentDir = System.getProperty("user.dir")
val outputStream = ByteArrayOutputStream()
val connector = GradleConnector.newConnector()
.forProjectDirectory(File(currentDir))
.connect()

connector.use {
it.newBuild()
.forTasks("smokeTest")
.setStandardOutput(outputStream)
.setStandardError(outputStream)
.setEnvironmentVariables(envVars)
.run()
}

smokeTests
.start()
.inputStream
.bufferedReader()
.forEachLine { output.append("$it\n") }

throw Exception(output.toString())
return output.toString()
return outputStream.toString()
}

0 comments on commit 08356db

Please sign in to comment.