Skip to content

Commit

Permalink
Review -> Scheduler -> validate health port
Browse files Browse the repository at this point in the history
start health server on defined ports (Port 0 is not allowed)
  • Loading branch information
asalan316 committed May 24, 2023
1 parent dc2876b commit 7aff4d1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/autoscaler/integration/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
opConfig "code.cloudfoundry.org/app-autoscaler/src/autoscaler/operator/config"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes"
seConfig "code.cloudfoundry.org/app-autoscaler/src/autoscaler/scalingengine/config"
"github.com/go-sql-driver/mysql"

"fmt"
"net/url"
Expand All @@ -21,7 +22,6 @@ import (
"strings"
"time"

"github.com/go-sql-driver/mysql"
. "github.com/onsi/gomega"
"github.com/tedsuo/ifrit/ginkgomon_v2"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -355,7 +355,7 @@ server.ssl.ciphers=TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_CBC_SHA2
server.port=%d
# Health Server
scheduler.healthserver.port=0
scheduler.healthserver.port=7000
scheduler.healthserver.username=test-user
scheduler.healthserver.password=test-password
client.httpClientTimeout=%d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ public TomcatServletWebServerFactory servletContainer() {
}

private Connector[] additionalConnector() {
if (healthServerConfig.getPort() == 0) {
return new Connector[0];
}
List<Connector> result = new ArrayList<>();
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(healthServerConfig.getPort());

result.add(connector);
return result.toArray(new Connector[] {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -17,11 +18,14 @@
public class HealthServerConfiguration {
private String username;
private String password;
private int port;
private Integer port;
private List<String> unprotectedEndpoints;

@PostConstruct
public void init() {

validatePort();

boolean basicAuthEnabled =
(unprotectedEndpoints != null || ObjectUtils.isEmpty(unprotectedEndpoints));
if (basicAuthEnabled
Expand All @@ -32,4 +36,12 @@ public void init() {
throw new IllegalArgumentException("Heath Server Basic Auth Username or password is not set");
}
}

private void validatePort() {
Optional<Integer> healthPortOptional = Optional.ofNullable(this.port);
if (!healthPortOptional.isPresent() || healthPortOptional.get() == 0) {
throw new IllegalArgumentException(
"Health Configuration: health server port not defined or port=0");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.cloudfoundry.autoscaler.scheduler.conf;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

Expand Down Expand Up @@ -51,4 +52,29 @@ void givenEmptyUnprotectedEndpointListWithUsernameOrPassword() {
assertDoesNotThrow(
() -> new HealthServerConfiguration("some-user", "some-test", 8081, List.of()).init());
}

@ParameterizedTest
@ValueSource(strings = {"null", "0", ""})
public void givenInvalidPortThrowsException(String healthPort) {

assertThrows(
IllegalArgumentException.class,
() ->
new HealthServerConfiguration("", "", Integer.parseInt(healthPort), List.of()).init());
}

@Test
void givenValidReturnsPort() {
HealthServerConfiguration healthServerConfiguration =
new HealthServerConfiguration("s", "s", 888, List.of());
healthServerConfiguration.init();
assertEquals(healthServerConfiguration.getPort(), 888);
}

@Test
void givenEmptyPortThrowsException() {
assertThrows(
IllegalArgumentException.class,
() -> new HealthServerConfiguration("", "", null, List.of()).init());
}
}

0 comments on commit 7aff4d1

Please sign in to comment.