Skip to content

Commit

Permalink
Merge pull request #5 from anusapan/iothub
Browse files Browse the repository at this point in the history
Add new cmdlets for iothub routes and endpoints
  • Loading branch information
anusapan authored Aug 17, 2018
2 parents 90e3d38 + 133335f commit 6534226
Show file tree
Hide file tree
Showing 71 changed files with 7,646 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.IotHub, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.IotHub.2.0.0\lib\net452\Microsoft.Azure.Management.IotHub.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.IotHub.2.1.0\lib\net452\Microsoft.Azure.Management.IotHub.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ScenarioTests\IotHubController.cs" />
<Compile Include="ScenarioTests\IotHubRoutingTests.cs" />
<Compile Include="ScenarioTests\IotHubTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -57,9 +58,15 @@
</None>
<None Include="ScenarioTests\Assert.ps1" />
<None Include="ScenarioTests\Common.ps1" />
<None Include="ScenarioTests\IotHubRoutingTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\IotHubTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubRoutingTests\TestAzureIotHubRoutingLifeCycle.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubTests\TestAzureIotHubCertificateLifeCycle.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
Expand Down Expand Up @@ -34,3 +34,4 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.0.0")]
[assembly: AssemblyFileVersion("3.0.0")]
[assembly: CollectionBehavior(DisableTestParallelization = true)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.Azure.Test;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.IotHub.Test.ScenarioTests
{
public class IotHubRoutingTests : RMTestBase
{
public XunitTracingInterceptor _logger;

public IotHubRoutingTests(ITestOutputHelper output)
{
_logger = new XunitTracingInterceptor(output);
XunitTracingInterceptor.AddToContext(_logger);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait("Re-record", "ClientRuntime changes")]
public void TestAzureIotHubRoutingLifeCycle()
{
IotHubController.NewInstance.RunPsTestWorkflow(
_logger,
() => { return new[] { "Test-AzureRmIotHubRoutingLifecycle" }; },
null,
null,
TestUtilities.GetCallingClass(),
TestUtilities.GetCurrentMethodName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------


#################################
## IotHub Routing Cmdlets ##
#################################

<#
.SYNOPSIS
Test all iothub routing cmdlets
#>
function Test-AzureRmIotHubRoutingLifecycle
{
$Location = Get-Location "Microsoft.Devices" "IotHub"
$IotHubName = getAssetName
$ResourceGroupName = getAssetName
$namespaceName = getAssetName 'eventHub'
$eventHubName = getAssetName
$authRuleName = getAssetName
$endpointName = getAssetName
$routeName = getAssetName
$Sku = "S1"

# Create or Update Resource Group
$resourceGroup = New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location

# Create Iot Hub
$iothub = New-AzureRmIotHub -Name $IotHubName -ResourceGroupName $ResourceGroupName -Location $Location -SkuName $Sku -Units 1

# Create new eventHub namespace
$eventHubNamespace = New-AzureRmEventHubNamespace -ResourceGroup $ResourceGroupName -NamespaceName $namespaceName -Location $Location
Wait-Seconds 15
Assert-True {$eventHubNamespace.ProvisioningState -eq "Succeeded"}
$regexMatches = $eventHubNamespace.Id | Select-String -Pattern '^/subscriptions/(.*)/resourceGroups/(.*)/providers/(.*)$'
$eventHubSubscriptionId = $regexMatches.Matches.Groups[1].Value
$eventHubResourceGroup = $regexMatches.Matches.Groups[2].Value

# Create new eventHub
$msgRetentionInDays = 3
$partionCount = 2
$eventHub = New-AzureRmEventHub -ResourceGroup $ResourceGroupName -NamespaceName $namespaceName -EventHubName $eventHubName -MessageRetentionInDays $msgRetentionInDays -PartitionCount $partionCount

# Create AuthRule for eventhub
$rights = "Listen","Send"
$authRule = New-AzureRmEventHubAuthorizationRule -ResourceGroup $ResourceGroupName -NamespaceName $namespaceName -EventHubName $eventHubName -AuthorizationRuleName $authRuleName -Rights $rights
$keys = Get-AzureRmEventHubKey -ResourceGroup $ResourceGroupName -NamespaceName $namespaceName -EventHubName $eventHubName -AuthorizationRuleName $authRuleName
$ehConnectionString = $keys.PrimaryConnectionString

# Get all routing endpoints
$routingEndpoints = Get-AzureRmIotHubRoutingEndpoint -ResourceGroupName $ResourceGroupName -Name $IotHubName
Assert-True { $routingEndpoints.Count -eq 0}

# Add event hub endpoint
$newRoutingEndpoint = Add-AzureRmIotHubRoutingEndpoint -ResourceGroupName $ResourceGroupName -Name $IotHubName -EndpointName $endpointName -EndpointType EventHub -EndpointResourceGroup $eventHubResourceGroup -EndpointSubscriptionId $eventHubSubscriptionId -ConnectionString $ehConnectionString
Assert-True { $newRoutingEndpoint.ResourceGroup -eq $eventHubResourceGroup}
Assert-True { $newRoutingEndpoint.SubscriptionId -eq $eventHubSubscriptionId}
Assert-True { $newRoutingEndpoint.Name -eq $endpointName}

# Get all routing endpoints
$updatedRoutingEndpoints = Get-AzureRmIotHubRoutingEndpoint -ResourceGroupName $ResourceGroupName -Name $IotHubName
Assert-True { $updatedRoutingEndpoints.Count -eq 1}
Assert-True { $updatedRoutingEndpoints[0].ResourceGroup -eq $eventHubResourceGroup}
Assert-True { $updatedRoutingEndpoints[0].SubscriptionId -eq $eventHubSubscriptionId}
Assert-True { $updatedRoutingEndpoints[0].Name -eq $endpointName}

# Get all routes
$routes = Get-AzureRmIotHubRoute -ResourceGroupName $ResourceGroupName -Name $IotHubName
Assert-True { $routingEndpoints.Count -eq 0}

# Add new route
$routeDataSource = 'DeviceMessages'
$newRoute = Add-AzureRmIotHubRoute -ResourceGroupName $ResourceGroupName -Name $IotHubName -RouteName $routeName -Source $routeDataSource -EndpointName $endpointName
Assert-True { $newRoute.Name -eq $routeName}
Assert-True { $newRoute.Source -eq $routeDataSource}
Assert-True { $newRoute.EndpointNames -eq $endpointName}
Assert-False { $newRoute.IsEnabled }

# Get all routes
$routes = Get-AzureRmIotHubRoute -ResourceGroupName $ResourceGroupName -Name $IotHubName
Assert-True { $routes.Count -eq 1}
Assert-True { $routes[0].Name -eq $routeName}
Assert-True { $routes[0].Source -eq $routeDataSource}
Assert-True { $routes[0].EndpointNames -eq $endpointName}
Assert-False { $routes[0].IsEnabled }

# Update route
$newRouteDataSource = 'TwinChangeEvents'
$updatedRoute = Update-AzureRmIotHubRoute -ResourceGroupName $ResourceGroupName -Name $IotHubName -RouteName $routeName -Source $newRouteDataSource -Enabled
Assert-True { $updatedRoute.Name -eq $routeName}
Assert-True { $updatedRoute.Source -eq $newRouteDataSource}
Assert-True { $updatedRoute.EndpointNames -eq $endpointName}
Assert-True { $updatedRoute.IsEnabled }

# Test All Routes
$testRouteOutput = Test-AzureRmIotHubRoute -ResourceGroupName $ResourceGroupName -Name $IotHubName -Source $newRouteDataSource
Assert-True { $testRouteOutput.Count -eq 1}
Assert-True { $testRouteOutput[0].Name -eq $routeName}
Assert-True { $testRouteOutput[0].Source -eq $newRouteDataSource}
Assert-True { $testRouteOutput[0].EndpointNames -eq $endpointName}
Assert-True { $testRouteOutput[0].IsEnabled }

# Delete Route
$result = Remove-AzureRmIotHubRoute -ResourceGroupName $ResourceGroupName -Name $IotHubName -RouteName $routeName -Passthru
Assert-True { $result }

# Delete routing endpoint
$result = Remove-AzureRmIotHubRoutingEndpoint -ResourceGroupName $ResourceGroupName -Name $IotHubName -EndpointName $endpointName -Passthru
Assert-True { $result }

# Remove IotHub
Remove-AzureRmIotHub -ResourceGroupName $ResourceGroupName -Name $IotHubName
}
Loading

0 comments on commit 6534226

Please sign in to comment.