diff --git a/ahoy-server/src/main/java/za/co/lsd/ahoy/server/environments/EnvironmentService.java b/ahoy-server/src/main/java/za/co/lsd/ahoy/server/environments/EnvironmentService.java index 46b32068..e45b3d43 100644 --- a/ahoy-server/src/main/java/za/co/lsd/ahoy/server/environments/EnvironmentService.java +++ b/ahoy-server/src/main/java/za/co/lsd/ahoy/server/environments/EnvironmentService.java @@ -124,7 +124,7 @@ private Map 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()); @@ -144,13 +144,15 @@ private void deployReleasesTo(Environment environment, Map()); + + 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 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 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 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 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 environmentArgumentCaptor = ArgumentCaptor.forClass(Environment.class); + verify(environmentRepository, times(1)).save(environmentArgumentCaptor.capture()); + Environment savedEnvironment = environmentArgumentCaptor.getValue(); + assertEquals(2L, savedEnvironment.getCluster().getId()); + + verifyNoMoreInteractions(releaseService); + } }