Skip to content

Commit

Permalink
Update SmokeTestDecoratorTest (smithy-lang#3840)
Browse files Browse the repository at this point in the history
## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here -->
Add tests the were skipped in
smithy-lang#3836 in favor of merging
and unblocking customer.

## Description
<!--- Describe your changes in detail -->
Testing our smoketest generator with the `UseDualStack` and `UseFips`
endpoint built-ins. Previously the smoketest generator assumed that
these were always available, but they are only available if those
built-ins are included in the endpoints rule set of the model. We now
test the smoke test with both of these built-ins, with each by itself,
and with neither.

## Testing
<!--- Please describe in detail how you tested your changes -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->


## Checklist
Just a test change, no changelog

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: ysaito1001 <awsaito@amazon.com>
  • Loading branch information
landonxjames and ysaito1001 authored Sep 18, 2024
1 parent b62000e commit 6b42eb5
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class SmokeTestsInstantiator(
) {
private val model = codegenContext.model
private val symbolProvider = codegenContext.symbolProvider

// Get list of the built-ins actually included in the model
private val builtInParamNames: List<String> by lazy {
val index = EndpointRulesetIndex.of(codegenContext.model)
val rulesOrNull = index.endpointRulesForService(codegenContext.serviceShape)
Expand Down Expand Up @@ -210,6 +212,8 @@ class SmokeTestsInstantiator(
"Region" to AwsRuntimeType.awsTypes(rc).resolve("region::Region"),
)

// The `use_dual_stack` and `use_fips` fields will only exist on the endpoint params if they built-ins are
// included in the model, so we check for that before setting them.
if (builtInParamNames.contains(dualStackName)) {
rust(".use_dual_stack(${params.useDualstack()})")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package software.amazon.smithy.rustsdk

import org.junit.jupiter.api.Test
import software.amazon.smithy.build.PluginContext
import software.amazon.smithy.model.Model
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenVisitor
import software.amazon.smithy.rust.codegen.client.smithy.customizations.NoAuthDecorator
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
Expand All @@ -24,16 +25,104 @@ import software.amazon.smithy.rust.codegen.core.util.toSnakeCase

class SmokeTestsDecoratorTest {
companion object {
val model =
"""
val imports = """
namespace test
use aws.api#service
use smithy.test#smokeTests
use aws.auth#sigv4
use aws.protocols#restJson1
use smithy.rules#endpointRuleSet
"""
val traitsWithAllBuiltIns =
"""
@service(sdkId: "dontcare")
@restJson1
@sigv4(name: "dontcare")
@auth([sigv4])
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
"Endpoint": {
"builtIn": "SDK::Endpoint",
"required": false,
"documentation": "Override the endpoint used to send this request",
"type": "String"
},
"UseFIPS": {
"builtIn": "AWS::UseFIPS",
"required": true,
"default": false,
"type": "Boolean"
},
"UseDualStack": {
"builtIn": "AWS::UseDualStack",
"required": true,
"default": false,
"type": "Boolean"
},
}
})
"""

val traitsWithNoDualStack =
"""
@service(sdkId: "dontcare")
@restJson1
@sigv4(name: "dontcare")
@auth([sigv4])
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
"Endpoint": {
"builtIn": "SDK::Endpoint",
"required": false,
"documentation": "Override the endpoint used to send this request",
"type": "String"
},
"UseFIPS": {
"builtIn": "AWS::UseFIPS",
"required": true,
"default": false,
"type": "Boolean"
},
}
})
"""

val traitsWithNoFips =
"""
@service(sdkId: "dontcare")
@restJson1
@sigv4(name: "dontcare")
@auth([sigv4])
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
"Endpoint": {
"builtIn": "SDK::Endpoint",
"required": false,
"documentation": "Override the endpoint used to send this request",
"type": "String"
},
"UseDualStack": {
"builtIn": "AWS::UseDualStack",
"required": true,
"default": false,
"type": "Boolean"
},
}
})
"""

val traitsWithNeither =
"""
@service(sdkId: "dontcare")
@restJson1
@sigv4(name: "dontcare")
Expand All @@ -45,6 +134,9 @@ class SmokeTestsDecoratorTest {
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
}
})
"""

val serviceDef = """
service TestService {
version: "2023-01-01",
operations: [SomeOperation]
Expand All @@ -54,6 +146,7 @@ class SmokeTestsDecoratorTest {
{
id: "SomeOperationSuccess",
params: {}
vendorParamsShape: "aws.test#AwsVendorParams",
vendorParams: {
region: "us-west-2"
}
Expand All @@ -62,6 +155,7 @@ class SmokeTestsDecoratorTest {
{
id: "SomeOperationFailure",
params: {}
vendorParamsShape: "aws.test#AwsVendorParams",
vendorParams: {
region: "us-west-2"
}
Expand All @@ -70,6 +164,7 @@ class SmokeTestsDecoratorTest {
{
id: "SomeOperationFailureExplicitShape",
params: {}
vendorParamsShape: "aws.test#AwsVendorParams",
vendorParams: {
region: "us-west-2"
}
Expand All @@ -94,11 +189,34 @@ class SmokeTestsDecoratorTest {
@error("server")
structure FooException { }
""".asSmithyModel(smithyVersion = "2")
"""
val model = (imports + traitsWithAllBuiltIns + serviceDef).asSmithyModel(smithyVersion = "2")
val modelWithNoDualStack = (imports + traitsWithNoDualStack + serviceDef).asSmithyModel(smithyVersion = "2")
val modelWithNoFips = (imports + traitsWithNoFips + serviceDef).asSmithyModel(smithyVersion = "2")
val modelWithNeitherBuiltIn = (imports + traitsWithNeither + serviceDef).asSmithyModel(smithyVersion = "2")
}

@Test
fun smokeTestSdkCodegen() {
testSmokeTestsWithModel(model)
}

@Test
fun smokeTestSdkCodegenNoDualStack() {
testSmokeTestsWithModel(modelWithNoDualStack)
}

@Test
fun smokeTestSdkCodegenNoFips() {
testSmokeTestsWithModel(modelWithNoFips)
}

@Test
fun smokeTestSdkCodegenNeitherBuiltIn() {
testSmokeTestsWithModel(modelWithNeitherBuiltIn)
}

fun testSmokeTestsWithModel(model: Model) {
val codegenContext = testClientCodegenContext(model)
val smokeTestedOperations = operationToTestCases(model)
awsSdkIntegrationTest(
Expand All @@ -107,9 +225,11 @@ class SmokeTestsDecoratorTest {
// `SdkSmokeTestsRustClientCodegenPlugin` only uses the minimal set of codegen decorators, which results
// in a significant amount of unused code. This can cause `clippy` to fail with the `--deny warnings`
// setting enabled by default in `.crate/config.toml` in test workspaces.
// To work around this issue, we unset `RUSTFLAGS` to allow unused and dead code.
// To work around this issue, we unset `RUSTFLAGS` to allow unused and dead code. To perform a compilation
// only test, we don't need to set `mapOf(Pair("RUSTFLAGS", "--cfg smoketests"))` since that would
// cause the tests to actually run (against a non-existent service) and fail.
environment = mapOf(Pair("RUSTFLAGS", "")),
test = { _, crate ->
test = { codegenContext, crate ->
// It should compile. We can't run the tests because they don't target a real service.
// They are skipped because the `smoketests` flag is unset for `rustc` in the `cargo test`
// invocation specified by `awsIntegrationTestParams`.
Expand All @@ -123,7 +243,12 @@ class SmokeTestsDecoratorTest {
// will suffice for the test.
configBuilderInitializer = { ->
writable {
rust("let conf = config::Builder::new()")
rust(
"""
let conf = config::Builder::new().build();
let params = ${codegenContext.moduleUseName()}::config::endpoint::Params::builder()
""".trimIndent(),
)
}
},
)
Expand Down

0 comments on commit 6b42eb5

Please sign in to comment.