Skip to content

Commit

Permalink
APP-2970: fixed infinite stack overflow (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
symphony-elias authored Aug 31, 2020
1 parent 5b30c83 commit e5c8a04
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import clients.symphony.api.constants.AgentConstants;
import clients.symphony.api.constants.CommonConstants;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import exceptions.SymClientException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -83,7 +84,10 @@ public void rotateAgent() {
newAgent = agentServers.get(currentAgentIndex);
break;
case external:
actualAgentHost = getActualAgentHost();
if (agentServers.size() != 1) {
throw new SymClientException("Load balancing configuration with method \"external\" must contain only one agent server");
}
actualAgentHost = getActualAgentHost(agentServers.get(0));
newAgent = actualAgentHost;
break;
default:
Expand All @@ -95,11 +99,9 @@ public void rotateAgent() {
* Retrieves actual Agent FQDN (Fully Qualified Domain Name) by calling /agent/v1/info endpoint.
* @return actual Agent host
*/
protected String getActualAgentHost() {

// using "super" to ensure that we retrieve fqdn through the LB
final String uri = super.getAgentUrl();

protected String getActualAgentHost(String externalLoadBalancerUrl) {
// we assume host + port + context is put in the agentServers in the lb-config
final String uri = CommonConstants.HTTPS_PREFIX + externalLoadBalancerUrl;
final Response response = getHttpClientBuilderWithTruststore(this).build().target(uri)
.path(AgentConstants.INFO)
.request(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package configuration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import exceptions.SymClientException;
import it.commons.BaseTest;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Test class for the {@link SymLoadBalancedConfig}.
*
Expand All @@ -26,4 +35,39 @@ public void testCloneAttributes() {
assertEquals("localhost", loadBalancedConfig.getPodHost());
assertEquals(8080, loadBalancedConfig.getPodPort());
}
}

@Test
public void testGetAgentWithExternalLoadBalancing() {
String loadBalancedUrl = "agent-1";

final SymLoadBalancedConfig config = spy(getSymLoadBalancedConfig(Collections.singletonList("agent")));
doReturn(loadBalancedUrl).when(config).getActualAgentHost(anyString());

assertEquals(loadBalancedUrl, config.getAgentHost());
}

@Test
public void testGetAgentWithExternalLoadBalancingAndNoAgentServer() {
final SymLoadBalancedConfig config = getSymLoadBalancedConfig(Collections.emptyList());

assertThrows(SymClientException.class, () -> config.getAgentHost());
}

@Test
public void testGetAgentWithExternalLoadBalancingAndSeveralAgentServers() {
final SymLoadBalancedConfig config = getSymLoadBalancedConfig(Arrays.asList("agent-1", "agent-2"));

assertThrows(SymClientException.class, () -> config.getAgentHost());
}

private SymLoadBalancedConfig getSymLoadBalancedConfig(List<String> agentServers) {
final SymLoadBalancedConfig config = new SymLoadBalancedConfig();

final LoadBalancing loadBalancing = new LoadBalancing();
loadBalancing.setMethod(LoadBalancingMethod.external);

config.setAgentServers(agentServers);
config.setLoadBalancing(loadBalancing);
return config;
}
}

0 comments on commit e5c8a04

Please sign in to comment.