private void checkSdkStatusAfterFailedUpdate() { try { if (managedCloudSdk.isInstalled()) { updateStatus(SdkStatus.READY); } else { updateStatus(SdkStatus.NOT_AVAILABLE); } } catch (Exception ex) { updateStatus(SdkStatus.NOT_AVAILABLE); } }
/** Mocks managed SDK as if installed and having App Engine Component. */ private void makeMockSdkInstalled(Path mockSdkPath) { try { when(mockManagedCloudSdk.isInstalled()).thenReturn(true); when(mockManagedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(true); when(mockManagedCloudSdk.getSdkHome()).thenReturn(mockSdkPath); } catch (Exception ex) { // shouldn't happen in the tests. throw new AssertionError(ex); } }
/** Mocks successful installation process with all steps included (SDK, App Engine Java). */ private void emulateMockSdkInstallationProcess(Path mockSdkPath) { try { when(mockManagedCloudSdk.isInstalled()).thenReturn(false); SdkInstaller mockInstaller = mock(SdkInstaller.class); when(mockManagedCloudSdk.newInstaller()).thenReturn(mockInstaller); when(mockInstaller.install(any(), any())).thenReturn(mockSdkPath); when(mockManagedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false); SdkComponentInstaller mockComponentInstaller = mock(SdkComponentInstaller.class); when(mockManagedCloudSdk.newComponentInstaller()).thenReturn(mockComponentInstaller); when(mockManagedCloudSdk.getSdkHome()).thenReturn(mockSdkPath); } catch (Exception ex) { // shouldn't happen in the tests. throw new AssertionError(ex); } }
@Test public void testDownloadCloudSdkAction_install() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); when(managedCloudSdk.isInstalled()).thenReturn(false); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk).newInstaller(); }
@Test public void testDownloadCloudSdkAction_skipInstallComponent() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); downloadCloudSdkTask.requiresAppEngineJava(false); when(managedCloudSdk.isInstalled()).thenReturn(true); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk, never()).newInstaller(); verify(managedCloudSdk, never()).newComponentInstaller(); }
@Test public void testDownloadCloudSdkAction_update() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); when(managedCloudSdk.isInstalled()).thenReturn(true); when(managedCloudSdk.isUpToDate()).thenReturn(false); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk, never()).newInstaller(); verify(managedCloudSdk, never()).newComponentInstaller(); verify(managedCloudSdk).newUpdater(); } }
@Test public void failed_update_invalidSdk_makesSdkStatus_notAvailable() throws Exception { makeMockSdkInstalled(MOCK_SDK_PATH); emulateMockSdkUpdateProcess(); SdkUpdater mockUpdater = mockManagedCloudSdk.newUpdater(); doThrow(new CommandExitException(-1, "")).when(mockUpdater).update(any(), any()); // initially SDK is installed, then update breaks SDK when(mockManagedCloudSdk.isInstalled()).thenReturn(true).thenReturn(false); sdkService.addStatusUpdateListener(mockStatusUpdateListener); sdkService.update(); ArgumentCaptor<SdkStatus> statusCaptor = ArgumentCaptor.forClass(SdkStatus.class); verify(mockStatusUpdateListener, times(2)).onSdkStatusChange(any(), statusCaptor.capture()); assertThat(statusCaptor.getAllValues()) .isEqualTo(Arrays.asList(SdkStatus.INSTALLING, SdkStatus.NOT_AVAILABLE)); }
@Test public void testDownloadCloudSdkAction_installComponent() throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException, InterruptedException, CommandExecutionException, SdkInstallerException, IOException, CommandExitException { downloadCloudSdkTask.setManagedCloudSdk(managedCloudSdk); downloadCloudSdkTask.requiresAppEngineJava(true); when(managedCloudSdk.isInstalled()).thenReturn(true); when(managedCloudSdk.hasComponent(SdkComponent.APP_ENGINE_JAVA)).thenReturn(false); downloadCloudSdkTask.downloadCloudSdkAction(); verify(managedCloudSdk, never()).newInstaller(); verify(managedCloudSdk).newComponentInstaller(); }
@Test public void install_blocks_whenSDKReadOperations_running() throws Exception { emulateMockSdkInstallationProcess(MOCK_SDK_PATH); SdkInstaller mockInstaller = mock(SdkInstaller.class); when(mockManagedCloudSdk.newInstaller()).thenReturn(mockInstaller); CloudSdkServiceManager.getInstance().getSdkReadLock().lock(); try { // signal when install is about to start write operation. CountDownLatch waitForInstallToStart = new CountDownLatch(1); doAnswer( invocationOnMock -> { waitForInstallToStart.countDown(); return false; }) .when(mockManagedCloudSdk) .isInstalled(); Runnable installProcess = () -> sdkService.install(); Thread installProcessThread = new Thread(installProcess, "test-install-blocking-thread"); installProcessThread.start(); // do timed wait in case of test issues not to cause it to hang. waitForInstallToStart.await(100, TimeUnit.MILLISECONDS); installProcessThread.interrupt(); // finalize install() installProcessThread.join(); // since the write block was not available at the install, install() should never be called. verifyNoMoreInteractions(mockInstaller); } finally { CloudSdkServiceManager.getInstance().getSdkReadLock().unlock(); } }
/** Installs core managed SDK if needed and returns its path if successful. */ private ManagedSdkJobResult installSdk() throws Exception { if (!safeCheckSdkStatus(() -> managedCloudSdk.isInstalled())) { ConsoleListener sdkConsoleListener = logger::debug; progressListener = ManagedCloudSdkServiceUiPresenter.getInstance().createProgressListener(this); notifySdkProcessingStarted(); executeWithSdkWriteLock( () -> managedCloudSdk.newInstaller().install(progressListener, sdkConsoleListener)); return ManagedSdkJobResult.PROCESSED; } return ManagedSdkJobResult.UP_TO_DATE; }
if (!managedCloudSdk.isInstalled()) { SdkInstaller installer = managedCloudSdk.newInstaller(); installer.install(progressListener, consoleListener);
ConsoleListener consoleListener = new CloudSdkDownloaderConsoleListener(log); if (!managedCloudSdk.isInstalled()) { managedCloudSdk.newInstaller().install(progressListener, consoleListener);
private ManagedSdkJobResult updateManagedSdk() throws Exception { if (safeCheckSdkStatus(() -> managedCloudSdk.isInstalled()) && !safeCheckSdkStatus(() -> managedCloudSdk.isUpToDate())) { ConsoleListener sdkUpdateListener = logger::debug; progressListener = ManagedCloudSdkServiceUiPresenter.getInstance().createProgressListener(this); notifySdkProcessingStarted(); executeWithSdkWriteLock( () -> managedCloudSdk.newUpdater().update(progressListener, sdkUpdateListener)); return ManagedSdkJobResult.PROCESSED; } else { return ManagedSdkJobResult.UP_TO_DATE; } }