winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

Shutdown.cs (5043B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="Shutdown.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace AppInstallerCLIE2ETests.Interop
      8 {
      9     using System;
     10     using System.Threading.Tasks;
     11     using AppInstallerCLIE2ETests.Helpers;
     12     using Microsoft.Management.Deployment;
     13     using Microsoft.Management.Deployment.Projection;
     14     using NUnit.Framework;
     15     using WinGetTestCommon;
     16 
     17     /// <summary>
     18     /// Shutdown testing.
     19     /// </summary>
     20     [TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.OutOfProcess), Category = nameof(InstanceInitializersSource.OutOfProcess))]
     21     public class Shutdown : BaseInterop
     22     {
     23         /// <summary>
     24         /// Initializes a new instance of the <see cref="Shutdown"/> class.
     25         /// </summary>
     26         /// <param name="initializer">Initializer.</param>
     27         public Shutdown(IInstanceInitializer initializer)
     28             : base(initializer)
     29         {
     30         }
     31 
     32         /// <summary>
     33         /// Checks that shutdown will proceed even though an object is active.
     34         /// </summary>
     35         [Test]
     36         public void NoActiveOperations()
     37         {
     38             var packageManager = this.TestFactory.CreatePackageManager();
     39 
     40             var servers = WinGetServerInstance.GetInstances();
     41             Assert.AreEqual(1, servers.Count);
     42 
     43             var server = servers[0];
     44             Assert.IsTrue(server.HasWindow);
     45 
     46             // This is the call pattern from Windows
     47             this.SendMessageAndLog(server, WindowMessage.QueryEndSession);
     48             this.SendMessageAndLog(server, WindowMessage.EndSession);
     49             this.SendMessageAndLog(server, WindowMessage.Close);
     50 
     51             Assert.IsTrue(server.Process.WaitForExit(5000));
     52         }
     53 
     54         /// <summary>
     55         /// Checks that shutdown will proceed even though an operation is active.
     56         /// </summary>
     57         /// <returns>The task.</returns>
     58         [Test]
     59         public async Task ActiveInstallOperation()
     60         {
     61             var packageManager = this.TestFactory.CreatePackageManager();
     62             var testSource = packageManager.GetPackageCatalogByName(Constants.TestSourceName);
     63             var installDir = TestCommon.GetRandomTestDir();
     64 
     65             var servers = WinGetServerInstance.GetInstances();
     66             Assert.AreEqual(1, servers.Count);
     67 
     68             var server = servers[0];
     69             Assert.IsTrue(server.HasWindow);
     70 
     71             // Find package
     72             var searchResult = this.FindOnePackage(testSource, PackageMatchField.Name, PackageFieldMatchOption.Equals, "InapplicableOsVersion");
     73 
     74             // Configure installation
     75             var installOptions = this.TestFactory.CreateInstallOptions();
     76             installOptions.PackageInstallMode = PackageInstallMode.Silent;
     77             installOptions.PreferredInstallLocation = installDir;
     78 
     79             // Install
     80             var installOperation = packageManager.InstallPackageAsync(searchResult.CatalogPackage, installOptions);
     81 
     82             // This is the call pattern from Windows
     83             this.SendMessageAndLog(server, WindowMessage.QueryEndSession);
     84             this.SendMessageAndLog(server, WindowMessage.EndSession);
     85             this.SendMessageAndLog(server, WindowMessage.Close);
     86 
     87             Assert.IsTrue(server.Process.WaitForExit(5000));
     88 
     89             InstallResult installResult = null;
     90             Exception exception = null;
     91 
     92             try
     93             {
     94                 installResult = await installOperation;
     95             }
     96             catch (Exception ex)
     97             {
     98                 exception = ex;
     99             }
    100 
    101             // We just expect some kind of signal to indicate the failed attempt.
    102             if (installResult != null)
    103             {
    104                 Assert.AreNotEqual(InstallResultStatus.Ok, installResult.Status);
    105             }
    106             else
    107             {
    108                 Assert.NotNull(exception);
    109             }
    110 
    111             Assert.False(TestCommon.VerifyTestExeInstalledAndCleanup(installDir));
    112         }
    113 
    114         private void SendMessageAndLog(WinGetServerInstance server, WindowMessage message)
    115         {
    116             TestContext.Out.WriteLine($"Sending message {message} to process {server.Process.Id}...");
    117             try
    118             {
    119                 if (server.SendMessage(message))
    120                 {
    121                     TestContext.Out.WriteLine("... succeeded.");
    122                 }
    123                 else
    124                 {
    125                     TestContext.Out.WriteLine("... failed.");
    126                 }
    127             }
    128             catch (Exception e)
    129             {
    130                 TestContext.Out.WriteLine($"... had exception: {e.Message}");
    131             }
    132         }
    133     }
    134 }