use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method GIVEN_all_fields_logger_config_WHEN_subscribe_THEN_correctly_reconfigures_all_loggers.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_all_fields_logger_config_WHEN_subscribe_THEN_correctly_reconfigures_all_loggers() throws IOException {
Path tempRootDir2 = tempRootDir.resolve("2");
Topics rootConfigTopics = mock(Topics.class);
when(rootConfigTopics.findOrDefault(any(), anyString(), anyString(), anyString())).thenReturn(new ArrayList<>());
when(configuration.lookup(anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
when(configuration.lookup(anyString(), anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
when(configuration.getRoot()).thenReturn(rootConfigTopics);
when(kernel.getConfig()).thenReturn(configuration);
when(kernel.getNucleusPaths()).thenReturn(nucleusPaths);
Topics loggingConfig = Topics.of(context, NUCLEUS_CONFIG_LOGGING_TOPICS, null);
loggingConfig.createLeafChild("level").withValue("TRACE");
loggingConfig.createLeafChild("fileSizeKB").withValue("10");
loggingConfig.createLeafChild("totalLogsSizeKB").withValue("1026");
loggingConfig.createLeafChild("format").withValue("TEXT");
loggingConfig.createLeafChild("outputType").withValue("FILE");
loggingConfig.createLeafChild("outputDirectory").withValue(tempRootDir2.toAbsolutePath().toString());
Topics topics = Topics.of(mock(Context.class), SERVICES_NAMESPACE_TOPIC, mock(Topics.class));
when(configuration.lookupTopics(anyString(), anyString(), anyString(), anyString())).thenReturn(loggingConfig);
when(configuration.lookupTopics(anyString())).thenReturn(topics);
when(configuration.lookupTopics(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY)).thenReturn(topics);
when(configuration.lookupTopics(SYSTEM_NAMESPACE_KEY)).thenReturn(topics);
new DeviceConfiguration(kernel);
assertEquals(Level.TRACE, LogManager.getRootLogConfiguration().getLevel());
assertEquals(LogStore.FILE, LogManager.getRootLogConfiguration().getStore());
assertEquals(LogFormat.TEXT, LogManager.getRootLogConfiguration().getFormat());
assertEquals(10, LogManager.getRootLogConfiguration().getFileSizeKB());
assertEquals(1026, LogManager.getRootLogConfiguration().getTotalLogStoreSizeKB());
assertEquals(tempRootDir2.toAbsolutePath(), LogManager.getRootLogConfiguration().getStoreDirectory().toAbsolutePath());
assertEquals(Level.TRACE, LogManager.getTelemetryConfig().getLevel());
assertEquals(LogStore.FILE, LogManager.getTelemetryConfig().getStore());
assertEquals(LogFormat.JSON, LogManager.getTelemetryConfig().getFormat());
assertEquals(10, LogManager.getTelemetryConfig().getFileSizeKB());
assertEquals(1026, LogManager.getTelemetryConfig().getTotalLogStoreSizeKB());
verify(nucleusPaths, times(1)).setLoggerPath(any(Path.class));
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method GIVEN_mock_service_logger_WHEN_reset_THEN_applied_correctly.
@Test
void GIVEN_mock_service_logger_WHEN_reset_THEN_applied_correctly() {
Path tempRootDir2 = tempRootDir.resolve("test_logs" + Utils.generateRandomString(8));
String mockServiceName = "MockService001";
when(mockGreengrassService.getServiceName()).thenReturn(mockServiceName);
Topics rootConfigTopics = mock(Topics.class);
when(rootConfigTopics.findOrDefault(any(), anyString(), anyString(), anyString())).thenReturn(new ArrayList<>());
when(configuration.lookup(anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
when(configuration.lookup(anyString(), anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
when(configuration.getRoot()).thenReturn(rootConfigTopics);
when(kernel.getConfig()).thenReturn(configuration);
when(kernel.getNucleusPaths()).thenReturn(nucleusPaths);
// Start with non-default configs
Topics loggingConfig = Topics.of(context, NUCLEUS_CONFIG_LOGGING_TOPICS, null);
loggingConfig.createLeafChild("fileSizeKB").withValue("10");
loggingConfig.createLeafChild("format").withValue("JSON");
loggingConfig.createLeafChild("outputType").withValue("CONSOLE");
loggingConfig.createLeafChild("outputDirectory").withValue(tempRootDir2.toAbsolutePath().toString());
Topics topics = Topics.of(mock(Context.class), SERVICES_NAMESPACE_TOPIC, mock(Topics.class));
when(configuration.lookupTopics(anyString(), anyString(), anyString(), anyString())).thenReturn(loggingConfig);
when(configuration.lookupTopics(anyString())).thenReturn(topics);
when(configuration.lookupTopics(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY)).thenReturn(topics);
when(configuration.lookupTopics(SYSTEM_NAMESPACE_KEY)).thenReturn(topics);
DeviceConfiguration deviceConfiguration = new DeviceConfiguration(kernel);
LogManagerHelper.getComponentLogger(mockGreengrassService);
LogConfig testLogConfig = LogManager.getLogConfigurations().get(mockServiceName);
PersistenceConfig defaultConfig = new PersistenceConfig(LOG_FILE_EXTENSION, LOGS_DIRECTORY);
// assert non-default configs
assertEquals(LogStore.CONSOLE, testLogConfig.getStore());
assertEquals(LogFormat.JSON, testLogConfig.getFormat());
assertEquals(10, testLogConfig.getFileSizeKB());
assertEquals(tempRootDir2.toAbsolutePath(), testLogConfig.getStoreDirectory().toAbsolutePath());
// reset individual configs
deviceConfiguration.handleLoggingConfigurationChanges(WhatHappened.childRemoved, loggingConfig.findNode("format"));
assertEquals(defaultConfig.getFormat(), testLogConfig.getFormat());
assertEquals(defaultConfig.getFormat(), LogManager.getRootLogConfiguration().getFormat());
deviceConfiguration.handleLoggingConfigurationChanges(WhatHappened.childRemoved, loggingConfig.findNode("outputDirectory"));
assertEquals(defaultConfig.getStoreDirectory(), testLogConfig.getStoreDirectory());
assertEquals(defaultConfig.getStoreDirectory(), LogManager.getRootLogConfiguration().getStoreDirectory());
deviceConfiguration.handleLoggingConfigurationChanges(WhatHappened.childRemoved, loggingConfig.findNode("outputType"));
assertEquals(defaultConfig.getStore(), testLogConfig.getStore());
assertEquals(defaultConfig.getStore(), LogManager.getRootLogConfiguration().getStore());
deviceConfiguration.handleLoggingConfigurationChanges(WhatHappened.childRemoved, loggingConfig.findNode("fileSizeKB"));
assertEquals(defaultConfig.getFileSizeKB(), testLogConfig.getFileSizeKB());
assertEquals(defaultConfig.getFileSizeKB(), LogManager.getRootLogConfiguration().getFileSizeKB());
// reset all configs together
deviceConfiguration.handleLoggingConfigurationChanges(WhatHappened.childChanged, loggingConfig);
deviceConfiguration.handleLoggingConfigurationChanges(WhatHappened.removed, null);
assertEquals(defaultConfig.getFormat(), testLogConfig.getFormat());
assertEquals(defaultConfig.getFormat(), LogManager.getRootLogConfiguration().getFormat());
assertEquals(defaultConfig.getStoreDirectory(), testLogConfig.getStoreDirectory());
assertEquals(defaultConfig.getStoreDirectory(), LogManager.getRootLogConfiguration().getStoreDirectory());
assertEquals(defaultConfig.getStore(), testLogConfig.getStore());
assertEquals(defaultConfig.getStore(), LogManager.getRootLogConfiguration().getStore());
assertEquals(defaultConfig.getFileSizeKB(), testLogConfig.getFileSizeKB());
assertEquals(defaultConfig.getFileSizeKB(), LogManager.getRootLogConfiguration().getFileSizeKB());
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method loggers_created_before_or_after_log_level_change_get_the_correct_level.
@Test
void loggers_created_before_or_after_log_level_change_get_the_correct_level() throws IOException {
try (Context context = new Context()) {
Configuration config = new Configuration(context);
Topics logTopics = config.lookupTopics(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY, NUCLEUS_CONFIG_LOGGING_TOPICS);
when(kernel.getConfig()).thenReturn(config);
lenient().when(kernel.getNucleusPaths()).thenReturn(mock(NucleusPaths.class));
when(mockGreengrassService.getServiceName()).thenReturn("MockService3");
Logger logger1 = LogManagerHelper.getComponentLogger(mockGreengrassService);
assertFalse(logger1.isDebugEnabled());
new DeviceConfiguration(kernel);
context.runOnPublishQueueAndWait(() -> logTopics.updateFromMap(Utils.immutableMap("level", "DEBUG"), new UpdateBehaviorTree(UpdateBehaviorTree.UpdateBehavior.REPLACE, System.currentTimeMillis())));
context.waitForPublishQueueToClear();
when(mockGreengrassService.getServiceName()).thenReturn("MockService4");
Logger logger2 = LogManagerHelper.getComponentLogger(mockGreengrassService);
assertTrue(logger2.isDebugEnabled());
assertTrue(logger1.isDebugEnabled());
}
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method beforeEach.
@BeforeEach
void beforeEach() {
Topics mqttNamespace = config.lookupTopics("mqtt");
Topics spoolerNamespace = config.lookupTopics("spooler");
mqttNamespace.lookup(MqttClient.MQTT_OPERATION_TIMEOUT_KEY).withValue(0);
mqttNamespace.lookup(MqttClient.MQTT_MAX_IN_FLIGHT_PUBLISHES_KEY).withValue(MqttClient.IOT_MAX_LIMIT_IN_FLIGHT_OF_QOS1_PUBLISHES + 1);
mqttNamespace.lookup(MqttClient.MQTT_MAX_OF_MESSAGE_SIZE_IN_BYTES_KEY).withValue(MQTT_MAX_LIMIT_OF_MESSAGE_SIZE_IN_BYTES + 1);
when(deviceConfiguration.getMQTTNamespace()).thenReturn(mqttNamespace);
lenient().when(deviceConfiguration.isDeviceConfiguredToTalkToCloud()).thenReturn(true);
lenient().when(deviceConfiguration.getSpoolerNamespace()).thenReturn(spoolerNamespace);
lenient().when(builder.build()).thenReturn(mockConnection);
lenient().when(mockConnection.connect()).thenReturn(CompletableFuture.completedFuture(false));
lenient().when(mockConnection.disconnect()).thenReturn(CompletableFuture.completedFuture(null));
lenient().when(mockConnection.subscribe(any(), any())).thenReturn(CompletableFuture.completedFuture(0));
lenient().when(mockConnection.unsubscribe(any())).thenReturn(CompletableFuture.completedFuture(0));
lenient().when(mockConnection.publish(any(), any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(0));
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalServiceTest method GIVEN_nested_bootstrap_definition_WHEN_isBootstrapRequired_THEN_return_false.
@Test
void GIVEN_nested_bootstrap_definition_WHEN_isBootstrapRequired_THEN_return_false() {
Topics bootstrap = Topics.of(context, Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, null);
bootstrap.createLeafChild("script").withValue("\necho complete\n");
doReturn(bootstrap).when(config).findNode(eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC), eq(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {
{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put(Lifecycle.LIFECYCLE_BOOTSTRAP_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put("script", "\necho complete\n");
}
});
}
});
}
}));
assertFalse(ges.isBootstrapRequired(new HashMap<String, Object>() {
{
put(VERSION_CONFIG_KEY, "1.0.0");
put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {
{
put("Bootstrap", new HashMap<String, Object>() {
{
put("script", "\necho complete\n");
}
});
}
});
}
}));
}
Aggregations