Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): Add test infrastructure and setup.ps1 for local setup #12719

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sdk/iot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.config.json
!*common.config.json
!*common.test.assets.config.json
*.etl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Prerequisites

## Install

### 1. Install the latest Azure CLI package

- If already installed, check latest version:
- Run `az --version` to make sure `azure-cli` is at least **version 2.0.8**
- If it isn't, update it
- Use this link to install [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest])

### 2. Install the Iot extension

- Open powershell window in admin mode.
- Run `az extension list`
- If you see azure-iot extension, update the extension by running `az extension update --name azure-iot`
- If you don't see the azure-iot extension, install it by running `az extension add --name azure-iot`
- See the top-level IoT commands with `az iot -h`

## Maintenance

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.
120 changes: 120 additions & 0 deletions sdk/iot/Azure.Iot.Hub.Service/tests/prerequisites/setup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
param(
[Parameter(Mandatory)]
[string] $Region,

[Parameter(Mandatory)]
[string] $ResourceGroup,

[Parameter(Mandatory)]
[string] $SubscriptionId,

[Parameter()]
[string] $IotHubName,

[Parameter()]
[string] $AppRegistrationName
)

Function Connect-AzureSubscription()
{
# Ensure the user is logged in
try
{
$azureContext = az account show
}
catch { }

if (-not $azureContext)
{
Write-Host "`nPlease login to Azure..."
az login
$azureContext = az account show
}

# Ensure the desired subscription is selected
$sub = az account show --output tsv --query id
if ($sub -ne $SubscriptionId)
{
Write-Host "`nSelecting subscription $SubscriptionId"
az account set --subscription $SubscriptionId
}

return $azureContext
}

$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $isAdmin)
{
throw "This script must be run in administrative mode."
}

Connect-AzureSubscription

$Region = $Region.Replace(' ', '')

if (-not $IotHubName)
{
$IotHubName = $ResourceGroup
}

if (-not $AppRegistrationName)
{
$AppRegistrationName = $ResourceGroup
}

$appId = az ad app list --show-mine --query "[?displayName=='$AppRegistrationName'].appId" --output tsv
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
if (-not $appId)
{
Write-Host "`nCreating App Registration $AppRegistrationName`n"
$appId = az ad app create --display-name $AppRegistrationName --native-app --query 'appId' --output tsv
}

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

$rgExists = az group exists --name $ResourceGroup
if ($rgExists -eq "False")
{
Write-Host "`nCreating Resource Group $ResourceGroup in $Region`n"
az group create --name $ResourceGroup --location $Region --output none
}

$hubExists = az iot hub list -g $ResourceGroup --query "[?name=='$IotHubName']" --output tsv --only-show-errors
if (-not $hubExists)
{
Write-Host "`nCreating IoT Hub $IotHubName`n"
az iot hub create -n $IotHubName -g $ResourceGroup --location $Region --output none --only-show-errors
}
$iotHubHostName = az iot hub show -n $IotHubName -g $ResourceGroup --query 'properties.hostName' --output tsv --only-show-errors

Write-Host("Set a new client secret for $appId`n")
$appSecret = az ad app credential reset --id $appId --years 2 --query 'password' --output tsv

$user = $env:UserName
$fileName = "$user.config.json"
Write-Host("Writing user config file - $fileName`n")
$appSecretJsonEscaped = ConvertTo-Json $appSecret
$config = @"
{
"IotHubHostName": "https://$($iotHubHostName)",
"ApplicationId": "$appId",
"ClientSecret": $appSecretJsonEscaped,
"TestMode": "Live"
}
"@
$config | Out-File "$PSScriptRoot\..\config\$fileName"

$userSettingsFileSuffix = ".test.assets.config.json"
$userSettingsFileName = "$user$userSettingsFileSuffix"
$userTestAssetSettingsFileName = "$PSScriptRoot\..\config\$userSettingsFileName"
if (-not (Test-Path $userTestAssetSettingsFileName))
{
Write-Host "Creating empty user test assets config file - $userSettingsFileName`n"
New-Item -ItemType File -Path $userTestAssetSettingsFileName -Value "{}" | Out-Null
}

Write-Host "Done!"
66 changes: 66 additions & 0 deletions sdk/iot/test-resources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
"contentVersion": "1.0.0.0",
"parameters": {
"baseName": {
"type": "string",
"defaultValue": "[resourceGroup().name]",
"metadata": {
"description": "The base resource name."
}
},
"testApplicationOid": {
"type": "string",
"metadata": {
"description": "The client OID to grant access to test resources."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "The location of the resource. By default, this is the same as the resource group."
}
}
},
"variables": {
"iotHubResourceId": "[resourceId('Microsoft.Devices/IotHubs', parameters('baseName'))]",
"rbacOwnerRoleDefinitionId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')]"
},
"resources": [
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2018-09-01-preview",
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
"name": "[guid(resourceGroup().id)]",
"properties": {
"roleDefinitionId": "[variables('rbacOwnerRoleDefinitionId')]",
"principalId": "[parameters('testApplicationOid')]"
}
},
{
"type": "Microsoft.Devices/IotHubs",
"apiVersion": "2020-03-01",
"name": "[parameters('baseName')]",
"location": "[parameters('location')]",
"sku": {
"name": "S1",
"capacity": 1
},
"properties": {
"eventHubEndpoints": {
"events": {
"retentionTimeInDays": 1,
"partitionCount": 4
}
},
"features": "None"
}
}
],
"outputs": {
"IOT_HUB_ENDPOINT_URL": {
"type": "string",
"value": "[concat('https://', reference(variables('iotHubResourceId'), '2020-03-01').hostName)]"
}
}
}
18 changes: 18 additions & 0 deletions sdk/iot/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
trigger: none

resources:
repositories:
- repository: azure-sdk-tools
type: github
name: Azure/azure-sdk-tools
endpoint: azure

jobs:
- template: ../../eng/pipelines/templates/jobs/archetype-sdk-tests.yml
parameters:
ServiceDirectory: iot
Location: westcentralus
SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources-preview)
EnvVars:
# Runs live tests.
AZURE_IOT_TEST_MODE: Live