OrchestratorImpl implements Orchestrator { @Override public void suspend(final String hostName) { UpdateHostResponse response; try { var params = new ConfigServerApi.Params().setConnectionTimeout(CONNECTION_TIMEOUT); response = configServerApi.put(getSuspendPath(hostName), Optional.empty(), UpdateHostResponse.class, params); } catch (HttpException.NotFoundException n) { throw new OrchestratorNotFoundException("Failed to suspend " + hostName + ", host not found"); } catch (HttpException e) { throw new OrchestratorException("Failed to suspend " + hostName + ": " + e.toString()); } catch (ConnectionException e) { throw new ConvergenceException("Failed to suspend " + hostName + ": " + e.getMessage()); } catch (RuntimeException e) { throw new RuntimeException("Got error on suspend", e); } Optional.ofNullable(response.reason()).ifPresent(reason -> { throw new OrchestratorException(reason.message()); }); } OrchestratorImpl(ConfigServerApi configServerApi); @Override void suspend(final String hostName); @Override void suspend(String parentHostName, List<String> hostNames); @Override void resume(final String hostName);  }
@Test public void testSuspendCall() { when(configServerApi.put( eq(OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName+ "/suspended"), eq(Optional.empty()), eq(UpdateHostResponse.class), any() )).thenReturn(new UpdateHostResponse(hostName, null)); orchestrator.suspend(hostName); }
@Test(expected=OrchestratorException.class) public void testSuspendCallWithFailureReason() { when(configServerApi.put( eq(OrchestratorImpl.ORCHESTRATOR_PATH_PREFIX_HOST_API + "/" + hostName+ "/suspended"), eq(Optional.empty()), eq(UpdateHostResponse.class), any() )).thenReturn(new UpdateHostResponse(hostName, new HostStateChangeDenialReason("hostname", "fail"))); orchestrator.suspend(hostName); }
@Test(expected=OrchestratorNotFoundException.class) public void testSuspendCallWithNotFound() { when(configServerApi.put(any(String.class), any(), any(), any())) .thenThrow(new HttpException.NotFoundException("Not Found")); orchestrator.suspend(hostName); }
@Test(expected=RuntimeException.class) public void testSuspendCallWithSomeOtherException() { when(configServerApi.put(any(String.class), any(), any(), any())) .thenThrow(new RuntimeException("Some parameter was wrong")); orchestrator.suspend(hostName); }
@Test(expected=RuntimeException.class) public void testResumeCallWithSomeOtherException() { when(configServerApi.put(any(String.class), any(), any(), any())) .thenThrow(new RuntimeException("Some parameter was wrong")); orchestrator.suspend(hostName); }
@Test public void testBatchSuspendCall() { String parentHostName = "host1.test.yahoo.com"; List<String> hostNames = List.of("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com"); when(configServerApi.put( eq("/orchestrator/v1/suspensions/hosts/host1.test.yahoo.com?hostname=a1.host1.test.yahoo.com&hostname=a2.host1.test.yahoo.com"), eq(Optional.empty()), eq(BatchOperationResult.class), any() )).thenReturn(BatchOperationResult.successResult()); orchestrator.suspend(parentHostName, hostNames); }
@Test(expected=OrchestratorException.class) public void testBatchSuspendCallWithFailureReason() { String parentHostName = "host1.test.yahoo.com"; List<String> hostNames = List.of("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com"); String failureReason = "Failed to suspend"; when(configServerApi.put( eq("/orchestrator/v1/suspensions/hosts/host1.test.yahoo.com?hostname=a1.host1.test.yahoo.com&hostname=a2.host1.test.yahoo.com"), eq(Optional.empty()), eq(BatchOperationResult.class), any() )).thenReturn(new BatchOperationResult(failureReason)); orchestrator.suspend(parentHostName, hostNames); }
@Test(expected=RuntimeException.class) public void testBatchSuspendCallWithSomeException() { String parentHostName = "host1.test.yahoo.com"; List<String> hostNames = List.of("a1.host1.test.yahoo.com", "a2.host1.test.yahoo.com"); String exceptionMessage = "Exception: Something crashed!"; when(configServerApi.put( eq("/orchestrator/v1/suspensions/hosts/host1.test.yahoo.com?hostname=a1.host1.test.yahoo.com&hostname=a2.host1.test.yahoo.com"), eq(Optional.empty()), eq(BatchOperationResult.class), any() )).thenThrow(new RuntimeException(exceptionMessage)); orchestrator.suspend(parentHostName, hostNames); }