-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
recorded::test
macro for recorded tests (#1926)
* Rename typespec_derive to typespec_macros Will be more consistent with upcoming azure_core_macros, and may not contain *just* derive macros anyway. Will keep feature as "derive", though, for derive macros. * Remove 128-bit number functions for Cosmos * Implement `#[recorded]` attribute macro * Allow live-only tests with no parameters * Replace test_e2e feature with `recorded` attribute macro * Refactor so that tests need only import azure_core_tests Also makes the attribute `#[recorded::test]` which, IMO, looks a bit better. * Fix build breaks We may need to set env vars for our Windows agents to find OpenSSL, which does appear to be installed. See #1746. For now, I'm removing `--all-features` from this PR since that issue is already tracking adding them separately.
- Loading branch information
Showing
43 changed files
with
572 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[toolchain] | ||
channel = "1.76.0" | ||
channel = "1.80.0" | ||
components = [ | ||
"rustc", | ||
"rustfmt", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
//! Shared utilities for testing client libraries built on `azure_core`. | ||
//! | ||
//! For a comprehensive suite of utilities for testing client libraries built on `azure_core`, | ||
//! read documentation for the `azure_core_test` crate. | ||
use crate::error::{Error, ErrorKind}; | ||
use std::{fmt, str::FromStr}; | ||
|
||
/// Whether to test client methods by playing back recordings, recording live sessions, or executing live sessions without recording. | ||
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] | ||
pub enum TestMode { | ||
/// Test client methods by playing back recordings. | ||
#[default] | ||
Playback, | ||
|
||
/// Record live sessions. | ||
Record, | ||
|
||
/// Execute live sessions without recording. | ||
Live, | ||
} | ||
|
||
impl TestMode { | ||
/// Gets the `TestMode` from the `AZURE_TEST_MODE` environment variable or returns the default if undefined. | ||
pub fn current() -> typespec::Result<Self> { | ||
std::env::var("AZURE_TEST_MODE").map_or_else(|_| Ok(TestMode::default()), |v| v.parse()) | ||
} | ||
} | ||
|
||
impl fmt::Debug for TestMode { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(self.into()) | ||
} | ||
} | ||
|
||
impl From<&TestMode> for &'static str { | ||
fn from(mode: &TestMode) -> Self { | ||
match mode { | ||
TestMode::Playback => "playback", | ||
TestMode::Record => "record", | ||
TestMode::Live => "live", | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for TestMode { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.to_ascii_lowercase().as_str() { | ||
"playback" => Ok(Self::Playback), | ||
"record" => Ok(Self::Record), | ||
"live" => Ok(Self::Live), | ||
_ => Err(Error::message( | ||
ErrorKind::DataConversion, | ||
"expected 'playback', 'record', or 'live'", | ||
)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "azure_core_test" | ||
version = "0.1.0" | ||
description = "Utilities for testing client libraries built on azure_core." | ||
readme = "README.md" | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage = "https://github.com/azure/azure-sdk-for-rust" | ||
documentation = "https://docs.rs/azure_core" | ||
keywords = ["sdk", "azure", "rest", "iot", "cloud"] | ||
categories = ["development-tools::testing"] | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
|
||
[dependencies] | ||
azure_core = { workspace = true, features = ["test"] } | ||
azure_core_test_macros.workspace = true | ||
|
||
[dev-dependencies] | ||
tokio.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Azure client library test utilities | ||
|
||
The types and functions in this crate help test client libraries built on `azure_core`. | ||
|
||
## Client methods | ||
|
||
To test client methods using our [Test Proxy], you can attribute both synchronous and asynchronous (recommend) tests | ||
using the `#[recorded::test]` attribute: | ||
|
||
```rust | ||
use azure_core_test::{recorded, TestContext}; | ||
use azure_core::Result; | ||
|
||
#[recorded::test] | ||
async fn get_secret(ctx: TestContext) -> Result<()> { | ||
todo!() | ||
} | ||
``` | ||
|
||
The `TestContext` parameter is required unless your test function is attribute as `#[recorded::test(live)]` (live-only), | ||
in which case it is optional. You can name the parameter whatever you want. | ||
The `TestContext` parameter is used to initialize an HTTP client to play back or record tests | ||
and provides other information to test functions that may be useful. | ||
|
||
[Test Proxy]: https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#![doc = include_str!("../README.md")] | ||
|
||
/// Live recording and playing back of client library tests. | ||
pub mod recorded { | ||
pub use azure_core_test_macros::test; | ||
} | ||
|
||
pub use azure_core::test::TestMode; | ||
|
||
/// Context information required by recorded client library tests. | ||
/// | ||
/// This context is required for any recorded tests not attributed as `#[recorded::test(live)]` | ||
/// to setup up the HTTP client to record or play back session records. | ||
#[derive(Clone, Debug)] | ||
pub struct TestContext { | ||
test_mode: TestMode, | ||
test_name: &'static str, | ||
} | ||
|
||
impl TestContext { | ||
/// Not intended for use outside the `azure_core` crates. | ||
#[doc(hidden)] | ||
pub fn new(test_mode: TestMode, test_name: &'static str) -> Self { | ||
Self { | ||
test_mode, | ||
test_name, | ||
} | ||
} | ||
|
||
/// Gets the current [`TestMode`]. | ||
pub fn test_mode(&self) -> TestMode { | ||
self.test_mode | ||
} | ||
|
||
/// Gets the current test function name. | ||
pub fn test_name(&self) -> &'static str { | ||
self.test_name | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_content_new() { | ||
let ctx = TestContext::new(TestMode::default(), "test_content_new"); | ||
assert_eq!(ctx.test_mode(), TestMode::Playback); | ||
assert_eq!(ctx.test_name(), "test_content_new"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "azure_core_test_macros" | ||
version = "0.1.0" | ||
description = "Procedural macros for testing client libraries built on azure_core." | ||
readme = "README.md" | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage = "https://github.com/azure/azure-sdk-for-rust" | ||
documentation = "https://docs.rs/azure_core" | ||
keywords = ["azure", "cloud", "iot", "rest", "sdk"] | ||
categories = ["development-tools"] | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
azure_core = { workspace = true, features = ["test"] } | ||
proc-macro2.workspace = true | ||
quote.workspace = true | ||
syn.workspace = true | ||
|
||
[dev-dependencies] | ||
azure_core_test.workspace = true | ||
tokio.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Azure client library test macros | ||
|
||
Macros for testing client libraries built on `azure_core`. | ||
|
||
## Client methods | ||
|
||
To test client methods using our [Test Proxy], you can attribute both synchronous and asynchronous (recommend) tests | ||
using the `#[recorded::test]` attribute: | ||
|
||
```rust | ||
use azure_core_test::{recorded, TestContext}; | ||
use azure_core::Result; | ||
|
||
#[recorded::test] | ||
async fn get_secret(ctx: TestContext) -> Result<()> { | ||
todo!() | ||
} | ||
``` | ||
|
||
The `TestContext` parameter is required unless your test function is attribute as `#[recorded::test(live)]` (live-only), | ||
in which case it is optional. You can name the parameter whatever you want. | ||
The `TestContext` parameter is used to initialize an HTTP client to play back or record tests | ||
and provides other information to test functions that may be useful. | ||
|
||
[Test Proxy]: https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#![doc = include_str!("../README.md")] | ||
|
||
mod test; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
/// Attribute client library tests to play back recordings, record sessions, or execute tests without recording. | ||
/// | ||
/// Read documentation for `azure_core_test` for more information and examples. | ||
#[proc_macro_attribute] | ||
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
test::parse_test(attr.into(), item.into()) | ||
.map_or_else(|e| e.into_compile_error().into(), |v| v.into()) | ||
} |
Oops, something went wrong.