Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
fix(vsts): Update build script to accept command line args (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhipsaMisra authored Dec 8, 2020
1 parent 9246956 commit 61baa48
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 37 deletions.
35 changes: 22 additions & 13 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ Function BuildProject($path, $message) {
}
}

Function RunApp($path, $message, $args) {
Function RunApp($path, $message, $params) {

$label = "RUN: --- $message $configuration ($args)---"
$label = "RUN: --- $message $configuration ($params)---"

Write-Host
Write-Host -ForegroundColor Cyan $label
cd (Join-Path $rootDir $path)

& dotnet run $args
$runCommand = "dotnet run -- $params"
Invoke-Expression $runCommand

if ($LASTEXITCODE -ne 0) {
throw "Tests failed: $label"
Expand All @@ -103,26 +104,34 @@ try {

if ($run)
{
# Run cleanup first so the samples don't get overloaded with old device instances
$sampleRunningTime = 60

# Run cleanup first so the samples don't get overloaded with old devices
RunApp provisioning\Samples\service\CleanupEnrollmentsSample "Provisioning\Service\CleanupEnrollmentsSample"
RunApp iot-hub\Samples\service\CleanUpDevicesSample "IoTHub\Service\CleanUpDevicesSample"

RunApp iot-hub\Samples\device\FileUploadSample "IoTHub\Device\FileUploadSample"
RunApp iot-hub\Samples\device\KeysRolloverSample "IoTHub\Device\KeysRolloverSample"
RunApp iot-hub\Samples\device\MessageSample "IoTHub\Device\MessageSample"
RunApp iot-hub\Samples\device\MethodSample "IoTHub\Device\MethodSample"
RunApp iot-hub\Samples\device\TwinSample "IoTHub\Device\TwinSample"
RunApp iot-hub\Samples\device\DeviceReconnectionSample "IoTHub\Device\DeviceReconnectionSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"" -r $sampleRunningTime"
RunApp iot-hub\Samples\device\FileUploadSample "IoTHub\Device\FileUploadSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
RunApp iot-hub\Samples\device\MessageReceiveSample "IoTHub\Device\MessageReceiveSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
RunApp iot-hub\Samples\device\MethodSample "IoTHub\Device\MethodSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
RunApp iot-hub\Samples\device\TwinSample "IoTHub\Device\TwinSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""

$pnpDeviceSecurityType = "connectionString"
RunApp iot-hub\Samples\device\PnpDeviceSamples\TemperatureController "IoTHub\Device\PnpDeviceSamples\TemperatureController" "-s $pnpDeviceSecurityType -p ""$env:PNP_TC_DEVICE_CONN_STRING"" -r $sampleRunningTime"
RunApp iot-hub\Samples\device\PnpDeviceSamples\Thermostat "IoTHub\Device\PnpDeviceSamples\Thermostat" "-s $pnpDeviceSecurityType -p ""$env:PNP_THERMOSTAT_DEVICE_CONN_STRING"" -r $sampleRunningTime"
# DeviceStreaming sample is not added since it is not available in all regions.

RunApp iot-hub\Samples\module\MessageSample "IoTHub\Module\MessageSample"
RunApp iot-hub\Samples\module\TwinSample "IoTHub\Module\TwinSample"
RunApp iot-hub\Samples\module\ModuleSample "IoTHub\Module\ModuleSample" "-p ""$env:IOTHUB_MODULE_CONN_STRING"" -r $sampleRunningTime"

RunApp iot-hub\Samples\service\AutomaticDeviceManagementSample "IoTHub\Service\AutomaticDeviceManagementSample"
RunApp iot-hub\Samples\service\EdgeDeploymentSample "IoTHub\Service\EdgeDeploymentSample"
RunApp iot-hub\Samples\service\JobsSample "IoTHub\Service\JobsSample"
RunApp iot-hub\Samples\service\RegistryManagerSample "IoTHub\Service\RegistryManagerSample"

$deviceId = ($Env:IOTHUB_DEVICE_CONN_STRING.Split(';') | where {$_ -like "DeviceId*"}).Split("=")[1]
Write-Warning $deviceId
RunApp iot-hub\Samples\service\ServiceClientSample "IoTHub\Service\ServiceClientSample" - $deviceId
Write-Warning "Using device $deviceId for the ServiceClientSample."
RunApp iot-hub\Samples\service\ServiceClientSample "IoTHub\Service\ServiceClientSample" "-c ""$env:IOTHUB_CONN_STRING_CSHARP"" -d $deviceId -r $sampleRunningTime"
# DigitalTwinClientSamples and PnpServiceSamples are not added here since they require the device counterparts to be running as well.

# TODO #11: Modify Provisioning\device samples to run unattended.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public DeviceReconnectionSample(List<string> deviceConnectionStrings, TransportT

private bool IsDeviceConnected => s_connectionStatus == ConnectionStatus.Connected;

public async Task RunSampleAsync()
public async Task RunSampleAsync(TimeSpan sampleRunningTime)
{
using var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource(sampleRunningTime);
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
Expand Down Expand Up @@ -117,7 +117,7 @@ private async Task InitializeAndOpenClientAsync()
// It is not good practice to have async void methods, however, DeviceClient.SetConnectionStatusChangesHandler() event handler signature has a void return type.
// As a result, any operation within this block will be executed unmonitored on another thread.
// To prevent multi-threaded synchronization issues, the async method InitializeClientAsync being called in here first grabs a lock
// before attempting to initailize or dispose the device client instance.
// before attempting to initialize or dispose the device client instance.
private async void ConnectionStatusChangeHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
{
_logger.LogDebug($"Connection status changed: status={status}, reason={reason}");
Expand Down
7 changes: 7 additions & 0 deletions iot-hub/Samples/device/DeviceReconnectionSample/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ internal class Parameters
HelpText = "The transport to use to communicate with the IoT Hub. Possible values include Mqtt, Mqtt_WebSocket_Only, Mqtt_Tcp_Only, Amqp, Amqp_WebSocket_Only, Amqp_Tcp_only, and Http1.")]
public TransportType TransportType { get; set; }

[Option(
'r',
"Application running time (in seconds)",
Required = false,
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
public double? ApplicationRunningTime { get; set; }

public List<string> GetConnectionStrings()
{
var cs = new List<string>(2)
Expand Down
8 changes: 6 additions & 2 deletions iot-hub/Samples/device/DeviceReconnectionSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Azure.Devices.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics.Tracing;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Devices.Client.Samples
Expand Down Expand Up @@ -46,8 +46,12 @@ public static async Task<int> Main(string[] args)
_ = new ConsoleEventListener(SdkEventProviderPrefix, logger);

// Run the sample
var runningTime = parameters.ApplicationRunningTime != null
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
: Timeout.InfiniteTimeSpan;

var sample = new DeviceReconnectionSample(parameters.GetConnectionStrings(), parameters.TransportType, logger);
await sample.RunSampleAsync();
await sample.RunSampleAsync(runningTime);

logger.LogInformation("Done.");
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.31.0" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ private async Task ReceiveC2dMessagesPollingAndComplete(TimeSpan timeout)

await _deviceClient.CompleteAsync(receivedMessage);
Console.WriteLine($"{DateTime.Now}> Completed C2D message with Id={receivedMessage.MessageId}.");

receivedMessage.Dispose();
}

sw.Stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ internal class Parameters
"\nDefaults to environment variable \"IOTHUB_DEVICE_DPS_DEVICE_KEY\".")]
public string DeviceSymmetricKey { get; set; } = Environment.GetEnvironmentVariable("IOTHUB_DEVICE_DPS_DEVICE_KEY");

[Option(
'r',
"Application running time (in seconds)",
Required = false,
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
public double? ApplicationRunningTime { get; set; }

public bool Validate(ILogger logger)
{
if (string.IsNullOrWhiteSpace(DeviceSecurityType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public static async Task Main(string[] args)
throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\"");
}

var runningTime = parameters.ApplicationRunningTime != null
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
: Timeout.InfiniteTimeSpan;

s_logger.LogInformation("Press Control+C to quit the sample.");
using var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource(runningTime);
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ internal class Parameters
"\nDefaults to environment variable \"IOTHUB_DEVICE_DPS_DEVICE_KEY\".")]
public string DeviceSymmetricKey { get; set; } = Environment.GetEnvironmentVariable("IOTHUB_DEVICE_DPS_DEVICE_KEY");

[Option(
'r',
"Application running time (in seconds)",
Required = false,
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
public double? ApplicationRunningTime { get; set; }

public bool Validate(ILogger logger)
{
if (string.IsNullOrWhiteSpace(DeviceSecurityType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public static async Task Main(string[] args)
throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\"");
}

var runningTime = parameters.ApplicationRunningTime != null
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
: Timeout.InfiniteTimeSpan;

s_logger.LogInformation("Press Control+C to quit the sample.");
using var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource(runningTime);
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
Expand Down
4 changes: 2 additions & 2 deletions iot-hub/Samples/device/TwinSample/TwinSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public async Task RunSampleAsync()
{
eventArgs.Cancel = true;
cts.Cancel();
Console.WriteLine("Sample execution cancellation requested; will exit.");
Console.WriteLine("Cancellation requested; will exit.");
};

var waitTime = TimeSpan.FromMinutes(1);
var timer = Stopwatch.StartNew();
Console.WriteLine($"Use the IoT Hub Azure Portal or IoT Explorer utility to change the twin desired properties.");

Console.WriteLine($"Waiting up to {waitTime} for IoT Hub method calls ...");
Console.WriteLine($"Waiting up to {waitTime} for receiving twin desired property updates ...");
while (!cts.IsCancellationRequested
&& timer.Elapsed < waitTime)
{
Expand Down
2 changes: 1 addition & 1 deletion iot-hub/Samples/device/TwinSample/TwinSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.31.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions iot-hub/Samples/module/ModuleSample/ModuleSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public ModuleSample(List<string> moduleConnectionStrings, TransportType transpor
InitializeClient();
}

public async Task RunSampleAsync()
public async Task RunSampleAsync(TimeSpan sampleRunningTime)
{
_logger.LogInformation(">> Press Control+C to quit the sample.");
using var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource(sampleRunningTime);
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
Expand Down
7 changes: 7 additions & 0 deletions iot-hub/Samples/module/ModuleSample/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ internal class Parameters
HelpText = "The transport to use to communicate with the IoT Hub. Possible values include Mqtt, Mqtt_WebSocket_Only, Mqtt_Tcp_Only, Amqp, Amqp_WebSocket_Only, Amqp_Tcp_only, and Http1.")]
public TransportType TransportType { get; set; }

[Option(
'r',
"Application running time (in seconds)",
Required = false,
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
public double? ApplicationRunningTime { get; set; }

public List<string> GetConnectionStrings()
{
var cs = new List<string>(2)
Expand Down
7 changes: 6 additions & 1 deletion iot-hub/Samples/module/ModuleSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Azure.Devices.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Devices.Client.Samples
Expand Down Expand Up @@ -46,8 +47,12 @@ public static async Task Main(string[] args)
_ = new ConsoleEventListener(SdkEventProviderPrefix, logger);

// Run the sample
var runningTime = parameters.ApplicationRunningTime != null
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
: Timeout.InfiniteTimeSpan;

var sample = new ModuleSample(parameters.GetConnectionStrings(), parameters.TransportType, logger);
await sample.RunSampleAsync();
await sample.RunSampleAsync(runningTime);

logger.LogInformation("Done.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ symmetrickey-registration-id-
Some-Provisioned-Device-
digitaltwine2e-
tpm-registration-id-
ModuleNotFoundTestSasl_
RegistryManager_
7 changes: 7 additions & 0 deletions iot-hub/Samples/service/ServiceClientSample/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ internal class Parameters
Required = false,
HelpText = "The transport to use to communicate with the IoT Hub. Possible values include Amqp and Amqp_WebSocket_Only.")]
public TransportType TransportType { get; set; }

[Option(
'r',
"Application running time (in seconds)",
Required = false,
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
public double? ApplicationRunningTime { get; set; }
}
}
7 changes: 6 additions & 1 deletion iot-hub/Samples/service/ServiceClientSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Azure.Devices.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Devices.Samples
Expand Down Expand Up @@ -45,8 +46,12 @@ public static async Task<int> Main(string[] args)
// Instantiating this seems to do all we need for outputting SDK events to our console log
_ = new ConsoleEventListener(SdkEventProviderPrefix, logger);

var runningTime = parameters.ApplicationRunningTime != null
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
: Timeout.InfiniteTimeSpan;

var sample = new ServiceClientSample(parameters.HubConnectionString, parameters.TransportType, parameters.DeviceId, logger);
await sample.RunSampleAsync();
await sample.RunSampleAsync(runningTime);

Console.WriteLine("Done.");
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public ServiceClientSample(string hubConnectionString, TransportType transportTy
_logger = logger;
}

public async Task RunSampleAsync()
public async Task RunSampleAsync(TimeSpan runningTime)
{
using var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource(runningTime);
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
Expand Down
8 changes: 5 additions & 3 deletions vsts/vsts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ jobs:
127.0.0.1:2322:2322
restartPolicy: unlessStopped

- powershell: ./build.ps1 -clean -configuration Release -run
- powershell: ./build.ps1 -clean -configuration Release -build -run
displayName: build and run
env:
IOTHUB_DEVICE_CONN_STRING: $(IOTHUB-DEVICE-CONN-STRING)
IOTHUB_DEVICE_CONN_STRING2: $(IOTHUB-DEVICE-CONN-STRING2)
IOTHUB_MODULE_CONN_STRING: $(IOTHUB-MODULE-CONN-STRING)
PNP_TC_DEVICE_CONN_STRING: $(PNP-TC-DEVICE-CONN-STRING)
PNP_THERMOSTAT_DEVICE_CONN_STRING: $(PNP-THERMOSTAT-DEVICE-CONN-STRING)
IOTHUB_CONN_STRING_CSHARP: $(IOTHUB-CONN-STRING-CSHARP)
IOTHUB_PFX_X509_THUMBPRINT: $(IOTHUB-PFX-X509-THUMBPRINT)
DPS_IDSCOPE: $(DPS-IDSCOPE)
Expand All @@ -52,8 +53,9 @@ jobs:
displayName: build and run
env:
IOTHUB_DEVICE_CONN_STRING: $(IOTHUB-DEVICE-CONN-STRING)
IOTHUB_DEVICE_CONN_STRING2: $(IOTHUB-DEVICE-CONN-STRING2)
IOTHUB_MODULE_CONN_STRING: $(IOTHUB-MODULE-CONN-STRING)
PNP_TC_DEVICE_CONN_STRING: $(PNP-TC-DEVICE-CONN-STRING)
PNP_THERMOSTAT_DEVICE_CONN_STRING: $(PNP-THERMOSTAT-DEVICE-CONN-STRING)
IOTHUB_CONN_STRING_CSHARP: $(IOTHUB-CONN-STRING-CSHARP)
IOTHUB_PFX_X509_THUMBPRINT: $(IOTHUB-PFX-X509-THUMBPRINT)
DPS_IDSCOPE: $(DPS-IDSCOPE)
Expand Down

0 comments on commit 61baa48

Please sign in to comment.