Skip to content

Commit

Permalink
Provide simulator/device flag in diagnostics data (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Nov 4, 2021
1 parent 5c299ed commit 4663a6e
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Microsoft.DotNet.XHarness.CLI.CommandArguments;
using Microsoft.DotNet.XHarness.CLI.Commands;
using Microsoft.DotNet.XHarness.Common;
Expand All @@ -20,5 +21,17 @@ protected AndroidCommand(string name, bool allowsExtraArgs, string? help = null)
{
_diagnosticsData = new(() => Services.BuildServiceProvider().GetRequiredService<IDiagnosticsData>());
}

protected static void FillDiagnosticData(
IDiagnosticsData data,
string deviceName,
int apiVersion,
IEnumerable<string> apkRequiredArchitecture)
{
data.Target = string.Join(",", apkRequiredArchitecture);
data.TargetOS = "API " + apiVersion;
data.Device = deviceName;
data.IsDevice = !deviceName.ToLowerInvariant().StartsWith("emulator");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ protected override Task<ExitCode> InvokeInternal(ILogger logger)

runner.SetActiveDevice(deviceToUse);

DiagnosticsData.Target = string.Join(",", apkRequiredArchitecture);
DiagnosticsData.TargetOS = "API " + runner.APIVersion;
DiagnosticsData.Device = deviceToUse;
FillDiagnosticData(DiagnosticsData, deviceToUse, runner.APIVersion, apkRequiredArchitecture);

Console.WriteLine(deviceToUse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ protected override Task<ExitCode> InvokeInternal(ILogger logger)
}
}

DiagnosticsData.Target = string.Join(",", apkRequiredArchitecture);

// Package Name is not guaranteed to match file name, so it needs to be mandatory.
try
{
Expand All @@ -86,7 +84,15 @@ protected override Task<ExitCode> InvokeInternal(ILogger logger)
return Task.FromResult(ExitCode.GENERAL_FAILURE);
}

public static ExitCode InvokeHelper(ILogger logger, string apkPackageName, string appPackagePath, IEnumerable<string> apkRequiredArchitecture, string? deviceId, TimeSpan bootTimeoutSeconds, AdbRunner runner, IDiagnosticsData diagnosticsData)
public static ExitCode InvokeHelper(
ILogger logger,
string apkPackageName,
string appPackagePath,
IEnumerable<string> apkRequiredArchitecture,
string? deviceId,
TimeSpan bootTimeoutSeconds,
AdbRunner runner,
IDiagnosticsData diagnosticsData)
{
using (logger.BeginScope("Initialization and setup of APK on device"))
{
Expand All @@ -105,8 +111,8 @@ public static ExitCode InvokeHelper(ILogger logger, string apkPackageName, strin
}

runner.SetActiveDevice(deviceId);
diagnosticsData.TargetOS = "API " + runner.APIVersion;
diagnosticsData.Device = deviceId;

FillDiagnosticData(diagnosticsData, deviceId, runner.APIVersion, apkRequiredArchitecture);

runner.TimeToWaitForBootCompletion = bootTimeoutSeconds;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.XHarness.Android;
using Microsoft.DotNet.XHarness.Android.Execution;
Expand Down Expand Up @@ -66,12 +67,16 @@ protected override Task<ExitCode> InvokeInternal(ILogger logger)
}

runner.SetActiveDevice(deviceId);
DiagnosticsData.TargetOS = "API " + runner.APIVersion;
DiagnosticsData.Device = deviceId;

// For test command, this was already filled during installation
if (DiagnosticsData.Device == null)
{
FillDiagnosticData(DiagnosticsData, deviceId, runner.APIVersion, Enumerable.Empty<string>());
}

runner.TimeToWaitForBootCompletion = Arguments.LaunchTimeout;

// Wait til at least device(s) are ready
// Wait till at least device(s) are ready
runner.WaitForDevice();

return Task.FromResult(InvokeHelper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ protected override Task<ExitCode> InvokeInternal(ILogger logger)
logger.LogInformation($"Will attempt to run device on detected architecture: '{string.Join("', '", apkRequiredArchitecture)}'");
}

DiagnosticsData.Target = string.Join(",", apkRequiredArchitecture);

// Package Name is not guaranteed to match file name, so it needs to be mandatory.
string apkPackageName = Arguments.PackageName;
string appPackagePath = Arguments.AppPackagePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.DotNet.XHarness.Common;
using Microsoft.DotNet.XHarness.Common.CLI;
using Microsoft.DotNet.XHarness.Common.Logging;
using Microsoft.DotNet.XHarness.iOS.Shared;
using Microsoft.DotNet.XHarness.iOS.Shared.Hardware;
using Microsoft.DotNet.XHarness.iOS.Shared.Logging;
using Microsoft.DotNet.XHarness.iOS.Shared.Utilities;
Expand Down Expand Up @@ -51,6 +52,7 @@ protected sealed override async Task<ExitCode> Invoke(Extensions.Logging.ILogger

var diagnosticsData = serviceProvider.GetRequiredService<IDiagnosticsData>();
diagnosticsData.Target = Arguments.Target.Value.AsString();
diagnosticsData.IsDevice = !Arguments.Target.Value.Platform.IsSimulator();

var cts = new CancellationTokenSource();
cts.CancelAfter(Arguments.Timeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected override async Task<ExitCode> Invoke(Extensions.Logging.ILogger logger

diagnosticsData.TargetOS = device.OSVersion.Split(' ', 2).Last();
diagnosticsData.Device = device.Name ?? device.UDID;
diagnosticsData.IsDevice = !target.Platform.IsSimulator();

Console.WriteLine(device.UDID);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.DotNet.XHarness.Common/CommandDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public interface IDiagnosticsData
/// Name of the used device.
/// </summary>
string? Device { get; set; }

/// <summary>
/// True when the target is a real HW device, false for simulators, maccatalyst..
/// </summary>
bool? IsDevice { get; set; }
}

/// <summary>
Expand All @@ -56,6 +61,9 @@ public class CommandDiagnostics : IDiagnosticsData
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Device { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public bool? IsDevice { get; set; }

public int Duration => (int)Math.Round(_timer.Elapsed.TotalSeconds);

public CommandDiagnostics(ILogger logger, TargetPlatform platform, string command)
Expand Down
4 changes: 4 additions & 0 deletions tests/integration-tests/Android/Device.Tests.proj
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<Project DefaultTargets="Test">
<PropertyGroup>
<IsPosixShell>false</IsPosixShell>
</PropertyGroup>

<Import Project="../Helix.SDK.configuration.props"/>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions tests/integration-tests/Helix.SDK.configuration.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<HelixBaseUri>https://helix.dot.net</HelixBaseUri>
<!-- Pick up the nupkg from this repo for testing purposes -->
<MicrosoftDotNetXHarnessCLIVersion>1.0.0-ci</MicrosoftDotNetXHarnessCLIVersion>

<!-- Upload the diagnostics.json with other results -->
<IsPosixShell Condition=" '$(IsPosixShell)' != 'false' ">true</IsPosixShell>
<HelixPostCommands Condition=" '$(IsPosixShell)' == 'true' ">cp "$XHARNESS_DIAGNOSTICS_PATH" "$HELIX_WORKITEM_UPLOAD_ROOT";$(HelixPostCommands)</HelixPostCommands>
<HelixPostCommands Condition=" '$(IsPosixShell)' == 'false' ">copy "%XHARNESS_DIAGNOSTICS_PATH%" "%HELIX_WORKITEM_UPLOAD_ROOT%";$(HelixPostCommands)</HelixPostCommands>
</PropertyGroup>

<!-- For non-ci local runs -->
Expand Down

0 comments on commit 4663a6e

Please sign in to comment.