-
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(kubernetes): Fail with a useful message if manifest not found (#4690
) * refactor(kubernetes): Clean up EnableDisableManifestDescription Mostly the goal here is to make loadBalancers Nonnull, which required overriding the lombok-generated accessor so that we can translate a null that's passed in to an empty list. While here, make the list immutable. Also add some tests, particularly on deserialization. I did *not* make the class immutable because one must draw some line at which to stop refactoring. * refactor(kubernetes): Make KubernetesManifestTraffic null-safe Also make it immutable, and add some tests. * fix(kubernetes): Fail with a useful message if manifest not found This commit adds nullity annotations to a number of functions related to the disable manifest stage so that we can better understand what can be null. It then throws a useful error message when a requested manifest is null, preventing what was a downstream error far from the actual issue (and that had less information about the error). * fix(kubernetes): Add missing parenthesis Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4142af8
commit 08917f9
Showing
10 changed files
with
231 additions
and
14 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
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
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
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
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
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
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
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
104 changes: 104 additions & 0 deletions
104
...river/kubernetes/description/manifest/KubernetesEnableDisableManifestDescriptionTest.java
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,104 @@ | ||
/* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* 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.kubernetes.description.manifest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
final class KubernetesEnableDisableManifestDescriptionTest { | ||
private static final JsonNodeFactory jsonFactory = JsonNodeFactory.instance; | ||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static final int DEFAULT_TARGET_PERCENTAGE = 100; | ||
|
||
@Test | ||
void deserializeEmpty() throws Exception { | ||
String serialized = jsonFactory.objectNode().toString(); | ||
KubernetesEnableDisableManifestDescription description = | ||
objectMapper.readValue(serialized, KubernetesEnableDisableManifestDescription.class); | ||
assertThat(description.getLoadBalancers()).isNotNull(); | ||
assertThat(description.getLoadBalancers()).isEmpty(); | ||
assertThat(description.getTargetPercentage()).isEqualTo(DEFAULT_TARGET_PERCENTAGE); | ||
} | ||
|
||
@Test | ||
void deserializeNullLoadBalancers() throws Exception { | ||
String serialized = | ||
jsonFactory | ||
.objectNode() | ||
.<ObjectNode>set("loadBalancers", jsonFactory.nullNode()) | ||
.toString(); | ||
KubernetesEnableDisableManifestDescription description = | ||
objectMapper.readValue(serialized, KubernetesEnableDisableManifestDescription.class); | ||
assertThat(description.getLoadBalancers()).isNotNull(); | ||
assertThat(description.getLoadBalancers()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void deserializEmptyLoadBalancers() throws Exception { | ||
String serialized = | ||
jsonFactory | ||
.objectNode() | ||
.<ObjectNode>set("loadBalancers", jsonFactory.arrayNode()) | ||
.toString(); | ||
KubernetesEnableDisableManifestDescription description = | ||
objectMapper.readValue(serialized, KubernetesEnableDisableManifestDescription.class); | ||
assertThat(description.getLoadBalancers()).isNotNull(); | ||
assertThat(description.getLoadBalancers()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void deserializNonEmptyLoadBalancers() throws Exception { | ||
String serialized = | ||
jsonFactory | ||
.objectNode() | ||
.<ObjectNode>set("loadBalancers", jsonFactory.arrayNode().add("abc").add("def")) | ||
.toString(); | ||
KubernetesEnableDisableManifestDescription description = | ||
objectMapper.readValue(serialized, KubernetesEnableDisableManifestDescription.class); | ||
assertThat(description.getLoadBalancers()).isNotNull(); | ||
assertThat(description.getLoadBalancers()).containsExactly("abc", "def"); | ||
} | ||
|
||
@Test | ||
void deserializeTargetPercentage() throws Exception { | ||
String serialized = jsonFactory.objectNode().put("targetPercentage", 50).toString(); | ||
KubernetesEnableDisableManifestDescription description = | ||
objectMapper.readValue(serialized, KubernetesEnableDisableManifestDescription.class); | ||
assertThat(description.getTargetPercentage()).isEqualTo(50); | ||
} | ||
|
||
@Test | ||
void deserializeManifestNameLocation() throws Exception { | ||
String serialized = | ||
jsonFactory | ||
.objectNode() | ||
.put("manifestName", "replicaSet my-rs") | ||
.put("location", "my-namespace") | ||
.toString(); | ||
KubernetesEnableDisableManifestDescription description = | ||
objectMapper.readValue(serialized, KubernetesEnableDisableManifestDescription.class); | ||
assertThat(description.getManifestName()).isEqualTo("replicaSet my-rs"); | ||
assertThat(description.getLocation()).isEqualTo("my-namespace"); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
.../spinnaker/clouddriver/kubernetes/description/manifest/KubernetesManifestTrafficTest.java
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,60 @@ | ||
/* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* 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.kubernetes.description.manifest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
final class KubernetesManifestTrafficTest { | ||
@Test | ||
final void createNullTraffic() { | ||
KubernetesManifestTraffic traffic = new KubernetesManifestTraffic(null); | ||
assertThat(traffic.getLoadBalancers()).isNotNull(); | ||
assertThat(traffic.getLoadBalancers()).isEmpty(); | ||
} | ||
|
||
@Test | ||
final void createEmptyTraffic() { | ||
KubernetesManifestTraffic traffic = new KubernetesManifestTraffic(ImmutableList.of()); | ||
assertThat(traffic.getLoadBalancers()).isEmpty(); | ||
} | ||
|
||
@Test | ||
final void createNonEmptyTraffic() { | ||
KubernetesManifestTraffic traffic = | ||
new KubernetesManifestTraffic(ImmutableList.of("abc", "def")); | ||
assertThat(traffic.getLoadBalancers()).containsExactly("abc", "def"); | ||
} | ||
|
||
@Test | ||
final void listIsImmutable() { | ||
List<String> loadBalancers = new ArrayList<>(); | ||
loadBalancers.add("abc"); | ||
KubernetesManifestTraffic traffic = new KubernetesManifestTraffic(loadBalancers); | ||
assertThat(traffic.getLoadBalancers()).containsExactly("abc"); | ||
|
||
loadBalancers.add("def"); | ||
assertThat(traffic.getLoadBalancers()).containsExactly("abc"); | ||
} | ||
} |