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());
}
}
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);
}
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))]);
}
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));
}
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());
}
Aggregations