Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Add gcloudMode config option for beta deploy (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
loosebazooka authored Jul 17, 2019
1 parent 0658b1a commit 4b16523
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-plugins-core</artifactId>
<version>0.7.5</version>
<version>0.7.6</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.cloud.tools.appengine.operations.cloudsdk.process.NonZeroExceptionExitListener;
import com.google.cloud.tools.appengine.operations.cloudsdk.process.ProcessHandler;
import com.google.cloud.tools.appengine.operations.cloudsdk.process.ProcessOutputLineListener;
import com.google.cloud.tools.managedcloudsdk.components.SdkComponent;
import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.io.IOException;
Expand All @@ -42,6 +43,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.logging.Log;

/** Factory for App Engine dependencies. */
Expand Down Expand Up @@ -130,12 +133,16 @@ static CloudSdk buildCloudSdk(
return cloudSdk;
} else {
// we need to use a managed cloud sdk
List<SdkComponent> requiredComponents = new ArrayList<>();
if (requiresAppEngineComponents) {
requiredComponents.add(SdkComponent.APP_ENGINE_JAVA);
}
return new CloudSdk.Builder()
.sdkPath(
cloudSdkDownloader.downloadIfNecessary(
mojo.getCloudSdkVersion(),
mojo.getLog(),
requiresAppEngineComponents,
requiredComponents,
mojo.getMavenSession().isOffline()))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.base.Strings;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Function;
import org.apache.maven.plugin.logging.Log;

Expand All @@ -48,7 +49,7 @@ public CloudSdkDownloader(Function<String, ManagedCloudSdk> managedCloudSdkFacto
* @return The cloud SDK installation directory
*/
public Path downloadIfNecessary(
String version, Log log, boolean requiresAppEngineComponents, boolean offline) {
String version, Log log, List<SdkComponent> components, boolean offline) {
ManagedCloudSdk managedCloudSdk = managedCloudSdkFactory.apply(version);
if (offline) { // in offline mode, don't download anything
return managedCloudSdk.getSdkHome();
Expand All @@ -61,11 +62,15 @@ public Path downloadIfNecessary(
managedCloudSdk.newInstaller().install(progressListener, consoleListener);
}

if (requiresAppEngineComponents
&& !managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)) {
managedCloudSdk
.newComponentInstaller()
.installComponent(SdkComponent.APP_ENGINE_JAVA, progressListener, consoleListener);
// install requested components
if (components != null) {
for (SdkComponent component : components) {
if (!managedCloudSdk.hasComponent(component)) {
managedCloudSdk
.newComponentInstaller()
.installComponent(component, progressListener, consoleListener);
}
}
}

if (!managedCloudSdk.isUpToDate()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public abstract class AbstractDeployMojo extends AbstractStageMojo {
@Parameter(alias = "deploy.bucket", property = "app.deploy.bucket")
private String bucket;

/** The preview mode to use gcloud injected before "app": gcloud "mode" app deploy. */
@Parameter(alias = "deploy.gcloudMode", property = "app.deploy.gcloudMode")
private String gcloudMode;

/**
* Deploy with a specific Docker image. Docker url must be from one of the valid gcr hostnames.
*
Expand Down Expand Up @@ -107,4 +111,8 @@ public Boolean getStopPreviousVersion() {
public String getVersion() {
return version;
}

public String getGcloudMode() {
return gcloudMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ static class ConfigBuilder {
DeployConfiguration buildDeployConfiguration(List<Path> deployables) {
return DeployConfiguration.builder(deployables)
.bucket(deployMojo.getBucket())
.gcloudMode(deployMojo.getGcloudMode())
.imageUrl(deployMojo.getImageUrl())
.projectId(configProcessor.processProjectId(deployMojo.getProjectId()))
.promote(deployMojo.getPromote())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.cloud.tools.appengine.operations.cloudsdk.CloudSdkNotFoundException;
import com.google.cloud.tools.appengine.operations.cloudsdk.CloudSdkOutOfDateException;
import com.google.cloud.tools.appengine.operations.cloudsdk.CloudSdkVersionFileException;
import com.google.cloud.tools.managedcloudsdk.components.SdkComponent;
import com.google.common.collect.ImmutableList;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -70,11 +72,17 @@ public void wireUp() {
doReturn(INSTALL_SDK_PATH)
.when(cloudSdkDownloader)
.downloadIfNecessary(
Mockito.isNull(), Mockito.eq(logMock), Mockito.anyBoolean(), Mockito.anyBoolean());
Mockito.isNull(),
Mockito.eq(logMock),
Mockito.<SdkComponent>anyList(),
Mockito.anyBoolean());
doReturn(INSTALL_SDK_PATH)
.when(cloudSdkDownloader)
.downloadIfNecessary(
Mockito.anyString(), Mockito.eq(logMock), Mockito.anyBoolean(), Mockito.anyBoolean());
Mockito.anyString(),
Mockito.eq(logMock),
Mockito.<SdkComponent>anyList(),
Mockito.anyBoolean());
}

@Test
Expand All @@ -96,7 +104,9 @@ public void testBuildCloudSdk_downloadWithVersion() {

// verify
Assert.assertEquals(INSTALL_SDK_PATH, sdk.getPath());
verify(cloudSdkDownloader).downloadIfNecessary(CLOUD_SDK_VERSION, logMock, true, false);
verify(cloudSdkDownloader)
.downloadIfNecessary(
CLOUD_SDK_VERSION, logMock, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), false);
verifyNoMoreInteractions(cloudSdkChecker);
}

Expand All @@ -111,7 +121,8 @@ public void testBuildCloudSdk_downloadWithoutVersion() {

// verify
Assert.assertEquals(INSTALL_SDK_PATH, sdk.getPath());
verify(cloudSdkDownloader).downloadIfNecessary(null, logMock, true, false);
verify(cloudSdkDownloader)
.downloadIfNecessary(null, logMock, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), false);
verifyNoMoreInteractions(cloudSdkChecker);
}

Expand All @@ -126,7 +137,8 @@ public void testBuildCloudSdk_offlinePassthrough() {
CloudSdkAppEngineFactory.buildCloudSdk(mojoMock, cloudSdkChecker, cloudSdkDownloader, true);

Assert.assertEquals(INSTALL_SDK_PATH, sdk.getPath());
verify(cloudSdkDownloader).downloadIfNecessary(null, logMock, true, true);
verify(cloudSdkDownloader)
.downloadIfNecessary(null, logMock, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), true);
verify(mavenSession).isOffline();
verifyNoMoreInteractions(cloudSdkChecker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.cloud.tools.maven.cloudsdk;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
Expand All @@ -27,10 +30,14 @@
import com.google.cloud.tools.managedcloudsdk.ManagedSdkVersionMismatchException;
import com.google.cloud.tools.managedcloudsdk.UnsupportedOsException;
import com.google.cloud.tools.managedcloudsdk.Version;
import com.google.cloud.tools.managedcloudsdk.command.CommandExecutionException;
import com.google.cloud.tools.managedcloudsdk.command.CommandExitException;
import com.google.cloud.tools.managedcloudsdk.components.SdkComponent;
import com.google.cloud.tools.managedcloudsdk.components.SdkComponentInstaller;
import com.google.cloud.tools.managedcloudsdk.components.SdkUpdater;
import com.google.cloud.tools.managedcloudsdk.install.SdkInstaller;
import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.function.Function;
import org.apache.maven.plugin.logging.Log;
import org.junit.Assert;
Expand Down Expand Up @@ -67,25 +74,56 @@ public void setup() {
public void testDownloadCloudSdk_install()
throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException {
when(managedCloudSdk.isInstalled()).thenReturn(false);
downloader.downloadIfNecessary(version, log, true, false);
downloader.downloadIfNecessary(
version, log, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), false);
verify(managedCloudSdk).newInstaller();
}

@Test
public void testDownloadCloudSdk_installAppEngine()
public void testDownloadCloudSdk_installSingeComponent()
throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException {
when(managedCloudSdk.isInstalled()).thenReturn(true);
when(managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false);
downloader.downloadIfNecessary(version, log, true, false);
downloader.downloadIfNecessary(
version, log, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), false);
verify(managedCloudSdk, never()).newInstaller();
verify(managedCloudSdk).newComponentInstaller();
}

@Test
public void testDownloadCloudSdk_ignoreAppEngineComponent()
public void testDownloadCloudSdk_installMultipleComponents()
throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
InterruptedException, CommandExitException, CommandExecutionException {
when(managedCloudSdk.isInstalled()).thenReturn(true);
when(managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false);
when(managedCloudSdk.hasComponent(SdkComponent.BETA)).thenReturn(false);
downloader.downloadIfNecessary(
version, log, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA, SdkComponent.BETA), false);
verify(managedCloudSdk, never()).newInstaller();
verify(managedCloudSdk, times(2)).newComponentInstaller();
verify(componentInstaller).installComponent(eq(SdkComponent.APP_ENGINE_JAVA), any(), any());
verify(componentInstaller).installComponent(eq(SdkComponent.BETA), any(), any());
}

@Test
public void testDownloadCloudSdk_installSomeOfMultipleComponents()
throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
InterruptedException, CommandExitException, CommandExecutionException {
when(managedCloudSdk.isInstalled()).thenReturn(true);
when(managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false);
when(managedCloudSdk.hasComponent(SdkComponent.BETA)).thenReturn(true);
downloader.downloadIfNecessary(
version, log, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA, SdkComponent.BETA), false);
verify(managedCloudSdk, never()).newInstaller();
verify(managedCloudSdk).newComponentInstaller();
verify(componentInstaller).installComponent(eq(SdkComponent.APP_ENGINE_JAVA), any(), any());
}

@Test
public void testDownloadCloudSdk_ignoreComponents()
throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException {
when(managedCloudSdk.isInstalled()).thenReturn(true);
downloader.downloadIfNecessary(version, log, false, false);
downloader.downloadIfNecessary(version, log, Collections.emptyList(), false);
verify(managedCloudSdk, never()).newInstaller();
verify(managedCloudSdk, never()).newComponentInstaller();
}
Expand All @@ -96,15 +134,17 @@ public void testDownloadCloudSdk_update()
when(managedCloudSdk.isInstalled()).thenReturn(true);
when(managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(true);
when(managedCloudSdk.isUpToDate()).thenReturn(false);
downloader.downloadIfNecessary(version, log, true, false);
downloader.downloadIfNecessary(
version, log, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), false);
verify(managedCloudSdk, never()).newInstaller();
verify(managedCloudSdk, never()).newComponentInstaller();
verify(managedCloudSdk).newUpdater();
}

@Test
public void testDownloadCloudSdk_offlineMode() {
downloader.downloadIfNecessary(version, log, true, true);
downloader.downloadIfNecessary(
version, log, ImmutableList.of(SdkComponent.APP_ENGINE_JAVA), true);
verify(managedCloudSdk).getSdkHome();
verifyNoMoreInteractions(managedCloudSdk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

import org.apache.maven.plugin.logging.Log;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AbstractDeployMojoTest {

@InjectMocks
private final AbstractDeployMojo testMojo =
new AbstractDeployMojo() {
@Override
Expand All @@ -37,11 +38,6 @@ public void execute() {

@Mock private Log mockLog;

@Before
public void setUp() {
testMojo.setLog(mockLog);
}

@Test
public void testGetProjectId_onlyProject() {
testMojo.project = "someProject";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public void testDeployQueue() throws MojoExecutionException, AppEngineException
public void testBuildDeployConfiguration() {
AbstractDeployMojo deployMojo = Mockito.mock(AbstractDeployMojo.class);
Mockito.when(deployMojo.getBucket()).thenReturn("testBucket");
Mockito.when(deployMojo.getGcloudMode()).thenReturn("beta");
Mockito.when(deployMojo.getImageUrl()).thenReturn("testImageUrl");
Mockito.when(deployMojo.getProjectId()).thenReturn("testProjectId");
Mockito.when(deployMojo.getPromote()).thenReturn(false);
Expand All @@ -211,6 +212,7 @@ public void testBuildDeployConfiguration() {
new ConfigBuilder(deployMojo, configProcessor).buildDeployConfiguration(deployables);

Assert.assertEquals(deployables, deployConfig.getDeployables());
Assert.assertEquals("beta", deployConfig.getGcloudMode());
Assert.assertEquals("testBucket", deployConfig.getBucket());
Assert.assertEquals("testImageUrl", deployConfig.getImageUrl());
Assert.assertEquals("processedTestProjectId", deployConfig.getProjectId());
Expand Down

0 comments on commit 4b16523

Please sign in to comment.