/** Checks if the provided path contains a valid Cloud SDK installation. */ public boolean isValidCloudSdk(String path) { return validateCloudSdk(path).isEmpty(); }
@Override public void notifyIfVersionOutOfDate() { CloudSdkValidator sdkValidator = CloudSdkValidator.getInstance(); if (sdkValidator .validateCloudSdk() .contains(CloudSdkValidationResult.CLOUD_SDK_NOT_MINIMUM_VERSION)) { String message = "<p>" + CloudSdkValidationResult.CLOUD_SDK_NOT_MINIMUM_VERSION.getMessage() + "</p>"; showNotification( CloudSdkMessageBundle.message("appengine.cloudsdk.version.support.title"), message); } }
@Test public void testValidateCloudSdk_goodString() { Set<CloudSdkValidationResult> results = sdkValidator.validateCloudSdk("/good/path"); assertEquals(0, results.size()); assertTrue(sdkValidator.isValidCloudSdk("/good/path")); }
/** * Checks if a given path is malformed or if it contains a valid Cloud SDK. * * <p>Windows' implementation of Paths doesn't handle well converting strings with certain special * characters to paths. This method should be called before {@code Paths.get(path)}. */ public Set<CloudSdkValidationResult> validateCloudSdk(String path) { if (path == null) { return ImmutableSet.of(CloudSdkValidationResult.CLOUD_SDK_NOT_FOUND); } if (isMalformedCloudSdkPath(path)) { return ImmutableSet.of(CloudSdkValidationResult.MALFORMED_PATH); } return validateCloudSdk(Paths.get(path)); }
@VisibleForTesting void showCloudSdkNotification(String notificationMessage, NotificationType notificationType) { if (!CloudSdkValidator.getInstance().isValidCloudSdk()) { Notification invalidSdkWarning = new Notification( new PropertiesFileFlagReader().getFlagString("notifications.plugin.groupdisplayid"), CloudSdkMessageBundle.message("cloudsdk.notification.title"), notificationMessage, notificationType); // add a link to SDK settings for a quick fix if this is a fatal error. if (notificationType == NotificationType.ERROR) { invalidSdkWarning.addAction( new AnAction( CloudSdkMessageBundle.message("appengine.deployment.error.sdk.settings.action")) { @Override public void actionPerformed(AnActionEvent e) { ShowSettingsUtil.getInstance() .showSettingsDialog( null /* IDE-wide settings, no project needed. */, CloudSdkConfigurable.class); // expire if action has been called to avoid error hanging out forever. invalidSdkWarning.expire(); } }); } Notifications.Bus.notify(invalidSdkWarning); } }
@Test public void testCheckSdk_nullSdk() { when(cloudSdkValidator.isValidCloudSdk(null)).thenReturn(false); panel.checkSdk(null); verify(panel, times(1)).showWarning(eq(MISSING_SDK_DIR_WARNING)); verify(panel, times(0)).hideWarning(); }
@Override public void notifyIfVersionParseError() { CloudSdkValidator sdkValidator = CloudSdkValidator.getInstance(); try { CloudSdk cloudSdk = sdkValidator.buildCloudSdk(); // Try to get the version; if fails with exception, then notify the user. cloudSdk.getVersion(); } catch (CloudSdkNotFoundException ex) { // Cloud SDK not found. Don't notify. } catch (CloudSdkVersionFileException ex) { String message = "<p>" + CloudSdkValidationResult.CLOUD_SDK_VERSION_FILE_ERROR.getMessage() + "</p>"; showNotification( CloudSdkMessageBundle.message("appengine.cloudsdk.version.file.error.title"), message); UsageTrackerService.getInstance() .trackEvent(GctTracking.SDK_VERSION_PARSE_ERROR) .addMetadata(GctTracking.METADATA_LABEL_KEY, ex.getMessage()) .ping(); } }
@Test public void testNotifyIfCloudSdkNotSupported_versionParseError() throws CloudSdkNotFoundException, CloudSdkVersionFileException { when(cloudSdkValidator.buildCloudSdk()).thenReturn(cloudSdk); when(cloudSdk.getVersion()).thenThrow(new CloudSdkVersionFileException("file error")); checker.notifyIfVersionParseError(); verify(checker, times(1)) .showNotification( "Unrecognized Cloud SDK Version", "<p>Operations may have unintended " + "results. You can install the Cloud SDK manually and set the path " + "via:<p><p>Settings -> Google -> Cloud Sdk -> Use a custom local " + "installation</p>"); }
@Override public SdkStatus getStatus() { String sdkPath = CloudSdkServiceUserSettings.getInstance().getCustomSdkPath(); if (Strings.isNullOrEmpty(sdkPath)) { return SdkStatus.NOT_AVAILABLE; } boolean malformedSdkPath = CloudSdkValidator.isMalformedCloudSdkPath(sdkPath); return malformedSdkPath ? SdkStatus.INVALID : SdkStatus.READY; }
@Override public void checkConfiguration() throws RuntimeConfigurationException { if (artifactPointer == null || artifactPointer.getArtifact() == null) { throw new RuntimeConfigurationError( AppEngineMessageBundle.message("appengine.run.server.artifact.missing")); } // do not check SDK if it supports dynamic install - the deployment runner will block itself // until installation is done. CloudSdkService cloudSdkService = CloudSdkService.getInstance(); SdkStatus sdkStatus = cloudSdkService.getStatus(); if (sdkStatus != SdkStatus.READY && !cloudSdkService.isInstallSupported()) { if (!CloudSdkValidator.getInstance().isValidCloudSdk()) { throw new RuntimeConfigurationError( AppEngineMessageBundle.message("appengine.run.server.sdk.misconfigured.panel.message")); } } if (ProjectRootManager.getInstance(commonModel.getProject()).getProjectSdk() == null) { throw new RuntimeConfigurationError( AppEngineMessageBundle.getString("appengine.run.server.nojdk")); } }
@Test public void testCheckSdk_emptySdk() { when(cloudSdkValidator.isValidCloudSdk("")).thenReturn(false); panel.checkSdk(""); verify(panel, times(1)).showWarning(eq(MISSING_SDK_DIR_WARNING)); verify(panel, times(0)).hideWarning(); }
@Before public void setUp() throws Exception { when(service.getSdkHomePath()).thenReturn(Paths.get("/home/path")); when(cloudSdkServiceManager.getCloudSdkService()).thenReturn(service); doReturn(mockSdk).when(sdkValidator).buildCloudSdkWithPath(any(Path.class)); }
@Nullable @Override public Path getSdkHomePath() { String sdkPath = CloudSdkServiceUserSettings.getInstance().getCustomSdkPath(); if (sdkPath != null) { // To let Windows users that persisted the old malformed path save a new one. // TODO(joaomartins): Delete this after a while so gets are faster. if (CloudSdkValidator.isMalformedCloudSdkPath(sdkPath)) { UsageTrackerService.getInstance().trackEvent(GctTracking.CLOUD_SDK_MALFORMED_PATH).ping(); return null; } return Paths.get(sdkPath); } // Let common library auto-discover Cloud SDK's location. try { return new CloudSdk.Builder().build().getPath(); } catch (AppEngineException aee) { return null; } }
/** Checks if the default SDK saved path contains a valid Cloud SDK installation. */ public boolean isValidCloudSdk() { return validateCloudSdk(CloudSdkService.getInstance().getSdkHomePath()).isEmpty(); }
@Test public void testValidateCloudSdk_valid() { Set<CloudSdkValidationResult> results = sdkValidator.validateCloudSdk(); assertEquals(0, results.size()); assertTrue(sdkValidator.isValidCloudSdk()); }
new TreeSet<>(CloudSdkValidator.getInstance().validateCloudSdk(path));
@Test public void testCheckSdk_invalidSdk() { setValidateCloudSdkResponse(CloudSdkValidationResult.CLOUD_SDK_NOT_FOUND); when(cloudSdkValidator.isValidCloudSdk("/non/empty/path")).thenReturn(false); panel.checkSdk("/non/empty/path"); verify(panel, times(1)).showWarning(eq(INVALID_SDK_DIR_WARNING)); verify(panel, times(0)).hideWarning(); }
/** Checks if the default SDK stored path contains a valid Cloud SDK. */ public Set<CloudSdkValidationResult> validateCloudSdk() { CloudSdkService instance = CloudSdkService.getInstance(); return validateCloudSdk(instance != null ? instance.getSdkHomePath() : null); }
@Test public void testValidateCloudSdk_cloudSdkNotFound() throws CloudSdkNotFoundException, CloudSdkOutOfDateException, CloudSdkVersionFileException { doThrow(CloudSdkNotFoundException.class).when(mockSdk).validateCloudSdk(); Set<CloudSdkValidationResult> results = sdkValidator.validateCloudSdk(); assertEquals(1, results.size()); assertEquals(CloudSdkValidationResult.CLOUD_SDK_NOT_FOUND, results.iterator().next()); assertFalse(sdkValidator.isValidCloudSdk()); }