Unverified Commit b6bb3991 authored by Aaron Todd's avatar Aaron Todd Committed by GitHub
Browse files

promote aws-smithy-mocks out of experimental (#4098)

## Motivation and Context
* https://github.com/smithy-lang/smithy-rs/issues/4074
* https://github.com/smithy-lang/smithy-rs/issues/3926

## Description
* Rename `aws-smithy-mocks-experimental` to `aws-smithy-mocks` and reset
version to `0.1.0`
* Fix [#3926](https://github.com/smithy-lang/smithy-rs/issues/3926) by
having `mock_client!` macro always set an HTTP client. By default it
returns a dummy I'm a teapot response to aid in debugging and be easy to
tell where that response is coming from. Also when a rule sets an HTTP
response it is not set as an extension in the request and returned by
the HTTP client. This allows for more thorough and accurate integration
testing as the client/runtime will see the response coming from the HTTP
client instead of being ignored and then replaced as it was before
* Added a new sequence builder API for allowing a rule to generate more
than one response. This allows for testing retries for example.
* Also deleted a hyper 1.x test that is no longer needed and was ignored
anyway

## Questions

* In this PR I've simply renamed/moved `aws-smithy-mocks-experimental`
to `aws-smithy-mocks`. Do we want or need to publish one last version of
`aws-smithy-mocks-experimental` with a deprecated API that instructs
users it has moved? Alternatively we could publish an [informational
advisory notice](https://github.com/RustSec/advisory-db#advisory-format)
about the rename. We don't currently promote the crate anywhere and the
dev guide will get updated so I'm on the fence for what we want to do.

## Testing
* Added new unit tests to exercise the API without needing an SDK client
by leveraging the manual `Operation` APIs.
* Added new integration tests for S3 to cover the macros and test the
library e2e.

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [x] For changes to the smithy-rs codegen or runtime crates, I have
created a changelog entry Markdown file in the `.changelog` directory,
specifying "client," "server," or both in the `applies_to` key.
- [x] For changes to the AWS SDK, generated SDK code, or SDK runtime
crates, I have created a changelog entry Markdown file in the
`.changelog` directory, specifying "aws-sdk-rust" in the `applies_to`
key.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent db90ed1f
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
---
applies_to:
- client
- aws-sdk-rust
authors:
- aajtodd
references:
  - smithy-rs#4074
  - smithy-rs#3926
breaking: false
new_feature: true
bug_fix: true
---
Promote `aws-smithy-mocks-experimental` to `aws-smithy-mocks`. This crate is now a recommended tool for testing
generated SDK clients. This release includes several fixes as well as a new sequence builder API that can be
used to test more complex scenarios such as retries.

```rust
use aws_sdk_s3::operation::get_object::GetObjectOutput;
use aws_sdk_s3::config::retry::RetryConfig;
use aws_smithy_types::byte_stream::ByteStream;
use aws_smithy_mocks::{mock, mock_client, RuleMode};

#[tokio::test]
async fn test_retry_behavior() {
    // Create a rule that returns 503 twice, then succeeds
    let retry_rule = mock!(aws_sdk_s3::Client::get_object)
        .sequence()
        .http_status(503, None)
        .times(2)                                            // Return 503 HTTP status twice
        .output(|| GetObjectOutput::builder()                // Finally return a successful output
            .body(ByteStream::from_static(b"success"))
            .build())
        .build();

    // Create a mocked client with the rule
    let s3 = mock_client!(
        aws_sdk_s3,
        RuleMode::Sequential,
        [&retry_rule],
        |client_builder| {
            client_builder.retry_config(RetryConfig::standard().with_max_attempts(3))
        }
    );

    // This should succeed after two retries
    let result = s3
        .get_object()
        .bucket("test-bucket")
        .key("test-key")
        .send()
        .await
        .expect("success after retries");

    // Verify the response
    let data = result.body.collect().await.expect("successful read").to_vec();
    assert_eq!(data, b"success");

    // Verify all responses were used
    assert_eq!(retry_rule.num_calls(), 3);
}
```
+1 −1
Original line number Diff line number Diff line
@@ -279,7 +279,7 @@ dependencies = [

[[package]]
name = "aws-smithy-http-client"
version = "1.0.1"
version = "1.0.2"
dependencies = [
 "aws-smithy-async",
 "aws-smithy-runtime-api",
+1 −1
Original line number Diff line number Diff line
@@ -252,7 +252,7 @@ dependencies = [

[[package]]
name = "aws-smithy-http-client"
version = "1.0.1"
version = "1.0.2"
dependencies = [
 "aws-smithy-async",
 "aws-smithy-protocol-test",
+2 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Compani
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.TracingSubscriber
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.TracingTest
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.smithyHttpClient
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.smithyMocks
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.smithyProtocolTestHelpers
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.smithyRuntime
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.smithyRuntimeApiTestUtil
@@ -170,5 +171,6 @@ class S3TestDependencies(private val runtimeConfig: RuntimeConfig) : LibRsCustom
            addDependency(TempFile)
            addDependency(TracingAppender)
            addDependency(TracingTest)
            addDependency(smithyMocks(runtimeConfig))
        }
}
+16 −2
Original line number Diff line number Diff line
@@ -892,6 +892,7 @@ dependencies = [
 "aws-smithy-http",
 "aws-smithy-http-client",
 "aws-smithy-json",
 "aws-smithy-mocks",
 "aws-smithy-protocol-test",
 "aws-smithy-runtime",
 "aws-smithy-runtime-api",
@@ -1272,7 +1273,7 @@ version = "0.60.3"

[[package]]
name = "aws-smithy-http-client"
version = "1.0.1"
version = "1.0.2"
dependencies = [
 "aws-smithy-async",
 "aws-smithy-protocol-test",
@@ -1320,9 +1321,22 @@ dependencies = [
 "serde_json",
]

[[package]]
name = "aws-smithy-mocks"
version = "0.1.0"
dependencies = [
 "aws-smithy-async",
 "aws-smithy-http-client",
 "aws-smithy-runtime",
 "aws-smithy-runtime-api",
 "aws-smithy-types",
 "http 1.3.1",
 "tokio",
]

[[package]]
name = "aws-smithy-mocks-experimental"
version = "0.2.3"
version = "0.2.4"
dependencies = [
 "aws-smithy-runtime-api",
 "aws-smithy-types",
Loading