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

[Azure.Core TestFramework] Fix Required Optional Environment Variable #14906

Merged
merged 1 commit into from
Sep 4, 2020
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
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pakrym: Based on your feedback on the intent of the options, I reordered things a bit to avoid triggering some code that didn't seem to apply when recording wasn't taking place. Please let me know if I overlooked something and I'll restore the original structure.

{
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are returning sanitized value here instead of an actual one. That's why tests were on fire.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just found the tests which I didn't realize were in the other solution and fixed the stupid.

}
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"));

}
}
}
}