Skip to content

Commit d363787

Browse files
authored
Add integration reset tests (#4013)
* Add integration reset tests * Updates based upon feedback
1 parent 5027b39 commit d363787

File tree

11 files changed

+558
-24
lines changed

11 files changed

+558
-24
lines changed

tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationResetTests.cs

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.

tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationRestoreTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

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

1616
// Setup:
1717
// The files live under https://github.com/Azure/azure-sdk-assets-integration/tree/main/pull/scenarios.
1818
// Each file contains nothing but a single version digit, which is used for verification purposes.
19-
// There are 3 pull test scenarios and each uses a different SHA. The scenarios are detailed down
19+
// There are restore test scenarios and each uses a different SHA. The scenarios are detailed down
2020
// below with their test functions.
2121
public class GitStoreIntegrationRestoreTests
2222
{
@@ -37,7 +37,8 @@ public GitStoreIntegrationRestoreTests()
3737
// Added file2.txt
3838
// Added file3.txt
3939
// Expect: each file should be version 1
40-
[Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")]
40+
[Theory(Skip = "Skipping because the integration branch permissions need to be set for the test suite to run.")]
41+
//[Theory]
4142
[InlineData(
4243
@"{
4344
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",
@@ -90,6 +91,7 @@ public async Task Scenario1(string inputJson)
9091
// file3 version 2
9192
// file4 version 1
9293
[Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")]
94+
//[Theory]
9395
[InlineData(
9496
@"{
9597
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",
@@ -144,6 +146,7 @@ public async Task Scenario2(string inputJson)
144146
// file4 version 1
145147
// file5 version 1
146148
[Theory(Skip = "Skipping because we the integration branch permissions set for the test suite to run.")]
149+
//[Theory]
147150
[InlineData(
148151
@"{
149152
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",

tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestHelpers.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,8 @@ public static bool VerifyFileVersion(string testFolder, string fileName, int exp
231231
/// Verify the version, inside the file, for a given file inside of a test folder.
232232
/// </summary>
233233
/// <param name="testFolder">The temporary test folder created by TestHelpers.DescribeTestFolder</param>
234-
/// <param name="fileName">The fileName whose version needs verification</param>
235-
/// <param name="expectedVersion">The expected version in the file</param>
236-
public static bool IncrementFileVersion(string testFolder, string fileName)
234+
/// <param name="fileName">The file whose version needs to be incremented</param>
235+
public static void IncrementFileVersion(string testFolder, string fileName)
237236
{
238237
string fullFileName = Path.Combine(testFolder, fileName);
239238
string stringVersion = "";
@@ -254,8 +253,24 @@ public static bool IncrementFileVersion(string testFolder, string fileName)
254253
{
255254
File.WriteAllText(fullFileName, (++intVersion).ToString());
256255
}
256+
}
257257

258-
return false;
258+
/// <summary>
259+
/// Create a new file with an initial version of 1
260+
/// </summary>
261+
/// <param name="testFolder">The temporary test folder created by TestHelpers.DescribeTestFolder</param>
262+
/// <param name="fileName">The file to be created</param>
263+
public static void CreateFileWithInitialVersion(string testFolder, string fileName)
264+
{
265+
string fullFileName = Path.Combine(testFolder, fileName);
266+
267+
if (File.Exists(fullFileName))
268+
{
269+
string errorString = String.Format("FileName {0} already exists", fullFileName);
270+
throw new ArgumentException(errorString);
271+
}
272+
273+
File.WriteAllText(fullFileName, "1");
259274
}
260275
}
261276
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+

2+
using System;
3+
4+
namespace Azure.Sdk.Tools.TestProxy.Console
5+
{
6+
/// <summary>
7+
/// Implementation of IConsoleWrapper that's simply a passthrough to the Console functions.
8+
/// </summary>
9+
public class ConsoleWrapper : IConsoleWrapper
10+
{
11+
public void Write(string message)
12+
{
13+
System.Console.Write(message);
14+
}
15+
public void WriteLine(string message)
16+
{
17+
System.Console.WriteLine(message);
18+
}
19+
public string ReadLine()
20+
{
21+
return System.Console.ReadLine();
22+
}
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
namespace Azure.Sdk.Tools.TestProxy.Console
4+
{
5+
/// <summary>
6+
/// Implementation of IConsoleWrapper that will be used to test commands, like Reset, that require user input.
7+
/// </summary>
8+
public class ConsoleWrapperTester : IConsoleWrapper
9+
{
10+
private string _readLineResponse;
11+
12+
public ConsoleWrapperTester() { }
13+
14+
/// <summary>
15+
/// Overloaded constructor takes in a string that'll be returned as the ReadLine response.
16+
/// </summary>
17+
/// <param name="readLineResponse">string that'll be returned as the ReadLine response</param>
18+
public ConsoleWrapperTester(string readLineResponse)
19+
{
20+
_readLineResponse = readLineResponse;
21+
}
22+
23+
/// <summary>
24+
/// Set the ReadLine response.
25+
/// </summary>
26+
/// <param name="readLineResponse">string that'll be returned as the ReadLine response</param>
27+
public void SetReadLineResponse(string readLineResponse)
28+
{
29+
_readLineResponse = readLineResponse;
30+
}
31+
public void Write(string message)
32+
{
33+
System.Console.Write(message);
34+
}
35+
public void WriteLine(string message)
36+
{
37+
System.Console.WriteLine(message);
38+
}
39+
public string ReadLine()
40+
{
41+
return _readLineResponse;
42+
}
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Azure.Sdk.Tools.TestProxy.Console
2+
{
3+
/// <summary>
4+
/// IConsoleWrapper is just an interface around Console functions. This is necessary for testing
5+
/// functions, like Reset, which require user input that we need to be able to control.
6+
/// </summary>
7+
public interface IConsoleWrapper
8+
{
9+
void Write(string message);
10+
void WriteLine(string message);
11+
string ReadLine();
12+
}
13+
}

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Reflection;
1818
using Microsoft.AspNetCore.Server.Kestrel.Core;
1919
using Azure.Sdk.Tools.TestProxy.Store;
20+
using Azure.Sdk.Tools.TestProxy.Console;
2021

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

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

6465
Environment.Exit(0);
6566
}
@@ -123,12 +124,12 @@ public static void Main(bool insecure = false, string storageLocation = null, st
123124
if (dump)
124125
{
125126
var config = app.Services?.GetService<IConfiguration>();
126-
Console.WriteLine("Dumping Resolved Configuration Values:");
127+
System.Console.WriteLine("Dumping Resolved Configuration Values:");
127128
if (config != null)
128129
{
129130
foreach (var c in config.AsEnumerable())
130131
{
131-
Console.WriteLine(c.Key + " = " + c.Value);
132+
System.Console.WriteLine(c.Key + " = " + c.Value);
132133
}
133134
}
134135
}
@@ -238,21 +239,21 @@ private static Thread PrintStatus(Func<object> status, bool newLine, Cancellatio
238239

239240
if (newLine)
240241
{
241-
Console.WriteLine(obj);
242+
System.Console.WriteLine(obj);
242243
}
243244
else
244245
{
245-
Console.Write(obj);
246+
System.Console.Write(obj);
246247
needsExtraNewline = true;
247248
}
248249
}
249250

250251
if (needsExtraNewline)
251252
{
252-
Console.WriteLine();
253+
System.Console.WriteLine();
253254
}
254255

255-
Console.WriteLine();
256+
System.Console.WriteLine();
256257
});
257258

258259
thread.Start();

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitProcessHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ public virtual bool TryRun(string arguments, GitAssetsConfiguration config, out
157157
result = new CommandResult()
158158
{
159159
ExitCode = process.ExitCode,
160-
StdErr = stdOut,
161-
StdOut = stdErr,
160+
StdErr = stdErr,
161+
StdOut = stdOut,
162162
Arguments = arguments
163163
};
164164
}

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Linq;
1111
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
1212
using Azure.Sdk.Tools.TestProxy.Common;
13+
using Azure.Sdk.Tools.TestProxy.Console;
1314

1415
namespace Azure.Sdk.Tools.TestProxy.Store
1516
{
@@ -26,11 +27,20 @@ public class DirectoryEvaluation
2627
public class GitStore : IAssetsStore
2728
{
2829
private HttpClient httpClient = new HttpClient();
30+
private IConsoleWrapper _consoleWrapper;
2931
public GitProcessHandler GitHandler = new GitProcessHandler();
3032
public string DefaultBranch = "main";
3133
public string FileName = "assets.json";
3234

33-
public GitStore() { }
35+
public GitStore()
36+
{
37+
_consoleWrapper = new ConsoleWrapper();
38+
}
39+
40+
public GitStore(IConsoleWrapper consoleWrapper)
41+
{
42+
_consoleWrapper = consoleWrapper;
43+
}
3444

3545
public GitStore(GitProcessHandler processHandler) {
3646
GitHandler = processHandler;
@@ -119,15 +129,16 @@ public async Task Restore(string pathToAssetsJson) {
119129
}
120130

121131
/// <summary>
122-
/// Resets a cloned assets repository to the default contained within the assets.json targeted commit.
132+
/// Resets a cloned assets repository to the default contained within the assets.json targeted commit. This
133+
/// function should only be called by the user as the server will only use Restore.
123134
/// </summary>
124135
/// <param name="pathToAssetsJson"></param>
125136
/// <returns></returns>
126-
// This should only ever be called by the user?
127-
public async Task Reset(string pathToAssetsJson) {
137+
public async Task Reset(string pathToAssetsJson)
138+
{
128139
var config = await ParseConfigurationFile(pathToAssetsJson);
129140
var initialized = config.IsAssetsRepoInitialized();
130-
var allowReset = true;
141+
var allowReset = false;
131142

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

139150
if (pendingChanges.Length > 0)
140151
{
141-
// TODO: Azure/azure-sdk-tools/3698
152+
_consoleWrapper.WriteLine("There are pending git chances, are you sure you want to reset? [Y|N]");
153+
while (true)
154+
{
155+
string response = _consoleWrapper.ReadLine();
156+
response = response.ToLowerInvariant();
157+
if (response.Equals("y"))
158+
{
159+
allowReset = true;
160+
break;
161+
}
162+
else if (response.Equals("n"))
163+
{
164+
allowReset = false;
165+
break;
166+
}
167+
else
168+
{
169+
_consoleWrapper.WriteLine("Please answer [Y|N]");
170+
}
171+
}
142172
}
143173

144174
if (allowReset)
145175
{
146176
try
147177
{
148178
GitHandler.Run("checkout *", config);
149-
GitHandler.Run("git clean -xdf", config);
179+
GitHandler.Run("clean -xdf", config);
150180
}
151181
catch(GitProcessException e)
152182
{
@@ -186,7 +216,9 @@ public string[] DetectPendingChanges(GitAssetsConfiguration config)
186216

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

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/IAssetsStore.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using System.Threading.Tasks;
3+
using Azure.Sdk.Tools.TestProxy.Console;
34

45
namespace Azure.Sdk.Tools.TestProxy.Store
56
{

0 commit comments

Comments
 (0)