Skip to content

Commit

Permalink
fixed service name for install multiple environments (#3455)
Browse files Browse the repository at this point in the history
* fix environment name for multiple running as a service

* add fix and test

* fix serviceDisplayName

* refactor

* refactor 2

* refactor naming

* fix request environmentName

Co-authored-by: Anna Opareva <[email protected]>
Co-authored-by: Nikita Ezzhev <[email protected]>
  • Loading branch information
3 people authored Jul 6, 2021
1 parent b92d3b4 commit 43997c4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Agent.Listener/Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public override async Task GetPoolIdAndName(AgentSettings agentSettings, Command

var environmentInstance = await GetEnvironmentAsync(_projectName, environmentName);

agentSettings.EnvironmentName = environmentName;
agentSettings.EnvironmentId = environmentInstance.Id;
agentSettings.ProjectName = environmentInstance.Project.Name;
agentSettings.ProjectId = environmentInstance.Project.Id.ToString();
Expand Down
18 changes: 14 additions & 4 deletions src/Agent.Listener/Configuration/ServiceControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public void CalculateServiceName(AgentSettings settings, string serviceNamePatte
throw new InvalidOperationException(StringUtil.Loc("CannotFindHostName", settings.ServerUrl));
}

serviceName = StringUtil.Format(serviceNamePattern, accountName, settings.PoolName, settings.AgentName);
string resourceCollection = settings.PoolName ?? settings.EnvironmentName;

serviceName = StringUtil.Format(serviceNamePattern, accountName, resourceCollection, settings.AgentName);

if (serviceName.Length > 80)
{
Expand All @@ -45,12 +47,18 @@ public void CalculateServiceName(AgentSettings settings, string serviceNamePatte
exceededCharLength -= accountName.Length - accountNameSubstring.Length;

string poolNameSubstring = StringUtil.SubstringPrefix(settings.PoolName, 25);
string environmentNameSubstring = StringUtil.SubstringPrefix(settings.EnvironmentName, 25);

if (settings.PoolName != null)
{
exceededCharLength -= settings.PoolName.Length - poolNameSubstring.Length;
}

if (settings.EnvironmentName != null)
{
exceededCharLength -= settings.EnvironmentName.Length - environmentNameSubstring.Length;
}

string agentNameSubstring = settings.AgentName;

// Only trim agent name if it's really necessary
Expand All @@ -59,11 +67,13 @@ public void CalculateServiceName(AgentSettings settings, string serviceNamePatte
agentNameSubstring = StringUtil.SubstringPrefix(settings.AgentName, settings.AgentName.Length - exceededCharLength);
}

serviceName = StringUtil.Format(serviceNamePattern, accountNameSubstring, poolNameSubstring, agentNameSubstring);
}
string resourceCollectionSubstring = poolNameSubstring ?? environmentNameSubstring;

serviceDisplayName = StringUtil.Format(serviceDisplayNamePattern, accountName, settings.PoolName, settings.AgentName);
serviceName = StringUtil.Format(serviceNamePattern, accountNameSubstring, resourceCollectionSubstring, agentNameSubstring);
}

serviceDisplayName = StringUtil.Format(serviceDisplayNamePattern, accountName, resourceCollection, settings.AgentName);

Trace.Info($"Service name '{serviceName}' display name '{serviceDisplayName}' will be used for service configuration.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public string Fingerprint

[DataMember(EmitDefaultValue = false)]
public int EnvironmentVMResourceId { get; set; }

[DataMember(EmitDefaultValue = false)]
public string EnvironmentName { get; set; }
}

[DataContract]
Expand Down
16 changes: 8 additions & 8 deletions src/Test/L0/Listener/Configuration/ServiceControlManagerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private class ServiceNameTest
public String AgentName;
public String PoolName;
public String Environment;
public String EnvironmentName;
public String ServerUrl;

}
Expand Down Expand Up @@ -74,23 +75,22 @@ public void CalculateServiceNameL0()
TestName = "Agent For Environment Test",
ServiceNamePattern = "vsts.agent.{0}.{1}.{2}.service",
ServiceDisplayPattern = "Azure Pipelines Agent ({0}.{1}.{2})",
AgentName = new string('X', 80),
AgentName = new string('X', 40),
EnvironmentName = new string('Y',40),
ServerUrl = "https://dev.azure.com/bar",
ExpectedServiceName = "vsts.agent.bar..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.service",
ExpectedServiceDisplayName = "Azure Pipelines Agent (bar..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)"
ExpectedServiceName = "vsts.agent.bar.YYYYYYYYYYYYYYYYYYYYYYYYY.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.service",
ExpectedServiceDisplayName = "Azure Pipelines Agent (bar.YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)"
},
};
foreach (var test in tests)
{
AgentSettings settings = new AgentSettings();
settings.ServerUrl = test.ServerUrl;
settings.AgentName = test.AgentName;
settings.PoolName = test.PoolName;

string serviceName;
string serviceDisplayName;
settings.PoolName = test.PoolName;
settings.EnvironmentName = test.EnvironmentName;

scm.CalculateServiceName(settings, test.ServiceNamePattern, test.ServiceDisplayPattern, out serviceName, out serviceDisplayName);
scm.CalculateServiceName(settings, test.ServiceNamePattern, test.ServiceDisplayPattern, out string serviceName, out string serviceDisplayName);

Assert.True(string.Equals(serviceName, test.ExpectedServiceName), $"{test.TestName} Service Name Expected: {test.ExpectedServiceName}, Got: {serviceName}");
Assert.True(serviceName.Length <= 80, $"{test.TestName} Service Name is <= 80");
Expand Down

0 comments on commit 43997c4

Please sign in to comment.