winget-cli

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

ConfigureTestCommand.cs (6033B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ConfigureTestCommand.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace AppInstallerCLIE2ETests
      8 {
      9     using System.IO;
     10     using AppInstallerCLIE2ETests.Helpers;
     11     using Microsoft.Win32;
     12     using NUnit.Framework;
     13 
     14     /// <summary>
     15     /// `Configure test` command tests.
     16     /// </summary>
     17     public class ConfigureTestCommand
     18     {
     19         private const string CommandAndAgreements = "configure test --accept-configuration-agreements";
     20 
     21         /// <summary>
     22         /// Setup done once before all the tests here.
     23         /// </summary>
     24         [OneTimeSetUp]
     25         public void OneTimeSetup()
     26         {
     27             this.DeleteResourceArtifacts();
     28             ConfigureCommand.EnsureTestResourcePresence();
     29         }
     30 
     31         /// <summary>
     32         /// Teardown done once after all the tests here.
     33         /// </summary>
     34         [OneTimeTearDown]
     35         public void OneTimeTeardown()
     36         {
     37             this.DeleteResourceArtifacts();
     38         }
     39 
     40         /// <summary>
     41         /// Checks for a resource not in the desired state.
     42         /// </summary>
     43         [Test]
     44         public void ConfigureTest_NotInDesiredState()
     45         {
     46             TestCommon.EnsureModuleState(Constants.SimpleTestModuleName, present: false);
     47             this.DeleteResourceArtifacts();
     48 
     49             var result = TestCommon.RunAICLICommand(CommandAndAgreements, TestCommon.GetTestDataFile("Configuration\\Configure_TestRepo.yml"));
     50             Assert.AreEqual(Constants.ErrorCode.S_FALSE, result.ExitCode);
     51             Assert.True(result.StdOut.Contains("System is not in the described configuration state."));
     52         }
     53 
     54         /// <summary>
     55         /// Checks for a resource in a desired state.
     56         /// </summary>
     57         [Test]
     58         public void ConfigureTest_InDesiredState()
     59         {
     60             TestCommon.EnsureModuleState(Constants.SimpleTestModuleName, present: false);
     61             this.DeleteResourceArtifacts();
     62 
     63             // Set up the expected state
     64             File.WriteAllText(TestCommon.GetTestDataFile("Configuration\\Configure_TestRepo.txt"), "Contents!");
     65 
     66             var result = TestCommon.RunAICLICommand(CommandAndAgreements, TestCommon.GetTestDataFile("Configuration\\Configure_TestRepo.yml"));
     67             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
     68             Assert.True(result.StdOut.Contains("System is in the described configuration state."));
     69         }
     70 
     71         /// <summary>
     72         /// One resource fails.
     73         /// </summary>
     74         [Test]
     75         public void ConfigureTest_TestFailure()
     76         {
     77             var result = TestCommon.RunAICLICommand(CommandAndAgreements, TestCommon.GetTestDataFile("Configuration\\IndependentResources_OneFailure.yml"));
     78             Assert.AreEqual(Constants.ErrorCode.CONFIG_ERROR_TEST_FAILED, result.ExitCode);
     79             Assert.True(result.StdOut.Contains("Some of the configuration units failed while testing their state."));
     80             Assert.True(result.StdOut.Contains("System is not in the described configuration state."));
     81         }
     82 
     83         /// <summary>
     84         /// Test from https configuration file.
     85         /// </summary>
     86         [Test]
     87         public void ConfigureTest_HttpsConfigurationFile()
     88         {
     89             var result = TestCommon.RunAICLICommand(CommandAndAgreements, $"{Constants.TestSourceUrl}/TestData/Configuration/Configure_TestRepo_Location.yml");
     90             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
     91             Assert.True(result.StdOut.Contains("System is in the described configuration state."));
     92         }
     93 
     94         /// <summary>
     95         /// Runs a configuration, then tests it from history.
     96         /// </summary>
     97         [Test]
     98         public void TestFromHistory()
     99         {
    100             var result = TestCommon.RunAICLICommand("configure --accept-configuration-agreements --verbose", TestCommon.GetTestDataFile("Configuration\\Configure_TestRepo.yml"));
    101             Assert.AreEqual(0, result.ExitCode);
    102 
    103             // The configuration creates a file next to itself with the given contents
    104             string targetFilePath = TestCommon.GetTestDataFile("Configuration\\Configure_TestRepo.txt");
    105             FileAssert.Exists(targetFilePath);
    106             Assert.AreEqual("Contents!", File.ReadAllText(targetFilePath));
    107 
    108             string guid = TestCommon.GetConfigurationInstanceIdentifierFor("Configure_TestRepo.yml");
    109             result = TestCommon.RunAICLICommand(CommandAndAgreements, $"-h {guid}");
    110             Assert.AreEqual(0, result.ExitCode);
    111 
    112             File.WriteAllText(targetFilePath, "Changed contents!");
    113 
    114             result = TestCommon.RunAICLICommand(CommandAndAgreements, $"-h {guid}");
    115             Assert.AreEqual(Constants.ErrorCode.S_FALSE, result.ExitCode);
    116         }
    117 
    118         /// <summary>
    119         /// Simple test to confirm that a resource is testable with DSC v3.
    120         /// </summary>
    121         [Test]
    122         public void ConfigureTest_DSCv3()
    123         {
    124             var result = TestCommon.RunAICLICommand(CommandAndAgreements, $"{TestCommon.GetTestDataFile("Configuration\\ShowDetails_DSCv3.yml")} --verbose");
    125             Assert.AreEqual(Constants.ErrorCode.S_FALSE, result.ExitCode);
    126             Assert.True(result.StdOut.Contains("System is not in the described configuration state."));
    127         }
    128 
    129         private void DeleteResourceArtifacts()
    130         {
    131             // Delete all .txt files in the test directory; they are placed there by the tests
    132             foreach (string file in Directory.GetFiles(TestCommon.GetTestDataFile("Configuration"), "*.txt"))
    133             {
    134                 File.Delete(file);
    135             }
    136         }
    137     }
    138 }