Skip to content
Draft
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
8 changes: 8 additions & 0 deletions src/Agent.Listener/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,14 @@ private bool SetupVstsProxySetting(IVstsAgentWebProxy vstsProxy, CommandSettings
vstsProxy.SetupProxy(proxyUrl, proxyUserName, proxyPassword);
saveProxySetting = true;
}
else if (!string.IsNullOrEmpty(vstsProxy.ProxyAddress))
{
// If no command line proxy args provided but proxy is configured via environment variables or config files,
// ensure the proxy settings are properly applied during configuration
Trace.Info("Applying proxy settings from environment variables or config files during configuration.");
vstsProxy.SetupProxy(vstsProxy.ProxyAddress, vstsProxy.ProxyUsername, vstsProxy.ProxyPassword);
saveProxySetting = true;
}

return saveProxySetting;
}
Expand Down
99 changes: 99 additions & 0 deletions src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,105 @@ private List<EnvironmentInstance> GetEnvironments(string projectName, Guid proje
}

// Init the Agent Config Provider
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "ConfigurationManagement")]
public void SetupVstsProxySetting_ShouldApplyEnvironmentVariableProxySettings()
{
using (TestHostContext tc = CreateTestContext())
{
// Arrange
var configManager = new ConfigurationManager();
configManager.Initialize(tc);

var command = new CommandSettings(tc, new string[0]);

// Mock proxy settings from environment variables
_vstsAgentWebProxy.Setup(x => x.ProxyAddress).Returns("http://proxy.company.com:8080");
_vstsAgentWebProxy.Setup(x => x.ProxyUsername).Returns("proxyuser");
_vstsAgentWebProxy.Setup(x => x.ProxyPassword).Returns("proxypass");

// Act - Use reflection to call the private SetupVstsProxySetting method
var setupMethod = typeof(ConfigurationManager).GetMethod("SetupVstsProxySetting",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
bool result = (bool)setupMethod.Invoke(configManager, new object[] { _vstsAgentWebProxy.Object, command });

// Assert
Assert.True(result, "SetupVstsProxySetting should return true when proxy is configured from environment variables");

// Verify that SetupProxy was called with the environment variable values
_vstsAgentWebProxy.Verify(x => x.SetupProxy("http://proxy.company.com:8080", "proxyuser", "proxypass"), Times.Once);
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "ConfigurationManagement")]
public void SetupVstsProxySetting_ShouldReturnFalseWhenNoProxyConfigured()
{
using (TestHostContext tc = CreateTestContext())
{
// Arrange
var configManager = new ConfigurationManager();
configManager.Initialize(tc);

var command = new CommandSettings(tc, new string[0]);

// Mock no proxy settings
_vstsAgentWebProxy.Setup(x => x.ProxyAddress).Returns((string)null);

// Act - Use reflection to call the private SetupVstsProxySetting method
var setupMethod = typeof(ConfigurationManager).GetMethod("SetupVstsProxySetting",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
bool result = (bool)setupMethod.Invoke(configManager, new object[] { _vstsAgentWebProxy.Object, command });

// Assert
Assert.False(result, "SetupVstsProxySetting should return false when no proxy is configured");

// Verify that SetupProxy was not called
_vstsAgentWebProxy.Verify(x => x.SetupProxy(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "ConfigurationManagement")]
public void SetupVstsProxySetting_ShouldPreferCommandLineOverEnvironmentVariables()
{
using (TestHostContext tc = CreateTestContext())
{
// Arrange
var configManager = new ConfigurationManager();
configManager.Initialize(tc);

// Mock command line arguments to include proxy settings
var args = new string[]
{
"--proxyurl", "http://cmdline-proxy:8080",
"--proxyusername", "cmduser",
"--proxypassword", "cmdpass"
};
var command = new CommandSettings(tc, args);

// Mock proxy settings from environment variables (should be ignored)
_vstsAgentWebProxy.Setup(x => x.ProxyAddress).Returns("http://env-proxy:8080");
_vstsAgentWebProxy.Setup(x => x.ProxyUsername).Returns("envuser");
_vstsAgentWebProxy.Setup(x => x.ProxyPassword).Returns("envpass");

// Act - Use reflection to call the private SetupVstsProxySetting method
var setupMethod = typeof(ConfigurationManager).GetMethod("SetupVstsProxySetting",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
bool result = (bool)setupMethod.Invoke(configManager, new object[] { _vstsAgentWebProxy.Object, command });

// Assert
Assert.True(result, "SetupVstsProxySetting should return true when command line proxy is provided");

// Verify that SetupProxy was called with command line values, not environment values
_vstsAgentWebProxy.Verify(x => x.SetupProxy("http://cmdline-proxy:8080", "cmduser", "cmdpass"), Times.Once);
_vstsAgentWebProxy.Verify(x => x.SetupProxy("http://env-proxy:8080", It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
}

private List<IConfigurationProvider> GetConfigurationProviderList(TestHostContext tc)
{
IConfigurationProvider buildReleasesAgentConfigProvider = new BuildReleasesAgentConfigProvider();
Expand Down