Skip to content

Commit

Permalink
Merge pull request #123 from Azure/bcdr-tests
Browse files Browse the repository at this point in the history
Add 2 tests
  • Loading branch information
Florian Eiden authored Jun 3, 2022
2 parents c5bca66 + 1e72c0b commit 3ab49b4
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 0 deletions.
159 changes: 159 additions & 0 deletions BCDRTests/Test 01 - 1 input - 2 jobs - 1 output/01-provision.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
$location = "Canada Central"
$runId = Get-Date -Format "yyyyMMddHHmmss"
$tempFolder = ".\"

######################################################################
# Resource Group
$rgName = "rg-asa-bcdr-$runId"
New-AzResourceGroup -Name $rgName -Location $location

######################################################################
# Event hub
$ehNamespace = "eh-asa-bcdr-$runId"
$ehNamespaceSku = "Standard"
$ehNamespaceCapacity = 1
$ehName = "eh-input"
$ehPartitionCount = 2
$ehAuthorizationRuleName = "eh-asa-bcdr-$runId"
$ehConsumerGroupName1 = "cg1"
$ehConsumerGroupName2 = "cg2"

New-AzEventHubNamespace -ResourceGroupName $rgName -NamespaceName $ehNamespace -Location $location -SkuName $ehNamespaceSku -SkuCapacity $ehNamespaceCapacity
New-AzEventHubAuthorizationRule -ResourceGroupName $rgName -NamespaceName $ehNamespace -AuthorizationRuleName $ehAuthorizationRuleName -Rights @("Listen","Send")
$ehKey = Get-AzEventHubKey -ResourceGroupName $rgName -Namespace $ehNamespace -AuthorizationRuleName $ehAuthorizationRuleName

New-AzEventHub -ResourceGroupName $rgName -NamespaceName $ehNamespace -Name $ehName -PartitionCount $ehPartitionCount
New-AzEventHubConsumerGroup -ResourceGroupName $rgName -Namespace $ehNamespace -EventHub $ehName -Name $ehConsumerGroupName1
New-AzEventHubConsumerGroup -ResourceGroupName $rgName -Namespace $ehNamespace -EventHub $ehName -Name $ehConsumerGroupName2

######################################################################
# Cosmos DB
$cosmosDBAccountName = "cosmos-asa-bcdr-$runId"
$cosmosDBDatabaseName = "cosmosDB-asa-bcdr-$runId"
$cosmosDBContainerName = "container"
$cosmosDBPartitionKey = "/deviceid"
$cosmosDBPartitionKeyKind = "Hash"
$cosmosDBUniqueKey = "/deviceid_minute"

New-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName -Location $location
$cosmosDBAccountKey = Get-AzCosmosDBAccountKey -ResourceGroupName $rgName -Name $cosmosDBAccountName


New-AzCosmosDBSqlDatabase -ResourceGroupName $rgName -AccountName $cosmosDBAccountName -Name $cosmosDBDatabaseName
$cosmosDBUniqueKeyPolicy = New-AzCosmosDBSqlUniqueKeyPolicy -UniqueKey (New-AzCosmosDBSqlUniqueKey -Path $cosmosDBUniqueKey)
New-AzCosmosDBSqlContainer -ResourceGroupName $rgName -AccountName $cosmosDBAccountName -DatabaseName $cosmosDBDatabaseName -Name $cosmosDBContainerName -PartitionKeyPath $cosmosDBPartitionKey -PartitionKeyKind $cosmosDBPartitionKeyKind -UniqueKeyPolicy $cosmosDBUniqueKeyPolicy

######################################################################
# Stream Analytics Jobs
$asaJobName1 = "asa-asa-bcdr-1-$runId"
$asaJobName2 = "asa-asa-bcdr-2-$runId"
$asaJobSKU = "Standard"
$asaInputName = "ehInput"
$asaOutputName = "cosmosOutput"
$asaJobStreamingUnit = 3
$asaJobQuery1 = "SELECT CAST(DeviceId AS NVARCHAR(MAX)) AS deviceid, COUNT(*) AS count15, SYSTEM.TIMESTAMP() AS windowend, CONCAT(CAST(DeviceId AS NVARCHAR(MAX)),'_',SUBSTRING(SYSTEM.TIMESTAMP(),1,16)) AS deviceid_minute, '$asaJobName1' AS jobname INTO $asaOutputName FROM $asaInputName GROUP BY DeviceId, HOPPING(second,60,10)"
$asaJobQuery2 = "SELECT CAST(DeviceId AS NVARCHAR(MAX)) AS deviceid, COUNT(*) AS count15, SYSTEM.TIMESTAMP() AS windowend, CONCAT(CAST(DeviceId AS NVARCHAR(MAX)),'_',SUBSTRING(SYSTEM.TIMESTAMP(),1,16)) AS deviceid_minute, '$asaJobName2' AS jobname INTO $asaOutputName FROM $asaInputName GROUP BY DeviceId, HOPPING(second,60,10)"


