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

Users/dogirala/ptr feature flag vss conn #4821

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 15 additions & 10 deletions src/Agent.Worker/TestResults/Legacy/LegacyTestRunDataPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.TeamFoundation.TestManagement.WebApi;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.VisualStudio.Services.Agent.Worker.CodeCoverage;
using Microsoft.VisualStudio.Services.Agent.Worker.TestResults;
using Microsoft.VisualStudio.Services.Agent.Worker.TestResults.Utils;
using Microsoft.VisualStudio.Services.WebApi;
Expand Down Expand Up @@ -34,8 +35,8 @@ public class LegacyTestRunDataPublisher : AgentService, ILegacyTestRunDataPublis
private const string _testRunSystemCustomFieldName = "TestRunSystem";
private readonly object _sync = new object();
private int _runCounter = 0;
private IFeatureFlagService _featureFlagService;
private bool _calculateTestRunSummary;
private bool _isFlakyCheckEnabled;
private string _testRunner;
private ITestResultsServer _testResultsServer;
private TestRunDataPublisherHelper _testRunPublisherHelper;
Expand All @@ -48,13 +49,11 @@ public void InitializePublisher(IExecutionContext context, string projectName, V
_testRunner = testRunner;
_resultReader = GetTestResultReader(_testRunner, publishRunLevelAttachments);
_testRunPublisher = HostContext.GetService<ITestRunPublisher>();
_featureFlagService = HostContext.GetService<IFeatureFlagService>();
_featureFlagService.InitializeFeatureService(_executionContext, connection);
_testRunPublisher.InitializePublisher(_executionContext, connection, projectName, _resultReader);
_testResultsServer = HostContext.GetService<ITestResultsServer>();
_testResultsServer.InitializeServer(connection, _executionContext);
_calculateTestRunSummary = _featureFlagService.GetFeatureFlagState(TestResultsConstants.CalculateTestRunSummaryFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid);
_testRunPublisherHelper = new TestRunDataPublisherHelper(_executionContext, null, _testRunPublisher, _testResultsServer);
LoadFeatureFlagState();
Trace.Leaving();
}

