Skip to content

Commit 6c66a8b

Browse files
azabbasiprmathur-microsoft
authored andcommitted
feat(tests): Add test infrastructure and setup.ps1 for local setup (#12719)
* Add test infrastructure and setup
1 parent bf2dc4b commit 6c66a8b

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

sdk/iot/Azure.Iot.Hub.Service/tests/prerequisites/prerequisites.readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
- If it isn't, update it
1010
- Use this link to install [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest])
1111

12+
### 2. Install the Iot extension
13+
14+
- Open powershell window in admin mode.
15+
- Run `az extension list`
16+
- If you see azure-iot extension, update the extension by running `az extension update --name azure-iot`
17+
- If you don't see the azure-iot extension, install it by running `az extension add --name azure-iot`
18+
- See the top-level IoT commands with `az iot -h`
19+
1220
## Maintenance
1321

1422
In order to maintain the functionality of the Setup.ps1 file, make sure this document stays updated with all the required changes if you run/alter this script.

sdk/iot/Azure.Iot.Hub.Service/tests/prerequisites/setup.ps1

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,27 @@ if (-not $appId)
6969
$appId = az ad app create --display-name $AppRegistrationName --native-app --query 'appId' --output tsv
7070
}
7171

72-
$sp = az ad sp list --show-mine --query "[?appId=='$appId'].appId" --output tsv
73-
if (-not $sp)
72+
$spExists = az ad sp list --show-mine --query "[?appId=='$appId'].appId" --output tsv
73+
if (-not $spExists)
7474
{
7575
Write-Host "`nCreating service principal for app $appId`n"
7676
az ad sp create --id $appId --output none
7777
}
7878

79-
# Get test application OID from the service principal
80-
$applicationOId = az ad sp show --id $appId --query "objectId" --output tsv
81-
8279
$rgExists = az group exists --name $ResourceGroup
8380
if ($rgExists -eq "False")
8481
{
8582
Write-Host "`nCreating Resource Group $ResourceGroup in $Region`n"
8683
az group create --name $ResourceGroup --location $Region --output none
8784
}
8885

89-
Write-Host "`nDeploying resources to $ResourceGroup in $Region`n"
90-
91-
$armTemplateFile = Join-Path -Path $PSScriptRoot -ChildPath "../../../test-resources.json"
92-
93-
if (-not (Test-Path $armTemplateFile -PathType leaf))
86+
$hubExists = az iot hub list -g $ResourceGroup --query "[?name=='$IotHubName']" --output tsv --only-show-errors
87+
if (-not $hubExists)
9488
{
95-
throw "`nARM template was not found. Please make sure you have an ARM template file named test-resources.json in the root of the service directory`n"
89+
Write-Host "`nCreating IoT Hub $IotHubName`n"
90+
az iot hub create -n $IotHubName -g $ResourceGroup --location $Region --output none --only-show-errors
9691
}
97-
98-
# Deploy test-resources.json ARM template.
99-
az deployment group create --resource-group $ResourceGroup --name $IotHubName --template-file $armTemplateFile --parameters `
100-
baseName=$IotHubName `
101-
testApplicationOid=$applicationOId `
102-
location=$Region
103-
104-
# Even though the output variable names are all capital letters in the script, ARM turns them into a strange casing
105-
# and we have to use that casing in order to get them from the deployment outputs.
106-
$iotHubConnectionString = az deployment group show -g $ResourceGroup -n $IotHubName --query 'properties.outputs.ioT_HUB_CONNECTIONSTRING.value' --output tsv
107-
$iotHubHostName = az deployment group show -g $ResourceGroup -n $IotHubName --query 'properties.outputs.ioT_HUB_ENDPOINT_URL.value' --output tsv
92+
$iotHubHostName = az iot hub show -n $IotHubName -g $ResourceGroup --query 'properties.hostName' --output tsv --only-show-errors
10893

10994
Write-Host("Set a new client secret for $appId`n")
11095
$appSecret = az ad app credential reset --id $appId --years 2 --query 'password' --output tsv
@@ -115,14 +100,21 @@ Write-Host("Writing user config file - $fileName`n")
115100
$appSecretJsonEscaped = ConvertTo-Json $appSecret
116101
$config = @"
117102
{
118-
"IotHubConnectionString": "$iotHubConnectionString",
119-
"IotHubHostName": "$iotHubHostName",
103+
"IotHubHostName": "https://$($iotHubHostName)",
120104
"ApplicationId": "$appId",
121105
"ClientSecret": $appSecretJsonEscaped,
122106
"TestMode": "Live"
123107
}
124108
"@
125-
126109
$config | Out-File "$PSScriptRoot\..\config\$fileName"
127110

111+
$userSettingsFileSuffix = ".test.assets.config.json"
112+
$userSettingsFileName = "$user$userSettingsFileSuffix"
113+
$userTestAssetSettingsFileName = "$PSScriptRoot\..\config\$userSettingsFileName"
114+
if (-not (Test-Path $userTestAssetSettingsFileName))
115+
{
116+
Write-Host "Creating empty user test assets config file - $userSettingsFileName`n"
117+
New-Item -ItemType File -Path $userTestAssetSettingsFileName -Value "{}" | Out-Null
118+
}
119+
128120
Write-Host "Done!"

sdk/iot/test-resources.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@
6161
"IOT_HUB_ENDPOINT_URL": {
6262
"type": "string",
6363
"value": "[concat('https://', reference(variables('iotHubResourceId'), '2020-03-01').hostName)]"
64-
},
65-
"IOT_HUB_CONNECTION_STRING":{
66-
"type": "string",
67-
"value": "[concat('HostName=', reference(resourceId('Microsoft.Devices/IoTHubs', parameters('baseName')), '2020-03-01').hostName, ';SharedAccessKeyName=iothubowner;SharedAccessKey=', listKeys(resourceId('Microsoft.Devices/IotHubs', parameters('baseName')), '2020-03-01').value[0].primaryKey)]"
6864
}
6965
}
7066
}

sdk/iot/test.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trigger: none
2+
3+
resources:
4+
repositories:
5+
- repository: azure-sdk-tools
6+
type: github
7+
name: Azure/azure-sdk-tools
8+
endpoint: azure
9+
10+
jobs:
11+
- template: ../../eng/pipelines/templates/jobs/archetype-sdk-tests.yml
12+
parameters:
13+
ServiceDirectory: iot
14+
Location: westcentralus
15+
SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources-preview)
16+
EnvVars:
17+
# Runs live tests.
18+
AZURE_IOT_TEST_MODE: Live

0 commit comments

Comments
 (0)