Skip to content

Commit

Permalink
Removed kiran's UTs from KubernetesCredentialsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-agrawal committed Jun 28, 2024
1 parent 69fddbc commit f203eb5
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@
import com.netflix.spinnaker.clouddriver.kubernetes.config.KubernetesCachingPolicy;
import com.netflix.spinnaker.clouddriver.kubernetes.config.LinkedDockerRegistryConfiguration;
import com.netflix.spinnaker.clouddriver.kubernetes.config.RawResourcesEndpointConfig;
import com.netflix.spinnaker.clouddriver.kubernetes.description.AccountResourcePropertyRegistry;
import com.netflix.spinnaker.clouddriver.kubernetes.description.GlobalResourcePropertyRegistry;
import com.netflix.spinnaker.clouddriver.kubernetes.description.JsonPatch;
import com.netflix.spinnaker.clouddriver.kubernetes.description.KubernetesCoordinates;
import com.netflix.spinnaker.clouddriver.kubernetes.description.KubernetesPatchOptions;
import com.netflix.spinnaker.clouddriver.kubernetes.description.KubernetesPodMetric;
import com.netflix.spinnaker.clouddriver.kubernetes.description.KubernetesResourceProperties;
import com.netflix.spinnaker.clouddriver.kubernetes.description.KubernetesSpinnakerKindMap;
import com.netflix.spinnaker.clouddriver.kubernetes.description.ResourcePropertyRegistry;
import com.netflix.spinnaker.clouddriver.kubernetes.description.*;
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesKind;
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesKindProperties;
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesManifest;
Expand Down Expand Up @@ -672,17 +664,6 @@ private <T> T runAndRecordMetrics(
private <T> T runAndRecordMetrics(
String action, List<KubernetesKind> kinds, String namespace, Supplier<T> op) {
return op.get();
/**
* Commenting below to avoid flood of metrics on every kubernetes operation Map<String, String>
* tags = new HashMap<>(); tags.put("action", action); tags.put( "kinds",
* kinds.stream().map(KubernetesKind::toString).sorted().collect(Collectors.joining(",")));
* tags.put("account", accountName); tags.put("namespace", Strings.isNullOrEmpty(namespace) ?
* "none" : namespace); tags.put("success", "true"); long startTime = clock.monotonicTime(); try
* { return op.get(); } catch (RuntimeException e) { tags.put("success", "false");
* tags.put("reason", e.getClass().getSimpleName()); throw e; } finally { registry
* .timer(registry.createId("kubernetes.api", tags)) .record(clock.monotonicTime() - startTime,
* TimeUnit.NANOSECONDS); }
*/
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.netflix.spinnaker.clouddriver.kubernetes.security;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -28,11 +27,8 @@
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonSyntaxException;
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.ManualClock;
import com.netflix.spectator.api.NoopRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Tag;
import com.netflix.spectator.api.Timer;
import com.netflix.spinnaker.clouddriver.data.task.DefaultTask;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.kubernetes.config.KubernetesAccountProperties.ManagedAccount;
Expand Down Expand Up @@ -88,57 +84,6 @@ private KubernetesManifest getManifest() {
return manifest;
}

@Test
void metricTagsForSuccessfulDeploy() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Registry registry = new DefaultRegistry();
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);
credentials.deploy(getManifest(), task, OP_NAME);

ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");
assertThat(timer.id().tags())
.containsExactlyInAnyOrder(
Tag.of("account", ACCOUNT_NAME),
Tag.of("action", "deploy"),
Tag.of("kinds", KubernetesKind.DEPLOYMENT.toString()),
Tag.of("namespace", NAMESPACE),
Tag.of("success", "true"));
}

@Test
void metricTagsForSuccessfulListNoNamespace() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Registry registry = new DefaultRegistry();
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);
credentials.list(ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET), null);
ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");

assertThat(timer.id().tags()).contains(Tag.of("namespace", "none"));
}

@Test
void metricTagsForSuccessfulListEmptyNamespace() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Registry registry = new DefaultRegistry();
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);
credentials.list(ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET), "");
ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");

assertThat(timer.id().tags()).contains(Tag.of("namespace", "none"));
}

