use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-cli by aws-greengrass.
the class IPCCliTest method prepareKernelFromConfigFile.
public static Kernel prepareKernelFromConfigFile(String configFile, Class testClass, String... serviceNames) throws InterruptedException, IOException {
Kernel kernel = new Kernel();
NoOpPathOwnershipHandler.register(kernel);
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, testClass.getResource(configFile));
CountDownLatch awaitIpcServiceLatch = new CountDownLatch(serviceNames.length);
GlobalStateChangeListener listener = getListenerForServiceRunning(awaitIpcServiceLatch, serviceNames);
kernel.getContext().addGlobalStateChangeListener(listener);
kernel.launch();
Assertions.assertTrue(awaitIpcServiceLatch.await(60L, TimeUnit.SECONDS));
kernel.getContext().removeGlobalStateChangeListener(listener);
return kernel;
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentServiceIntegrationTest method GIVEN_device_deployment_not_started_WHEN_new_deployment_THEN_first_deployment_cancelled.
@Test
void GIVEN_device_deployment_not_started_WHEN_new_deployment_THEN_first_deployment_cancelled() throws Exception {
CountDownLatch cdlDeployNonDisruptable = new CountDownLatch(1);
CountDownLatch cdlDeployRedSignal = new CountDownLatch(1);
CountDownLatch cdlRedeployNonDisruptable = new CountDownLatch(1);
Consumer<GreengrassLogMessage> listener = m -> {
if (m.getMessage() != null) {
if (m.getMessage().contains("Current deployment finished") && m.getContexts().get("DeploymentId").equals("deployNonDisruptable")) {
cdlDeployNonDisruptable.countDown();
}
if (m.getMessage().contains("Discarding device deployment") && m.getContexts().get("DEPLOYMENT_ID").equals("deployRedSignal")) {
cdlDeployRedSignal.countDown();
}
if (m.getMessage().contains("Current deployment finished") && m.getContexts().get("DeploymentId").equals("redeployNonDisruptable")) {
cdlRedeployNonDisruptable.countDown();
}
}
};
try (AutoCloseable l = TestUtils.createCloseableLogListener(listener)) {
submitSampleJobDocument(DeploymentServiceIntegrationTest.class.getResource("FleetConfigWithNonDisruptableService.json").toURI(), "deployNonDisruptable", DeploymentType.SHADOW);
CountDownLatch nonDisruptableServiceServiceLatch = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("NonDisruptableService") && newState.equals(State.RUNNING)) {
nonDisruptableServiceServiceLatch.countDown();
}
});
assertTrue(nonDisruptableServiceServiceLatch.await(30, TimeUnit.SECONDS));
try (EventStreamRPCConnection connection = IPCTestUtils.getEventStreamRpcConnection(kernel, "NonDisruptableService")) {
GreengrassCoreIPCClient ipcEventStreamClient = new GreengrassCoreIPCClient(connection);
ipcEventStreamClient.subscribeToComponentUpdates(new SubscribeToComponentUpdatesRequest(), Optional.of(new StreamResponseHandler<ComponentUpdatePolicyEvents>() {
@Override
public void onStreamEvent(ComponentUpdatePolicyEvents streamEvent) {
if (streamEvent.getPreUpdateEvent() != null) {
try {
DeferComponentUpdateRequest deferComponentUpdateRequest = new DeferComponentUpdateRequest();
deferComponentUpdateRequest.setRecheckAfterMs(TimeUnit.SECONDS.toMillis(60));
deferComponentUpdateRequest.setMessage("Test");
ipcEventStreamClient.deferComponentUpdate(deferComponentUpdateRequest, Optional.empty()).getResponse().get(DEFAULT_IPC_API_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
}
}
}
@Override
public boolean onStreamError(Throwable error) {
logger.atError().setCause(error).log("Caught error stream when subscribing for component " + "updates");
return false;
}
@Override
public void onStreamClosed() {
}
}));
assertTrue(cdlDeployNonDisruptable.await(30, TimeUnit.SECONDS));
submitSampleJobDocument(DeploymentServiceIntegrationTest.class.getResource("FleetConfigWithRedSignalService.json").toURI(), "deployRedSignal", DeploymentType.SHADOW);
submitSampleJobDocument(DeploymentServiceIntegrationTest.class.getResource("FleetConfigWithNonDisruptableService.json").toURI(), "redeployNonDisruptable", DeploymentType.SHADOW);
assertTrue(cdlRedeployNonDisruptable.await(15, TimeUnit.SECONDS));
assertTrue(cdlDeployRedSignal.await(1, TimeUnit.SECONDS));
}
}
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentTaskIntegrationTest method GIVEN_deployment_in_progress_WHEN_deployment_task_is_cancelled_THEN_stop_processing.
@Test
@Order(99)
@SuppressWarnings({ "PMD.CloseResource", "PMD.AvoidCatchingGenericException" })
void GIVEN_deployment_in_progress_WHEN_deployment_task_is_cancelled_THEN_stop_processing() throws Exception {
Future<DeploymentResult> resultFuture = submitSampleJobDocument(DeploymentTaskIntegrationTest.class.getResource("AddNewServiceWithSafetyCheck.json").toURI(), System.currentTimeMillis());
resultFuture.get(DEPLOYMENT_TIMEOUT, TimeUnit.SECONDS);
String authToken = IPCTestUtils.getAuthTokeForService(kernel, "NonDisruptableService");
final EventStreamRPCConnection clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
SubscribeToComponentUpdatesRequest subscribeToComponentUpdatesRequest = new SubscribeToComponentUpdatesRequest();
GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
CompletableFuture<SubscribeToComponentUpdatesResponse> fut = greengrassCoreIPCClient.subscribeToComponentUpdates(subscribeToComponentUpdatesRequest, Optional.of(new StreamResponseHandler<ComponentUpdatePolicyEvents>() {
@Override
public void onStreamEvent(ComponentUpdatePolicyEvents streamEvent) {
if (streamEvent.getPreUpdateEvent() != null) {
DeferComponentUpdateRequest deferComponentUpdateRequest = new DeferComponentUpdateRequest();
deferComponentUpdateRequest.setRecheckAfterMs(Duration.ofSeconds(60).toMillis());
deferComponentUpdateRequest.setMessage("Test");
deferComponentUpdateRequest.setDeploymentId(streamEvent.getPreUpdateEvent().getDeploymentId());
greengrassCoreIPCClient.deferComponentUpdate(deferComponentUpdateRequest, Optional.empty());
}
}
@Override
public boolean onStreamError(Throwable error) {
logger.atError().setCause(error).log("Stream closed due to error");
return false;
}
@Override
public void onStreamClosed() {
}
})).getResponse();
try {
fut.get(DEPLOYMENT_TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
logger.atError().setCause(e).log("Error when subscribing to component updates");
fail("Caught exception when subscribing to component updates");
}
List<String> services = kernel.orderedDependencies().stream().filter(greengrassService -> greengrassService instanceof GenericExternalService).map(GreengrassService::getName).collect(Collectors.toList());
// should contain main, Nucleus, NonDisruptableService 1.0.0
assertEquals(3, services.size(), "Actual services: " + services);
assertThat(services, containsInAnyOrder("main", DEFAULT_NUCLEUS_COMPONENT_NAME, "NonDisruptableService"));
CountDownLatch cdlUpdateStarted = new CountDownLatch(1);
CountDownLatch cdlMergeCancelled = new CountDownLatch(1);
Consumer<GreengrassLogMessage> listener = m -> {
if (m.getMessage() != null && m.getMessage().contains("deferred for 60000 millis with message Test")) {
cdlUpdateStarted.countDown();
}
if (m.getMessage() != null && m.getMessage().contains("Cancelled deployment merge future due to interrupt")) {
cdlMergeCancelled.countDown();
}
};
Slf4jLogAdapter.addGlobalListener(listener);
try {
resultFuture = submitSampleJobDocument(DeploymentTaskIntegrationTest.class.getResource("UpdateServiceWithSafetyCheck.json").toURI(), System.currentTimeMillis());
assertTrue(cdlUpdateStarted.await(40, TimeUnit.SECONDS));
resultFuture.cancel(true);
assertTrue(cdlMergeCancelled.await(DEPLOYMENT_TIMEOUT, TimeUnit.SECONDS));
services = kernel.orderedDependencies().stream().filter(greengrassService -> greengrassService instanceof GenericExternalService).map(GreengrassService::getName).collect(Collectors.toList());
// should contain main, Nucleus, NonDisruptableService 1.0.0
assertEquals(3, services.size());
assertThat(services, containsInAnyOrder("main", DEFAULT_NUCLEUS_COMPONENT_NAME, "NonDisruptableService"));
assertEquals("1.0.0", kernel.findServiceTopic("NonDisruptableService").find("version").getOnce());
} finally {
Slf4jLogAdapter.removeGlobalListener(listener);
clientConnection.close();
}
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class IPCTestUtils method prepareKernelFromConfigFile.
public static Kernel prepareKernelFromConfigFile(String configFile, Class testClass, String... serviceNames) throws InterruptedException, IOException {
Kernel kernel = new Kernel();
NoOpPathOwnershipHandler.register(kernel);
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, testClass.getResource(configFile));
// ensure awaitIpcServiceLatch starts
CountDownLatch awaitIpcServiceLatch = new CountDownLatch(serviceNames.length);
GlobalStateChangeListener listener = getListenerForServiceRunning(awaitIpcServiceLatch, serviceNames);
kernel.getContext().addGlobalStateChangeListener(listener);
kernel.launch();
assertTrue(awaitIpcServiceLatch.await(10, TimeUnit.SECONDS));
kernel.getContext().removeGlobalStateChangeListener(listener);
return kernel;
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class FileLoggerTest method GIVEN_root_path_given_as_kernel_param_WHEN_kernel_launches_THEN_logs_written_to_correct_directory.
@Test
void GIVEN_root_path_given_as_kernel_param_WHEN_kernel_launches_THEN_logs_written_to_correct_directory() throws Exception {
// launch Nucleus without config arg
String randomDirectory = UUID.randomUUID().toString();
kernel = new Kernel().parseArgs("-r", tempRootDir.resolve(randomDirectory).toAbsolutePath().toString()).launch();
GreengrassService mainService = kernel.locate("main");
assertNotNull(mainService);
// verify that log file exists are the correct location.
File logFile = tempRootDir.resolve(randomDirectory).resolve("logs").resolve("greengrass.log").toFile();
File oldLogFile = tempRootDir.resolve("logs").resolve("greengrass.log").toFile();
MatcherAssert.assertThat(logFile, aFileNamed(equalToIgnoringCase("greengrass.log")));
assertEquals(0, oldLogFile.length());
assertTrue(logFile.length() > 0);
}
Aggregations