Skip to content

Commit

Permalink
fix(aws): Fixes name resolver when stack is empty. (#5337)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazquezglez authored Apr 20, 2021
1 parent b599931 commit 8c377ef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public EcsServerGroupName resolveNextName(Moniker currentName) {
for (Service service : result.getServices()) {
Moniker moniker = naming.deriveMoniker(new EcsResourceService(service));

if (StringUtils.equals(currentName.getApp(), moniker.getApp())
&& StringUtils.equals(currentName.getDetail(), moniker.getDetail())
&& StringUtils.equals(currentName.getStack(), moniker.getStack())) {
if (isSameName(currentName.getApp(), moniker.getApp())
&& isSameName(currentName.getDetail(), moniker.getDetail())
&& isSameName(currentName.getStack(), moniker.getStack())) {
takenSequences.add(moniker.getSequence());
}
}
Expand Down Expand Up @@ -107,6 +107,11 @@ public EcsServerGroupName resolveNextName(Moniker currentName) {
return new EcsServerGroupName(newMoniker);
}

private boolean isSameName(String name, String name2) {
return (StringUtils.isBlank(name) && StringUtils.isBlank(name2))
|| StringUtils.equals(name, name2);
}

private boolean isNotTaken(Moniker newMoniker) {
String newServiceName = new EcsServerGroupName(newMoniker).getServiceName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,46 @@ class EcsServerGroupNameResolverSpec extends Specification {
nextServerGroupName.getFamilyName() == "application--details"
}

void "should skip name if stack name is null and the existing one is empty"() {
given:
def resolver = new EcsServerGroupNameResolver(ecsClusterName, ecsClient, region, new EcsDefaultNamer())
ecsClient.listServices(_) >> new ListServicesResult().withServiceArns(
"application--details-v001",
"application--details-v002"
)

and: 'two existing and active services'
ecsClient.describeServices({ DescribeServicesRequest request ->
request.cluster == ecsClusterName
request.services == ["application--details-v001", "application--details-v002"]
}) >> new DescribeServicesResult().withServices(
new Service(serviceName: "application--details-v001", createdAt: new Date(1), status: "ACTIVE"),
new Service(serviceName: "application--details-v002", createdAt: new Date(2), status: "ACTIVE")
)

and: 'one missing service'
ecsClient.describeServices({ DescribeServicesRequest request ->
request.cluster == ecsClusterName
request.services == ["application--details-v003"]
}) >> new DescribeServicesResult().withFailures(
new Failure(arn: "application--details-v003", reason: "MISSING")
)


when: 'stack has an empty value on resolving name'
def nextServerGroupName = resolver.resolveNextName('application', '', 'details')

then: 'it will have the same result as if it was null'
nextServerGroupName.getServiceName() == "application--details-v003"
nextServerGroupName.getFamilyName() == "application--details"

// If this is called it means `resolveNextName` failed to add the taken sequences (1 and 2)
0 * ecsClient.describeServices({ DescribeServicesRequest request ->
request.cluster == ecsClusterName
request.services == ["application--details-v000"]
})
}

void "should generate name with null details and stack"() {
given:
def resolver = new EcsServerGroupNameResolver(ecsClusterName, ecsClient, region, new EcsDefaultNamer())
Expand Down

0 comments on commit 8c377ef

Please # to comment.