Search in sources :

Example 16 with Context

use of com.aws.greengrass.dependency.Context in project aws-greengrass-cli by aws-greengrass.

the class CLIEventStreamAgentTest method test_createDebugPassword.

@Test
void test_createDebugPassword() throws IOException {
    CreateDebugPasswordRequest request = new CreateDebugPasswordRequest();
    try (Context context = new Context()) {
        Topics topics = Topics.of(context, "", null);
        CreateDebugPasswordResponse response = cliEventStreamAgent.getCreateDebugPasswordHandler(mockContext, topics).handleRequest(request);
        assertEquals("debug", response.getUsername());
        // Length is 43 due to Base64 encoding overhead
        assertThat(response.getPassword(), hasLength(43));
        assertNotNull(topics.findTopics("_debugPassword", "debug", response.getPassword()));
        assertEquals(response.getPasswordExpiration().toEpochMilli(), Coerce.toLong(topics.find("_debugPassword", "debug", response.getPassword(), "expiration")));
        assertNull(response.getCertificateSHA256Hash());
        assertNull(response.getCertificateSHA1Hash());
        topics.lookup("_certificateFingerprint", "SHA-1").withValue("ABCD");
        topics.lookup("_certificateFingerprint", "SHA-256").withValue("ABCDE");
        response = cliEventStreamAgent.getCreateDebugPasswordHandler(mockContext, topics).handleRequest(request);
        assertEquals("ABCD", response.getCertificateSHA1Hash());
        assertEquals("ABCDE", response.getCertificateSHA256Hash());
    }
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Context(com.aws.greengrass.dependency.Context) OperationContinuationHandlerContext(software.amazon.awssdk.eventstreamrpc.OperationContinuationHandlerContext) CreateDebugPasswordRequest(software.amazon.awssdk.aws.greengrass.model.CreateDebugPasswordRequest) Topics(com.aws.greengrass.config.Topics) CreateDebugPasswordResponse(software.amazon.awssdk.aws.greengrass.model.CreateDebugPasswordResponse) Test(org.junit.jupiter.api.Test)

Example 17 with Context

use of com.aws.greengrass.dependency.Context in project aws-greengrass-nucleus by aws-greengrass.

the class BaseITCase method setup.

@BeforeAll
static void setup() throws IOException, InterruptedException {
    if (!PlatformResolver.isWindows) {
        return;
    }
    // To test runWith on Windows, need to prepare user and credential
    createWindowsTestUser(WINDOWS_TEST_UESRNAME, WINDOWS_TEST_PASSWORD);
    createWindowsTestUser(WINDOWS_TEST_UESRNAME_2, WINDOWS_TEST_PASSWORD);
    WindowsCredUtils.add(WINDOWS_TEST_UESRNAME, WINDOWS_TEST_PASSWORD.getBytes(WindowsCredUtils.getCharsetForSystem()));
    WindowsCredUtils.add(WINDOWS_TEST_UESRNAME_2, WINDOWS_TEST_PASSWORD.getBytes(WindowsCredUtils.getCharsetForSystem()));
    // mock runas path
    KernelAlternatives mockKernelAlts = mock(KernelAlternatives.class);
    when(mockKernelAlts.getBinDir()).thenReturn(Paths.get("scripts").toAbsolutePath());
    testContext = new Context();
    testContext.put(KernelAlternatives.class, mockKernelAlts);
}
Also used : Context(com.aws.greengrass.dependency.Context) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 18 with Context

use of com.aws.greengrass.dependency.Context in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleTest method setupContext.

@BeforeEach
void setupContext() throws IOException {
    context = new Context();
    ScheduledThreadPoolExecutor ses = new ScheduledThreadPoolExecutor(4);
    ExecutorService executorService = Executors.newCachedThreadPool();
    context.put(ScheduledThreadPoolExecutor.class, ses);
    context.put(ScheduledExecutorService.class, ses);
    context.put(Executor.class, executorService);
    context.put(ExecutorService.class, executorService);
    context.put(ThreadPoolExecutor.class, ses);
    context.put(Clock.class, Clock.systemUTC());
    context.put(Kernel.class, mock(Kernel.class));
    Topics rootConfig = new Configuration(context).getRoot();
    config = rootConfig.createInteriorChild(GreengrassService.SERVICES_NAMESPACE_TOPIC).createInteriorChild("MockService");
    try (InputStream inputStream = new ByteArrayInputStream(BLANK_CONFIG_YAML_WITH_TIMEOUT.getBytes())) {
        config.updateFromMap(new YAMLMapper().readValue(inputStream, Map.class), new UpdateBehaviorTree(UpdateBehaviorTree.UpdateBehavior.MERGE, 0));
    }
    lenient().when(greengrassService.getConfig()).thenReturn(config);
    lenient().when(greengrassService.getRuntimeConfig()).thenReturn(config.lookupTopics(RUNTIME_STORE_NAMESPACE_TOPIC));
    lenient().when(greengrassService.getPrivateConfig()).thenReturn(config.lookupTopics(PRIVATE_STORE_NAMESPACE_TOPIC));
    lenient().when(greengrassService.getContext()).thenReturn(context);
    lenient().when(greengrassService.dependencyReady()).thenReturn(true);
    lenient().when(greengrassService.getState()).thenAnswer((a) -> State.values()[Coerce.toInt(greengrassService.getPrivateConfig().findLeafChild(STATE_TOPIC_NAME))]);
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) Configuration(com.aws.greengrass.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) UpdateBehaviorTree(com.aws.greengrass.config.UpdateBehaviorTree) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Map(java.util.Map) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with Context

use of com.aws.greengrass.dependency.Context 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 20 with Context

use of com.aws.greengrass.dependency.Context 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)

Aggregations

Context (com.aws.greengrass.dependency.Context)36 Topics (com.aws.greengrass.config.Topics)24 Test (org.junit.jupiter.api.Test)21 Configuration (com.aws.greengrass.config.Configuration)19 BeforeEach (org.junit.jupiter.api.BeforeEach)15 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)15 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)14 IOException (java.io.IOException)8 ExecutorService (java.util.concurrent.ExecutorService)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 ChildChanged (com.aws.greengrass.config.ChildChanged)7 WhatHappened (com.aws.greengrass.config.WhatHappened)7 DEVICE_MQTT_NAMESPACE (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE)7 DEVICE_PARAM_AWS_REGION (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION)7 DEVICE_PARAM_CERTIFICATE_FILE_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH)7 DEVICE_PARAM_IOT_DATA_ENDPOINT (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT)7 DEVICE_PARAM_PRIVATE_KEY_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH)7 DEVICE_PARAM_ROOT_CA_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH)7 DEVICE_PARAM_THING_NAME (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME)7 DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT (com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT)7