@Test
void returnValueForSuccessfulList() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Expand All @@ -154,65 +99,6 @@ void returnValueForSuccessfulList() {
assertThat(result).containsExactly(manifest);
}

@Test
void timeRecordedForSuccessfulList() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);

ManualClock clock = new ManualClock();
Registry registry = new DefaultRegistry(clock);
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);

clock.setMonotonicTime(1000);
when(jobExecutor.list(eq(credentials), any(), any(), any()))
.then(
call -> {
clock.setMonotonicTime(1500);
return ImmutableList.of();
});
credentials.list(
ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET), NAMESPACE);

ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");
assertThat(timer.totalTime()).isEqualTo(500);
}

@Test
void metricTagsForListThrowingKubectlException() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Registry registry = new DefaultRegistry();
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);

when(jobExecutor.list(eq(credentials), any(), any(), any()))
.thenThrow(
new KubectlException(
"Failed to parse kubectl output: failure", new JsonSyntaxException("failure")));

assertThatThrownBy(
() ->
credentials.list(
ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET),
NAMESPACE))
.isInstanceOf(KubectlException.class);
ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");

assertThat(timer.id().tags())
.containsExactlyInAnyOrder(
Tag.of("account", ACCOUNT_NAME),
Tag.of("action", "list"),
Tag.of("kinds", "deployment,replicaSet"),
Tag.of("namespace", NAMESPACE),
Tag.of("success", "false"),
Tag.of("reason", "KubectlException"));
}

@Test
void propagatedExceptionForListThrowingKubectlException() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Expand All @@ -233,98 +119,6 @@ void propagatedExceptionForListThrowingKubectlException() {
.isEqualTo(exception);
}

@Test
void timeRecordedForListThrowingKubectlException() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);

ManualClock clock = new ManualClock();
Registry registry = new DefaultRegistry(clock);
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);

clock.setMonotonicTime(1000);
when(jobExecutor.list(eq(credentials), any(), any(), any()))
.then(
call -> {
clock.setMonotonicTime(1500);
throw new KubectlException(
"Failed to parse kubectl output: failure", new JsonSyntaxException("failure"));
});
assertThatThrownBy(
() ->
credentials.list(
ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET),
NAMESPACE))
.isInstanceOf(KubectlException.class);

ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");
assertThat(timer.totalTime()).isEqualTo(500);
}

@Test
void metricTagsForListThrowingOtherException() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Registry registry = new DefaultRegistry();
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);

when(jobExecutor.list(eq(credentials), any(), any(), any()))
.thenThrow(new CustomException("Kubernetes error"));

assertThatThrownBy(
() ->
credentials.list(
ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET),
NAMESPACE))
.isInstanceOf(CustomException.class);
ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");

assertThat(timer.id().tags())
.containsExactlyInAnyOrder(
Tag.of("account", ACCOUNT_NAME),
Tag.of("action", "list"),
Tag.of("kinds", "deployment,replicaSet"),
Tag.of("namespace", NAMESPACE),
Tag.of("success", "false"),
Tag.of("reason", "CustomException"));
}

@Test
void timeRecordedForListThrowingOtherException() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);

ManualClock clock = new ManualClock();
Registry registry = new DefaultRegistry(clock);
KubernetesCredentials credentials = getCredentials(registry, jobExecutor);

clock.setMonotonicTime(1000);
when(jobExecutor.list(eq(credentials), any(), any(), any()))
.then(
call -> {
clock.setMonotonicTime(1500);
throw new CustomException("Kubernetes errror");
});
assertThatThrownBy(
() ->
credentials.list(
ImmutableList.of(KubernetesKind.DEPLOYMENT, KubernetesKind.REPLICA_SET),
NAMESPACE))
.isInstanceOf(CustomException.class);

ImmutableList<Timer> timers = registry.timers().collect(toImmutableList());
assertThat(timers).hasSize(1);

Timer timer = timers.get(0);
assertThat(timer.id().name()).isEqualTo("kubernetes.api");
assertThat(timer.totalTime()).isEqualTo(500);
}

@Test
void propagatedExceptionForListThrowingOtherException() {
KubectlJobExecutor jobExecutor = mock(KubectlJobExecutor.class);
Expand Down

0 comments on commit f203eb5

Please sign in to comment.