Skip to content

Commit

Permalink
[Azure.Core TestFramework] Fix Required Optional Environment Variable
Browse files Browse the repository at this point in the history
The focus of these changes is to bypass the attempt to santitize an optional
environment variable when no value was present.  Previously, if the
variable was not read, sanitizing would result in an exception.
  • Loading branch information
jsquire committed Sep 4, 2020
1 parent 4aea0c3 commit dc93ecf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 11 additions & 4 deletions sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ protected string GetRecordedOptionalVariable(string name, Action<RecordedVariabl

string value = GetOptionalVariable(name);

var optionsInstance = new RecordedVariableOptions();
options?.Invoke(optionsInstance);
var sanitizedValue = optionsInstance.Apply(value);

if (!Mode.HasValue)
{
return value;
Expand All @@ -191,6 +187,17 @@ protected string GetRecordedOptionalVariable(string name, Action<RecordedVariabl
throw new InvalidOperationException("Recorded value should not be set outside the test method invocation");
}

// If the value was populated, sanitize before recording it.

string sanitizedValue = value;

if (!string.IsNullOrEmpty(value))
{
var optionsInstance = new RecordedVariableOptions();
options?.Invoke(optionsInstance);
sanitizedValue = optionsInstance.Apply(sanitizedValue);
}

_recording?.SetVariable(name, sanitizedValue);
return value;
}
Expand Down
20 changes: 19 additions & 1 deletion sdk/core/Azure.Core/tests/TestEnvironmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ public void RecordedVariableSanitized()
Assert.AreEqual("endpoint=1;key=Sanitized", testRecording.GetVariable("ConnectionStringWithSecret", ""));
}

[Test]
public void RecordedOptionalVariableNotSanitizedIfMissing()
{
var tempFile = Path.GetTempFileName();
var env = new MockTestEnvironment();
var testRecording = new TestRecording(RecordedTestMode.Record, tempFile, new RecordedTestSanitizer(), new RecordMatcher());
env.Mode = RecordedTestMode.Record;
env.SetRecording(testRecording);

Assert.IsNull(env.MissingOptionalSecret);

testRecording.Dispose();
testRecording = new TestRecording(RecordedTestMode.Playback, tempFile, new RecordedTestSanitizer(), new RecordMatcher());

Assert.IsNull(testRecording.GetVariable(nameof(env.MissingOptionalSecret), null));
}

private class RecordedVariableMisuse : RecordedTestBase<MockTestEnvironment>
{
// To make NUnit happy
Expand Down Expand Up @@ -114,8 +131,9 @@ public MockTestEnvironment(): base("core")
public string Base64Secret => GetRecordedVariable("Base64Secret", option => option.IsSecret(SanitizedValue.Base64));
public string DefaultSecret => GetRecordedVariable("DefaultSecret", option => option.IsSecret(SanitizedValue.Default));
public string CustomSecret => GetRecordedVariable("CustomSecret", option => option.IsSecret("Custom"));
public string MissingOptionalSecret => GetRecordedOptionalVariable("MissingOptionalSecret", option => option.IsSecret("INVALID"));
public string ConnectionStringWithSecret => GetRecordedVariable("ConnectionStringWithSecret", option => option.HasSecretConnectionStringParameter("key"));

}
}
}
}

0 comments on commit dc93ecf

Please sign in to comment.