Expand Down Expand Up @@ -208,9 +207,7 @@ private async Task<bool> PublishAllTestResultsToSingleTestRunAsync(List<string>

// Check failed results for flaky aware
// Fallback to flaky aware if there are any failures.
bool isFlakyCheckEnabled = _featureFlagService.GetFeatureFlagState(TestResultsConstants.EnableFlakyCheckInAgentFeatureFlag, TestResultsConstants.TCMServiceInstanceGuid);

if (isTestRunOutcomeFailed && isFlakyCheckEnabled)
if (isTestRunOutcomeFailed && _isFlakyCheckEnabled)
{
IList<TestRun> publishedRuns = new List<TestRun>();
publishedRuns.Add(updatedRun);
Expand Down Expand Up @@ -313,9 +310,7 @@ private async Task<bool> PublishToNewTestRunPerTestResultFileAsync(List<string>

// Check failed results for flaky aware
// Fallback to flaky aware if there are any failures.
bool isFlakyCheckEnabled = _featureFlagService.GetFeatureFlagState(TestResultsConstants.EnableFlakyCheckInAgentFeatureFlag, TestResultsConstants.TCMServiceInstanceGuid);

if (isTestRunOutcomeFailed && isFlakyCheckEnabled)
if (isTestRunOutcomeFailed && _isFlakyCheckEnabled)
{
var runOutcome = _testRunPublisherHelper.CheckRunsForFlaky(publishedRuns, _projectName);
if (runOutcome != null && runOutcome.HasValue)
Expand Down Expand Up @@ -408,5 +403,15 @@ private bool GetTestRunOutcome(TestRunData testRunData, TestRunSummary testRunSu
}
return anyFailedTests;
}

private void LoadFeatureFlagState()
{
using (var connection = WorkerUtilities.GetVssConnection(_executionContext))
{
var featureFlagService = _executionContext.GetHostContext().GetService<IFeatureFlagService>();
_calculateTestRunSummary = featureFlagService.GetFeatureFlagStateByName(_executionContext, TestResultsConstants.CalculateTestRunSummaryFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid, connection);
_isFlakyCheckEnabled = featureFlagService.GetFeatureFlagStateByName(_executionContext, TestResultsConstants.EnableFlakyCheckInAgentFeatureFlag, TestResultsConstants.TCMServiceInstanceGuid, connection);
}
}
}
}
7 changes: 3 additions & 4 deletions src/Agent.Worker/TestResults/ResultsCommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,9 @@ private void LoadFeatureFlagState()
using (var connection = WorkerUtilities.GetVssConnection(_executionContext))
{
var featureFlagService = _executionContext.GetHostContext().GetService<IFeatureFlagService>();
featureFlagService.InitializeFeatureService(_executionContext, connection);
_publishTestResultsLibFeatureState = featureFlagService.GetFeatureFlagState(TestResultsConstants.UsePublishTestResultsLibFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid);
_enableAzureTestPlanFeatureState = featureFlagService.GetFeatureFlagState(TestResultsConstants.EnableAzureTestPlanTaskFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid);
_triggerCoverageMergeJobFeatureState = featureFlagService.GetFeatureFlagState(CodeCoverageConstants.TriggerCoverageMergeJobFF, TestResultsConstants.TFSServiceInstanceGuid);
_publishTestResultsLibFeatureState = featureFlagService.GetFeatureFlagStateByName(_executionContext, TestResultsConstants.UsePublishTestResultsLibFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid, connection);
_enableAzureTestPlanFeatureState = featureFlagService.GetFeatureFlagStateByName(_executionContext, TestResultsConstants.EnableAzureTestPlanTaskFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid, connection);
_triggerCoverageMergeJobFeatureState = featureFlagService.GetFeatureFlagStateByName(_executionContext, CodeCoverageConstants.TriggerCoverageMergeJobFF, TestResultsConstants.TFSServiceInstanceGuid, connection);
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/Agent.Worker/TestResults/TestDataPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public sealed class TestDataPublisher : AgentService, ITestDataPublisher
private IFeatureFlagService _featureFlagService;
private string _testRunner;
private bool _calculateTestRunSummary;
private bool _isFlakyCheckEnabled;
private TestRunDataPublisherHelper _testRunPublisherHelper;
private ITestResultsServer _testResultsServer;

Expand All @@ -60,6 +61,7 @@ public void InitializePublisher(IExecutionContext context, string projectName, V
_featureFlagService.InitializeFeatureService(_executionContext, connection);
_parser = (extensionManager.GetExtensions<IParser>()).FirstOrDefault(x => _testRunner.Equals(x.Name, StringComparison.OrdinalIgnoreCase));
_testRunPublisherHelper = new TestRunDataPublisherHelper(_executionContext, _testRunPublisher, null, _testResultsServer);
LoadFeatureFlagState();
Trace.Leaving();
}

Expand All @@ -86,8 +88,6 @@ public void InitializePublisher(IExecutionContext context, string projectName, V

IList<TestRun> publishedRuns = publishtestRunDataTask.Result;

_calculateTestRunSummary = _featureFlagService.GetFeatureFlagState(TestResultsConstants.CalculateTestRunSummaryFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid);

var isTestRunOutcomeFailed = GetTestRunOutcome(_executionContext, testRunData, out TestRunSummary testRunSummary);

// Storing testrun summary in environment variable, which will be read by PublishPipelineMetadataTask and publish to evidence store.
Expand All @@ -98,9 +98,7 @@ public void InitializePublisher(IExecutionContext context, string projectName, V

// Check failed results for flaky aware
// Fallback to flaky aware if there are any failures.
bool isFlakyCheckEnabled = _featureFlagService.GetFeatureFlagState(TestResultsConstants.EnableFlakyCheckInAgentFeatureFlag, TestResultsConstants.TCMServiceInstanceGuid);

if (isTestRunOutcomeFailed && isFlakyCheckEnabled)
if (isTestRunOutcomeFailed && _isFlakyCheckEnabled)
{
var runOutcome = _testRunPublisherHelper.CheckRunsForFlaky(publishedRuns, _projectName);
if (runOutcome != null && runOutcome.HasValue)
Expand Down Expand Up @@ -188,8 +186,6 @@ public void InitializePublisher(IExecutionContext context, string projectName, V

IList<TestRun> publishedRuns = publishtestRunDataTask.Result;

_calculateTestRunSummary = _featureFlagService.GetFeatureFlagState(TestResultsConstants.CalculateTestRunSummaryFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid);

var isTestRunOutcomeFailed = GetTestRunOutcome(_executionContext, testRunData, out TestRunSummary testRunSummary);

// Storing testrun summary in environment variable, which will be read by PublishPipelineMetadataTask and publish to evidence store.
Expand All @@ -200,9 +196,7 @@ public void InitializePublisher(IExecutionContext context, string projectName, V

// Check failed results for flaky aware
// Fallback to flaky aware if there are any failures.
bool isFlakyCheckEnabled = _featureFlagService.GetFeatureFlagState(TestResultsConstants.EnableFlakyCheckInAgentFeatureFlag, TestResultsConstants.TCMServiceInstanceGuid);

if (isTestRunOutcomeFailed && isFlakyCheckEnabled)
if (isTestRunOutcomeFailed && _isFlakyCheckEnabled)
{
var runOutcome = _testRunPublisherHelper.CheckRunsForFlaky(publishedRuns, _projectName);
if (runOutcome != null && runOutcome.HasValue)
Expand Down Expand Up @@ -327,5 +321,15 @@ private async Task<Guid> GetProjectId(string projectName)

return proj.Id;
}

private void LoadFeatureFlagState()
{
using (var connection = WorkerUtilities.GetVssConnection(_executionContext))
{
var featureFlagService = _executionContext.GetHostContext().GetService<IFeatureFlagService>();
_calculateTestRunSummary = featureFlagService.GetFeatureFlagStateByName(_executionContext, TestResultsConstants.CalculateTestRunSummaryFeatureFlag, TestResultsConstants.TFSServiceInstanceGuid, connection);
_isFlakyCheckEnabled = featureFlagService.GetFeatureFlagStateByName(_executionContext, TestResultsConstants.EnableFlakyCheckInAgentFeatureFlag, TestResultsConstants.TCMServiceInstanceGuid, connection);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Agent.Worker/TestResults/TestRunDataPublisherHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ internal protected virtual CancellationToken GetCancellationToken()
return new CancellationToken();
}

protected internal virtual bool GetFeatureFlagState(string featureFlagName, Guid serviceInstaceGuid)
protected internal virtual bool GetFeatureFlagState(string featureFlagName, Guid serviceInstanceGuid)
{
var featureFlagValue = false;

using (var connection = WorkerUtilities.GetVssConnection(_executionContext))
{
var featureFlagService = _executionContext.GetHostContext().GetService<IFeatureFlagService>();
featureFlagService.InitializeFeatureService(_executionContext, connection);
featureFlagValue = featureFlagService.GetFeatureFlagState(featureFlagName, serviceInstaceGuid);
featureFlagValue = featureFlagService.GetFeatureFlagState(featureFlagName, serviceInstanceGuid);
}

return featureFlagValue;
Expand Down
22 changes: 22 additions & 0 deletions src/Agent.Worker/TestResults/Utils/FeatureFlagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface IFeatureFlagService : IAgentService
void InitializeFeatureService(IExecutionContext executionContext, VssConnection connection);

bool GetFeatureFlagState(string featureFlagName, Guid serviceInstanceId);

bool GetFeatureFlagStateByName(IExecutionContext executionContext, string featureFlagName, Guid serviceInstanceId, VssConnection connection);
}

public class FeatureFlagService : AgentService, IFeatureFlagService
Expand Down Expand Up @@ -50,5 +52,25 @@ public bool GetFeatureFlagState(string featureFlagName, Guid serviceInstanceId)
}
return false;
}

public bool GetFeatureFlagStateByName(IExecutionContext executionContext, string featureFlagName, Guid serviceInstanceId, VssConnection connection)
{
try
{
FeatureAvailabilityHttpClient featureAvailabilityHttpClient = connection.GetClient<FeatureAvailabilityHttpClient>(serviceInstanceId);
var featureFlag = featureAvailabilityHttpClient?.GetFeatureFlagByNameAsync(featureFlagName).Result;
if (featureFlag != null && featureFlag.EffectiveState.Equals("On", StringComparison.OrdinalIgnoreCase))
{
executionContext.Debug(StringUtil.Format("{0} is on", featureFlagName));
return true;
}
executionContext.Debug(StringUtil.Format("{0} is off", featureFlagName));
}
catch
{
executionContext.Debug(StringUtil.Format("Failed to get FF {0} Value.", featureFlagName));
}
return false;
}
}
}
Loading