Skip to content

Commit

Permalink
Enable to playback track 1 session record for track 2 mgmt sdk test c…
Browse files Browse the repository at this point in the history
…ases (#11535)
  • Loading branch information
erich-wang authored Apr 27, 2020
1 parent 9b59704 commit 9d389d3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ public void Intercept(IInvocation invocation)
}
}
}
}
}
7 changes: 7 additions & 0 deletions sdk/core/Azure.Core/tests/TestFramework/RecordEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class RecordEntry

public string RequestUri { get; set; }

public bool IsTrack1Recording { get; set; }

public RequestMethod RequestMethod { get; set; }

public int StatusCode { get; set; }
Expand All @@ -36,6 +38,11 @@ public static RecordEntry Deserialize(JsonElement element)
record.RequestUri = property.GetString();
}

if (element.TryGetProperty("EncodedRequestUri", out property))
{
record.IsTrack1Recording = true;
}

if (element.TryGetProperty("RequestHeaders", out property))
{
DeserializeHeaders(record.Request.Headers, property);
Expand Down
20 changes: 18 additions & 2 deletions sdk/core/Azure.Core/tests/TestFramework/RecordMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ public virtual RecordEntry FindMatch(Request request, IList<RecordEntry> entries
{
int score = 0;

if (!AreUrisSame(entry.RequestUri, uri))
var recordRequestUri = entry.RequestUri;
if (entry.IsTrack1Recording)
{
//there's no domain name for request uri in track 1 record, so add it from reqeust uri
int len = 8; //length of "https://"
int domainEndingIndex = uri.IndexOf('/', len);
if (domainEndingIndex > 0)
{
recordRequestUri = uri.Substring(0, domainEndingIndex) + recordRequestUri;
}
}

if (!AreUrisSame(recordRequestUri, uri))
{
score++;
}
Expand All @@ -84,7 +96,11 @@ public virtual RecordEntry FindMatch(Request request, IList<RecordEntry> entries
score++;
}

score += CompareHeaderDictionaries(headers, entry.Request.Headers, ExcludeHeaders);
//we only check Uri + RequestMethod for track1 record
if (entry.IsTrack1Recording)
{
score += CompareHeaderDictionaries(headers, entry.Request.Headers, ExcludeHeaders);
}

if (score == 0)
{
Expand Down
16 changes: 16 additions & 0 deletions sdk/core/Azure.Core/tests/TestFramework/RecordSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class RecordSession

public SortedDictionary<string, string> Variables { get; } = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

//Used only for deserializing track 1 session record files
public Dictionary<string, Queue<string>> Names { get; set; } = new Dictionary<string, Queue<string>>();

public void Serialize(Utf8JsonWriter jsonWriter)
{
jsonWriter.WriteStartObject();
Expand Down Expand Up @@ -52,6 +55,19 @@ public static RecordSession Deserialize(JsonElement element)
session.Variables[item.Name] = item.Value.GetString();
}
}

if (element.TryGetProperty(nameof(Names), out property))
{
foreach (JsonProperty item in property.EnumerateObject())
{
var queue = new Queue<string>();
foreach (JsonElement subItem in item.Value.EnumerateArray())
{
queue.Enqueue(subItem.GetString());
}
session.Names[item.Name] = queue;
}
}
return session;
}

Expand Down
29 changes: 28 additions & 1 deletion sdk/core/Azure.Core/tests/TestFramework/TestRecording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -90,7 +92,15 @@ public TestRandom Random
_random = new TestRandom(Mode, seed);
break;
case RecordedTestMode.Playback:
_random = new TestRandom(Mode, int.Parse(_session.Variables[RandomSeedVariableKey]));
if (IsTrack1SessionRecord())
{
//random is not really used for track 1 playback, so randomly pick one as seed
_random = new TestRandom(Mode, (int)DateTime.UtcNow.Ticks);
}
else
{
_random = new TestRandom(Mode, int.Parse(_session.Variables[RandomSeedVariableKey]));
}
break;
default:
throw new ArgumentOutOfRangeException();
Expand Down Expand Up @@ -201,6 +211,23 @@ public string GenerateId()
return Random.Next().ToString();
}

public string GenerateAssetName(string prefix, [CallerMemberName]string callerMethodName = "testframework_failed")
{
if (Mode == RecordedTestMode.Playback && IsTrack1SessionRecord())
{
return _session.Names[callerMethodName].Dequeue();
}
else
{
return prefix + Random.Next(9999);
}
}

public bool IsTrack1SessionRecord()
{
return _session.Entries.FirstOrDefault()?.IsTrack1Recording ?? false;
}

public string GetVariable(string variableName, string defaultValue)
{
switch (Mode)
Expand Down

0 comments on commit 9d389d3

Please sign in to comment.