Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration reset tests #4013

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace Azure.Sdk.Tools.TestProxy.Tests.IntegrationTests
{
// Pull Test Scenarios involving https://github.com/Azure/azure-sdk-assets-integration
// Restore Test Scenarios involving https://github.com/Azure/azure-sdk-assets-integration

// Setup:
// The files live under https://github.com/Azure/azure-sdk-assets-integration/tree/main/pull/scenarios.
// Each file contains nothing but a single version digit, which is used for verification purposes.
// There are 3 pull test scenarios and each uses a different SHA. The scenarios are detailed down
// There are restore test scenarios and each uses a different SHA. The scenarios are detailed down
// below with their test functions.
public class GitStoreIntegrationRestoreTests
{
Expand All @@ -37,7 +37,8 @@ public GitStoreIntegrationRestoreTests()
// Added file2.txt
// Added file3.txt
// Expect: each file should be version 1
[Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")]
[Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")]
//[Theory]
[InlineData(
@"{
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",
Expand Down Expand Up @@ -90,6 +91,7 @@ public async Task Scenario1(string inputJson)
// file3 version 2
// file4 version 1
[Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")]
//[Theory]
[InlineData(
@"{
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",
Expand Down Expand Up @@ -144,6 +146,7 @@ public async Task Scenario2(string inputJson)
// file4 version 1
// file5 version 1
[Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")]
//[Theory]
[InlineData(
@"{
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",
Expand Down
23 changes: 19 additions & 4 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@ public static bool VerifyFileVersion(string testFolder, string fileName, int exp
/// Verify the version, inside the file, for a given file inside of a test folder.
/// </summary>
/// <param name="testFolder">The temporary test folder created by TestHelpers.DescribeTestFolder</param>
/// <param name="fileName">The fileName whose version needs verification</param>
/// <param name="expectedVersion">The expected version in the file</param>
public static bool IncrementFileVersion(string testFolder, string fileName)
/// <param name="fileName">The file whose version needs to be incremented</param>
public static void IncrementFileVersion(string testFolder, string fileName)
{
string fullFileName = Path.Combine(testFolder, fileName);
string stringVersion = "";
Expand All @@ -254,8 +253,24 @@ public static bool IncrementFileVersion(string testFolder, string fileName)
{
File.WriteAllText(fullFileName, (++intVersion).ToString());
}
}

return false;
/// <summary>
/// Create a new file with an initial version of 1
/// </summary>
/// <param name="testFolder">The temporary test folder created by TestHelpers.DescribeTestFolder</param>
/// <param name="fileName">The file to be created</param>
public static void CreateFileWithInitialVersion(string testFolder, string fileName)
{
string fullFileName = Path.Combine(testFolder, fileName);

if (File.Exists(fullFileName))
{
string errorString = String.Format("FileName {0} already exists", fullFileName);
throw new ArgumentException(errorString);
}

File.WriteAllText(fullFileName, "1");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

using System;

namespace Azure.Sdk.Tools.TestProxy.Console
{
/// <summary>
/// Implementation of IConsoleWrapper that's simply a passthrough to the Console functions.
/// </summary>
public class ConsoleWrapper : IConsoleWrapper
{
public void Write(string message)
{
System.Console.Write(message);
}
public void WriteLine(string message)
{
System.Console.WriteLine(message);
}
public string ReadLine()
{
return System.Console.ReadLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;

namespace Azure.Sdk.Tools.TestProxy.Console
{
/// <summary>
/// Implementation of IConsoleWrapper that will be used to test commands, like Reset, that require user input.
/// </summary>
public class ConsoleWrapperTester : IConsoleWrapper
{
private string _readLineResponse;

public ConsoleWrapperTester() { }

/// <summary>
/// Overloaded constructor takes in a string that'll be returned as the ReadLine response.
/// </summary>
/// <param name="readLineResponse">string that'll be returned as the ReadLine response</param>
public ConsoleWrapperTester(string readLineResponse)
{
_readLineResponse = readLineResponse;
}

/// <summary>
/// Set the ReadLine response.
/// </summary>
/// <param name="readLineResponse">string that'll be returned as the ReadLine response</param>
public void SetReadLineResponse(string readLineResponse)
{
_readLineResponse = readLineResponse;
}
public void Write(string message)
{
System.Console.Write(message);
}
public void WriteLine(string message)
{
System.Console.WriteLine(message);
}
public string ReadLine()
{
return _readLineResponse;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Azure.Sdk.Tools.TestProxy.Console
{
/// <summary>
/// IConsoleWrapper is just an interface around Console functions. This is necessary for testing
/// functions, like Reset, which require user input that we need to be able to control.
/// </summary>
public interface IConsoleWrapper
{
void Write(string message);
void WriteLine(string message);
string ReadLine();
}
}
15 changes: 8 additions & 7 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Reflection;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Azure.Sdk.Tools.TestProxy.Store;
using Azure.Sdk.Tools.TestProxy.Console;

namespace Azure.Sdk.Tools.TestProxy
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public static void Main(bool insecure = false, string storageLocation = null, st
var semanticVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
var assemblyVersion = assembly.GetName().Version;

Console.WriteLine($"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}-dev.{semanticVersion}");
System.Console.WriteLine($"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}-dev.{semanticVersion}");

Environment.Exit(0);
}
Expand Down Expand Up @@ -123,12 +124,12 @@ public static void Main(bool insecure = false, string storageLocation = null, st
if (dump)
{
var config = app.Services?.GetService<IConfiguration>();
Console.WriteLine("Dumping Resolved Configuration Values:");
System.Console.WriteLine("Dumping Resolved Configuration Values:");
if (config != null)
{
foreach (var c in config.AsEnumerable())
{
Console.WriteLine(c.Key + " = " + c.Value);
System.Console.WriteLine(c.Key + " = " + c.Value);
}
}
}
Expand Down Expand Up @@ -238,21 +239,21 @@ private static Thread PrintStatus(Func<object> status, bool newLine, Cancellatio

if (newLine)
{
Console.WriteLine(obj);
System.Console.WriteLine(obj);
}
else
{
Console.Write(obj);
System.Console.Write(obj);
needsExtraNewline = true;
}
}

if (needsExtraNewline)
{
Console.WriteLine();
System.Console.WriteLine();
}

Console.WriteLine();
System.Console.WriteLine();
});

thread.Start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public virtual bool TryRun(string arguments, GitAssetsConfiguration config, out
result = new CommandResult()
{
ExitCode = process.ExitCode,
StdErr = stdOut,
StdOut = stdErr,
StdErr = stdErr,
StdOut = stdOut,
Arguments = arguments
};
}
Expand Down
48 changes: 40 additions & 8 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Console;

namespace Azure.Sdk.Tools.TestProxy.Store
{
Expand All @@ -26,11 +27,20 @@ public class DirectoryEvaluation
public class GitStore : IAssetsStore
{
private HttpClient httpClient = new HttpClient();
private IConsoleWrapper _consoleWrapper;
public GitProcessHandler GitHandler = new GitProcessHandler();
public string DefaultBranch = "main";
public string FileName = "assets.json";

public GitStore() { }
public GitStore()
{
_consoleWrapper = new ConsoleWrapper();
}

public GitStore(IConsoleWrapper consoleWrapper)
{
_consoleWrapper = consoleWrapper;
}

public GitStore(GitProcessHandler processHandler) {
GitHandler = processHandler;
Expand Down Expand Up @@ -119,15 +129,16 @@ public async Task Restore(string pathToAssetsJson) {
}

/// <summary>
/// Resets a cloned assets repository to the default contained within the assets.json targeted commit.
/// Resets a cloned assets repository to the default contained within the assets.json targeted commit. This
/// function should only be called by the user as the server will only use Restore.
/// </summary>
/// <param name="pathToAssetsJson"></param>
/// <returns></returns>
// This should only ever be called by the user?
public async Task Reset(string pathToAssetsJson) {
public async Task Reset(string pathToAssetsJson)
{
var config = await ParseConfigurationFile(pathToAssetsJson);
var initialized = config.IsAssetsRepoInitialized();
var allowReset = true;
var allowReset = false;

if (!initialized)
{
Expand All @@ -138,15 +149,34 @@ public async Task Reset(string pathToAssetsJson) {

if (pendingChanges.Length > 0)
{
// TODO: Azure/azure-sdk-tools/3698
_consoleWrapper.WriteLine("There are pending git chances, are you sure you want to reset? [Y|N]");
while (true)
{
string response = _consoleWrapper.ReadLine();
response = response.ToLowerInvariant();
if (response.Equals("y"))
{
allowReset = true;
break;
}
else if (response.Equals("n"))
{
allowReset = false;
break;
}
else
{
_consoleWrapper.WriteLine("Please answer [Y|N]");
}
}
}

if (allowReset)
{
try
{
GitHandler.Run("checkout *", config);
GitHandler.Run("git clean -xdf", config);
GitHandler.Run("clean -xdf", config);
}
catch(GitProcessException e)
{
Expand Down Expand Up @@ -186,7 +216,9 @@ public string[] DetectPendingChanges(GitAssetsConfiguration config)

if (!string.IsNullOrWhiteSpace(diffResult.StdOut))
{
var individualResults = diffResult.StdOut.Split(Environment.NewLine).Select(x => x.Trim()).ToArray();
// Normally, we'd use Environment.NewLine here but this doesn't work on Windows since its NewLine is \r\n and
// Git's NewLine is just \n
var individualResults = diffResult.StdOut.Split("\n").Select(x => x.Trim()).ToArray();
return individualResults;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Threading.Tasks;
using Azure.Sdk.Tools.TestProxy.Console;

namespace Azure.Sdk.Tools.TestProxy.Store
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Threading.Tasks;
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
using Azure.Sdk.Tools.TestProxy.Console;

namespace Azure.Sdk.Tools.TestProxy.Store
{
Expand Down