-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gce): gracefully handle null port in GCE health checks (#5643)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
86a14d1
commit b89f420
Showing
2 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...flix/spinnaker/clouddriver/google/provider/agent/GoogleHealthCheckCachingAgentTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2019 Google, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.google.provider.agent | ||
|
||
import com.netflix.spinnaker.clouddriver.google.model.GoogleHealthCheck | ||
|
||
import static org.assertj.core.api.Assertions.assertThat | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.google.api.services.compute.Compute | ||
import com.google.api.services.compute.model.* | ||
import com.google.common.collect.ImmutableList | ||
import com.google.common.collect.ImmutableMap | ||
import com.netflix.spectator.api.DefaultRegistry | ||
import com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.platform.runner.JUnitPlatform | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(JUnitPlatform.class) | ||
class GoogleHealthCheckCachingAgentTest { | ||
|
||
private static final String ACCOUNT_NAME = "partypups" | ||
private static final String PROJECT = "myproject" | ||
private static final String REGION = "myregion" | ||
private static final String REGION_URL = "http://compute/regions/" + REGION | ||
private static final String ZONE = REGION + "-myzone" | ||
private static final String ZONE_URL = "http://compute/zones/" + ZONE | ||
|
||
private ObjectMapper objectMapper | ||
private GoogleHealthCheckCachingAgent healthCheckAgent | ||
|
||
@BeforeEach | ||
void createTestObjects() { | ||
objectMapper = new ObjectMapper() | ||
|
||
Compute compute = new StubComputeFactory().create() | ||
GoogleNamedAccountCredentials credentials = | ||
new GoogleNamedAccountCredentials.Builder() | ||
.project(PROJECT) | ||
.name(ACCOUNT_NAME) | ||
.compute(compute) | ||
.regionToZonesMap(ImmutableMap.of(REGION, ImmutableList.of(ZONE))) | ||
.build() | ||
healthCheckAgent = new GoogleHealthCheckCachingAgent( | ||
"app-name", | ||
credentials, | ||
objectMapper, | ||
new DefaultRegistry(), | ||
) | ||
} | ||
|
||
private static HealthCheck buildBaseHealthCheck(String name, String region) { | ||
HealthCheck hc = new HealthCheck() | ||
hc.setName(name) | ||
hc.setSelfLink("http://selflink") | ||
hc.setRegion(region) | ||
hc.setCheckIntervalSec(60) | ||
hc.setTimeoutSec(10) | ||
hc.setHealthyThreshold(1) | ||
hc.setUnhealthyThreshold(3) | ||
return hc | ||
} | ||
|
||
@Test | ||
void createsValidHttpHealthCheck() { | ||
HTTPHealthCheck httpHealthCheck = new HTTPHealthCheck() | ||
httpHealthCheck.setPort(1234) | ||
httpHealthCheck.setRequestPath("/healthz") | ||
|
||
HealthCheck hc = buildBaseHealthCheck("valid", REGION) | ||
hc.setHttpHealthCheck(httpHealthCheck) | ||
hc.setType("HTTP") | ||
|
||
GoogleHealthCheck ghc = healthCheckAgent.toGoogleHealthCheck(hc, REGION) | ||
assertThat(ghc.getPort()).isEqualTo(1234) | ||
assertThat(ghc.getRegion()).isEqualTo(REGION) | ||
assertThat(ghc.getRequestPath()).isEqualTo("/healthz") | ||
assertThat(ghc.getHealthCheckType()).isEqualTo(GoogleHealthCheck.HealthCheckType.HTTP) | ||
} | ||
|
||
@Test | ||
void handlesHttpHealthCheckWithoutPort() { | ||
HTTPHealthCheck httpHealthCheck = new HTTPHealthCheck() | ||
httpHealthCheck.setRequestPath("/healthz") | ||
|
||
HealthCheck hc = buildBaseHealthCheck("no-port", REGION) | ||
hc.setHttpHealthCheck(httpHealthCheck) | ||
hc.setType("HTTP") | ||
|
||
GoogleHealthCheck ghc = healthCheckAgent.toGoogleHealthCheck(hc, REGION) | ||
assertThat(ghc).isNull() | ||
} | ||
} |