New-AzStreamAnalyticsJob -ResourceGroupName $rgName -Name $asaJobName1 -Location $location -SkuName $asaJobSKU
New-AzStreamAnalyticsJob -ResourceGroupName $rgName -Name $asaJobName2 -Location $location -SkuName $asaJobSKU

$asaInputProperties1 = @{
properties = @{
type = "Stream"
serialization = @{
type = "Json"
properties = @{
encoding = "UTF8"
}
}
compression = @{
type = "None"
}
datasource = @{
type = "Microsoft.EventHub/EventHub"
properties = @{
serviceBusNamespace = $ehNamespace
sharedAccessPolicyName = $ehAuthorizationRuleName
sharedAccessPolicyKey = $ehKey.PrimaryKey
authenticationMode = "ConnectionString"
eventHubName = $ehName
consumerGroupName = $ehConsumerGroupName1
}
}
}
}
$asaInputProperties1 | ConvertTo-JSON -depth 4 | Out-File -Path "$($tempFolder)tempInput1.json"
New-AzStreamAnalyticsInput -ResourceGroupName $rgName -JobName $asaJobName1 -Name $asaInputName -File "$($tempFolder)tempInput1.json"
Remove-Item -Path "$($tempFolder)tempInput1.json"

$asaInputProperties2 = @{
properties = @{
type = "Stream"
serialization = @{
type = "Json"
properties = @{
encoding = "UTF8"
}
}
compression = @{
type = "None"
}
datasource = @{
type = "Microsoft.EventHub/EventHub"
properties = @{
serviceBusNamespace = $ehNamespace
sharedAccessPolicyName = $ehAuthorizationRuleName
sharedAccessPolicyKey = $ehKey.PrimaryKey
authenticationMode = "ConnectionString"
eventHubName = $ehName
consumerGroupName = $ehConsumerGroupName2
}
}
}
}
$asaInputProperties2 | ConvertTo-JSON -depth 4 | Out-File -Path "$($tempFolder)tempInput2.json"
New-AzStreamAnalyticsInput -ResourceGroupName $rgName -JobName $asaJobName2 -Name $asaInputName -File "$($tempFolder)tempInput2.json"
Remove-Item -Path "$($tempFolder)tempInput2.json"


$asaOutputProperties = @{
properties = @{
datasource = @{
type = "Microsoft.Storage/DocumentDB"
properties = @{
accountId = $cosmosDBAccountName
accountKey = $cosmosDBAccountKey.PrimaryMasterKey
collectionNamePattern = $cosmosDBContainerName
database = $cosmosDBDatabaseName
documentId = $cosmosDBUniqueKey.replace("/","")
partitionKey = $cosmosDBPartitionKey.replace("/","")
}
}
}
}

$asaOutputProperties | ConvertTo-JSON -depth 4 | Out-File -Path "$($tempFolder)tempOutput.json"

New-AzStreamAnalyticsOutput -ResourceGroupName $rgName -JobName $asaJobName1 -Name $asaOutputName -File "$($tempFolder)tempOutput.json"
New-AzStreamAnalyticsOutput -ResourceGroupName $rgName -JobName $asaJobName2 -Name $asaOutputName -File "$($tempFolder)tempOutput.json"
Remove-Item -Path "$($tempFolder)tempOutput.json"

New-AzStreamAnalyticsTransformation -ResourceGroupName $rgName -JobName $asaJobName1 -Name $asaJobName1 -StreamingUnit $asaJobStreamingUnit -Query $asaJobQuery1
New-AzStreamAnalyticsTransformation -ResourceGroupName $rgName -JobName $asaJobName2 -Name $asaJobName2 -StreamingUnit $asaJobStreamingUnit -Query $asaJobQuery2

######################################################################
#Gather necessary variables, paste results into next session

