Skip to content

Commit 8ad3d4f

Browse files
Switch EH to use a bicep file for deployment; Updated benchmarks to work. (Azure#2640)
* Switch EH to use a bicep file for deployment; updated EH benchmarks to use .env files * PR feedback - split out the dotenv logic into a public azure_core_test function; cleaned up bicep file * Removed wasm32 exclusion for find_ancestor_file * Fixed regression in PR * Cleaned up bicep file
1 parent 8e63b14 commit 8ad3d4f

File tree

6 files changed

+252
-291
lines changed

6 files changed

+252
-291
lines changed

.vscode/cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"devicecode",
4040
"docsrs",
4141
"doctest",
42+
"dotenv",
4243
"downcasted",
4344
"downcasting",
4445
"entra",
@@ -201,4 +202,4 @@
201202
]
202203
}
203204
]
204-
}
205+
}

sdk/core/azure_core_test/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,31 @@ impl TestContext {
162162
}
163163
}
164164

165+
/// Imports the contents of a `.env` file if it exists.
166+
///
167+
/// This function searches for a `.env` file starting from the current crate directory and going up to the repository root.
168+
/// It loads the environment variables defined in that file, which can be useful for tests that require specific configurations or secrets.
169+
///
170+
/// Note that if no `.env` file is found, this function does nothing and returns `Ok(())`.
171+
///
172+
/// # Arguments
173+
///
174+
/// * `cargo_dir` - The directory of the Cargo package, typically the value of the `CARGO_MANIFEST_DIR` environment variable.
175+
pub fn load_dotenv_file(cargo_dir: impl AsRef<Path>) -> azure_core::Result<()> {
176+
if let Ok(path) = find_ancestor_file(cargo_dir, ".env") {
177+
tracing::debug!("loading environment variables from {}", path.display());
178+
179+
use azure_core::error::ResultExt as _;
180+
dotenvy::from_filename(&path).with_context(azure_core::error::ErrorKind::Io, || {
181+
format!(
182+
"failed to load environment variables from {}",
183+
path.display()
184+
)
185+
})?;
186+
}
187+
Ok(())
188+
}
189+
165190
/// Finds `name` under `dir` and returns the path to the parent `dir`.
166191
///
167192
/// This function does *not* check the file system.
@@ -182,7 +207,6 @@ fn parent_of<'a>(dir: &'a str, name: &'static str) -> Option<&'a str> {
182207
/// Finds `name` under `dir` and returns the path to the named entry.
183208
///
184209
/// This function does check the file system.
185-
#[cfg(not(target_arch = "wasm32"))]
186210
fn find_ancestor_file(dir: impl AsRef<Path>, name: &str) -> azure_core::Result<PathBuf> {
187211
for dir in dir.as_ref().ancestors() {
188212
let path = dir.join(name);

sdk/core/azure_core_test/src/recorded.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,7 @@ pub async fn start(
7070
);
7171

7272
// Attempt to read any .env file up to the repo root.
73-
#[cfg(not(target_arch = "wasm32"))]
74-
if let Ok(path) = crate::find_ancestor_file(ctx.crate_dir, ".env") {
75-
tracing::debug!("loading environment variables from {}", path.display());
76-
77-
use azure_core::error::ResultExt as _;
78-
dotenvy::from_filename(&path).with_context(azure_core::error::ErrorKind::Io, || {
79-
format!(
80-
"failed to load environment variables from {}",
81-
path.display()
82-
)
83-
})?;
84-
};
73+
crate::load_dotenv_file(ctx.crate_dir)?;
8574

8675
recording.start().await?;
8776

sdk/eventhubs/azure_messaging_eventhubs/benches/benchmarks.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
77
use std::{env, sync::Arc};
88
use tokio::runtime::Runtime;
99

10-
//static INIT_LOGGING: std::sync::Once = std::sync::Once::new();
10+
// static INIT_LOGGING: std::sync::Once = std::sync::Once::new();
1111

1212
fn setup() {
1313
// INIT_LOGGING.call_once(|| {
@@ -21,6 +21,9 @@ fn setup() {
2121
// .with_writer(std::io::stderr)
2222
// .init();
2323
// });
24+
25+
azure_core_test::load_dotenv_file(env!("CARGO_MANIFEST_DIR"))
26+
.expect("Failed to load environment variables from .env file");
2427
}
2528

2629
fn send_batch_benchmark(c: &mut Criterion) {
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
@description('Base name of the resource to be created.')
2+
@minLength(16)
3+
param baseName string = resourceGroup().name
4+
5+
@description('The location where the resources will be created.')
6+
param location string = resourceGroup().location
7+
8+
@description('The suffix for the storage endpoint, typically based on the Azure environment.')
9+
param storageEndpointSuffix string = environment().suffixes.storage
10+
11+
@description('Indicates if the tenant is a TME tenant. If true, local (SAS) authentication is enabled.')
12+
param tenantIsTME bool = false
13+
14+
@description('The client OID to grant access to test resources.')
15+
param testApplicationOid string
16+
17+
var eventhubNamespaceName = 'eh-${baseName}'
18+
var storageAccountName = 'blb${baseName}'
19+
var eventHubName = 'testeventhub'
20+
21+
var eventHubsDataOwnerRoleId = 'f526a384-b230-433a-b45c-95f59c4a2dec'
22+
var blobDataOwnerRoleId = 'b7e6dc6d-f1e8-4753-8033-0f276bb0955b'
23+
var azureContributorRoleId = 'b24988ac-6180-42a0-ab88-20f7382dd24c'
24+
25+
resource eventhubNamespace 'Microsoft.EventHub/namespaces@2024-05-01-preview' = {
26+
name: eventhubNamespaceName
27+
location: location
28+
sku: {
29+
name: 'Standard'
30+
tier: 'Standard'
31+
capacity: 5
32+
}
33+
properties: {
34+
geoDataReplication: {
35+
maxReplicationLagDurationInSeconds: 0
36+
locations: [
37+
{
38+
locationName: location
39+
roleType: 'Primary'
40+
}
41+
]
42+
}
43+
minimumTlsVersion: '1.2'
44+
publicNetworkAccess: 'Enabled'
45+
disableLocalAuth: !tenantIsTME // Disable local auth for TME tenants
46+
zoneRedundant: false
47+
isAutoInflateEnabled: false
48+
maximumThroughputUnits: 0
49+
kafkaEnabled: true
50+
}
51+
resource authorization 'authorizationrules@2024-05-01-preview' = {
52+
name: 'RootManageSharedAccessKey'
53+
properties: {
54+
rights: [
55+
'Listen'
56+
'Manage'
57+
'Send'
58+
]
59+
}
60+
}
61+
resource authorizedListenOnly 'AuthorizationRules@2017-04-01' = {
62+
name: 'ListenOnly'
63+
properties: {
64+
rights: [
65+
'Listen'
66+
]
67+
}
68+
}
69+
70+
resource authorizedSendOnly 'AuthorizationRules@2017-04-01' = {
71+
name: 'SendOnly'
72+
properties: {
73+
rights: [
74+
'Send'
75+
]
76+
}
77+
}
78+
resource eventHub 'eventhubs@2024-05-01-preview' = {
79+
name: eventHubName
80+
properties: {
81+
messageTimestampDescription: {
82+
timestampType: 'LogAppend'
83+
}
84+
retentionDescription: {
85+
cleanupPolicy: 'Delete'
86+
retentionTimeInHours: 24
87+
}
88+
messageRetentionInDays: 1
89+
partitionCount: 4
90+
status: 'Active'
91+
}
92+
resource consumerGroup 'consumergroups@2024-05-01-preview' = {
93+
name: '$Default'
94+
properties: {}
95+
}
96+
97+
resource defaultGroup 'consumergroups@2024-05-01-preview' = {
98+
name: 'defaultGroup'
99+
properties: {}
100+
}
101+
}
102+
103+
resource eventhubNamespace_networkruleset_default 'networkrulesets@2024-05-01-preview' = {
104+
name: 'default'
105+
properties: {
106+
publicNetworkAccess: 'Enabled'
107+
defaultAction: 'Allow'
108+
virtualNetworkRules: []
109+
ipRules: []
110+
trustedServiceAccessEnabled: false
111+
}
112+
}
113+
}
114+
115+
resource roleAssignments_ehDataOwner 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
116+
name: guid(eventhubNamespace.id, 'Azure Event Hubs Data Owner')
117+
118+
properties: {
119+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', eventHubsDataOwnerRoleId) // Azure Event Hubs Data Owner
120+
principalId: testApplicationOid
121+
}
122+
dependsOn: [
123+
eventhubNamespace::eventHub
124+
storageAccount
125+
]
126+
}
127+
128+
resource roleAssignments_contributor 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
129+
name: guid(eventhubNamespace.id, 'Azure Contributor')
130+
131+
properties: {
132+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', azureContributorRoleId) // Azure Contributor
133+
principalId: testApplicationOid
134+
}
135+
dependsOn: [
136+
eventhubNamespace::eventHub
137+
storageAccount
138+
]
139+
}
140+
141+
resource roleAssignments_storageDataOwner 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
142+
name: guid(eventhubNamespace.id, 'Storage Blob Data Owner')
143+
144+
properties: {
145+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', blobDataOwnerRoleId) // Storage Blob Data Owner
146+
principalId: testApplicationOid
147+
}
148+
dependsOn: [
149+
eventhubNamespace::eventHub
150+
storageAccount
151+
]
152+
}
153+
154+
resource storageAccount 'Microsoft.Storage/storageAccounts@2024-01-01' = {
155+
name: storageAccountName
156+
location: location
157+
sku: {
158+
name: 'Standard_LRS'
159+
}
160+
kind: 'BlobStorage'
161+
properties: {
162+
allowCrossTenantReplication: false
163+
minimumTlsVersion: 'TLS1_2'
164+
allowBlobPublicAccess: false
165+
allowSharedKeyAccess: false
166+
networkAcls: {
167+
bypass: 'AzureServices'
168+
virtualNetworkRules: []
169+
ipRules: []
170+
defaultAction: 'Allow'
171+
}
172+
supportsHttpsTrafficOnly: true
173+
encryption: {
174+
services: {
175+
file: {
176+
keyType: 'Account'
177+
enabled: true
178+
}
179+
blob: {
180+
keyType: 'Account'
181+
enabled: true
182+
}
183+
}
184+
keySource: 'Microsoft.Storage'
185+
}
186+
accessTier: 'Hot'
187+
}
188+
resource storageAccount_default 'blobServices@2024-01-01' = {
189+
name: 'default'
190+
properties: {
191+
cors: {
192+
corsRules: []
193+
}
194+
deleteRetentionPolicy: {
195+
allowPermanentDelete: false
196+
enabled: false
197+
}
198+
}
199+
resource storageAccount_blobContainer 'containers@2019-04-01' = {
200+
name: 'container'
201+
properties: {}
202+
}
203+
}
204+
}
205+
206+
// Outputs
207+
output EVENTHUB_NAME string = eventhubNamespace::eventHub.name
208+
output EVENTHUBS_NAMESPACE string = eventhubNamespace.name
209+
210+
output EVENTHUBS_HOST string = replace(
211+
replace(eventhubNamespace.properties.serviceBusEndpoint, ':443/', ''),
212+
'https://',
213+
''
214+
)
215+
//output EVENTHUBS_CONNECTION_STRING string = listKeys(eventHubAuthRule.id, eventHubAuthRule.apiVersion).primaryConnectionString
216+
output AZURE_STORAGE_ACCOUNT_NAME string = storageAccount.name
217+
//output AZURE_STORAGE_ACCOUNT_KEY string = listKeys(storage.id, storage.apiVersion).keys[0].value
218+
output STORAGE_ENDPOINT_SUFFIX string = storageEndpointSuffix
219+
output AZURE_STORAGE_BLOB_ENDPOINT string = storageAccount.properties.primaryEndpoints.blob
220+
output RESOURCE_GROUP string = resourceGroup().name

0 commit comments

Comments
 (0)