Skip to content

Commit

Permalink
As a Release Manager, I'd like to be able to move an environment from…
Browse files Browse the repository at this point in the history
… the current cluster to another. Adding tests
  • Loading branch information
rainerschamm committed Oct 19, 2021
1 parent 41a5a1f commit c187c4e
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private Map<EnvironmentReleaseId, ReleaseVersion> undeployReleasesFrom(Environme
log.info("Checking if there are deployed releases in {}", environment.getName());
environment.getEnvironmentReleases().forEach((environmentRelease) -> {
if (environmentRelease.hasCurrentReleaseVersion()) {
log.info("{} is currently deployed in {}, undeploying...", environmentRelease.getRelease().getName(), environment.getName());
log.info("Release {} is currently deployed in {}, undeploying...", environmentRelease.getRelease().getName(), environment.getName());

previouslyDeployedReleases.put(environmentRelease.getId(), environmentRelease.getCurrentReleaseVersion());

Expand All @@ -144,13 +144,15 @@ private void deployReleasesTo(Environment environment, Map<EnvironmentReleaseId,
if (previouslyDeployedReleases.containsKey(environmentRelease.getId())) {
ReleaseVersion releaseVersion = previouslyDeployedReleases.get(environmentRelease.getId());

String message = String.format("Redeployed %s:%s to %s in %s after moving environment to new cluster",
environmentRelease.getRelease().getName(),
releaseVersion.getVersion(),
environment.getName(),
environment.getCluster().getName());
log.debug("Release {}:{} was previously deployed in {}, redeploying...", environmentRelease.getRelease().getName(), releaseVersion.getVersion(), environment.getName());

try {
String message = String.format("Redeployed %s:%s to %s in %s after moving environment to new cluster",
environmentRelease.getRelease().getName(),
releaseVersion.getVersion(),
environment.getName(),
environment.getCluster().getName());

DeployOptions deployOptions = new DeployOptions(releaseVersion.getId(), message);
releaseService.deploy(environmentRelease.getId(), deployOptions);
log.info(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,32 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import za.co.lsd.ahoy.server.AhoyServerApplication;
import za.co.lsd.ahoy.server.DeployOptions;
import za.co.lsd.ahoy.server.ReleaseService;
import za.co.lsd.ahoy.server.applications.Application;
import za.co.lsd.ahoy.server.applications.ApplicationVersion;
import za.co.lsd.ahoy.server.cluster.Cluster;
import za.co.lsd.ahoy.server.cluster.ClusterRepository;
import za.co.lsd.ahoy.server.cluster.ClusterType;
import za.co.lsd.ahoy.server.environmentrelease.EnvironmentRelease;
import za.co.lsd.ahoy.server.environmentrelease.EnvironmentReleaseId;
import za.co.lsd.ahoy.server.releases.Release;
import za.co.lsd.ahoy.server.releases.ReleaseVersion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(SpringExtension.class)
Expand All @@ -33,6 +52,10 @@
class EnvironmentServiceTest {
@MockBean
private EnvironmentRepository environmentRepository;
@MockBean
private ClusterRepository clusterRepository;
@MockBean
private ReleaseService releaseService;
@Autowired
private EnvironmentService environmentService;

Expand All @@ -44,4 +67,127 @@ void updateOrderIndex() {
// then
verify(environmentRepository, times(1)).updateOrderIndex(eq(1L), eq(500.0));
}

@Test
void moveNoPreviousDeployments() {
// given
Cluster cluster1 = new Cluster(1L, "test-cluster-1", "https://kubernetes1.default.svc", ClusterType.KUBERNETES);
Cluster cluster2 = new Cluster(2L, "test-cluster-2", "https://kubernetes2.default.svc", ClusterType.KUBERNETES);

Environment environment = new Environment(1L, "dev", cluster1);
environment.setEnvironmentReleases(new ArrayList<>());

Release release = new Release(1L, "release1");
EnvironmentReleaseId environmentReleaseId = new EnvironmentReleaseId(1L, 1L);
EnvironmentRelease environmentRelease = new EnvironmentRelease(environmentReleaseId, environment, release);

environmentRelease.setCurrentReleaseVersion(null);
environment.getEnvironmentReleases().add(environmentRelease);

when(environmentRepository.findById(1L)).thenReturn(Optional.of(environment));
when(clusterRepository.findById(2L)).thenReturn(Optional.of(cluster2));
when(environmentRepository.save(same(environment))).thenReturn(environment);

// when
MoveOptions moveOptions = new MoveOptions(2L, true);
environmentService.move(1L, moveOptions);

// then
ArgumentCaptor<Environment> environmentArgumentCaptor = ArgumentCaptor.forClass(Environment.class);
verify(environmentRepository, times(1)).save(environmentArgumentCaptor.capture());
Environment savedEnvironment = environmentArgumentCaptor.getValue();
assertEquals(2L, savedEnvironment.getCluster().getId());

verifyNoMoreInteractions(releaseService);
}

@Test
void moveWithRedeploy() throws ExecutionException, InterruptedException {
// given
Cluster cluster1 = new Cluster(1L, "test-cluster-1", "https://kubernetes1.default.svc", ClusterType.KUBERNETES);
Cluster cluster2 = new Cluster(2L, "test-cluster-2", "https://kubernetes2.default.svc", ClusterType.KUBERNETES);

Environment environment = new Environment(1L, "dev", cluster1);
environment.setEnvironmentReleases(new ArrayList<>());

Release release = new Release(1L, "release1");
EnvironmentReleaseId environmentReleaseId = new EnvironmentReleaseId(1L, 1L);
EnvironmentRelease environmentRelease = new EnvironmentRelease(environmentReleaseId, environment, release);

Application application = new Application("app1");
ApplicationVersion applicationVersion = new ApplicationVersion("1.0.0", application);
ReleaseVersion releaseVersion = new ReleaseVersion(1L, "1.0.0", release, Collections.singletonList(applicationVersion));

environmentRelease.setCurrentReleaseVersion(releaseVersion);
environment.getEnvironmentReleases().add(environmentRelease);

// The future is needed for deploy/undeploy but the value is never used
Future<EnvironmentRelease> environmentReleaseFuture = mock(Future.class);
when(environmentReleaseFuture.get()).thenReturn(new EnvironmentRelease());

when(environmentRepository.findById(1L)).thenReturn(Optional.of(environment));
when(clusterRepository.findById(2L)).thenReturn(Optional.of(cluster2));
when(releaseService.undeploy(same(environmentReleaseId))).thenReturn(environmentReleaseFuture);
when(environmentRepository.save(same(environment))).thenReturn(environment);
when(releaseService.deploy(same(environmentReleaseId), any(DeployOptions.class))).thenReturn(environmentReleaseFuture);

// when
MoveOptions moveOptions = new MoveOptions(2L, true);
environmentService.move(1L, moveOptions);

// then
verify(releaseService, times(1)).undeploy(same(environmentReleaseId));

ArgumentCaptor<Environment> environmentArgumentCaptor = ArgumentCaptor.forClass(Environment.class);
verify(environmentRepository, times(1)).save(environmentArgumentCaptor.capture());
Environment savedEnvironment = environmentArgumentCaptor.getValue();
assertEquals(2L, savedEnvironment.getCluster().getId());

verify(releaseService, times(1)).deploy(same(environmentReleaseId), any(DeployOptions.class));
verifyNoMoreInteractions(releaseService);
}

@Test
void moveWithoutRedeploy() throws ExecutionException, InterruptedException {
// given
Cluster cluster1 = new Cluster(1L, "test-cluster-1", "https://kubernetes1.default.svc", ClusterType.KUBERNETES);
Cluster cluster2 = new Cluster(2L, "test-cluster-2", "https://kubernetes2.default.svc", ClusterType.KUBERNETES);

Environment environment = new Environment(1L, "dev", cluster1);
environment.setEnvironmentReleases(new ArrayList<>());

Release release = new Release(1L, "release1");
EnvironmentReleaseId environmentReleaseId = new EnvironmentReleaseId(1L, 1L);
EnvironmentRelease environmentRelease = new EnvironmentRelease(environmentReleaseId, environment, release);

Application application = new Application("app1");
ApplicationVersion applicationVersion = new ApplicationVersion("1.0.0", application);
ReleaseVersion releaseVersion = new ReleaseVersion(1L, "1.0.0", release, Collections.singletonList(applicationVersion));

environmentRelease.setCurrentReleaseVersion(releaseVersion);
environment.getEnvironmentReleases().add(environmentRelease);

// The future is needed for deploy/undeploy but the value is never used
Future<EnvironmentRelease> environmentReleaseFuture = mock(Future.class);
when(environmentReleaseFuture.get()).thenReturn(new EnvironmentRelease());

when(environmentRepository.findById(1L)).thenReturn(Optional.of(environment));
when(clusterRepository.findById(2L)).thenReturn(Optional.of(cluster2));
when(releaseService.undeploy(same(environmentReleaseId))).thenReturn(environmentReleaseFuture);
when(environmentRepository.save(same(environment))).thenReturn(environment);

// when
MoveOptions moveOptions = new MoveOptions(2L, false);
environmentService.move(1L, moveOptions);

// then
verify(releaseService, times(1)).undeploy(same(environmentReleaseId));

ArgumentCaptor<Environment> environmentArgumentCaptor = ArgumentCaptor.forClass(Environment.class);
verify(environmentRepository, times(1)).save(environmentArgumentCaptor.capture());
Environment savedEnvironment = environmentArgumentCaptor.getValue();
assertEquals(2L, savedEnvironment.getCluster().getId());

verifyNoMoreInteractions(releaseService);
}
}

0 comments on commit c187c4e

Please sign in to comment.