|
| 1 | +package aws.sdk.kotlin.codegen.smoketests |
| 2 | + |
| 3 | +import aws.sdk.kotlin.codegen.AwsRuntimeTypes |
| 4 | +import software.amazon.smithy.kotlin.codegen.KotlinSettings |
| 5 | +import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes |
| 6 | +import software.amazon.smithy.kotlin.codegen.core.getContextValue |
| 7 | +import software.amazon.smithy.kotlin.codegen.core.withBlock |
| 8 | +import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration |
| 9 | +import software.amazon.smithy.kotlin.codegen.integration.SectionWriterBinding |
| 10 | +import software.amazon.smithy.kotlin.codegen.model.hasTrait |
| 11 | +import software.amazon.smithy.kotlin.codegen.rendering.smoketests.* |
| 12 | +import software.amazon.smithy.kotlin.codegen.utils.topDownOperations |
| 13 | +import software.amazon.smithy.model.Model |
| 14 | +import software.amazon.smithy.smoketests.traits.SmokeTestsTrait |
| 15 | + |
| 16 | +/** |
| 17 | + * Generates AWS specific code for smoke test runners |
| 18 | + */ |
| 19 | +class AwsSmokeTestsRunnerGeneratorIntegration : KotlinIntegration { |
| 20 | + override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = |
| 21 | + model.topDownOperations(settings.service).any { it.hasTrait<SmokeTestsTrait>() } |
| 22 | + |
| 23 | + override val sectionWriters: List<SectionWriterBinding> |
| 24 | + get() = listOf( |
| 25 | + AwsSmokeTestsRunnerGenerator.regionEnvironmentVariable, |
| 26 | + AwsSmokeTestsRunnerGenerator.clientConfig, |
| 27 | + AwsSmokeTestsRunnerGenerator.defaultClientConfig, |
| 28 | + AwsSmokeTestsRunnerGenerator.skipTagsEnvironmentVariable, |
| 29 | + AwsSmokeTestsRunnerGenerator.serviceFilterEnvironmentVariable, |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * The section writer bindings used by [AwsSmokeTestsRunnerGeneratorIntegration] |
| 35 | + */ |
| 36 | +private object AwsSmokeTestsRunnerGenerator { |
| 37 | + /** |
| 38 | + * Adds region environment variable support to AWS smoke test runners. |
| 39 | + * Preserves other environment variables added via section writer binding, if any. |
| 40 | + */ |
| 41 | + val regionEnvironmentVariable = |
| 42 | + SectionWriterBinding(SmokeTestSectionIds.AdditionalEnvironmentVariables) { writer, previous -> |
| 43 | + writer.write("#L", previous) |
| 44 | + writer.write( |
| 45 | + "private val regionOverride = #T.System.getenv(#S)", |
| 46 | + RuntimeTypes.Core.Utils.PlatformProvider, |
| 47 | + AWS_REGION, |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Add AWS specific client config support to AWS smoke test runners |
| 53 | + */ |
| 54 | + val clientConfig = |
| 55 | + SectionWriterBinding(SmokeTestSectionIds.ClientConfig) { writer, _ -> |
| 56 | + val name = writer.getContextValue(SmokeTestSectionIds.ClientConfig.Name) |
| 57 | + val value = writer.getContextValue(SmokeTestSectionIds.ClientConfig.Value) |
| 58 | + |
| 59 | + // Normalize client config names |
| 60 | + val newName = when (name) { |
| 61 | + "uri" -> "endpointProvider" |
| 62 | + "useDualstack" -> "useDualStack" |
| 63 | + "sigv4aRegionSet" -> "sigV4aSigningRegionSet" |
| 64 | + "useAccountIdRouting" -> "accountIdEndpointMode" |
| 65 | + "useAccelerate" -> "enableAccelerate" |
| 66 | + "useMultiRegionAccessPoints" -> "disableMrap" |
| 67 | + "useGlobalEndpoint" -> { |
| 68 | + writer.write("throw #T(#S)", RuntimeTypes.Core.SmokeTests.SmokeTestsException, "'useGlobalEndpoint' is not supported by the SDK") |
| 69 | + return@SectionWriterBinding |
| 70 | + } |
| 71 | + else -> name |
| 72 | + } |
| 73 | + writer.writeInline("#L = ", newName) |
| 74 | + |
| 75 | + // Normalize client values |
| 76 | + when (newName) { |
| 77 | + "endpointProvider" -> { |
| 78 | + val endpointProvider = writer.getContextValue(SmokeTestSectionIds.ClientConfig.EndpointProvider) |
| 79 | + val endpointParameters = writer.getContextValue(SmokeTestSectionIds.ClientConfig.EndpointParams) |
| 80 | + |
| 81 | + writer.withBlock("object : #T {", "}", endpointProvider) { |
| 82 | + write( |
| 83 | + "override suspend fun resolveEndpoint(params: #1T): #2T = #2T(#3L)", |
| 84 | + endpointParameters, |
| 85 | + RuntimeTypes.SmithyClient.Endpoints.Endpoint, |
| 86 | + value, |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + "sigV4aSigningRegionSet" -> { |
| 91 | + // Render new value |
| 92 | + writer.write("#L.toSet()", value) |
| 93 | + // Also configure sigV4a - TODO: Remove once sigV4a is supported for default signer. |
| 94 | + writer.write( |
| 95 | + "authSchemes = listOf(#T(#T))", |
| 96 | + RuntimeTypes.Auth.HttpAuthAws.SigV4AsymmetricAuthScheme, |
| 97 | + RuntimeTypes.Auth.AwsSigningCrt.CrtAwsSigner, |
| 98 | + ) |
| 99 | + } |
| 100 | + "accountIdEndpointMode" -> { |
| 101 | + when (value) { |
| 102 | + "true" -> writer.write("#T.REQUIRED", AwsRuntimeTypes.Config.Endpoints.AccountIdEndpointMode) |
| 103 | + "false" -> writer.write("#T.DISABLED", AwsRuntimeTypes.Config.Endpoints.AccountIdEndpointMode) |
| 104 | + } |
| 105 | + } |
| 106 | + "disableMrap" -> { |
| 107 | + when (value) { |
| 108 | + "true" -> writer.write("false") |
| 109 | + "false" -> writer.write("true") |
| 110 | + } |
| 111 | + } |
| 112 | + "region" -> { |
| 113 | + writer.write("regionOverride ?: #L", value) |
| 114 | + } |
| 115 | + else -> writer.write("#L", value) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Add default client config to AWS smoke test runners. |
| 121 | + * Preserves previous default config if any. |
| 122 | + */ |
| 123 | + val defaultClientConfig = |
| 124 | + SectionWriterBinding(SmokeTestSectionIds.DefaultClientConfig) { writer, previous -> |
| 125 | + writer.write("#L", previous) |
| 126 | + writer.write("region = regionOverride") |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Replaces environment variable with one specific to AWS smoke test runners |
| 131 | + */ |
| 132 | + val skipTagsEnvironmentVariable = |
| 133 | + SectionWriterBinding(SmokeTestSectionIds.SkipTags) { writer, _ -> writer.writeInline("#S", AWS_SKIP_TAGS) } |
| 134 | + |
| 135 | + /** |
| 136 | + * Replaces environment variable with one specific to AWS smoke test runners |
| 137 | + */ |
| 138 | + val serviceFilterEnvironmentVariable = |
| 139 | + SectionWriterBinding(SmokeTestSectionIds.ServiceFilter) { writer, _ -> writer.writeInline("#S", AWS_SERVICE_FILTER) } |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Env var for AWS smoke test runners. |
| 144 | + * Should be a string that corresponds to an AWS region. |
| 145 | + * The region to use when executing smoke tests. This value MUST override any value supplied in the smoke tests themselves. |
| 146 | + */ |
| 147 | +private const val AWS_REGION = "AWS_SMOKE_TEST_REGION" |
| 148 | + |
| 149 | +/** |
| 150 | + * Env var for AWS smoke test runners. |
| 151 | + * Should be a comma-delimited list of strings that correspond to tags on the test cases. |
| 152 | + * If a test case is tagged with one of the tags indicated by AWS_SMOKE_TEST_SKIP_TAGS, it MUST be skipped by the smoke test runner. |
| 153 | + */ |
| 154 | +const val AWS_SKIP_TAGS = "AWS_SMOKE_TEST_SKIP_TAGS" |
| 155 | + |
| 156 | +/** |
| 157 | + * Env var for AWS smoke test runners. |
| 158 | + * Should be a comma-separated list of service identifiers to test. |
| 159 | + */ |
| 160 | +const val AWS_SERVICE_FILTER = "AWS_SMOKE_TEST_SERVICE_IDS" |
0 commit comments