Search in sources :

Example 96 with Topics

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));
}
Also used : Path(java.nio.file.Path) Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Topic(com.aws.greengrass.config.Topic) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Test(org.junit.jupiter.api.Test)

Example 97 with Topics

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());
}
Also used : Path(java.nio.file.Path) Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) PersistenceConfig(com.aws.greengrass.logging.impl.config.PersistenceConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Topic(com.aws.greengrass.config.Topic) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) LogConfig(com.aws.greengrass.logging.impl.config.LogConfig) Test(org.junit.jupiter.api.Test)

Example 98 with Topics

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());
    }
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) UpdateBehaviorTree(com.aws.greengrass.config.UpdateBehaviorTree) NucleusPaths(com.aws.greengrass.util.NucleusPaths) Logger(com.aws.greengrass.logging.api.Logger) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Test(org.junit.jupiter.api.Test)

Example 99 with Topics

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));
}
Also used : Topics(com.aws.greengrass.config.Topics) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 100 with Topics

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");
                        }
                    });
                }
            });
        }
    }));
}
Also used : Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

Topics (com.aws.greengrass.config.Topics)151 Test (org.junit.jupiter.api.Test)104 Topic (com.aws.greengrass.config.Topic)65 HashMap (java.util.HashMap)50 Map (java.util.Map)29 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)28 Context (com.aws.greengrass.dependency.Context)24 CountDownLatch (java.util.concurrent.CountDownLatch)24 IOException (java.io.IOException)23 ExecutionException (java.util.concurrent.ExecutionException)22 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)21 HashSet (java.util.HashSet)19 BeforeEach (org.junit.jupiter.api.BeforeEach)18 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)17 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ArrayList (java.util.ArrayList)15 Path (java.nio.file.Path)13 ComponentRecipe (com.aws.greengrass.componentmanager.models.ComponentRecipe)12