Write-Host "`
`$rgName = `"$rgName`"`
`$asaJobName1 = `"$asaJobName1`"`
`$asaJobName2 = `"$asaJobName2`"`
`$ehNamespace = `"$ehNamespace`"`
`$ehName = `"$ehName`"`
`$ehAuthorizationRuleName = `"$ehAuthorizationRuleName`"`
`$ehKey = `"$($ehKey.PrimaryKey)`"`
`$cosmosDBAccountKey = `"$($cosmosDBAccountKey.PrimaryMasterKey)`"`
`$cosmosDBAccountName = `"$cosmosDBAccountName`"`
`$cosmosDBDatabaseName = `"$cosmosDBDatabaseName`"`
`$cosmosDBContainerName = `"$cosmosDBContainerName`""
64 changes: 64 additions & 0 deletions BCDRTests/Test 01 - 1 input - 2 jobs - 1 output/02-run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
######################################################################
#Gather necessary variables from previous terminal if necessary, and paste them into a new window

Write-Host "`
`$rgName = `"$rgName`"`
`$asaJobName1 = `"$asaJobName1`"`
`$asaJobName2 = `"$asaJobName2`"`
`$ehNamespace = `"$ehNamespace`"`
`$ehName = `"$ehName`"`
`$ehAuthorizationRuleName = `"$ehAuthorizationRuleName`"`
`$ehKey = `"$($ehKey.PrimaryKey)`"`
`$cosmosDBAccountKey = `"$($cosmosDBAccountKey.PrimaryMasterKey)`"`
`$cosmosDBAccountName = `"$cosmosDBAccountName`"`
`$cosmosDBDatabaseName = `"$cosmosDBDatabaseName`"`
`$cosmosDBContainerName = `"$cosmosDBContainerName`""


######################################################################
#Starting jobs
Start-AzStreamAnalyticsJob -ResourceGroupName $rgName -Name $asaJobName1
Start-AzStreamAnalyticsJob -ResourceGroupName $rgName -Name $asaJobName2

######################################################################
#Emitting records

##this will block the current terminal
##if necessary, install https://www.powershellgallery.com/packages/Azure.EventHub/

$s = Get-AzureEHSASToken `
-URI "$ehNamespace.servicebus.windows.net/$ehName" `
-AccessPolicyName $ehAuthorizationRuleName `
-AccessPolicyKey $ehKey

while ($True) {
Send-AzureEHDatagram `
-URI "$ehNamespace.servicebus.windows.net/$ehName" `
-SASToken $s `
-Datagram ('{"DeviceId": '+(Get-Random -Maximum 8)+',"readingTimestamp": "'+(Get-Date -Format o)+'", "readingNum":'+(Get-Random -Maximum 1024)+'}')
}

######################################################################
#Observing records in Cosmos DB

##in a new terminal
##if necessary, install https://www.powershellgallery.com/packages/CosmosDB/

$primaryKey = ConvertTo-SecureString -String $cosmosDBAccountKey -AsPlainText -Force
$cosmosDbContext = New-CosmosDbContext -Account $cosmosDBAccountName -Database $cosmosDBDatabaseName -Key $primaryKey

$query = "SELECT * FROM customers c WHERE c.deviceid = '0' ORDER BY c.windowend DESC"
(Get-CosmosDbDocument -Context $cosmosDbContext -CollectionId $cosmosDBContainerName -Query $query) | Select-Object {$_.deviceid_minute,$_.jobname}

## We should obvserve a random alternance of jobnames over time

######################################################################
#Stopping one job

Stop-AzStreamAnalyticsJob -ResourceGroupName $rgName -Name $asaJobName1

## From that point forward, new records will only be associated to the remaining ASA job

$query = "SELECT * FROM customers c WHERE c.deviceid = '0' ORDER BY c.windowend DESC"
(Get-CosmosDbDocument -Context $cosmosDbContext -CollectionId $cosmosDBContainerName -Query $query) | Select-Object {$_.deviceid_minute,$_.jobname}

10 changes: 10 additions & 0 deletions BCDRTests/Test 01 - 1 input - 2 jobs - 1 output/03-cleanup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
######################################################################
#Gather necessary variables from previous terminal if necessary, and paste them into a new window

Write-Host "`
`$rgName = `"$rgName`""

######################################################################
#Delete the resource group

Remove-AzResourceGroup -Name $rgName -Force
Loading

0 comments on commit 3ab49b4

Please sign in to comment.