use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class ServiceDependencyLifecycleTest method GIVEN_hard_dependency_WHEN_dependency_goes_through_lifecycle_events_THEN_customer_app_is_impacted.
@Test
void GIVEN_hard_dependency_WHEN_dependency_goes_through_lifecycle_events_THEN_customer_app_is_impacted() throws Throwable {
// setup
kernel = new Kernel();
URL configFile = ServiceDependencyLifecycleTest.class.getResource("service_with_hard_dependency.yaml");
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, configFile);
// WHEN_kernel_launch_THEN_customer_app_starts_after_hard_dependency_is_running
LinkedList<ExpectedStateTransition> expectedDuringLaunch = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(HardDependency, State.INSTALLED, State.STARTING), new ExpectedStateTransition(HardDependency, State.STARTING, State.RUNNING), new ExpectedStateTransition(CustomerApp, State.INSTALLED, State.STARTING), new ExpectedStateTransition(CustomerApp, State.STARTING, State.RUNNING), new ExpectedStateTransition("main", State.STOPPING, State.FINISHED)));
testRoutine(TEST_ROUTINE_SHORT_TIMEOUT, kernel, kernel::launch, "kernel launched", expectedDuringLaunch, Collections.emptySet());
// WHEN_dependency_removed_THEN_customer_app_stays_running
LinkedList<ExpectedStateTransition> expectedDepRemoved = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(HardDependency, State.RUNNING, State.STOPPING)));
Set<ExpectedStateTransition> unexpectedDepRemoved = new HashSet<>(Arrays.asList(new ExpectedStateTransition(CustomerApp, State.RUNNING, State.STOPPING), new ExpectedStateTransition(CustomerApp, State.STOPPING, State.FINISHED)));
Map<String, Object> configRemoveDep = new HashMap<String, Object>() {
{
put(SERVICES_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put("main", new HashMap<String, Object>() {
{
put(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, Arrays.asList(CustomerApp, DEFAULT_NUCLEUS_COMPONENT_NAME));
}
});
put(CustomerApp, new HashMap<String, Object>() {
{
putAll(kernel.findServiceTopic(CustomerApp).toPOJO());
put(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, Collections.emptyList());
}
});
put(DEFAULT_NUCLEUS_COMPONENT_NAME, new HashMap<String, Object>() {
{
putAll(kernel.findServiceTopic(DEFAULT_NUCLEUS_COMPONENT_NAME).toPOJO());
}
});
}
});
}
};
DeploymentConfigMerger configMerger = kernel.getContext().get(DeploymentConfigMerger.class);
DeploymentDocument doc1 = mock(DeploymentDocument.class);
when(doc1.getTimestamp()).thenReturn(System.currentTimeMillis());
when(doc1.getDeploymentId()).thenReturn("removeHardDep");
when(doc1.getFailureHandlingPolicy()).thenReturn(FailureHandlingPolicy.DO_NOTHING);
testRoutine(TEST_ROUTINE_LONG_TIMEOUT, kernel, () -> configMerger.mergeInNewConfig(createMockDeployment(doc1), configRemoveDep).get(60, TimeUnit.SECONDS), "dependency removed", expectedDepRemoved, unexpectedDepRemoved);
// WHEN_dependency_added_THEN_customer_app_restarts
LinkedList<ExpectedStateTransition> expectedDepAdded = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(CustomerApp, State.RUNNING, State.STOPPING), new ExpectedStateTransition(CustomerApp, State.STARTING, State.RUNNING)));
Map<String, Object> configAddDep = ConfigPlatformResolver.resolvePlatformMap(configFile);
DeploymentDocument doc2 = mock(DeploymentDocument.class);
when(doc2.getTimestamp()).thenReturn(System.currentTimeMillis());
when(doc2.getDeploymentId()).thenReturn("addHardDep");
when(doc2.getFailureHandlingPolicy()).thenReturn(FailureHandlingPolicy.DO_NOTHING);
testRoutine(60, kernel, () -> configMerger.mergeInNewConfig(createMockDeployment(doc2), configAddDep).get(10, TimeUnit.SECONDS), "dependency added", expectedDepAdded, Collections.emptySet());
// WHEN_dependency_errored_THEN_customer_app_restarts
LinkedList<ExpectedStateTransition> expectedDuringDepError = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(HardDependency, State.RUNNING, State.ERRORED), new ExpectedStateTransition(CustomerApp, State.RUNNING, State.STOPPING), new ExpectedStateTransition(CustomerApp, State.STARTING, State.RUNNING)));
testRoutine(TEST_ROUTINE_SHORT_TIMEOUT, kernel, () -> kernel.locate(HardDependency).serviceErrored("mock dependency error"), "dependency errored", expectedDuringDepError, Collections.emptySet());
// WHEN_dependency_stops_THEN_customer_app_stays_running
LinkedList<ExpectedStateTransition> expectedDepFinish = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(HardDependency, State.STOPPING, State.FINISHED)));
Set<ExpectedStateTransition> unexpectedDepFinish = new HashSet<>(Arrays.asList(new ExpectedStateTransition(CustomerApp, State.RUNNING, State.STOPPING), new ExpectedStateTransition(CustomerApp, State.STOPPING, State.FINISHED)));
testRoutine(TEST_ROUTINE_MEDIUM_TIMEOUT, kernel, () -> kernel.locate(HardDependency).requestStop(), "dependency stop", expectedDepFinish, unexpectedDepFinish);
// WHEN_dependency_restarts_THEN_customer_app_restarts
LinkedList<ExpectedStateTransition> expectedDepRestart = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(HardDependency, State.STARTING, State.RUNNING), new ExpectedStateTransition(CustomerApp, State.STARTING, State.RUNNING)));
testRoutine(TEST_ROUTINE_SHORT_TIMEOUT, kernel, () -> kernel.locate(HardDependency).requestRestart(), "dependency restart", expectedDepRestart, Collections.emptySet());
// WHEN_dependency_reinstalled_THEN_customer_app_restarts
LinkedList<ExpectedStateTransition> expectedDepReinstall = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(HardDependency, State.NEW, State.INSTALLED), new ExpectedStateTransition(HardDependency, State.STARTING, State.RUNNING), new ExpectedStateTransition(CustomerApp, State.STARTING, State.RUNNING)));
testRoutine(TEST_ROUTINE_SHORT_TIMEOUT, kernel, () -> kernel.locate(HardDependency).requestReinstall(), "dependency reinstall", expectedDepReinstall, Collections.emptySet());
// WHEN_kernel_shutdown_THEN_hard_dependency_waits_for_customer_app_to_close
LinkedList<ExpectedStateTransition> expectedDuringShutdown = new LinkedList<>(Arrays.asList(new ExpectedStateTransition(CustomerApp, State.STOPPING, State.FINISHED), new ExpectedStateTransition(HardDependency, State.STOPPING, State.FINISHED)));
testRoutine(TEST_ROUTINE_SHORT_TIMEOUT, kernel, kernel::shutdown, "kernel shutdown", expectedDuringShutdown, Collections.emptySet());
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class UnloadableServiceIntegTest method beforeEach.
@BeforeEach
void beforeEach() {
kernel = new Kernel();
NoOpPathOwnershipHandler.register(kernel);
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientPublishTest method before.
@BeforeEach
void before() throws IOException, InterruptedException, ExecutionException {
kernel = new Kernel();
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, MqttClientPublishTest.class.getResource("config.yaml"));
config = kernel.getConfig();
deviceConfiguration = mock(DeviceConfiguration.class);
spool = mock(Spool.class);
builder = mock(AwsIotMqttConnectionBuilder.class);
connection = mock(MqttClientConnection.class);
Topics mqttNamespace = config.lookupTopics("mqtt");
when(deviceConfiguration.getMQTTNamespace()).thenReturn(mqttNamespace);
lenient().when(deviceConfiguration.isDeviceConfiguredToTalkToCloud()).thenReturn(true);
lenient().when(builder.build()).thenReturn(connection);
lenient().when(connection.connect()).thenReturn(CompletableFuture.completedFuture(false));
lenient().when(connection.disconnect()).thenReturn(CompletableFuture.completedFuture(null));
lenient().when(connection.publish(any(), any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(0));
mqttClient = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
CountDownLatch awaitIpcServiceLatch = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals(TEST_SERVICE_NAME) && newState.equals(State.FINISHED)) {
awaitIpcServiceLatch.countDown();
}
});
kernel.getContext().put(MqttClient.class, mqttClient);
kernel.launch();
assertTrue(awaitIpcServiceLatch.await(10, TimeUnit.SECONDS));
Topics servicePrivateConfig = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC, TEST_SERVICE_NAME, PRIVATE_STORE_NAMESPACE_TOPIC);
String authToken = Coerce.toString(servicePrivateConfig.find(SERVICE_UNIQUE_ID_KEY));
socketOptions = TestUtils.getSocketOptionsForIPC();
clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-nucleus by aws-greengrass.
the class AuthorizationPolicyParser method parseAllAuthorizationPolicies.
/**
* Given a kernel object, construct and return a map of AuthorizationPolicy objects that may exist,
* grouped into lists of the same destination component.
* This is used only upon kernel startup, to initialize all policies.
* Never returns null.
*
* @param kernel Kernel
* @return {@Map} of {@String} keys and {@List} of {@AuthorizationPolicy}'s as values"
*/
public Map<String, List<AuthorizationPolicy>> parseAllAuthorizationPolicies(Kernel kernel) {
Map<String, List<AuthorizationPolicy>> primaryAuthorizationPolicyMap = new HashMap<>();
Topics allServices = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC);
if (allServices == null) {
logger.atWarn("load-authorization-all-services-component-config-retrieval-error").log("Unable to retrieve services config");
return primaryAuthorizationPolicyMap;
}
// For each component
for (Node service : allServices) {
if (service == null) {
continue;
}
if (!(service instanceof Topics)) {
continue;
}
Topics serviceConfig = (Topics) service;
String componentName = Kernel.findServiceForNode(serviceConfig);
Node accessControlMapTopic = serviceConfig.findNode(CONFIGURATION_CONFIG_KEY, ACCESS_CONTROL_NAMESPACE_TOPIC);
if (accessControlMapTopic == null) {
continue;
}
// Retrieve all policies, mapped to each policy type
Map<String, List<AuthorizationPolicy>> componentAuthorizationPolicyMap = parseAllPoliciesForComponent(accessControlMapTopic, componentName);
// For each policy type (e.g. aws.greengrass.ipc.pubsub)
for (Map.Entry<String, List<AuthorizationPolicy>> policyTypeList : componentAuthorizationPolicyMap.entrySet()) {
String policyType = policyTypeList.getKey();
List<AuthorizationPolicy> policyList = policyTypeList.getValue();
// If multiple components have policies for the same policy type
primaryAuthorizationPolicyMap.computeIfAbsent(policyType, k -> new ArrayList<>()).addAll(policyList);
}
}
return primaryAuthorizationPolicyMap;
}
use of com.aws.greengrass.lifecyclemanager.Kernel in project aws-greengrass-cli by aws-greengrass.
the class IPCCliTest method GIVEN_kernel_running_WHEN_create_deployment_after_recipe_update_THEN_kernel_runs_latest_recipe.
@Test
@Order(7)
void GIVEN_kernel_running_WHEN_create_deployment_after_recipe_update_THEN_kernel_runs_latest_recipe(ExtensionContext context) throws Exception {
// updated recipes
Path recipesPath = Paths.get(this.getClass().getResource("recipes").toURI());
// Deployment to add a component
CreateLocalDeploymentRequest createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap(TEST_SERVICE_NAME, "1.0.1"));
createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
CountDownLatch serviceLatch = waitForServiceToComeInState(TEST_SERVICE_NAME, State.RUNNING, kernel);
CreateLocalDeploymentResponse addComponentDeploymentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
String deploymentId1 = addComponentDeploymentResponse.getDeploymentId();
CountDownLatch deploymentLatch = waitForDeploymentToBeSuccessful(deploymentId1, kernel);
assertTrue(serviceLatch.await(SERVICE_STATE_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS));
assertTrue(deploymentLatch.await(LOCAL_DEPLOYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
GetComponentDetailsRequest getComponentDetailsRequest = new GetComponentDetailsRequest();
getComponentDetailsRequest.setComponentName(TEST_SERVICE_NAME);
ComponentDetails componentDetails = clientConnection.getComponentDetails(getComponentDetailsRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails();
assertEquals("1.0.1", componentDetails.getVersion());
// Deployment to remove a component
createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setRootComponentsToRemove(Arrays.asList(TEST_SERVICE_NAME));
serviceLatch = waitForServiceToComeInState(TEST_SERVICE_NAME, State.FINISHED, kernel);
CreateLocalDeploymentResponse deploymentToRemoveComponentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
String deploymentId2 = deploymentToRemoveComponentResponse.getDeploymentId();
deploymentLatch = waitForDeploymentToBeSuccessful(deploymentId2, kernel);
assertTrue(serviceLatch.await(SERVICE_STATE_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS));
assertTrue(deploymentLatch.await(LOCAL_DEPLOYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
ignoreExceptionOfType(context, ServiceLoadException.class);
GetComponentDetailsRequest getRemovedComponent = new GetComponentDetailsRequest();
getRemovedComponent.setComponentName(TEST_SERVICE_NAME);
ExecutionException executionException = assertThrows(ExecutionException.class, () -> clientConnection.getComponentDetails(getRemovedComponent, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails());
assertEquals(ResourceNotFoundError.class, executionException.getCause().getClass());
ListLocalDeploymentsRequest listLocalDeploymentsRequest = new ListLocalDeploymentsRequest();
ListLocalDeploymentsResponse listLocalDeploymentsResponse = clientConnection.listLocalDeployments(listLocalDeploymentsRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
List<String> localDeploymentIds = listLocalDeploymentsResponse.getLocalDeployments().stream().map(ld -> ld.getDeploymentId()).collect(Collectors.toList());
assertThat(localDeploymentIds, containsInAnyOrder(deploymentId1, deploymentId2));
}
Aggregations