Skip to content

Commit

Permalink
Disable auto updating on device and better prepare logcat (#20732)
Browse files Browse the repository at this point in the history
* Disable updated and setup logcat for tests

* - move shell commands to more generic spot

* Update android.cake

* - add test filter as arg

* Update android.cake

* - try removing play store

* Update android.cake

* - formalize retries a bit

* - fix

* Update android.cake

* Update android.cake
  • Loading branch information
PureWeen authored Mar 6, 2024
1 parent f76cc06 commit 51cabfd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
11 changes: 11 additions & 0 deletions eng/cake/dotnet.cake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var localDotnet = GetBuildVariable("workloads", "local") == "local";
var vsVersion = GetBuildVariable("VS", "");
string MSBuildExe = Argument("msbuild", EnvironmentVariable("MSBUILD_EXE", ""));
string nugetSource = Argument("nugetsource", "");
string testFilter = Argument("test-filter", EnvironmentVariable("TEST_FILTER"));

string TestTFM = Argument("testtfm", "");
var useNuget = Argument("usenuget", true);
Expand Down Expand Up @@ -731,6 +732,16 @@ void RunTestWithLocalDotNet(string csproj)

void RunTestWithLocalDotNet(string csproj, string config, string pathDotnet = null, Dictionary<string,string> argsExtra = null, bool noBuild = false, string resultsFileNameWithoutExtension = null, string filter = "")
{
if (string.IsNullOrWhiteSpace(filter))
{
filter = testFilter;
}

if (!string.IsNullOrWhiteSpace(filter))
{
Information("Run Tests With Filter {0}", filter);
}

string binlog;
string results;
var name = System.IO.Path.GetFileNameWithoutExtension(csproj);
Expand Down
74 changes: 72 additions & 2 deletions eng/devices/android.cake
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Setup(context =>
var sdk = api >= 27 ? "google_apis_playstore" : "google_apis";
if (api == 27 && DEVICE_ARCH == "x86_64")
sdk = "default";

ANDROID_AVD_IMAGE = $"system-images;android-{api};{sdk};{DEVICE_ARCH}";

Information("Going to run image: {0}", ANDROID_AVD_IMAGE);
Expand Down Expand Up @@ -143,6 +144,12 @@ Setup(context =>
Information("Starting Emulator: {0}...", ANDROID_AVD);
emulatorProcess = AndroidEmulatorStart(ANDROID_AVD, emuSettings);
}

if (IsCIBuild())
{
AdbLogcat(new AdbLogcatOptions() { Clear = true });
AdbShell("logcat -G 16M");
}
});

Teardown(context =>
Expand Down Expand Up @@ -343,8 +350,29 @@ Task("uitest")

SetEnvironmentVariable("APPIUM_LOG_FILE", $"{BINLOG_DIR}/appium_android.log");

Information("Run UITests project {0}", PROJECT.FullPath);
RunTestWithLocalDotNet(PROJECT.FullPath, CONFIGURATION, noBuild: true, resultsFileNameWithoutExtension: $"{name}-{CONFIGURATION}-android");
Information("Run UITests project {0}", PROJECT.FullPath);

int numOfRetries = 0;

if (IsCIBuild())
numOfRetries = 1;

for(int retryCount = 0; retryCount <= numOfRetries; retryCount++)
{
try
{
RunTestWithLocalDotNet(PROJECT.FullPath, CONFIGURATION, noBuild: true, resultsFileNameWithoutExtension: $"{name}-{CONFIGURATION}-android");
break;
}
catch(Exception)
{
if (retryCount == numOfRetries)
{
WriteLogCat();
throw;
}
}
}
});

Task("cg-uitest")
Expand Down Expand Up @@ -392,8 +420,50 @@ Task("cg-uitest")
FailRunOnOnlyInconclusiveTests(System.IO.Path.Combine(nunitSettings.Work.FullPath, "TestResult.xml"));
});


Task("logcat")
.Does(() =>
{
WriteLogCat();
});

RunTarget(TARGET);

void WriteLogCat(string filename = null)
{
if (string.IsNullOrWhiteSpace(filename))
{
var timeStamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
filename = $"logcat_{TARGET}_{timeStamp}.log";
}

EnsureDirectoryExists(GetLogDirectory());
// I tried AdbLogcat here but the pipeline kept reporting "cannot create file"
var location = $"{GetLogDirectory()}/{filename}";
Information("Writing logcat to {0}", location);

var processSettings = new ProcessSettings();
processSettings.RedirectStandardOutput = true;
processSettings.RedirectStandardError = true;
var adb = $"{ANDROID_SDK_ROOT}/platform-tools/adb";

Information("Running: {0} logcat -d", adb);
processSettings.Arguments = $"logcat -d";
using (var fs = new System.IO.FileStream(location, System.IO.FileMode.Create))
using (var sw = new StreamWriter(fs))
{
processSettings.RedirectedStandardOutputHandler = (output) => {
sw.WriteLine(output);
return output;
};

var process = StartProcess($"{adb}", processSettings);
Information("exit code {0}", process);
}

Information("Logcat written to {0}", location);
}

void SetupAppPackageNameAndResult()
{
if (string.IsNullOrEmpty(TEST_APP)) {
Expand Down
3 changes: 2 additions & 1 deletion eng/pipelines/common/ui-tests-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ steps:
- pwsh: ./build.ps1 -Script eng/devices/${{ parameters.platform }}.cake --target=uitest --project="${{ parameters.path }}" --appproject="${{ parameters.app }}" --device="${{ parameters.device }}" --apiversion="${{ parameters.version }}" --configuration="${{ parameters.configuration }}" --results="$(TestResultsDirectory)" --binlog="$(LogDirectory)" ${{ parameters.cakeArgs }} --verbosity=diagnostic
displayName: $(Agent.JobName)
retryCountOnTaskFailure: 1
${{ if ne(parameters.platform, 'android')}}:
retryCountOnTaskFailure: 1

- bash: |
suffix=$(date +%Y%m%d%H%M%S)
Expand Down

0 comments on commit 51cabfd

Please sign in to comment.